o
    i]                     @  s  U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ ddlZddlZddlZdad	ed
< G dd dZeG dd dZeddG dd dZ e edddde _!e Z"eG dd dZ#G dd deZ$eddG dd dZ%eddG dd dZ&G dd  d eZ'G d!d" d"Z(eddG d#d$ d$Z)ed%e*Z+	 e+dZ,	 e+d&Z-	 e+d'Z.	 G d(d) d)eZ/G d*d+ d+eZ0G d,d- d-ej1j2Z3ed.Z4G d/d0 d0ej1j5ee4 Z6G d1d2 d2ej1j7e6ej8j9j7 Z:G d3d4 d4ej1j;e6ej8j9j; Z<G d5d6 d6ej1j=e6ej8j9j= Z>G d7d8 d8ej1j?e6ej8j9j@ ZAG d9d: d:ej1jBe6ej8j9jB ZCG d;d< d<ej1jDe6ej8j9jD ZEdS )=z Runtime for clients and workers.    )annotationsN)	dataclassfield)	timedelta)Enum)ClassVarGenericMappingNewTypeOptionalSequenceTypeVarUnion)ProtocolSelfzOptional[Runtime]_default_runtimec                   @  sJ   e Zd ZdZedddZedddddZdddZe	dddZ
dS )Runtimea  Runtime for Temporal Python SDK.

    Users are encouraged to use :py:meth:`default`. It can be set with
    :py:meth:`set_default`. Every time a new runtime is created, a new internal
    thread pool is created.

    Runtimes do not work across forks.
    returnc                 C  s   t s| t da t S )a  Get the default runtime, creating if not already created.

        If the default runtime needs to be different, it should be done with
        :py:meth:`set_default` before this is called or ever used.

        Returns:
            The default runtime.
        	telemetry)r   TelemetryConfig)cls r   X/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/runtime.pydefault(   s   zRuntime.defaultT)error_if_already_setruntimer   boolNonec                C  s   t r|rtd| a dS )a  Set the default runtime to the given runtime.

        This should be called before any Temporal client is created, but can
        change the existing one. Any clients and workers created with the
        previous runtime will stay on that runtime.

        Args:
            runtime: The runtime to set.
            error_if_already_set: If True and default is already set, this will
                raise a RuntimeError.
        zRuntime default already setN)r   RuntimeError)r   r   r   r   r   set_default7   s   zRuntime.set_defaultr   r   c                C  sb   t jjj| d| _t|jtr| |j_	t jj
j| j}|s(t jjj| _dS t||j| _dS )zCreate a default runtime with the given telemetry config.

        Each new runtime creates a new internal thread pool, so use sparingly.
        r   N)
temporaliobridger   r   _to_bridge_config_core_runtime
isinstancemetricsMetricBuffer_runtimemetricMetricMetercreatecommonnoop_metric_meter_MetricMeterdefault_attributes)selfr   
core_meterr   r   r   __init__I   s   zRuntime.__init__temporalio.common.MetricMeterc                 C     | j S )zkMetric meter for this runtime. This is a no-op metric meter if no
        metrics were configured.
        )r.   r1   r   r   r   metric_meterY   s   zRuntime.metric_meterN)r   r   )r   r   r   r   r   r   )r   r   r   r   )r   r4   )__name__
__module____qualname____doc__classmethodr   staticmethodr    r3   propertyr7   r   r   r   r   r      s    	
r   c                   @  s0   e Zd ZU dZded< 	 ded< 	 d	ddZdS )
TelemetryFilterzFilter for telemetry use.str
core_levelother_levelr   c                 C  s"   | j  d| j d| j d| j S )z'Return a formatted form of this filter.z,temporal_sdk_core=z,temporal_client=z,temporal_sdk=)rB   rA   r6   r   r   r   	formattedo   s   "zTelemetryFilter.formattedNr   r@   )r8   r9   r:   r;   __annotations__rC   r   r   r   r   r?   a   s   
 r?   T)frozenc                   @  s>   e Zd ZU dZded< 	 dZded< 	 ded< 	 dddZdS )LoggingConfigz"Configuration for runtime logging.zUnion[TelemetryFilter, str]filterNzOptional[LogForwardingConfig]
