Skip to content

[Cosmos] [Embedding V0] Diagnostics: cosmos.embedding_generation OpenTelemetry span #46734

@ananth7592

Description

@ananth7592

Diagnostics: OpenTelemetry span cosmos.embedding_generation

Parent: 46729
Depends on: 46732

Goal

Surface embedding-generation latency, count, and generator type as a first-class telemetry item, so customers and support can see the new step in distributed traces.

Scope

  1. Inside _resolve_embeddings (and async sibling), wrap the call to generator.generate_embeddings(...) in:
    from azure.core.settings import settings
    
    tracer = settings.tracing_implementation()
    if tracer is not None:
        with tracer.span(name="cosmos.embedding_generation") as span:
            span.add_attribute("cosmos.embedding.count", len(texts))
            span.add_attribute("cosmos.embedding.generator_type", type(generator).__name__)
            start = time.perf_counter()
            vectors = generator.generate_embeddings(list(texts))
            span.add_attribute("cosmos.embedding.latency_ms",
                               int((time.perf_counter() - start) * 1000))
    else:
        start = time.perf_counter()
        vectors = generator.generate_embeddings(list(texts))
        _logger.info(
            "embedding_generation count=%d generator_type=%s latency_ms=%d",
            len(texts), type(generator).__name__,
            int((time.perf_counter() - start) * 1000),
        )
  2. Mirror in async aggregator (await the call inside the span).
  3. Privacy:
    • Never log raw input strings.
    • Never log returned vectors.
    • It is OK to log the parameter key names (e.g. @documentdb-hybridsearchquery-embedding-0) — they are not customer data.
  4. If a tracer is configured, also record an event on the span when an exception is raised by the generator (so failures show up in traces, not just logs).

Non-goals

  • Do NOT introduce a new logger — reuse the package logger (logging.getLogger("azure.cosmos")).
  • Do NOT add new public diagnostics types (no Python equivalent of .NET's EmbeddingGenerationDatum).

Files touched

  • sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/hybrid_search_aggregator.py
  • sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/aio/hybrid_search_aggregator.py

Acceptance

  • With an OpenTelemetry tracer configured, a query that triggers embedding generation produces a child span named cosmos.embedding_generation with the three documented attributes.
  • Without a tracer configured, an INFO-level log line is produced.
  • Tests assert raw text / vectors do not appear in span attributes or log output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Cosmosfeature-requestThis issue requires a new behavior in the product in order be resolved.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions