Skip to content

feat(core): wire a MeterProvider into the Python runtime (gated OTEL_METRICS_ENABLED) - #2672

Open
MartinForReal wants to merge 1 commit into
kagent-dev:mainfrom
MartinForReal:issue/2458-python-meter-provider
Open

feat(core): wire a MeterProvider into the Python runtime (gated OTEL_METRICS_ENABLED)#2672
MartinForReal wants to merge 1 commit into
kagent-dev:mainfrom
MartinForReal:issue/2458-python-meter-provider

Conversation

@MartinForReal

Copy link
Copy Markdown

What

Fixes the Python half of the metrics gap (#2458): the Python runtime never installed a MeterProvider, so the seven GenAI metric instruments that google-adk already defines and records (in src/google/adk/telemetry/_metrics.py under meter scope gcp.vertex.agent) were silently discarded on every invocation. This wires a meter provider (gated OTEL_METRICS_ENABLED, default-OFF) with no new instrument definitions and no new recording call sites — upstream already records them. Companion to #2148 / #2149 for Go.

Changes

  • kagent.core.tracing.configure() (python/packages/kagent-core/src/kagent/core/tracing/_utils.py):
    • New metrics_enabled = os.getenv("OTEL_METRICS_ENABLED", "false") == "true" gate, mirroring OTEL_TRACING_ENABLED / OTEL_LOGGING_ENABLED.
    • New _create_metric_exporter() helper reusing _resolve_otlp_protocol("METRICS"), the shared endpoint resolution, and _resolve_otlp_timeout_seconds("METRICS"), so protocol/endpoint/timeout semantics match traces and logs.
    • Builds MeterProvider(resource=resource, metric_readers=[PeriodicExportingMetricReader(OTLPMetricExporter(...))]) and calls metrics.set_meter_provider.
    • Extends force_flush to drain the meter provider, covering the Agent Substrate checkpoint hazard that motivated KAGENT_PRE_RESPONSE_TRACE_FLUSH.
  • helm/kagent/values.yaml: new otel.metrics block alongside otel.tracing / otel.logging (default enabled: false).
  • helm/kagent/templates/controller-configmap.yaml: maps otel.metrics.* to OTEL_METRICS_ENABLED / OTEL_EXPORTER_OTLP_METRICS_* with the same unified-vs-separate-endpoint branch as traces/logs.

Default-OFF / no behaviour change

When OTEL_METRICS_ENABLED is unset (the default), configure installs no meter provider and behavior is byte-identical to before. Existing gates unchanged.

Testing

  • uv run pytest packages/kagent-core/tests/test_tracing_configure.py -q30 passed (added tests for default-off, endpoint/timeout reuse, meter-provider install, and force_flush draining the meter provider).
  • uv run ruff check and uv run ruff format --diff clean on changed files.

Refs: #2458

DCO sign-off via git commit -s.

…METRICS_ENABLED)

google-adk already defines and records its GenAI metric instruments (duration histograms, inference/tool-call counts, gen_ai.client.token.usage) under meter scope gcp.vertex.agent, but kagent.core.tracing.configure only installed a TracerProvider and LoggerProvider, so those data points were silently discarded on every invocation.

Add a metric_exporter/PeriodicExportingMetricReader OTLP pipeline mirroring traces and logs (reusing _resolve_otlp_protocol/_resolve_otlp_timeout_seconds) behind a default-OFF OTEL_METRICS_ENABLED gate, flush metrics from force_flush alongside spans so the Agent Substrate pre-response checkpoint hazard is covered, and expose otel.metrics.* from the Helm chart.

Signed-off-by: MartinForReal <fanshangxiang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant