Skip to content
All flows

OpenTelemetry Collector to Kafka

Writes logs, metrics, and traces as OTLP protobuf to three Kafka topics that gateway collectors or other consumers read.

The kafka exporter writes spans to otlp_spans, metrics to otlp_metrics, and logs to otlp_logs as OTLP protobuf, matching the kafka receiver's defaults, so consumer collectors need no topic settings. Kafka keeps records for each topic's retention period, and consumers read at their own pace, so a slow or down backend backs up in Kafka, not collector memory. For JSON consumers, set each signal's encoding to otlp_json here and on the receiver.

Each batch becomes one Kafka record per trace, keyed by trace ID, or per resource for metrics and logs, keyed by a hash of the resource attributes. The default partitioner keeps each trace and source on one partition, and Kafka gives each partition to one consumer in a group at a time, so a tail-sampling gateway gets whole traces.

Each write waits for acks from every in-sync replica (required_acks: -1). Topic auto-creation is off (allow_auto_topic_creation: false), so the command below creates the topics with replication factor 3 and min.insync.replicas=2, the pairing Kafka's docs give for acks=all.

A record over max_message_bytes (1,000,000 bytes by default, counted before compression) is dropped, not retried. A full batch of 500 log records from one service fits while they average under about 2 KB. Lower send_batch_max_size for larger ones.

Run it as an agent or gateway. Set KAFKA_BROKERS to comma-separated bootstrap brokers with no spaces, such as broker-1:9092,broker-2:9092. Set KAFKA_SASL_MECHANISM to SCRAM-SHA-512, SCRAM-SHA-256, or PLAIN, and KAFKA_USERNAME and KAFKA_PASSWORD to a Kafka user that can write to the topics. The exporter uses TLS with the system root CAs. Add tls::ca_file for a private CA.

Create the topics before the collector starts
# admin.properties holds the CLI's connection settings for a SASL over TLS cluster, e.g.
#   security.protocol=SASL_SSL
#   sasl.mechanism=SCRAM-SHA-512
#   sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="<password>";
for t in otlp_spans otlp_metrics otlp_logs; do
  kafka-topics.sh --bootstrap-server "$KAFKA_BROKERS" --command-config admin.properties \
    --create --topic "$t" --partitions 12 --replication-factor 3 \
    --config min.insync.replicas=2
done
How the data moves
receiverprocessorexporterextension
traces
otlpmemory_limiterbatchkafka
metrics
otlpmemory_limiterbatchkafka
logs
otlpmemory_limiterbatchkafka
Collector config (YAML)Show
config.yaml
# Ship OTel data to Kafka
# Built by Telflo

receivers:
  otlp:
    protocols:
      grpc:
        endpoint: '0.0.0.0:4317'
      http:
        endpoint: '0.0.0.0:4318'
processors:
  memory_limiter:
    check_interval: '1s'
    limit_mib: 1600
    spike_limit_mib: 400
  batch:
    send_batch_size: 256
    send_batch_max_size: 500
    timeout: '1s'
exporters:
  kafka:
    brokers: '${env:KAFKA_BROKERS}'
    client_id: 'otel-collector'
    tls:
      insecure: false
    auth:
      sasl:
        mechanism: '${env:KAFKA_SASL_MECHANISM}'
        username: '${env:KAFKA_USERNAME}'
        password: '${env:KAFKA_PASSWORD}'
    traces:
      topic: 'otlp_spans'
      encoding: 'otlp_proto'
    metrics:
      topic: 'otlp_metrics'
      encoding: 'otlp_proto'
    logs:
      topic: 'otlp_logs'
      encoding: 'otlp_proto'
    partition_traces_by_id: true
    partition_metrics_by_resource_attributes: true
    partition_logs_by_resource_attributes: true
    producer:
      required_acks: -1
      compression: 'lz4'
      max_message_bytes: 1000000
      allow_auto_topic_creation: false
    timeout: '5s'
    retry_on_failure:
      enabled: true
      initial_interval: '5s'
      max_interval: '30s'
      max_elapsed_time: '300s'
    sending_queue:
      enabled: true
      num_consumers: 10
      queue_size: 1000
      storage: 'file_storage'
extensions:
  health_check:
    endpoint: '0.0.0.0:13133'
  file_storage:
    directory: '/var/lib/otelcol/file_storage'
    create_directory: true
service:
  extensions: [health_check, file_storage]
  pipelines:
    traces:
      receivers: [otlp]
      processors: [memory_limiter, batch]
      exporters: [kafka]
    metrics:
      receivers: [otlp]
      processors: [memory_limiter, batch]
      exporters: [kafka]
    logs:
      receivers: [otlp]
      processors: [memory_limiter, batch]
      exporters: [kafka]

Use this flow

Open the config in Telflo and it becomes a working pipeline on the canvas: adapt what's specific to you, test it against recorded traffic, and push it to your fleet. Free account, no card.

Components

What's in it, and why

otlp

Takes all three signals from SDKs, agents, and other collectors over gRPC (4317) and HTTP (4318).

memory_limiter

Targets a 1600 MiB heap for a 2 GiB host, refusing data above 1200 MiB (1600 minus the 400 MiB spike allowance) so senders back off and retry. Scale both on bigger hosts.

batch

Sends a batch at 256 items or after one second, capped at 500 to keep Kafka records under the size limit.

kafka

Compresses records with lz4 and retries each batch for up to five minutes while Kafka is unreachable.

health_check

Serves a liveness endpoint on port 13133 for the container runtime or load balancer.

file_storage

Stores the exporter's queue, up to 1,000 batches, on disk under /var/lib/otelcol/file_storage. Put that directory on a persistent volume so they survive a restart.

Notes

Gotchas

  • 1

    The collector splits KAFKA_BROKERS on commas and keeps spaces, so broker-1:9092, broker-2:9092 yields a second address starting with a space, and its DNS lookup fails.

  • 2

    The exporter's top-level topic and encoding were deprecated in v0.124.0 and removed in v0.148.0, so on 0.159.0 a top-level topic fails validation with 'kafkaexporter.Config' has invalid keys: topic. Set it under traces, metrics, and logs.

  • 3

    An oversized record logs kafka record exceeds max message size with its size, and otelcol_exporter_send_failed_* counts every item in its batch with error_permanent="true", though the batch's other records reached Kafka. For bigger records, raise max_message_bytes and the topic's max.message.bytes (1,048,588 bytes by default), which the broker checks per batch after compression.

  • 4

    A missing topic logs UNKNOWN_TOPIC_OR_PARTITION, and the exporter retries until it exists or five minutes pass.

  • 5

    With 12 partitions per topic, at most 12 gateways in one consumer group read a topic in parallel. Adding partitions later can send a key's new records to another partition, so size topics for the planned gateway count before going live.

  • 6

    With no committed offset, the kafka receiver starts at the latest offset. Set initial_offset: earliest on a new group to read existing data.

  • 7

    For a plaintext listener with no auth, delete the tls and auth blocks. For Amazon MSK IAM auth, set KAFKA_SASL_MECHANISM to AWS_MSK_IAM_OAUTHBEARER, add aws_msk::region under auth::sasl, and keep tls: insecure: false.

Sources and references (23)Show

Everything consulted while researching and fact-checking this flow, including the README of every component it uses.

Test it before your fleet runs it

Free account, no card. Open this flow in the editor, adapt it, and see what it does to real data before anything ships.