|
8 | 8 |
|
9 | 9 | import json |
10 | 10 | import logging |
11 | | -from typing import Any, Dict, Optional, Sequence, Union |
| 11 | +from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Union |
12 | 12 |
|
13 | 13 | from posthog.client import Client as PostHogClient |
14 | 14 |
|
| 15 | +if TYPE_CHECKING: |
| 16 | + from opentelemetry.sdk.trace import ReadableSpan |
| 17 | + from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
| 18 | + from opentelemetry.trace import StatusCode |
| 19 | + |
15 | 20 | try: |
16 | 21 | from opentelemetry.sdk.trace import ReadableSpan |
17 | 22 | from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
18 | 23 | from opentelemetry.trace import StatusCode |
19 | 24 |
|
20 | 25 | OTEL_AVAILABLE = True |
| 26 | + _BASE_CLASS = SpanExporter |
21 | 27 | except ImportError: |
22 | 28 | OTEL_AVAILABLE = False |
23 | | - # Define stub types for type hints when OTel is not installed |
24 | | - ReadableSpan = Any # type: ignore |
25 | | - SpanExporter = object # type: ignore |
26 | | - SpanExportResult = Any # type: ignore |
27 | | - StatusCode = Any # type: ignore |
| 29 | + _BASE_CLASS = object |
28 | 30 |
|
29 | 31 | logger = logging.getLogger(__name__) |
30 | 32 |
|
@@ -77,7 +79,7 @@ class GenAIAttributes: |
77 | 79 | SERVER_PORT = "server.port" |
78 | 80 |
|
79 | 81 |
|
80 | | -class PostHogSpanExporter(SpanExporter if OTEL_AVAILABLE else object): |
| 82 | +class PostHogSpanExporter(_BASE_CLASS): # type: ignore[valid-type,misc] |
81 | 83 | """ |
82 | 84 | OpenTelemetry SpanExporter that sends AI/LLM spans to PostHog. |
83 | 85 |
|
@@ -284,7 +286,7 @@ def _is_agent_span(self, span_name: str, attrs: Dict[str, Any]) -> bool: |
284 | 286 | """Check if span represents an agent run.""" |
285 | 287 | return span_name in ("agent run", "invoke_agent") or attrs.get( |
286 | 288 | GenAIAttributes.AGENT_NAME |
287 | | - ) |
| 289 | + ) is not None |
288 | 290 |
|
289 | 291 | def _is_tool_span(self, span_name: str, attrs: Dict[str, Any]) -> bool: |
290 | 292 | """Check if span represents a tool/function execution.""" |
|
0 commit comments