|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import TYPE_CHECKING, Any |
| 18 | + |
| 19 | +from opentelemetry.semconv._incubating.attributes import ( |
| 20 | + gen_ai_attributes as GenAI, |
| 21 | +) |
| 22 | +from opentelemetry.semconv.attributes import server_attributes |
| 23 | + |
| 24 | +from opentelemetry.trace import SpanKind |
| 25 | + |
| 26 | +from opentelemetry.util.genai.types import Error, GenAIInvocation |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from opentelemetry.util.genai.handler import TelemetryHandler |
| 30 | + |
| 31 | + |
| 32 | +class EmbeddingInvocation(GenAIInvocation): |
| 33 | + """Represents a single embedding model invocation. |
| 34 | +
|
| 35 | + Use handler.start_embedding(provider) or the handler.embedding(provider) |
| 36 | + context manager rather than constructing this directly. |
| 37 | + """ |
| 38 | + |
| 39 | + @property |
| 40 | + def operation_name(self) -> str: |
| 41 | + return GenAI.GenAiOperationNameValues.EMBEDDINGS.value |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + handler: TelemetryHandler, |
| 46 | + provider: str, |
| 47 | + *, |
| 48 | + request_model: str | None = None, |
| 49 | + server_address: str | None = None, |
| 50 | + server_port: int | None = None, |
| 51 | + encoding_formats: list[str] | None = None, |
| 52 | + input_tokens: int | None = None, |
| 53 | + dimension_count: int | None = None, |
| 54 | + response_model_name: str | None = None, |
| 55 | + attributes: dict[str, Any] | None = None, |
| 56 | + metric_attributes: dict[str, Any] | None = None, |
| 57 | + ) -> None: |
| 58 | + """Use handler.start_embedding(provider) or handler.embedding(provider) instead of calling this directly.""" |
| 59 | + super().__init__(handler, attributes=attributes, metric_attributes=metric_attributes) |
| 60 | + self.provider = provider # e.g., azure.ai.openai, openai, aws.bedrock |
| 61 | + self.request_model = request_model |
| 62 | + self.server_address = server_address |
| 63 | + self.server_port = server_port |
| 64 | + # encoding_formats can be multi-value -> combinational cardinality risk. |
| 65 | + # Keep on spans/events only. |
| 66 | + self.encoding_formats = encoding_formats |
| 67 | + self.input_tokens = input_tokens |
| 68 | + self.dimension_count = dimension_count |
| 69 | + self.response_model_name = response_model_name |
| 70 | + self._span_name = f"{self.operation_name} {request_model}" if request_model else self.operation_name |
| 71 | + self._span_kind = SpanKind.CLIENT |
| 72 | + handler._start(self) |
| 73 | + |
| 74 | + def _apply_finish(self, error: Error | None = None) -> None: |
| 75 | + optional_attrs = ( |
| 76 | + (GenAI.GEN_AI_PROVIDER_NAME, self.provider), |
| 77 | + (server_attributes.SERVER_ADDRESS, self.server_address), |
| 78 | + (server_attributes.SERVER_PORT, self.server_port), |
| 79 | + (GenAI.GEN_AI_REQUEST_MODEL, self.request_model), |
| 80 | + (GenAI.GEN_AI_EMBEDDINGS_DIMENSION_COUNT, self.dimension_count), |
| 81 | + (GenAI.GEN_AI_REQUEST_ENCODING_FORMATS, self.encoding_formats), |
| 82 | + (GenAI.GEN_AI_RESPONSE_MODEL, self.response_model_name), |
| 83 | + (GenAI.GEN_AI_USAGE_INPUT_TOKENS, self.input_tokens), |
| 84 | + ) |
| 85 | + attributes: dict[str, Any] = { |
| 86 | + GenAI.GEN_AI_OPERATION_NAME: self.operation_name, |
| 87 | + **{key: value for key, value in optional_attrs if value is not None}, |
| 88 | + } |
| 89 | + if error is not None: |
| 90 | + self._apply_error_attributes(error) |
| 91 | + attributes.update(self.attributes) |
| 92 | + self.span.set_attributes(attributes) |
| 93 | + # Metrics recorder currently supports InferenceInvocation fields only. |
| 94 | + # No-op until dedicated embedding metric support is added. |
0 commit comments