o
    is                     @   s|   d dl mZmZ d dlmZmZmZ 	 G dd deZG dd deZdee	 fdd	Z
dee	 fd
dZG dd dZdS )    )IntFlagauto)ListOptionalSequencec                   @   s:   e Zd ZdZe Ze Ze Ze Ze Z	e Z
e ZdS )MetricGroupz+Metric groups that can be enabled/disabled.N)__name__
__module____qualname____doc__r   
RESILIENCYCONNECTION_BASICCONNECTION_ADVANCEDCOMMANDCSC	STREAMINGPUBSUB r   r   N/home/ubuntu/.local/lib/python3.10/site-packages/redis/observability/config.pyr      s    
r   c                   @   s   e Zd ZdZe ZdS )TelemetryOptionzTelemetry options to export.N)r   r	   r
   r   r   METRICSr   r   r   r   r      s    
r   returnc                   C      g dS )N)-C6?gMb0?Mb@?MbP?g{Gzd?{Gzt?{Gz?g?皙?皙?g      ?      ?   g      @r   r   r   r   r   "default_operation_duration_buckets      r"   c                   C   r   )N)r   r   r   r   r   r   r   r    r!      
   r   r   r   r   r   default_histogram_buckets1   r#   r&   c                   @   s   e Zd ZdZejZejej	B Z
dddddde e e e f
deee  deee  deee  deee  ded	ed
ee dee dee dee fddZdefddZdedefddZdefddZdS )
OTelConfiga  
    Configuration for OpenTelemetry observability in redis-py.

    This class manages all OTel-related settings including metrics, traces (future),
    and logs (future). Configuration can be provided via constructor parameters or
    environment variables (OTEL_* spec).

    Constructor parameters take precedence over environment variables.

    Args:
        enabled_telemetry: Enabled telemetry options to export (default: metrics). Traces and logs will be added
                           in future phases.
        metric_groups: Group of metrics that should be exported.
        include_commands: Explicit allowlist of commands to track
        exclude_commands: Blocklist of commands to track
        hide_pubsub_channel_names: If True, hide PubSub channel names in metrics (default: False)
        hide_stream_names: If True, hide stream names in streaming metrics (default: False)

    Note:
        Redis-py uses the global MeterProvider set by your application.
        Set it up before initializing observability:

            from opentelemetry import metrics
            from opentelemetry.sdk.metrics import MeterProvider
            from opentelemetry.sdk.metrics._internal.view import View
            from opentelemetry.sdk.metrics._internal.aggregation import ExplicitBucketHistogramAggregation

            # Configure histogram bucket boundaries via Views
            views = [
                View(
                    instrument_name="db.client.operation.duration",
                    aggregation=ExplicitBucketHistogramAggregation(
                        boundaries=[0.0001, 0.00025, 0.0005, 0.001, 0.0025, 0.005,
                                    0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5]
                    ),
                ),
                # Add more views for other histograms...
            ]

            provider = MeterProvider(views=views, metric_readers=[reader])
            metrics.set_meter_provider(provider)

            # Then initialize redis-py observability
            from redis.observability import get_observability_instance, OTelConfig
            otel = get_observability_instance()
            otel.init(OTelConfig())
    NFenabled_telemetrymetric_groupsinclude_commandsexclude_commandshide_pubsub_channel_nameshide_stream_namesbuckets_operation_duration"buckets_stream_processing_durationbuckets_connection_create_timebuckets_connection_wait_timec                 C   s   |d u r	| j | _ntd| _|D ]	}|  j|O  _q|d u r#| j| _ntd| _|D ]	}|  j|O  _q*|r:t|nd | _|rCt|nt | _|| _	|| _
|| _|| _|	| _|
| _d S )Nr   )DEFAULT_TELEMETRYr(   r   DEFAULT_METRIC_GROUPSr)   r   setr*   r+   r,   r-   r.   r/   r0   r1   )selfr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   optionmetric_groupr   r   r   __init__i   s$   




zOTelConfig.__init__r   c                 C   s
   t | jS )z.Check if any observability feature is enabled.)boolr(   r5   r   r   r   
is_enabled   s   
zOTelConfig.is_enabledcommand_namec                 C   s&   |  }| jdur|| jv S || jvS )a  
        Determine if a command should be tracked based on include/exclude lists.

        Args:
            command_name: The Redis command name (e.g., 'GET', 'SET')

        Returns:
            True if the command should be tracked, False otherwise
        N)upperr*   r+   )r5   r<   command_upperr   r   r   should_track_command   s   



zOTelConfig.should_track_commandc                 C   s   d| j  S )NzOTelConfig(enabled_telemetry=)r(   r:   r   r   r   __repr__   s   zOTelConfig.__repr__)r   r	   r
   r   r   r   r2   r   r   r   r3   r"   r&   r   r   strr9   r   floatr8   r;   r?   r@   r   r   r   r   r'   5   sT    0





4r'   N)enumr   r   typingr   r   r   r   r   rB   r"   r&   r'   r   r   r   r   <module>   s    