Ingesting logs from Kafka into Loki

Consume existing Kafka log topics into self-hosted Loki with Grafana Alloy or the OpenTelemetry Kafka receiver, using consumer groups, authenticated connections and checks at each step.

On this page

Ingesting logs from Kafka into Loki with Grafana Alloy

A team that already publishes logs to Apache Kafka and wants them searchable in self-hosted Loki can do it without changing its producers: Grafana Alloy reads the existing topics through a consumer group and pushes the records to Loki. Alloy offers two components for this. loki.source.kafka is the Loki-native option, which consumes a topic through a consumer group and forwards entries to a loki.write component. otelcol.receiver.kafka is the OpenTelemetry option, which receives telemetry from a Kafka broker and passes it into an OpenTelemetry pipeline that ends in a Loki exporter. Both are consumers of topics that already exist. Neither creates the producers, and neither changes how applications write to Kafka.

Choose between the two by looking at what the topic contains and where the logs should go. If the messages are plain log lines or JSON that you want to label directly in Alloy, loki.source.kafka keeps the path short. If the topic already carries OpenTelemetry log records, or you want the same pipeline to handle other telemetry, otelcol.receiver.kafka fits better and reuses the OTLP mapping rules described in the sibling OpenTelemetry section. Confirm the exact arguments, consumer group settings and supported encodings for your Alloy version in the Loki Alloy Kafka example before writing configuration, because the two components expose different options.

Before any configuration, agree with the Kafka owners on the consumer group name, the topics to read, the authentication mechanism and the offset behavior. Kafka's consumer group model means each group tracks its own position, so a second reader using a different group name will receive the same records independently, which matters if another system already consumes the logs. Confirm with the cluster team that Alloy's identity has read access to the topics and group through the cluster's authorization settings, and that the listener Alloy connects to uses the TLS or SASL mechanisms the cluster enforces.

What you need before ingesting logs from Kafka into Loki

Ingesting Kafka topics into Loki depends on four components that must be in place first: a Loki write path, a Grafana Alloy installation, a Kafka cluster carrying the log topic, and a consumer credential. Confirm each one in your own environment before you write any pipeline configuration.

A reachable Loki write path

Alloy pushes records to Loki's push endpoint, so the write path must be reachable from the host or pod running Alloy, with the same network route and TLS trust you expect in production. Confirm that the Loki deployment and its gateway accept writes from that network segment before debugging anything in Alloy. If the cluster is a single binary, a simple scalable install, or a microservices install, the endpoint may differ, so check the deployment you actually run.

Loki deployment modes

Tenant setup matching your Loki configuration

If Loki runs with auth_enabled set to true, every write from Alloy must carry an X-Scope-OrgID header, so the Kafka pipeline needs a tenant that exists and is permitted to write. Agree the tenant ID for Kafka-sourced logs with the team that owns Loki, and confirm it in your own environment rather than assuming a default. Mismatched tenant IDs are a common reason logs appear to vanish.

Loki multi-tenancy docs

Alloy version confirmed for your environment

Install a Grafana Alloy release that includes the loki.source.kafka component and the otelcol.receiver.kafka component you plan to use, and record the exact version in your change record. Check the release notes in your own environment, since this page does not pin a minimum Alloy version. Keep the Alloy version aligned with the Loki version you run so that write-path behavior is tested together.

Alloy Kafka example

Supported Kafka release and KRaft mode

The Apache Kafka downloads page lists 4.3.1, 4.2.2 and 4.1.2 as supported releases. Kafka 4.0 and later support only KRaft mode, so a ZooKeeper-based cluster must migrate to KRaft before it can run these releases. Confirm the broker version and mode before you plan the consumer group, because older clusters need an upgrade path first.

Kafka downloads

Java runtime for brokers and clients

Since Kafka 4.0, brokers, Kafka Connect and tools require Java 17, while clients and Kafka Streams applications need at least Java 11. Alloy is a Go binary and does not run on the JVM, so these requirements apply to the Kafka brokers and to any Java tooling your team uses to manage the cluster. Check the broker hosts' Java runtime before the upgrade window, not during it.

