|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import time |
| 16 | +from threading import Lock |
| 17 | +from typing import List, Optional |
| 18 | +from uuid import UUID |
| 19 | + |
| 20 | +from .types import LLMInvocation, ToolInvocation |
| 21 | +from .exporters import SpanMetricEventExporter, SpanMetricExporter |
| 22 | +from .data import Message, ChatGeneration, Error, ToolOutput, ToolFunction |
| 23 | + |
| 24 | +from opentelemetry.instrumentation.langchain.version import __version__ |
| 25 | +from opentelemetry.metrics import get_meter |
| 26 | +from opentelemetry.trace import get_tracer |
| 27 | +from opentelemetry._events import get_event_logger |
| 28 | +from opentelemetry._logs import get_logger |
| 29 | +from opentelemetry.semconv.schemas import Schemas |
| 30 | + |
| 31 | + |
| 32 | +class TelemetryClient: |
| 33 | + """ |
| 34 | + High-level client managing GenAI invocation lifecycles and exporting |
| 35 | + them as spans, metrics, and events. |
| 36 | + """ |
| 37 | + def __init__(self, exporter_type_full: bool = True, **kwargs): |
| 38 | + tracer_provider = kwargs.get("tracer_provider") |
| 39 | + self._tracer = get_tracer( |
| 40 | + __name__, __version__, tracer_provider, schema_url=Schemas.V1_28_0.value |
| 41 | + ) |
| 42 | + |
| 43 | + meter_provider = kwargs.get("meter_provider") |
| 44 | + self._meter = get_meter( |
| 45 | + __name__, __version__, meter_provider, schema_url=Schemas.V1_28_0.value |
| 46 | + ) |
| 47 | + |
| 48 | + event_logger_provider = kwargs.get("event_logger_provider") |
| 49 | + self._event_logger = get_event_logger( |
| 50 | + __name__, __version__, event_logger_provider=event_logger_provider, schema_url=Schemas.V1_28_0.value |
| 51 | + ) |
| 52 | + |
| 53 | + logger_provider = kwargs.get("logger_provider") |
| 54 | + self._logger = get_logger( |
| 55 | + __name__, __version__, logger_provider=logger_provider, schema_url=Schemas.V1_28_0.value |
| 56 | + ) |
| 57 | + |
| 58 | + self._exporter = ( |
| 59 | + SpanMetricEventExporter(tracer=self._tracer, meter=self._meter, event_logger=self._event_logger, logger=self._event_logger) |
| 60 | + if exporter_type_full |
| 61 | + else SpanMetricExporter(tracer=self._tracer, meter=self._meter) |
| 62 | + ) |
| 63 | + |
| 64 | + self._llm_registry: dict[UUID, LLMInvocation] = {} |
| 65 | + self._tool_registry: dict[UUID, ToolInvocation] = {} |
| 66 | + self._lock = Lock() |
| 67 | + |
| 68 | + def start_llm(self, prompts: List[Message], tool_functions: List[ToolFunction], run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes): |
| 69 | + invocation = LLMInvocation(messages=prompts , tool_functions=tool_functions, run_id=run_id, parent_run_id=parent_run_id, attributes=attributes) |
| 70 | + with self._lock: |
| 71 | + self._llm_registry[invocation.run_id] = invocation |
| 72 | + self._exporter.init_llm(invocation) |
| 73 | + |
| 74 | + def stop_llm(self, run_id: UUID, chat_generations: List[ChatGeneration], **attributes) -> LLMInvocation: |
| 75 | + with self._lock: |
| 76 | + invocation = self._llm_registry.pop(run_id) |
| 77 | + invocation.end_time = time.time() |
| 78 | + invocation.chat_generations = chat_generations |
| 79 | + invocation.attributes.update(attributes) |
| 80 | + self._exporter.export_llm(invocation) |
| 81 | + return invocation |
| 82 | + |
| 83 | + def fail_llm(self, run_id: UUID, error: Error, **attributes) -> LLMInvocation: |
| 84 | + with self._lock: |
| 85 | + invocation = self._llm_registry.pop(run_id) |
| 86 | + invocation.end_time = time.time() |
| 87 | + invocation.attributes.update(**attributes) |
| 88 | + self._exporter.error_llm(error, invocation) |
| 89 | + return invocation |
| 90 | + |
| 91 | + def start_tool(self, input_str: str, run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes): |
| 92 | + invocation = ToolInvocation(input_str=input_str , run_id=run_id, parent_run_id=parent_run_id, attributes=attributes) |
| 93 | + with self._lock: |
| 94 | + self._tool_registry[invocation.run_id] = invocation |
| 95 | + self._exporter.init_tool(invocation) |
| 96 | + |
| 97 | + def stop_tool(self, run_id: UUID, output: ToolOutput, **attributes) -> ToolInvocation: |
| 98 | + with self._lock: |
| 99 | + invocation = self._tool_registry.pop(run_id) |
| 100 | + invocation.end_time = time.time() |
| 101 | + invocation.output = output |
| 102 | + self._exporter.export_tool(invocation) |
| 103 | + return invocation |
| 104 | + |
| 105 | + def fail_tool(self, run_id: UUID, error: Error, **attributes) -> ToolInvocation: |
| 106 | + with self._lock: |
| 107 | + invocation = self._tool_registry.pop(run_id) |
| 108 | + invocation.end_time = time.time() |
| 109 | + invocation.attributes.update(**attributes) |
| 110 | + self._exporter.error_tool(error, invocation) |
| 111 | + return invocation |
| 112 | + |
| 113 | +# Singleton accessor |
| 114 | +_default_client: TelemetryClient | None = None |
| 115 | + |
| 116 | +def get_telemetry_client(exporter_type_full: bool = True, **kwargs) -> TelemetryClient: |
| 117 | + global _default_client |
| 118 | + if _default_client is None: |
| 119 | + _default_client = TelemetryClient(exporter_type_full=exporter_type_full, **kwargs) |
| 120 | + return _default_client |
| 121 | + |
| 122 | +# Module‐level convenience functions |
| 123 | +def llm_start(prompts: List[Message], run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes): |
| 124 | + return get_telemetry_client().start_llm(prompts=prompts, run_id=run_id, parent_run_id=parent_run_id, **attributes) |
| 125 | + |
| 126 | +def llm_stop(run_id: UUID, chat_generations: List[ChatGeneration], **attributes) -> LLMInvocation: |
| 127 | + return get_telemetry_client().stop_llm(run_id=run_id, chat_generations=chat_generations, **attributes) |
| 128 | + |
| 129 | +def llm_fail(run_id: UUID, error: Error, **attributes) -> LLMInvocation: |
| 130 | + return get_telemetry_client().fail_llm(run_id=run_id, error=error, **attributes) |
0 commit comments