forwardingzClassVar[LoggingConfig]r   r   'temporalio.bridge.runtime.LoggingConfigc                 C  s<   t jjjt| jtr| jn| j | jsd dS | jj	dS )N)rH   
forward_to)
r!   r"   r   rG   r%   rH   r@   rC   rI   _on_logsr6   r   r   r   r#      s   
zLoggingConfig._to_bridge_config)r   rJ   )r8   r9   r:   r;   rE   rI   r#   r   r   r   r   rG   v   s   
 rG   WARNERROR)rA   rB   )rH   c                   @  s^   e Zd ZU dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded	< 	 dddZ	dS )LogForwardingConfiga  Configuration for log forwarding from Core.

    Configuring this will send logs from Core to the given Python logger. By
    default, log timestamps are overwritten and internally throttled/buffered
    for a few milliseconds to prevent overloading Python. This means those log
    records may have a time in the past and technically may appear out of order
    with Python-originated log messages by a few milliseconds.

    If for some reason lots of logs occur within the buffered time (i.e.
    thousands), they may be sent earlier. Users are discouraged from using this
    with ``TRACE`` Core logging.

    All log records produced have a ``temporal_log`` attribute that contains a
    representation of the Core log. This representation has a ``fields``
    attribute which has arbitrary extra data from Core. By default a string
    representation of this extra ``fields`` attribute is appended to the
    message.
    zlogging.LoggerloggerTr   append_target_to_nameprepend_target_on_messageoverwrite_log_record_timeappend_log_fields_to_messagelogs4Sequence[temporalio.bridge.runtime.BufferedLogEntry]r   r   c                 C  s   |D ]m}|j }| j|sq| jj}| jr|d|j 7 }|j}| jr,d|j d| }| jr>z
|d|j	 7 }W n   Y | j
||dd|dd dd|id 
}| jri|j|_|jt|j d	 |_|jt d	 | _| j| qd S )
Nz-sdk_core::z[sdk_core::z]  z
(sdk-core)r   r   temporal_log  )levelrP   isEnabledFornamerQ   targetmessagerR   rT   fields
makeRecordrS   timecreatedintmsecs_module_start_timerelativeCreatedhandle)r1   rU   logrZ   r\   r^   recordr   r   r   rL      sB   zLogForwardingConfig._on_logsN)rU   rV   r   r   )
r8   r9   r:   r;   rE   rQ   rR   rS   rT   rL   r   r   r   r   rO      s   
 rO   c                   @  s   e Zd ZdZdZdZdS )OpenTelemetryMetricTemporalityz&Temporality for OpenTelemetry metrics.      N)r8   r9   r:   r;   
CUMULATIVEDELTAr   r   r   r   rj      s    rj   c                   @  sb   e Zd ZU dZded< dZded< dZded< ejZ	d	ed
< dZ
ded< dZded< dddZdS )OpenTelemetryConfigz*Configuration for OpenTelemetry collector.r@   urlNzOptional[Mapping[str, str]]headerszOptional[timedelta]metric_periodicityrj   metric_temporalityFr   durations_as_secondshttpr   -temporalio.bridge.runtime.OpenTelemetryConfigc                 C  sF   t jjj| j| jp
i | jsd nt| j d | j	t
jk| j| jdS )NrY   )rp   rq   metric_periodicity_millismetric_temporality_deltart   ru   )r!   r"   r   ro   rp   rq   rr   roundtotal_secondsrs   rj   rn   rt   ru   r6   r   r   r   r#      s   
z%OpenTelemetryConfig._to_bridge_config)r   rv   )r8   r9   r:   r;   rE   rq   rr   rj   rm   rs   rt   ru   r#   r   r   r   r   ro      s   
 
