GenAI Utils | Embedding Type and Span Creation#4219
GenAI Utils | Embedding Type and Span Creation#4219shuningc wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
| - Add `EmbeddingInvocation` span lifecycle support (`start_embedding`, | ||
| `stop_embedding`, `fail_embedding`), embedding span tests, and defer | ||
| embedding metrics emission (current no-op) to a follow-up PR. | ||
| ([#<PR_NUMBER>](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/<PR_NUMBER>)) |
There was a problem hiding this comment.
nit: <PR_NUMBER> placeholder is still present; can we replace this with #4219 before merge?
| - `gen_ai.provider.name`: Str(openai) | ||
| - `gen_ai.operation.name`: Str(embeddings) | ||
| - `gen_ai.request.model`: Str(text-embedding-3-small) | ||
| - `gen_ai.embedding.dimension_count`: Int(1536) |
There was a problem hiding this comment.
nit: the attribute name here looks outdated; semconv key is gen_ai.embeddings.dimension.count (plural + dotted), not gen_ai.embedding.dimension_count.
| """Get GenAI request semantic convention attributes.""" | ||
| optional_attrs = ( | ||
| (GenAI.GEN_AI_REQUEST_MODEL, invocation.request_model), | ||
| (GenAI.GEN_AI_EMBEDDING_DIMENSION_COUNT, invocation.dimension_count), |
There was a problem hiding this comment.
Potential blocker: GenAI.GEN_AI_EMBEDDING_DIMENSION_COUNT does not exist in the current semconv module (the available constant is GEN_AI_EMBEDDINGS_DIMENSION_COUNT). This path is exercised in stop_embedding/fail_embedding, so this currently raises AttributeError before cleanup. Could we switch to the ...EMBEDDINGS... constant and align the related assertions/docs?
|
|
||
| operation_name: str = field( | ||
| default=GenAI.GenAiOperationNameValues.EMBEDDINGS.value, | ||
| metadata={"semconv": GenAI.GEN_AI_OPERATION_NAME}, |
There was a problem hiding this comment.
Remove metadata={"semconv": ...} from all fields — no auto-mapping infrastructure reads this metadata.
The mapping is done explicitly in span_utils.py.
|
|
||
| server_address: str | None = None | ||
| server_port: int | None = None | ||
| error_type: str | None = None |
There was a problem hiding this comment.
Remove error_type — not a request-time field. Error type is derived from Error.type.qualname in fail_embedding.
| EmbeddingInvocation, | ||
| Error, | ||
| LLMInvocation, | ||
| ) |
There was a problem hiding this comment.
Missing embedding() context manager.
@contextmanager
def embedding()
| def _get_llm_span_name(invocation: LLMInvocation) -> str: | ||
| """Get the span name for an LLM invocation.""" | ||
| return f"{invocation.operation_name} {invocation.request_model}".strip() | ||
|
|
||
|
|
||
| def _get_embedding_span_name(invocation: EmbeddingInvocation) -> str: | ||
| """Get the span name for an Embedding invocation.""" | ||
| return f"{invocation.operation_name} {invocation.request_model}".strip() | ||
|
|
||
|
|
There was a problem hiding this comment.
Repeating code, a refactor here could happen if we move operation_name and request_model to GenAIInvocation instead of on each type.
| Additional attributes to set on metrics. Must be of a low cardinality. | ||
| These attributes will not be set on spans or events. | ||
| """ | ||
| monotonic_start_s: float | None = None |
There was a problem hiding this comment.
Some of these fields are the same as LLMInvocation fields. I think this is an ideal candidate for moving some of these fields to GenAIInvocation.
This could simplify the metrics recording once this supports Embedding Metrics. Also could simplify fetching attributes for spans
Description
This PR adds initial embedding lifecycle coverage, to add metrics in the next PR.
Type of change
How Has This Been Tested?
Ran from repo root with local package path:
PYTHONPATH=util/opentelemetry-util-genai/src pytest -q util/opentelemetry-util-genai/tests/test_utils.py
PYTHONPATH=util/opentelemetry-util-genai/src pytest -q util/opentelemetry-util-genai/tests/test_handler_metrics.py
Results:
[test_utils.py]: 23 passed
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.