Kafka 4.3 upgrade notes

Topic and consumer group credential

Create the log topic, its partition count and retention with the platform team, then issue a credential that allows the Alloy consumer group to read it. Kafka supports SSL and SASL authentication, including SCRAM-SHA-512 and OAUTHBEARER, and ACLs in KRaft clusters restrict resources without rules to super users unless allow.everyone.if.no.acl.found is set. Confirm the authentication mechanism and the read ACL for the consumer group before starting Alloy.

Kafka authorization docs

How Alloy reads Kafka topics and pushes them to Loki

The flow starts with producers writing log records to Kafka topics on the brokers. Alloy joins a consumer group, reads those topics, and pushes the entries to the Loki push endpoint. When multi-tenancy is on, Alloy sends the X-Scope-OrgID header so Loki assigns each entry to the right tenant. Check the trust boundaries at each hand-off: broker authentication and TLS, the Alloy host's credentials, and the authenticating proxy in front of Loki.

Log records flow from Kafka topics through an Alloy consumer group into Loki across three trust zones.

loki.source.kafka or otelcol.receiver.kafka: which one to choose

Choose loki.source.kafka when the goal is to get log lines into Loki with as little translation as possible. The component joins a Kafka consumer group, reads records from the topics you list, and emits log entries that Alloy forwards directly to Loki. Choose otelcol.receiver.kafka when the records already follow OpenTelemetry's log model, or when a team already runs an OpenTelemetry Collector and wants Kafka-sourced logs to pass through the same processors and exporters as its other telemetry. The receiver accepts telemetry from a broker and hands it to an OpenTelemetry pipeline, so the mapping to Loki happens in the collector configuration rather than in a source component.

The first question is the shape of the messages on the topic. If producers already write OTLP-formatted log records, the OpenTelemetry path keeps resource attributes and log attributes intact and lets you decide which become Loki labels and which become structured metadata. If the topic carries plain text or application JSON, the Alloy source is usually simpler, because there is no OTLP envelope to unpack and no collector to operate. The second question is what already runs. A team with an OpenTelemetry Collector fleet and established label rules will likely want one place to define them. A team with Alloy on its log path will want Kafka handled the same way as its other sources. Neither path changes what Loki stores: labels still drive stream selection and cardinality, so the label set you choose matters more than the component.

Confirm the choice with a test on a sample topic before you commit. Point each candidate at a small copy of real messages, check consumer group offsets and lag, and query the result in Loki to see which labels appear, whether attributes landed as structured metadata, and how timestamps were set. Record the label set each option produces and the number of streams it creates. The Loki Alloy Kafka example shows the Alloy configuration to start from. Confirm with your Kafka administrators which consumer group identity and authentication method you will use before the test, because those settings are the same under either path.

Ingesting logs from Kafka into Loki step by step