ro   c                   @  sT   e Zd ZU dZded< dZded< dZded< dZded< d	Zd
ed< dddZ	d	S )PrometheusConfigz.Configuration for Prometheus metrics endpoint.r@   bind_addressFr   counters_total_suffixunit_suffixrt   Nz'Optional[Mapping[str, Sequence[float]]]histogram_bucket_overridesr   *temporalio.bridge.runtime.PrometheusConfigc                 C  s"   t jjj| j| j| j| j| jdS )N)r|   r}   r~   rt   r   )	r!   r"   r   r{   r|   r}   r~   rt   r   r6   r   r   r   r#     s   z"PrometheusConfig._to_bridge_config)r   r   )
r8   r9   r:   r;   rE   r}   r~   rt   r   r#   r   r   r   r   r{     s   
 r{   c                   @  s   e Zd ZdZdZ	 dZdS )MetricBufferDurationFormatz2How durations are represented for metrics buffers.rk   rl   N)r8   r9   r:   r;   MILLISECONDSSECONDSr   r   r   r   r   $  s    r   c                   @  s*   e Zd ZdZejfddd	ZdddZdS )r'   a  A buffer that can be set on :py:class:`TelemetryConfig` to record
    metrics instead of ignoring/exporting them.

    .. warning::
        It is important that the buffer size is set to a high number and that
        :py:meth:`retrieve_updates` is called regularly to drain the buffer. If
        the buffer is full, metric updates will be dropped and an error will be
        logged.
    buffer_sizerc   duration_formatr   r   r   c                 C  s   || _ d| _|tjk| _dS )a  Create a buffer with the given size.

        .. warning::
            It is important that the buffer size is set to a high number and is
            drained regularly. See :py:class:`MetricBuffer` warning.

        Args:
            buffer_size: Size of the buffer. Set this to a large value. A value
                in the tens of thousands or higher is plenty reasonable.
            duration_format: Which duration format to use.
        N)_buffer_sizer(   r   r   _durations_as_seconds)r1   r   r   r   r   r   r3   9  s   zMetricBuffer.__init__Sequence[BufferedMetricUpdate]c                 C  s   | j std| j j| jS )zDrain the buffer and return all metric updates.

        .. warning::
            It is important that this is called regularly. See
            :py:class:`MetricBuffer` warning.

        Returns:
            A sequence of metric updates.
        z5Attempting to retrieve updates before runtime created)r(   r   r$   retrieve_buffered_metricsr   r6   r   r   r   retrieve_updatesO  s
   
zMetricBuffer.retrieve_updatesN)r   rc   r   r   r   r   )r   r   )r8   r9   r:   r;   r   r   r3   r   r   r   r   r   r'   .  s
    r'   c                   @  sj   e Zd ZU dZejZded< 	 dZded< 	 e	e
dZded	< 	 d
Zded< 	 dZded< 	 dddZdS )r   z!Configuration for Core telemetry.zOptional[LoggingConfig]loggingNzDOptional[Union[OpenTelemetryConfig, PrometheusConfig, MetricBuffer]]r&   )default_factoryzMapping[str, str]global_tagsTr   attach_service_nameOptional[str]metric_prefixr   )temporalio.bridge.runtime.TelemetryConfigc              
   C  s   t jjj| js	d n| j | jsd dS t jjjt| jt	s d n| j t| jt
s,d n| j t| jts8dn| jj| j| jpAd | jddS )Nr   )opentelemetry
prometheusbuffered_with_sizer   r   r   )r   r&   )r!   r"   r   r   r   r#   r&   MetricsConfigr%   ro   r{   r'   r   r   r   r   r6   r   r   r   r#   t  s(   


z!TelemetryConfig._to_bridge_config)r   r   )r8   r9   r:   r;   rG   r   r   rE   r&   r   dictr   r   r   r#   r   r   r   r   r   `  s   
 r   BufferedMetricKindrk   rl   c                   @  sH   e Zd ZdZedddZedddZedd	d
