Shipping Logs: Promtail to Grafana Alloy
Loki doesn't collect logs; an agent on every node tails log files (or the container runtime's logs), attaches labels, and pushes batches to Loki. For years that agent was Promtail. It no longer is:
Promtail reached end of life on March 2, 2026: no more updates or support. Grafana's replacement is Grafana Alloy, and all new development happens there. (The deprecation doesn't cover
lambda-promtail.)
If you meet Promtail in an existing cluster, plan a migration; for anything new, use Alloy, the OpenTelemetry Collector, or Fluent Bit (next lesson).
Grafana Alloy
Alloy is Grafana's distribution of the OpenTelemetry Collector plus Prometheus and Loki pipelines, in one binary, configured in Alloy syntax (blocks wired together by references). One Alloy per node can collect logs, scrape metrics and receive traces. A minimal file-tailing pipeline:
// config.alloy
local.file_match "linkly" {
path_targets = [{"__path__" = "/var/log/linkly/*.log", "app" = "linkly", "env" = "prod"}]
}
loki.source.file "linkly" {
targets = local.file_match.linkly.targets
forward_to = [loki.process.json.receiver]
}
loki.process "json" {
stage.json {
expressions = {level = "level", trace_id = "trace_id", ts = "time"}
}
stage.timestamp {
source = "ts"
format = "RFC3339"
}
stage.labels {
values = {level = ""} // a bounded value: OK as a label
}
stage.structured_metadata {
values = {trace_id = ""} // unbounded: structured metadata, not a label
}
forward_to = [loki.write.default.receiver]
}
loki.write "default" {
endpoint {
url = "http://loki:3100/loki/api/v1/push"
}
}Each block is a component with a type (loki.process) and a name ("json"); forward_to and the .targets/.receiver references build the pipeline graph, which Alloy shows in its UI on port 12345. Stages of loki.process correspond one to one with Promtail's pipeline_stages.
In Kubernetes, Alloy reads pod logs through the API or from /var/log/pods with discovery.kubernetes + discovery.relabel + loki.source.kubernetes, and is installed with the grafana/alloy Helm chart (or the k8s-monitoring chart, which bundles it with sensible defaults).
Migrating from Promtail
A Promtail config for the same job:
# promtail.yaml (legacy)
server: {http_listen_port: 9080}
positions: {filename: /var/lib/promtail/positions.yaml}
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: linkly
static_configs:
- targets: [localhost]
labels: {app: linkly, env: prod, __path__: /var/log/linkly/*.log}
pipeline_stages:
- json: {expressions: {level: level, trace_id: trace_id, ts: time}}
- timestamp: {source: ts, format: RFC3339}
- labels: {level: ""}
- structured_metadata: {trace_id: ""}Alloy converts it for you, and can even run the old file directly while you migrate:
# sample session
$ alloy convert --source-format=promtail --output=config.alloy promtail.yaml
$ alloy run --config.format=promtail promtail.yaml # run the Promtail file as is (temporary)
$ alloy fmt config.alloy # format the converted fileReview the converted file (names are generated from job names), keep the positions file path in mind (Alloy keeps its own; without care it re-reads files from the start or from the end), and roll out node by node.
A shipper you can run here: the Collector
Alloy isn't in the sandbox, but its OpenTelemetry side is: the OpenTelemetry Collector can tail files too, and Loki 3 accepts OTLP natively on /otlp. This session ships a JSON log file to a real Loki with the Collector's file_log receiver, using each line's own time as the timestamp:
cat > loki.yaml <<'YML'
auth_enabled: false
server: {http_listen_address: 127.0.0.1, http_listen_port: 3100, grpc_listen_address: 127.0.0.1, log_level: error}
common:
path_prefix: /home/learner/loki
instance_addr: 127.0.0.1
replication_factor: 1
ring: {kvstore: {store: inmemory}}
storage: {filesystem: {chunks_directory: /home/learner/loki/chunks, rules_directory: /home/learner/loki/rules}}
schema_config:
configs:
- {from: 2020-01-01, store: tsdb, object_store: filesystem, schema: v13, index: {prefix: index_, period: 24h}}
limits_config: {reject_old_samples: false}
ingester: {max_chunk_age: 87600h, chunk_idle_period: 87600h, lifecycler: {min_ready_duration: 0s}}
querier: {query_ingesters_within: 87600h}
analytics: {reporting_enabled: false}
YML
loki -config.file=loki.yaml >loki.log 2>&1 &
mkdir logs && cat > logs/linkly.log <<'LOG'
{"time":"2026-01-01T10:00:01Z","level":"INFO","msg":"redirect","status":302,"duration_ms":4}
{"time":"2026-01-01T10:00:05Z","level":"ERROR","msg":"create link failed","status":500,"duration_ms":2003,"trace_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
{"time":"2026-01-01T10:00:07Z","level":"WARN","msg":"slow redirect","status":302,"duration_ms":480}
LOG
cat > otel.yaml <<'YML'
receivers:
file_log:
include: [./logs/*.log]
start_at: beginning
operators:
- type: json_parser # line → attributes; time and level from the JSON
timestamp: {parse_from: attributes.time, layout_type: gotime, layout: '2006-01-02T15:04:05Z07:00'}
severity: {parse_from: attributes.level}
processors:
resource:
attributes:
- {key: service.name, value: linkly, action: upsert}
- {key: deployment.environment.name, value: prod, action: upsert}
exporters:
otlp_http:
endpoint: http://127.0.0.1:3100/otlp # Loki's native OTLP endpoint
service:
telemetry: {logs: {level: error}}
pipelines:
logs:
receivers: [file_log]
processors: [resource]
exporters: [otlp_http]
YML
otelcol-contrib validate --config=otel.yaml && echo "config valid"
for i in $(seq 150); do [ "$(curl -s 127.0.0.1:3100/ready)" = ready ] && break; sleep 0.2; done
otelcol-contrib --config=otel.yaml >collector.log 2>&1 &
R='--from=2026-01-01T10:00:00Z --to=2026-01-01T11:00:00Z'
for i in $(seq 50); do [ "$(logcli query --quiet $R -o raw '{service_name="linkly"}' | wc -l)" = 3 ] && break; sleep 0.2; done
echo "--- stream labels:"; logcli labels --quiet $R
echo "--- all lines:"; logcli query --quiet $R --forward -o raw '{service_name="linkly"}'
echo "--- filter on structured metadata, no parsing:"
logcli query --quiet $R -o raw '{service_name="linkly"} | severity_text = "ERROR" | trace_id != ""'config valid
--- stream labels:
deployment_environment_name
service_name
--- all lines:
{"time":"2026-01-01T10:00:01Z","level":"INFO","msg":"redirect","status":302,"duration_ms":4}
{"time":"2026-01-01T10:00:05Z","level":"ERROR","msg":"create link failed","status":500,"duration_ms":2003,"trace_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
{"time":"2026-01-01T10:00:07Z","level":"WARN","msg":"slow redirect","status":302,"duration_ms":480}
--- filter on structured metadata, no parsing:
{"time":"2026-01-01T10:00:05Z","level":"ERROR","msg":"create link failed","status":500,"duration_ms":2003,"trace_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
- Loki turned two resource attributes into stream labels (
service_name,deployment_environment_name; dots become underscores) and stored the per-line attributes (level,status,trace_id, severity…) as structured metadata, so the last query filters on them without a| jsonstage. - The lines keep their original timestamps (10:00:01…), not the time the Collector read them: that's what the
timestampsetting of the parser does. Without it, a backlog shipped late lands at the wrong time. - In production, add the
file_storageextension so the Collector remembers how far it read each file across restarts (Alloy and Promtail call it the positions file).
Test yourself
What is the status of Promtail?
- The recommended Loki agent
- End of life since March 2, 2026; replaced by Grafana Alloy
- Merged into Loki
- Renamed to Fluent Bit
Which command converts a Promtail config to Alloy syntax?
promtail migratealloy convert --source-format=promtail --output=config.alloy promtail.yamlloki convertalloy import promtail
In the Alloy pipeline, why is trace_id stored as structured metadata rather than a label?
- Labels can't contain hex
- It's unbounded; as a label it would create one stream per trace
- Structured metadata is indexed
- Loki requires it