This sequence takes a Kafka log topic into self-hosted Loki through Alloy, from a dedicated Kafka principal to a running consumer group and verified log lines in Grafana.

  1. Confirm the topic, partitions and retention

    Your Kafka or platform team confirms the name of the log topic, its partition count, the message format and how long messages are retained on the brokers. Record which consumer group name Alloy will use, because offsets are tracked per group. Check that Alloy can reach every broker address advertised by the cluster, not only a bootstrap address. Done when the team has a written list of topic, group and broker endpoints that the rest of the sequence refers to.

  2. Create a dedicated Kafka principal with read access

    A Kafka administrator creates a separate principal for Alloy and grants it read access to the log topic and to the consumer group it will use. In KRaft clusters, the StandardAuthorizer stores these ACLs in the cluster metadata log, and a resource with no ACL is restricted to super users unless allow.everyone.if.no.acl.found is set to true, so confirm explicit grants exist before testing. Keep the principal away from write and admin permissions. Done when a test client authenticated as that principal can read the topic and cannot produce to it.

  3. Configure TLS and SASL on the Alloy side

    The platform team stores the Kafka credentials and the broker CA certificate in the secret store used for Alloy, then configures Alloy's Kafka connection to use TLS and one of the supported SASL mechanisms agreed with the Kafka team, such as SCRAM-SHA-512 or OAUTHBEARER. The Kafka security overview confirms that TLS and SASL are optional at cluster level, so confirm the listener you are targeting actually requires them. Done when Alloy starts without authentication errors in its logs and the broker accepts the connection.

  4. Define the loki.source.kafka component

    Write the Alloy configuration with a loki.source.kafka component that lists the broker addresses, the topic, the consumer group name and the labels to attach to each stream. Choose a small set of low-cardinality labels such as the topic name and environment, and keep message content out of labels. Pipe the component output to a forwarding component rather than writing directly to storage. Done when the configuration validates and Alloy reports the component as healthy in its component view.

  5. Set the Loki write endpoint and tenant header

    Point the Loki write component at your Loki push endpoint and, when auth_enabled is true, set the X-Scope-OrgID tenant header to the tenant that should own these logs. Remember that Loki has no built-in authentication, so the endpoint must sit behind an authenticating reverse proxy that Alloy can reach with its credentials. Done when a manual test push with the same tenant header succeeds and the tenant appears in your query view.

  6. Start one Alloy instance and confirm group assignment

    Start a single Alloy instance first so that offsets and group behavior are easy to read. Check the Kafka side: the consumer group should list the topic partitions as assigned to the Alloy member. A second instance joining the same group will split partitions between members, so add replicas only after you understand that behavior. Done when the group shows every partition assigned and no rebalance loop appears in logs.

  7. Verify ingestion and consumer lag

    Produce a test message with a unique marker string to the topic, then query for that marker in Grafana using the tenant selected in the previous step. Confirm the labels match what you defined. Watch the consumer group lag for several minutes under normal traffic; lag that stays stable indicates Alloy keeps pace, while lag that grows steadily means throughput or downstream write limits need attention before go-live. Done when the test message is queryable and lag holds steady.

How Kafka record fields map to Loki labels and log lines

This table shows where each Kafka record field is likely to land in Loki: as a stream label, as structured metadata, or in the log line body. Use it to decide which values identify a stream and which should stay out of labels to protect cardinality. Confirm exact label names and any relabeling against the Alloy component reference for your chosen version.

Kafka fieldSuggested Loki destinationWhyCheck before rollout
Topic nameStream labelLow cardinality and the most common way to select a log source in queriesConfirm the label name the component sets by default in your Alloy version
Environment or cluster nameStream label, added by pipeline configFixed per deployment, so it stays bounded and supports per-environment accessSet it as a static value in the Alloy pipeline, not from the record
Partition numberUsually omit as a label, or keep only if partitions are fewBounded by partition count, but adds one stream per partition and can fragment queriesCount partitions per topic and test stream growth before enabling it
Record keyStructured metadata or log line, not a labelKeys such as user or order IDs are high cardinality and create many streamsConfirm whether the component exposes the key and how your Loki version stores it
Record headersStructured metadata for selected headersHeaders such as trace or request IDs are useful for lookup but vary per recordSelect only the headers you need and confirm their exposed names
Message valueLog line bodyThe payload is the log content that LogQL filters and parses at query timeConfirm the format (JSON, plain text) so parsing stages match it
Offset and timestampTimestamp and metadata, not labelsEach record is unique, so these must never become labelsConfirm whether the Kafka record timestamp or ingest time is used for Loki entries

Consumer group layout for one Alloy instance or many

A single Alloy instance is the simplest layout and often fits a low-volume log topic, a single site or a pilot. One consumer group with one member reads every partition of the topic, so there is no assignment to plan and no coordination between replicas. The trade-off is that one process is a single point of failure for ingestion, and its throughput has to cover the whole topic. Kafka keeps the group's committed offsets, so a restart resumes where the consumer left off rather than from the beginning, provided the group name stays the same.

  • Assignment: all partitions belong to the one member, so the consumer reads the full topic.
  • Failure impact: if the instance stops, ingestion pauses until it restarts, and logs wait in Kafka until then.
  • Capacity check: confirm that one Alloy process keeps up with peak production rates measured on your topic, not average rates.
  • Upgrades: plan a short restart window, since the group has no other member to take over the partitions.
  • Naming: pick a stable group name now, because changing it later changes where reading starts.

Before you rely on this layout, measure the topic's peak message rate and lag during a load test in a non-production environment and confirm the restart behavior with your Alloy version.

Securing the Kafka connection and the path into Loki

Kafka security is optional, so a log pipeline that reads from a broker is only as protected as the listeners, credentials and ACLs the team configures. The cards below cover the controls that connect the Kafka consumer to Loki, from client authentication and encryption to topic permissions and the access controls in front of Loki itself.

Authenticate the consumer client

Give the Alloy or OpenTelemetry consumer its own Kafka identity using SSL client certificates or one of the documented SASL mechanisms: GSSAPI (Kerberos), PLAIN, SCRAM-SHA-256, SCRAM-SHA-512 or OAUTHBEARER. Pick one mechanism per listener and confirm the broker accepts it. Do not share the producer's credentials with the consumer, so that revoking one does not break the other. Ask your Kafka team which mechanism is already used for the cluster and how credentials are rotated.

Kafka security overview

Encrypt every listener the consumer uses

Kafka's security is optional, so a cluster can expose plaintext listeners next to secured ones. Check the bootstrap servers and every advertised broker address the consumer receives, and confirm each one uses SSL/TLS. A consumer that connects to a secured bootstrap listener can still be sent to a plaintext listener for partition traffic if the advertised addresses are wrong. Verify this with a test connection and the broker configuration, not only the client config file.

Grant read on the log topic only

In KRaft clusters, the StandardAuthorizer stores ACLs in the cluster metadata log. Grant the consumer's principal READ on the log topic and READ on its consumer group, and nothing else. Avoid wildcard grants that give read access to every topic, because log topics often carry application data that was never meant for the logging team. Ask for the ACL list as an export so the team can review it before go-live.

Kafka authorization docs

Check the default for resources without ACLs

Under the KRaft authorizer, resources with no ACL are restricted to super users unless allow.everyone.if.no.acl.found is set to true. Leave that setting false in production. If it is true, any authenticated client can read topics that have no ACL, which can expose a log topic that was created without access rules. Confirm the setting on every broker, since a single misconfigured node can change the effective policy for the cluster.

Authenticate access into Loki

Loki has no built-in authentication layer, so the path from the pipeline to Loki and from users to Grafana must pass through an authenticating reverse proxy such as nginx, HAProxy, OAuth2 Proxy or Pomerium. Set auth_enabled deliberately and send each tenant's data with its own X-Scope-OrgID header. Confirm that the Kafka-sourced stream is written to the tenant it belongs to, and that no client can query another tenant's logs through the proxy.

Loki authentication docs

Common failures when ingesting logs from Kafka into Loki

This table pairs the symptoms operators usually see when Alloy or the OpenTelemetry Kafka receiver reads log topics into Loki with their likely causes, fixes and the first check to run. Read it from the top when a pipeline stops, and start with the consumer group and authorization checks before touching Loki itself.

