Skip to content

[feature request] Expose librdkafka client statistics as OpenTelemetry metrics #5203

Description

@tombiddulph

Component

OpenTelemetry.Instrumentation.ConfluentKafka

Is your feature request related to a problem?

The instrumentation currently emits these messaging operation metrics:

  • messaging.client.operation.duration
  • messaging.client.sent.messages
  • messaging.client.consumed.messages

These describe application-level send and consume operations, but do not expose the operational state of the underlying Kafka client. Important signals such as consumer lag, producer queue depth, retries, request latency, throttling, and bytes transferred are therefore unavailable.

The OpenTelemetry Java Kafka instrumentation exposes a broad set of native Kafka client metrics through Kafka's MetricsReporter interface. Confluent.Kafka does not provide the same interface, but its underlying librdkafka client emits detailed statistics as JSON through SetStatisticsHandler when statistics.interval.ms is configured.

What is the expected behavior?

Provide an opt-in bridge that converts a curated set of librdkafka statistics into OpenTelemetry metrics.

An initial set could include:

Consumer

  • Records and bytes consumed
  • Consumer lag by topic and partition
  • Assigned partition count
  • Rebalance count
  • Fetch queue messages and bytes

Producer

  • Records and bytes transmitted
  • Queued messages and bytes
  • Messages or requests in flight
  • Request retries
  • Request timeouts

Shared client and broker

  • Requests and responses
  • Bytes transmitted and received
  • Request latency
  • Broker throttle time
  • Connection state or count

The exact metric names, units, instrument types, and attributes should be agreed upon before implementation. The goal would be equivalent operational coverage to the Java instrumentation where the librdkafka measurements have compatible semantics, rather than strict one-to-one parity.

Possible implementation

The instrumentation could:

  1. Respect an existing statistics.interval.ms value or configure a user-specified interval.
  2. Install a composite statistics handler that preserves an existing user SetStatisticsHandler callback.
  3. Parse the periodic librdkafka statistics JSON.
  4. Store the latest snapshot for each producer or consumer.
  5. Publish selected values using observable counters and gauges.
  6. Remove observations when the corresponding client is disposed.

Configuration might resemble:

metering.AddKafkaConsumerInstrumentation<string, string>(
    consumerBuilder,
    options =>
    {
        options.EnableClientMetrics = true;
        options.StatisticsInterval = TimeSpan.FromSeconds(10);
        options.EnablePartitionMetrics = true;
    });

This API is only illustrative.

Cardinality and performance considerations

Native statistics collection should be opt-in because generating and parsing the JSON has a runtime cost.

Low-cardinality client-level metrics could be enabled separately from topic-, partition-, and broker-level metrics. Partition-level consumer lag is particularly valuable, but could create a large number of time series.

The implementation should also:

  • Avoid exporting librdkafka's generated handle name as an attribute.
  • Avoid double-counting values available at client, broker, topic, and partition granularities.
  • Distinguish existing application operation counters from native transmitted or received message counters.
  • Ignore unknown JSON fields and treat missing fields as unavailable rather than zero.
  • Handle counter resets when clients restart.
  • Filter librdkafka sentinel offset and lag values.
  • Ensure parsing or instrumentation failures cannot interrupt Kafka processing.

Which alternative solutions or features have you considered?

Applications can currently register their own SetStatisticsHandler, parse the JSON, and implement custom metrics. However, this duplicates mapping, lifecycle, cardinality, and callback-composition logic across applications.

A separate package could provide this bridge, but it would need to duplicate or integrate with the producer and consumer wrapping already performed by this instrumentation.

Additional context

Relevant references:

I would be happy to produce a proof of concept for an agreed initial metric set and API design.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions