
    jKj                     ~    d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ d	d
lmZmZ  G d de      Zy)zPostHog span processor for OpenTelemetry.

Provides a self-contained SpanProcessor that filters AI-related spans and
exports them to PostHog's OTLP endpoint. This is the recommended integration
for setups using TracerProvider.add_span_processor().
    )Optional)Context)ReadableSpanSpanSpanProcessor)BatchSpanProcessor)OTLPSpanExporter   )*warn_if_posthog_ai_gateway_otel_attributes   )DEFAULT_HOST
is_ai_spanc                   p    e Zd ZdZefdedefdZddedee	   ddfd	Z
deddfd
ZddZddee   defdZy)PostHogSpanProcessora+  Span processor that filters AI spans and exports them to PostHog.

    Wraps a BatchSpanProcessor and OTLPSpanExporter internally, configured
    to send to PostHog's OTLP traces endpoint. Only spans identified as
    AI-related (by name or attribute prefix) are forwarded for export.

    Usage::

        from opentelemetry.sdk.trace import TracerProvider
        from posthog.ai.otel import PostHogSpanProcessor

        provider = TracerProvider()
        provider.add_span_processor(
            PostHogSpanProcessor(api_key="phc_...")
        )
    api_keyhostc                     || _         |j                  d      | _        t        | j                   ddd| j                    i      }t	        |      | _        y)z
        Args:
            api_key: PostHog project API key.
            host: PostHog host URL. Defaults to US cloud.
        /z/i/v0/ai/otelAuthorizationzBearer )endpointheadersN)_api_keyrstrip_hostr	   r   
_processor)selfr   r   exporters       j/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/ai/otel/processor.py__init__zPostHogSpanProcessor.__init__%   sT      [[%
#

|=1$&?@
 -X6    Nspanparent_contextreturnc                      y)z
        Handle span start notifications.

        This processor does not need to do work at span start; filtering happens
        in ``on_end``.
        N )r   r!   r"   s      r   on_startzPostHogSpanProcessor.on_start8   s     	r    c                 |    t        |      syt        |j                         | j                  j	                  |       y)zz
        Export an ended span if it is AI-related.

        Args:
            span: The ended OpenTelemetry span.
        N)r   r   
attributesr   on_end)r   r!   s     r   r)   zPostHogSpanProcessor.on_endA   s-     $24??Ct$r    c                 8    | j                   j                          y)z.Shut down the underlying batch span processor.N)r   shutdown)r   s    r   r+   zPostHogSpanProcessor.shutdownM   s      "r    timeout_millisc                 p    || j                   j                  |      S | j                   j                         S )a  
        Flush pending spans from the underlying batch span processor.

        Args:
            timeout_millis: Optional flush timeout in milliseconds.

        Returns:
            True if the flush succeeded within the timeout, False otherwise.
        )r   force_flush)r   r,   s     r   r.   z PostHogSpanProcessor.force_flushQ   s2     %??..~>>**,,r    )N)r#   N)__name__
__module____qualname____doc__r   strr   r   r   r   r&   r   r)   r+   intboolr.   r%   r    r   r   r      sr    ( !77 7&T 8G3D PT 
%< 
%D 
%#-(3- -4 -r    r   N)r2   typingr   opentelemetry.contextr   opentelemetry.sdk.tracer   r   r   opentelemetry.sdk.trace.exportr   5opentelemetry.exporter.otlp.proto.http.trace_exporterr	   gatewayr   spansr   r   r   r%   r    r   <module>r=      s2     ) E E = R @ +J-= J-r    