SymptomLikely causeFix and first check
No logs arrive and the consumer group shows no assignmentsThe group has no partitions assigned, often because the topic has fewer partitions than consumers or the topic name is wrongConfirm the topic name and partition count, then check the group's member assignments with Kafka's consumer group tooling before changing Alloy
Consumer fails with authorization or authentication errorsThe client's SASL or TLS settings do not match the broker listener, or its principal lacks read access and the group ACLVerify the listener, SASL mechanism and certificates, then confirm the principal has Read on the topic and the consumer group in the authorizer
Topic reads return nothing for a resource with no ACLsKRaft's StandardAuthorizer restricts resources without ACLs to super users unless allow.everyone.if.no.acl.found is enabledAdd explicit ACLs for the Alloy principal rather than enabling open access, and test with the same credentials Alloy uses
Pushes to Loki are rejected or land in the wrong tenantMulti-tenancy is enabled but the X-Scope-OrgID header is missing or does not match a configured tenantSet the tenant ID in the Alloy Loki writer, then send one test line and query that tenant to confirm the header is applied
Consumer lag grows steadilyThe Loki write path is slow or rejecting batches, so Alloy commits offsets slowly and falls behind the topicCheck Loki write and ingester health and rate limit errors first, then review batch sizes and the number of consumers in the group
Loki reports too many streams or high cardinalityLabels were taken from message content, such as request IDs or user names, creating a new stream per valueKeep only low-cardinality labels at the edge, move other fields to structured metadata or the log line, and review the stream count per tenant
Old log lines are ingested again after a restartThe consumer group was renamed or reset, so it starts from the earliest retained offset instead of its last commitConfirm the group ID and the offset reset setting before restarting, and expect duplicate lines in Loki if the topic still retains the old data
Duplicate or missing logs after rebalancingConsumers rebalanced while offsets were uncommitted, or two pipelines share one group IDGive each pipeline its own group ID and check that only one Alloy instance reads each partition set at a time

How this differs from Loki's own Kafka-based ingestion

This page covers a pipeline in which Grafana Alloy or the OpenTelemetry Collector reads topics that already exist on your Kafka cluster and forwards the records to Loki. Loki's own Kafka-based ingestion is a separate path, and this page does not describe it. Before you rely on it, confirm in the current Loki documentation whether your Loki version supports it, what it requires, and whether it is intended for your workload. Do not assume that a feature name that sounds similar behaves the same way. Treat the Alloy or Collector route as the one this page documents, and check the Loki send data documentation for the supported ingestion methods in your release.

Start by answering three questions for your environment. First, which path does your Loki version support, and which path does your team actually want to operate? Second, who operates the consumer: your platform team running Alloy or the Collector as a service, an application team that owns the topic, or a partner? A consumer that is not clearly owned will stop quietly, and nobody will be paged for log gaps. Third, where do offsets and retries live? With loki.source.kafka, the consumer group and its committed offsets determine what is read again after a restart, so the group ID and offset behavior should be written down and reviewed.

Retry and failure handling also differ by path. If a write to Loki fails in the Alloy or Collector pipeline, you need to know whether the records are retried, dropped, or held back from committing offsets, and what happens when the pipeline is restarted during a Loki outage. Confirm these behaviors against the component documentation for your Alloy or Collector version, test them with a deliberate Loki outage in a non-production environment, and record the expected loss window. Only then decide which ingestion path your Loki deployment should standardize on.

Cost and effort for ingesting logs from Kafka into Loki

What drives the cost and effort of ingesting Kafka logs into Loki?

The main drivers are the number of topics, the record formats inside them, the label design, the security model and whether Kafka ACLs already exist. Each topic needs its own consumer group, parsing rules and label decisions, so more topics and more formats add work in roughly the same proportion. Mixed formats such as JSON in some topics and plain text or syslog in others multiply the parsing and testing. Existing SASL or SSL setup on the brokers shortens the security work, while a cluster with no ACLs needs authorization designed from the start. Ask any partner to break the quote down by these drivers.

How long does a first Kafka to Loki pipeline take to validate?

The duration depends on the scope, so no fixed figure applies. A single topic with a known format and existing broker access is a much smaller job than several topics with mixed formats, TLS or SASL to configure and tenant separation to design. Confirm the validation timeline in the written plan, including what counts as validated, such as records arriving in Grafana under the expected labels and tenant, and how long a test run covers. Ask for the milestones and the point at which the team signs off on them.

What must our team provide before an implementation starts?

