Metrics

October 22, 2025 ยท View on GitHub

@platformatic/kafka supports integration with Prometheus via prom-client.

To ensure maximum compatibility, no prom-client version is shipped with the package but you are instead required to provide your own via metrics.client option when creating any client.

If you provide both the metrics.client and metrics.registry (an instance of Registry) options, then @platformatic/kafka will register and provide the following metrics:

NameTypeDescription
kafka_producersGaugeNumber of active Kafka producers.
kafka_produced_messagesCounterNumber of produced Kafka messages.
kafka_consumersGaugeNumber of active Kafka consumers.
kafka_consumers_streamsGaugeNumber of active Kafka consumers streams.
kafka_consumers_topicsGaugeNumber of topics being consumed.
kafka_consumers_lagsHistogramLag of active Kafka consumers
kafka_consumed_messagesCounterNumber of consumed Kafka messages.

Optionally, you can provide labels with the metrics.label option.

Example

import * as client from 'prom-client'
import { Producer, stringSerializer } from '@platformatic/kafka'

const registry = new client.Registry()

// Create a producer with string serialisers
const producer = new Producer({
  clientId: 'my-producer',
  bootstrapBrokers: ['localhost:9092'],
  serializers: stringSerializers,
  metrics: { client, registry }
})

// Send messages
await producer.send({
  messages: [
    {
      topic: 'events',
      key: 'user-123',
      value: JSON.stringify({ name: 'John', action: 'login' }),
      headers: { source: 'web-app' }
    }
  ]
})

// Close the producer when done
await producer.close()

console.log(JSON.stringify(await registry.getMetricsAsJSON(), null, 2))