Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The `signal_to_metrics` connector derives the `gen_ai.invoke_agent.duration` metric from the `invoke_agent` spans that the `kagent` runtime emits. That runtime does not record the metric itself.

The connector reads the spans before any sampling in the collector. Its conditions skip the two A2A server instrumentation scopes. A span from those scopes carries the `invoke_agent` operation only when that layer also records the metric, so skipping the scopes prevents a double count.

The connector emits delta histograms, and the Prometheus exporter accepts only cumulative ones. The `delta_to_cumulative` processor converts them. Without it, the exporter logs `Dropped misaligned histogram datapoint` and keeps one point per series, so the duration queries return almost nothing.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ config:
endpoint: 0.0.0.0:4318
processors:
batch: {}
delta_to_cumulative: {}
connectors:
signal_to_metrics:
spans:
- name: gen_ai.invoke_agent.duration
description: The duration of one agent invocation, derived from its span.
unit: s
conditions:
- span.attributes["gen_ai.operation.name"] == "invoke_agent" and instrumentation_scope.name != "github.com/kagent-dev/kagent/go/adk/pkg/a2a/server" and instrumentation_scope.name != "kagent.core.a2a"
histogram:
buckets: [0.5, 1, 2, 5, 10, 30, 60, 120, 300]
value: Seconds(span.end_time - span.start_time)
attributes:
- key: gen_ai.agent.name
optional: true
- key: gen_ai.request.model
optional: true
- key: error.type
optional: true
include_resource_attributes:
- key: service.name
- key: service.namespace
optional: true
- key: service.version
optional: true
- key: kagent.runtime
optional: true
- key: gen_ai.main_agent.id
optional: true
exporters:
debug:
verbosity: basic
Expand All @@ -168,18 +197,20 @@ config:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp_grpc/jaeger]
exporters: [signal_to_metrics, otlp_grpc/jaeger]
logs:
receivers: [otlp]
processors: [batch]
exporters: [debug]
metrics:
receivers: [otlp]
processors: [batch]
receivers: [otlp, signal_to_metrics]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: The prometheus exporter drops almost all of the derived metric.

I ran this collector config on otel/opentelemetry-collector-contrib:0.159.0 and sent it synthetic invoke_agent spans with telemetrygen. The connector emits delta histograms with a zero start timestamp, as the debug exporter shows:

-> Name: gen_ai.invoke_agent.duration
-> AggregationTemporality: Delta
StartTimestamp: 1970-01-01 00:00:00 +0000 UTC

The prometheus exporter accepts only cumulative data, so it logs Dropped misaligned histogram datapoint and keeps only the first point. 20 traces (40 matching spans) ended at gen_ai_invoke_agent_duration_seconds_count 2, with 40 drop warnings. The p95 query in metrics.md would therefore return almost nothing. The upstream reference config does not hit this, because it exports over OTLP.

Adding a deltatocumulative processor to the metrics pipeline was not a simple fix in my test. _count kept rising after the spans stopped (100, then 346, for 3 traces). Could you or a kagent maintainer pick a fix and test it? The same pipeline is in otel-stack.md line 310.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced and fixed. Thank you for the precise repro — the drop count made this quick to confirm.

On a 1.37 kind cluster with chart 0.172.0 (which ships otel/opentelemetry-collector-contrib:0.159.0, the same build you used), 20 telemetrygen traces carrying gen_ai.operation.name=invoke_agent produced 38 Dropped misaligned histogram datapoint warnings and gen_ai_invoke_agent_duration_seconds_count 2, exactly as you saw.

Adding a deltatocumulative processor to the metrics pipeline fixes it. Same config, same 20 traces: zero drop warnings and _count 40, flat across twelve samples over three minutes. A second batch of 20 took it to exactly 80, so it accumulates correctly rather than creeping. I could not reproduce the runaway _count you hit — I also tried both [deltatocumulative, batch] and [batch, deltatocumulative], and both give 40.

Both guides now declare the processor and put it in the metrics pipeline, and the shared snippet explains why it is there.

You were also right that the reference config does not hit this: it exports to otlp/backend, so delta passes straight through.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One correction to my reply above: the processor is spelled delta_to_cumulative, and that is what both guides now use.

deltatocumulative is a deprecated alias. It works, but a collector logs "deltatocumulative" alias is deprecated; use "delta_to_cumulative" instead on startup. Re-tested with the current spelling on the chart's own 0.159.0: _count 40, zero drops, and no deprecation warning.

processors: [delta_to_cumulative, batch]
exporters: [prometheus]
EOF
```

{{< reuse "kagent-docs/snippets/signal-to-metrics-connector.md" >}}

## Send kagent telemetry to the collector

Turn on the kagent trace, log, and metrics exporters, and point them at the collector. Also turn on the controller's metrics endpoint, and allow Prometheus to scrape it.
Expand Down
6 changes: 3 additions & 3 deletions docs-site/content/kagent/1.x/observability/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ The Agent Substrate `atecontroller` component reports the same `controller_runti

## Agent runtime metrics

When the kagent Helm release sets an endpoint in `config.runtimeDefaults.telemetry.metrics`, agent runtimes export metrics over OTLP. The `claude` and `codex` runtimes are the only ones that report the histogram in this section. The stack guides convert these OpenTelemetry metrics to Prometheus names in the collector.
When the kagent Helm release sets an endpoint in `config.runtimeDefaults.telemetry.metrics`, agent runtimes export metrics over OTLP. A runtime reports the histogram in this section only when kagent's own A2A wrapper opens the `invoke_agent` span for it. For the runtimes that open that span themselves, both stack guides derive the metric from the spans instead. The stack guides convert these OpenTelemetry metrics to Prometheus names in the collector.

| Metric | Type | Measures |
| ------ | ---- | -------- |
| `gen_ai_invoke_agent_duration_seconds` | Histogram | Time that one `invoke_agent` execution segment takes. Only the `claude` and `codex` runtimes record it. The `kagent` runtime emits its own `invoke_agent` spans, so its wrapper stays a transport span and records no duration. For that runtime, derive the metric from those spans in the collector before sampling. |
| `gen_ai_invoke_agent_duration_seconds` | Histogram | Time that one `invoke_agent` execution segment takes. The `claude`, `codex`, `langgraph`, `crewai`, and `byo` runtimes record it themselves, because kagent's wrapper opens their `invoke_agent` span and measures it. The `kagent`, `adk-python`, and `openai-agents` runtimes open that span in their own instrumentation, so the wrapper stays a transport span and records no duration. Both stack guides add a `signal_to_metrics` connector that derives the metric from those spans before any sampling in the collector. Between the two paths, every runtime reports the metric, except a `byo` image that is not built on `kagent-core` and opens no `invoke_agent` span of its own. |

The metric carries labels for the Agent name, model, and error type when those values exist. When the collector copies resource attributes to labels, the metric also carries labels such as `service_name`, `service_namespace`, `service_version`, `kagent_runtime`, and `gen_ai_main_agent_id`.

Expand Down Expand Up @@ -121,7 +121,7 @@ Run the following queries in Prometheus or Grafana.
sum by (controller) (rate(controller_runtime_reconcile_errors_total{job="kagent-controller-metrics"}[5m]))
```

- The 95th percentile agent invocation duration, per Agent. The query returns data only for Agents on the `claude` and `codex` runtimes.
- The 95th percentile agent invocation duration, per Agent. Because both stack guides configure the collector to derive the metric, the query covers every runtime that opens an `invoke_agent` span.
```text
histogram_quantile(0.95, sum by (le, gen_ai_agent_name) (rate(gen_ai_invoke_agent_duration_seconds_bucket[5m])))
```
Expand Down
37 changes: 34 additions & 3 deletions docs-site/content/kagent/1.x/observability/otel-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ Install a collector that receives OTLP on ports `4317` for gRPC and `4318` for H
endpoint: 0.0.0.0:4318
processors:
batch: {}
delta_to_cumulative: {}
connectors:
signal_to_metrics:
spans:
- name: gen_ai.invoke_agent.duration
description: The duration of one agent invocation, derived from its span.
unit: s
conditions:
- span.attributes["gen_ai.operation.name"] == "invoke_agent" and instrumentation_scope.name != "github.com/kagent-dev/kagent/go/adk/pkg/a2a/server" and instrumentation_scope.name != "kagent.core.a2a"
histogram:
buckets: [0.5, 1, 2, 5, 10, 30, 60, 120, 300]
value: Seconds(span.end_time - span.start_time)
attributes:
- key: gen_ai.agent.name
optional: true
- key: gen_ai.request.model
optional: true
- key: error.type
optional: true
include_resource_attributes:
- key: service.name
- key: service.namespace
optional: true
- key: service.version
optional: true
- key: kagent.runtime
optional: true
- key: gen_ai.main_agent.id
optional: true
exporters:
otlp_grpc/tempo:
endpoint: tempo.telemetry.svc.cluster.local:4317
Expand All @@ -273,20 +302,22 @@ Install a collector that receives OTLP on ports `4317` for gRPC and `4318` for H
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp_grpc/tempo]
exporters: [signal_to_metrics, otlp_grpc/tempo]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp_http/loki]
metrics:
receivers: [otlp]
processors: [batch]
receivers: [otlp, signal_to_metrics]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: This has the same delta-into-prometheus problem as the comment on lightweight-otel-stack.md line 207.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here too. This pipeline now declares deltatocumulative and runs it in the metrics pipeline, same as the lightweight guide. The test evidence is on that thread.

processors: [delta_to_cumulative, batch]
exporters: [prometheus]
EOF
```

The `resource_to_telemetry_conversion` setting copies OTel resource attributes, such as the service name, onto each metric as labels. Without it, you cannot tell which Agent Substrate component reported a metric.

{{< reuse "kagent-docs/snippets/signal-to-metrics-connector.md" >}}

2. Create a ServiceMonitor so that Prometheus scrapes the metrics that the collector serves.
```yaml
kubectl apply -f - <<EOF
Expand Down
Loading