Most of the delay in these projects comes from missing inputs, so prepare these before work starts:

  • Cluster access: broker addresses, the listener to use, and a service account with read rights on the chosen topics and consumer group permissions.
  • A topic list: each topic to ingest, its owner, expected volume and the retention it has in Kafka.
  • Sample records: representative messages for every format, including malformed examples, so parsing can be tested.
  • A tenant plan: which Loki tenant each topic maps to, as set through the X-Scope-OrgID header when multi-tenancy is enabled.

Ask the partner to confirm this list in the written scope.

Can our in-house team build this without a partner?

Yes, a team that already runs Alloy, Kafka and Loki can build this pipeline, and the configuration is documented in the Loki examples for Alloy. The skills needed are Kafka consumer group behavior, Kafka security settings, Alloy component configuration, Loki label design and tenant handling. The usual gap is not the first working pipeline but the repeatable parts: label standards across topics, verification of what is dropped, and monitoring of consumer lag. Compare your team's time against the list of drivers above and decide whether a partner should take the design, the build or only the review.

Does using Kafka with Loki require a Kafka license?

Apache Kafka is licensed under the Apache License 2.0 (SPDX: Apache-2.0), and the Apache Software Foundation project has no paid edition of its own. Running the open-source brokers therefore carries no license fee from the project. Costs still come from the infrastructure, the people who operate the cluster, and any commercial Kafka distribution or managed service you choose, which has its own terms. Confirm the licensing of any vendor distribution or hosted service before you rely on it in the plan.

Which costs recur after the pipeline goes live?

The one-time costs are design, build, and the first validation. Recurring costs include running the Alloy instances that consume the topics, storage and query load in Loki, broker capacity, monitoring of consumer lag, and upgrades of Alloy, Loki and Kafka. Kafka upgrades also carry their own requirements, such as Java 17 for brokers since Kafka 4.0 and KRaft-only operation. Ask for the recurring effort in the quote, including who performs upgrades and how often topic and label changes are reviewed.

Does a security review add cost to the Kafka pipeline?

Yes, when the security model is not already settled. Kafka authenticates clients with SSL or SASL, and the documented SASL mechanisms include GSSAPI, PLAIN, SCRAM-SHA-256, SCRAM-SHA-512 and OAUTHBEARER. Authorization in KRaft clusters uses ACLs stored in the metadata log, and resources without ACLs are restricted to super users unless the allow.everyone.if.no.acl.found setting is true. Reviewing these choices, testing the consumer permissions and documenting the evidence for your security team adds effort, so include it explicitly in the scope.

How DeployOpen scopes and delivers the Kafka to Loki integration

DeployOpen plans, builds and documents the Kafka to Loki path inside your own infrastructure, starting from the topics, consumer groups and access rules your teams already run. Engagements are fixed-scope, with deliverables and timelines agreed in writing before work starts.

How we scope

  • Inventory the Kafka topics, brokers, listeners and authentication method that the log pipeline must read from
  • Confirm whether Alloy's loki.source.kafka or the OpenTelemetry Kafka receiver fits your existing Alloy and collector estate
  • Agree consumer group naming, offset handling and the labels each topic will produce in Loki

How we build

  • Configure the Alloy or collector pipeline with TLS and SASL settings taken from your broker security policy
  • Test consumer group rebalancing, restarts and lag behavior against a non-production topic before cutover
  • Verify that Loki receives the expected streams and that label cardinality stays within the limits you agreed

How we hand over

  • Deliver the pipeline configuration, topic-to-tenant mapping and ACL requirements as version-controlled files
  • Document the runbook for consumer lag, credential rotation and reprocessing from Kafka offsets
  • Walk your platform and security teams through the setup so they can review and operate it

How we support

  • Offer ongoing support, upgrades and continuous testing as a retainer after the handover
  • Re-test the pipeline when Kafka or Alloy versions change, including authentication and authorization settings
  • Confirm support terms, response expectations and included upgrade work in the written scope before starting

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.