ZedddZdS )BufferedMetriczA metric for a buffered update.

    The same metric for the same name and runtime is guaranteed to be the exact
    same object for performance reasons. This means py:func:`id` will be the
    same for the same metric across updates.
    r   r@   c                 C     dS )zGet the name of the metric.Nr   r6   r   r   r   r\        zBufferedMetric.namer   c                 C  r   )z)Get the description of the metric if any.Nr   r6   r   r   r   description  r   zBufferedMetric.descriptionc                 C  r   )z"Get the unit of the metric if any.Nr   r6   r   r   r   unit  r   zBufferedMetric.unitr   c                 C  r   )zGet the metric kind.

        This is one of :py:const:`BUFFERED_METRIC_KIND_COUNTER`,
        :py:const:`BUFFERED_METRIC_KIND_GAUGE`, or
        :py:const:`BUFFERED_METRIC_KIND_HISTOGRAM`.
        Nr   r6   r   r   r   kind     zBufferedMetric.kindNrD   r   r   )r   r   )	r8   r9   r:   r;   r>   r\   r   r   r   r   r   r   r   r     s    r   c                   @  s:   e Zd ZdZedddZedddZedd
dZdS )BufferedMetricUpdatezA single metric value update.r   r   c                 C  r   )zMetric being updated.

        For performance reasons, this is the same object across updates for the
        same metric. This means py:func:`id` will be the same for the same
        metric across updates.
        Nr   r6   r   r   r   r)     r   zBufferedMetricUpdate.metricUnion[int, float]c                 C  r   )zValue for the update.

        For counters this is a delta, for gauges and histograms this is just the
        value.
        Nr   r6   r   r   r   value  s   zBufferedMetricUpdate.value"temporalio.common.MetricAttributesc                 C  r   )a  Attributes for the update.

        For performance reasons, this is the same object across updates for the
        same attribute set. This means py:func:`id` will be the same for the
        same attribute set across updates. Note this is for same "attribute set"
        as created by the metric creator, but different attribute sets may have
        the same values.

        Do not mutate this.
        Nr   r6   r   r   r   
attributes  s   zBufferedMetricUpdate.attributesN)r   r   )r   r   )r   r   )r8   r9   r:   r;   r>   r)   r   r   r   r   r   r   r     s    	r   c                   @  st   e Zd Zd&ddZ		d'd(ddZ		d'd)ddZ		d'd*ddZ		d'd+ddZ		d'd,ddZ		d'd-dd Z	d.d$d%Z
d	S )/r/   r2   $temporalio.bridge.metric.MetricMeter
core_attrs)temporalio.bridge.metric.MetricAttributesr   r   c                 C  s   || _ || _d S N)_core_meter_core_attrs)r1   r2   r   r   r   r   r3     s   
z_MetricMeter.__init__Nr\   r@   r   r   r   temporalio.common.MetricCounterc              
   C  $   t |||tjj| j|||| jS r   )_MetricCounterr!   r"   r)   MetricCounterr   r   r1   r\   r   r   r   r   r   create_counter     
z_MetricMeter.create_counter!temporalio.common.MetricHistogramc              
   C  r   r   )_MetricHistogramr!   r"   r)   MetricHistogramr   r   r   r   r   r   create_histogram  r   z_MetricMeter.create_histogram&temporalio.common.MetricHistogramFloatc              
   C  r   r   )_MetricHistogramFloatr!   r"   r)   MetricHistogramFloatr   r   r   r   r   r   create_histogram_float  r   z#_MetricMeter.create_histogram_float*temporalio.common.MetricHistogramTimedeltac              
   C  r   r   )_MetricHistogramTimedeltar!   r"   r)   MetricHistogramDurationr   r   r   r   r   r   create_histogram_timedelta  r   z'_MetricMeter.create_histogram_timedeltatemporalio.common.MetricGaugec              
   C  r   r   )_MetricGauger!   r"   r)   MetricGauger   r   r   r   r   r   create_gauge  r   z_MetricMeter.create_gauge"temporalio.common.MetricGaugeFloatc              
   C  r   r   )_MetricGaugeFloatr!   r"   r)   MetricGaugeFloatr   r   r   r   r   r   create_gauge_float*  r   z_MetricMeter.create_gauge_floatadditional_attributesr   r4   c                 C  s   t | j| j|S r   )r/   r   r   with_additional_attributesr1   r   r   r   r   r   7  s   
