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.
# 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
doneCollector config (YAML)ShowHide
# 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
Takes all three signals from SDKs, agents, and other collectors over gRPC (4317) and HTTP (4318).
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.
Sends a batch at 256 items or after one second, capped at 500 to keep Kafka records under the size limit.
Compresses records with lz4 and retries each batch for up to five minutes while Kafka is unreachable.
Serves a liveness endpoint on port 13133 for the container runtime or load balancer.
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_BROKERSon commas and keeps spaces, sobroker-1:9092, broker-2:9092yields a second address starting with a space, and its DNS lookup fails. - 2
The exporter's top-level
topicandencodingwere deprecated in v0.124.0 and removed in v0.148.0, so on 0.159.0 a top-leveltopicfails validation with'kafkaexporter.Config' has invalid keys: topic. Set it undertraces,metrics, andlogs. - 3
An oversized record logs
kafka record exceeds max message sizewith its size, andotelcol_exporter_send_failed_*counts every item in its batch witherror_permanent="true", though the batch's other records reached Kafka. For bigger records, raisemax_message_bytesand the topic'smax.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
kafkareceiver starts at the latest offset. Setinitial_offset: earlieston a new group to read existing data. - 7
For a plaintext listener with no auth, delete the
tlsandauthblocks. For Amazon MSK IAM auth, setKAFKA_SASL_MECHANISMtoAWS_MSK_IAM_OAUTHBEARER, addaws_msk::regionunderauth::sasl, and keeptls: insecure: false.
Sources and references (23)ShowHide
Everything consulted while researching and fact-checking this flow, including the README of every component it uses.
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/exporter/kafkaexporter/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/exporter/kafkaexporter/config.go
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/exporter/kafkaexporter/kafka_exporter.go
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/exporter/kafkaexporter/internal/kafkaclient/franzgo.go
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/pkg/kafka/configkafka/config.go
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/receiver/kafkareceiver/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/processor/tailsamplingprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/CHANGELOG.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/receiver/otlpreceiver/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/processor/memorylimiterprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/processor/batchprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/exporter/exporterhelper/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/config/configtls/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/confmap/internal/decoder.go
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/extension/healthcheckextension/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/extension/storage/filestorage/README.md
- kafka.apache.org/43/getting-started/introduction/
- kafka.apache.org/43/getting-started/quickstart/
- kafka.apache.org/43/design/design/
- kafka.apache.org/43/configuration/broker-configs/
- kafka.apache.org/43/configuration/topic-configs/
- kafka.apache.org/43/configuration/producer-configs/
- kafka.apache.org/43/operations/basic-kafka-operations/
More flows
Related flows
Survive backend outages with a persistent queue
Replaces the default in-memory exporter queue with a disk-backed queue that survives collector restarts, sized in items instead of opaque requests.
Fail over to S3 when ClickHouse backs up
A ClickHouse gateway that fails each signal over to an S3 overflow bucket when inserts back up and fails back on its own once ClickHouse recovers, so an ingest stall never becomes data loss.
Replace vendor agents on your VM fleet
Scrapes CPU, memory, load, disk, filesystem, and network metrics plus system and application logs on every VM.
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.