Ë
    kQêjà  ã                  óf   — d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
Z
 erddlmZ da	 d
	 	 	 dd	„Zy)a¶  Bridge LanceDB's internal metrics into OpenTelemetry.

LanceDB (through Lance core) publishes metrics (currently object store request
counts, bytes, latency, errors, and throttles) through the Rust ``metrics``
facade. This module installs a process-global recorder that aggregates them and
registers OpenTelemetry observable instruments that report the aggregated values
into the user's ``MeterProvider``.

The bridge is generic: every metric LanceDB describes is surfaced automatically,
with no per-metric Python code. Histograms have no asynchronous OpenTelemetry
instrument, so each is exported Prometheus-style as cumulative ``le`` buckets
plus ``_count`` and ``_sum`` observable counters.
é    )ÚannotationsN)ÚTYPE_CHECKINGÚOptionalé   )Úlancedb_metrics_catalogÚ!register_lancedb_metrics_recorderÚsnapshot_lancedb_metrics)ÚMeterProviderFc                ó  ‡
— 	 ddl mŠ
m} t	        «       st        j                  dd¬«       yt        ry	| xs  |«       }|j                  d
«      }dˆ
fd„}dˆ
fd„}dˆ
fd„}t        «       D �]w  }|j                  xs d}	|j                  dk(  r;|j                  |j                   ||j                  «      g|	|j                  ¬«       Œ^|j                  dk(  r;|j                  |j                   ||j                  «      g|	|j                  ¬«       Œ¨|j                  dk(  sŒ¸|j                  |j                  › d� ||j                  «      g|j                  › d�¬«       |j                  |j                  › d� ||j                  d«      g|j                  › d�¬«       |j                  |j                  › d� ||j                  d«      g|	|j                  › d�¬«       �Œz d	ay	# t        $ r}t        d«      |‚d}~ww xY w)aˆ  Register LanceDB metrics as OpenTelemetry observable instruments.

    Installs a process-global metrics recorder and creates one observable
    instrument per LanceDB metric on the given (or global) ``MeterProvider``. The
    user's configured ``MetricReader`` then collects them on its own schedule.

    Counters and gauges map directly to observable counters/gauges. Each
    histogram is exported as cumulative ``le`` bucket counts (``<name>_bucket``,
    with an ``le`` attribute) plus ``<name>_count`` and ``<name>_sum``.

    Parameters
    ----------
    meter_provider : opentelemetry.metrics.MeterProvider, optional
        The provider to register instruments on. Defaults to the global provider
        from ``opentelemetry.metrics.get_meter_provider()``.

    Returns
    -------
    bool
        ``True`` if the recorder is installed and instruments are registered.
        ``False`` if a different ``metrics`` recorder is already installed in
        this process (``metrics`` permits only one global recorder), in which
        case a warning is emitted and no instruments are created.

    Notes
    -----
    Requires the OpenTelemetry API (``pip install lancedb[otel]``) and, to
    actually export, an OpenTelemetry SDK (``pip install opentelemetry-sdk``)
    configured by the application. Calling this more than once is safe;
    instruments are created only on the first successful call.
    r   )ÚObservationÚget_meter_providerzŽinstrument_lancedb_metrics requires the OpenTelemetry API/SDK. Install it with `pip install lancedb[otel]` or `pip install opentelemetry-sdk`.Nz¨Could not install the LanceDB metrics recorder: another `metrics` recorder is already installed in this process. LanceDB metrics will not be exported via OpenTelemetry.é   )Ú
stacklevelFTÚlancedbc                ó   •‡ — ˆˆ fd„}|S )Nc                óª   •— t        «       D �cg c]:  }|j                  ‰k(  r)|j                  � ‰|j                  |j                  «      ‘Œ< c}S c c}w ©N)r	   ÚnameÚvalueÚ
attributes)Ú_optionsÚpointr   Úmetric_names     €€úQ/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/otel.pyÚcallbackzEinstrument_lancedb_metrics.<locals>.scalar_callback.<locals>.callback`   sN   ø€ ô 6Ó7öàØ—:‘: Ò,°·±Ð1Hñ ˜EŸK™K¨×)9Ñ)9Õ:òð ùò s   Ž?A© ©r   r   r   s   ` €r   Úscalar_callbackz3instrument_lancedb_metrics.<locals>.scalar_callback_   s   ù€ õ	ð ˆó    c                ó   •‡ — ˆˆ fd„}|S )Nc                óî   •— g }t        «       D ]d  }|j                  ‰k7  s|j                  €Œ|j                  D ]7  \  }}t        |j                  «      }||d<   |j                   ‰||«      «       Œ9 Œf |S )NÚle)r	   r   ÚbucketsÚdictr   Úappend)r   Úobservationsr   r"   Ú
cumulativer   r   r   s         €€r   r   zEinstrument_lancedb_metrics.<locals>.bucket_callback.<locals>.callbackj   s„   ø€ ØˆLÜ1Ó3ò M�Ø—:‘: Ò,°·±Ð0EØØ&+§m¡mò M‘N�B˜
Ü!% e×&6Ñ&6Ó!7�JØ')�J˜tÑ$Ø ×'Ñ'©°JÀ
Ó(KÕLñMðMð  Ðr   r   r   s   ` €r   Úbucket_callbackz3instrument_lancedb_metrics.<locals>.bucket_callbacki   s   ù€ õ		 ð ˆr   c                ó   •‡ ‡— ˆˆˆ fd„}|S )Nc                ó¬   •— g }t        «       D ]C  }|j                  ‰k7  rŒt        |‰«      }|€Œ"|j                   ‰||j                  «      «       ŒE |S r   )r	   r   Úgetattrr%   r   )r   r&   r   r   r   Úfieldr   s       €€€r   r   zDinstrument_lancedb_metrics.<locals>.field_callback.<locals>.callbackx   s`   ø€ ØˆLÜ1Ó3ò N�Ø—:‘: Ò,ØÜ  uÓ-�ØÑ$Ø ×'Ñ'©°E¸5×;KÑ;KÓ(LÕMðNð  Ðr   r   )r   r,   r   r   s   `` €r   Úfield_callbackz2instrument_lancedb_metrics.<locals>.field_callbackw   s   ú€ ö	 ð ˆr   Ú Úcounter)Ú	callbacksÚunitÚdescriptionÚgaugeÚ	histogramÚ_bucketz (cumulative buckets))r0   r2   Ú_countÚcountz (count)Ú_sumÚsumz (sum))r   Ústr)r   r:   r,   r:   )Úopentelemetry.metricsr   r   ÚImportErrorr   ÚwarningsÚwarnÚ_INSTRUMENTEDÚ	get_meterr   r1   ÚkindÚcreate_observable_counterr   r2   Úcreate_observable_gauge)Úmeter_providerr   ÚexcÚproviderÚmeterr   r(   r-   Údescr1   r   s             @r   Úinstrument_lancedb_metricsrI   #   s  ø€ ðHßIô -Ô.Ü�‰ð1ð õ		
ð åØàÒ5Ñ!3Ó!5€HØ×Ñ˜yÓ)€Eõõõô (Ó)ó #ˆØ�y‰yŠ˜BˆØ�9‰9˜	Ò!Ø×+Ñ+Ø—	‘	Ù*¨4¯9©9Ó5Ð6ØØ ×,Ñ,ð	 ,õ ð �Y‰Y˜'Ò!Ø×)Ñ)Ø—	‘	Ù*¨4¯9©9Ó5Ð6ØØ ×,Ñ,ð	 *õ ð �Y‰Y˜+Ó%ð ×+Ñ+Ø—9‘9�+˜WÐ%Ù*¨4¯9©9Ó5Ð6Ø#×/Ñ/Ð0Ð0EÐFð ,ô ð
 ×+Ñ+Ø—9‘9�+˜VÐ$Ù)¨$¯)©)°WÓ=Ð>Ø#×/Ñ/Ð0°Ð9ð ,ô ð
 ×+Ñ+Ø—9‘9�+˜TÐ"Ù)¨$¯)©)°UÓ;Ð<ØØ#×/Ñ/Ð0°Ð7ð	 ,ö ð=#ðJ €MØøôC ò Üð/ó
ð ð		ûðús   ƒG* Ç*	HÇ3G?Ç?Hr   )rD   zOptional['MeterProvider']ÚreturnÚbool)Ú__doc__Ú
__future__r   r=   Útypingr   r   Ú_lancedbr   r   r	   r;   r
   r?   rI   r   r   r   ú<module>rP      sI   ðñõ #ã ß *÷ñ ñ Ý3à€ð 15ðGØ-ðGà	ôGr   