z'_MetricMeter.with_additional_attributes)r2   r   r   r   r   r   )NN)r\   r@   r   r   r   r   r   r   )r\   r@   r   r   r   r   r   r   )r\   r@   r   r   r   r   r   r   )r\   r@   r   r   r   r   r   r   )r\   r@   r   r   r   r   r   r   )r\   r@   r   r   r   r   r   r   )r   r   r   r4   )r8   r9   r:   r3   r   r   r   r   r   r   r   r   r   r   r   r/     s    
	r/   _CoreMetricTypec                   @  sJ   e Zd ZdddZedddZedddZedddZdddZdS )_MetricCommonr\   r@   r   r   r   core_metricr   r   r   r   r   c                 C  s"   || _ || _|| _|| _|| _d S r   )_name_description_unit_core_metricr   )r1   r\   r   r   r   r   r   r   r   r3   D  s
   
z_MetricCommon.__init__c                 C  r5   r   )r   r6   r   r   r   r\   R     z_MetricCommon.namec                 C  r5   r   )r   r6   r   r   r   r   V  r   z_MetricCommon.descriptionc                 C  r5   r   )r   r6   r   r   r   r   Z  r   z_MetricCommon.unitr   r   r   c              	   C  s"   |  | j| j| j| j| j|S r   )	__class__r   r   r   r   r   r   r   r   r   r   r   ^  s   
z(_MetricCommon.with_additional_attributesN)r\   r@   r   r   r   r   r   r   r   r   r   r   rD   r   )r   r   r   r   )	r8   r9   r:   r3   r>   r\   r   r   r   r   r   r   r   r   C  s    
r   c                   @     e Zd Z	d
ddd	ZdS )r   Nr   rc   r   ,Optional[temporalio.common.MetricAttributes]r   r   c                 C  6   |dk rt d| j}|r||}| j|| d S Nr   Metric value cannot be negative)
ValueErrorr   r   r   addr1   r   r   r   r   r   r   r   n     
z_MetricCounter.addr   r   rc   r   r   r   r   )r8   r9   r:   r   r   r   r   r   r   j      r   c                   @  r   )r   Nr   rc   r   r   r   r   c                 C  r   r   r   r   r   r   ri   r   r   r   r   ri     r   z_MetricHistogram.recordr   r   r8   r9   r:   ri   r   r   r   r   r   {  r   r   c                   @  r   )r   Nr   floatr   r   r   r   c                 C  r   r   r   r   r   r   r   ri     r   z_MetricHistogramFloat.recordr   r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @  r   )r   Nr   r   r   r   r   r   c                 C  sV   |j dk r	td| j}|r||}| j|j d d |jd  |jd  | d S )Nr   r   iQ rY   )daysr   r   r   r   ri   secondsmicrosecondsr   r   r   r   ri     s   

z _MetricHistogramTimedelta.recordr   )r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @  r   )r   Nr   rc   r   r   r   r   c                 C  r   r   r   r   r   r   setr   r   r   r   r     r   z_MetricGauge.setr   r   r8   r9   r:   r   r   r   r   r   r     s    r   c                   @  r   )r   Nr   r   r   r   r   r   c                 C  r   r   r   r   r   r   r   r     r   z_MetricGaugeFloat.setr   r   r   r   r   r   r   r     r   r   )Fr;   
__future__r   r   ra   dataclassesr   r   datetimer   enumr   typingr   r   r	   r
   r   r   r   r   typing_extensionsr   r   temporalio.bridge.metricr!   temporalio.bridge.runtimetemporalio.commonr   rE   r   r?   rG   r   re   rO   rj   ro   r{   r   r'   r   rc   r   BUFFERED_METRIC_KIND_COUNTERBUFFERED_METRIC_KIND_GAUGEBUFFERED_METRIC_KIND_HISTOGRAMr   r   r,   r*   r/   r   MetricCommonr   r   r"   r)   r   r   r   r   r   MetricHistogramTimedeltar   r   r   r   r   r   r   r   r   r   <module>   s    (C
S
2
)#%`
'




