Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Grafana Loki ने Container Logs एकत्र करा (Beginner Path)

Loki + agent + Grafana DS + LogQL labels first + low cardinality + retention.

Friends, Loki stores logs like Prometheus stores metrics — labels matter. Chala mitrano — never make request_id a label.

Quick answer

Hold this vertical picture:

  1. Loki start.
  2. agent push.
  3. Grafana DS.
  4. {app=} |= ERROR.
  5. cardinality; retention.

Language: English · मराठी · हिंदी

ContainersWrite logs Agent tailsAdd labels Loki storesIndexed labels Grafana ExploreLogQL Find ERROR lineFix faster
Loki path

Loki का

central > SSH.

Why: pod गेला = logs गेले. What: agent→Loki→Explore. How: labels index.

Example 1: Why central logs

Type this:

printf '%s\n' 'ok: stop SSH-grep on eight nodes — ship logs to one place'

You will see:

ok: stop SSH-grep on eight nodes — ship logs to one place

What it means: Outages hide when logs stay on dying pods.

Example 2: Loki config sketch

Type this:

mkdir -p /tmp/loki && printf '%s\n' 'auth_enabled: false' 'server: {http_listen_port: 3100}' 'common:' '  path_prefix: /tmp/loki-data' '  storage: {filesystem: {chunks_directory: /tmp/loki-data/chunks}}' 'schema_config:' '  configs:' '  - from: 2024-01-01' '    store: tsdb' '    object_store: filesystem' '    schema: v13' '    index: {prefix: index_, period: 24h}' > /tmp/loki/loki.yml && head -3 /tmp/loki/loki.yml

You will see:

auth_enabled: false

What it means: Lab only — turn auth on for real clusters.

Example 3: Agent ships container logs

Type this:

printf '%s\n' 'Promtail/alloy/agent tails /var/log/containers and pushes to Loki push API'

You will see:

Promtail/alloy/agent tails /var/log/containers and pushes to Loki push API

What it means: Agent labels include namespace, pod, container.

Concept 1: Ship/query

DS + LogQL.

Why: wrong range = empty. What: {app=} then |=. How: Explore.

Example 4: Grafana Loki datasource

Type this:

printf '%s\n' '{' '  "name": "Loki",' '  "type": "loki",' '  "url": "http://loki:3100"' '}' > /tmp/loki/ds.json && cat /tmp/loki/ds.json

You will see:

{

What it means: Explore uses this URL.

Example 5: LogQL by app

Type this:

printf '%s\n' '{app="payments"} |= "ERROR"'

You will see:

{app="payments"} |= "ERROR"

What it means: Label filter first; then line filter — cheaper.

Example 7: Time range tip

Type this:

printf '%s\n' 'Widen the Grafana time picker when the incident started earlier than you think'

You will see:

Widen the Grafana time picker when the incident started earlier than you think

What it means: Wrong range looks like "no logs".

Concept 2: Metrics+logs

when vs why.

Why: CPU without logs. What: same range/pod. How: count_over_time.

Example 6: Count errors

Type this:

printf '%s\n' 'sum(count_over_time({app="payments"} |= "ERROR" [5m]))'

You will see:

sum(count_over_time({app="payments"} |= "ERROR" [5m]))

What it means: Turn logs into a number for a panel.

Example 9: Correlate with metrics

Type this:

printf '%s\n' 'When CPU spikes, jump to Loki with the same time range and pod label'

You will see:

When CPU spikes, jump to Loki with the same time range and pod label

What it means: Metrics find when; logs find why.

Example 12: Prove with Explore

Type this:

printf '%s\n' 'Grafana Explore → Loki → {app="payments"} → Run query'

You will see:

Grafana Explore → Loki → {app="payments"} → Run query

What it means: If empty: agent, labels, time range, auth.

Concept 3: Cardinality

index/disk वाचवा.

Why: request_id label = melt. What: low-card; retention. How: JSON care.

Example 8: Label cardinality warning

Type this:

printf '%s\n' 'Do not put request_id as a label — keep it in the line; labels must stay low-cardinality'

You will see:

Do not put request_id as a label — keep it in the line; labels must stay low-cardinality

What it means: High cardinality melts Loki — interview favorite.

Example 10: Retention

Type this:

printf '%s\n' 'Set retention so demo logs do not fill the disk — e.g. 7 or 14 days for labs'

You will see:

Set retention so demo logs do not fill the disk — e.g. 7 or 14 days for labs

What it means: Prod retention follows compliance.

Example 11: Pipeline stages idea

Type this:

printf '%s\n' 'Agent can parse JSON and promote level=error into a label carefully'

You will see:

Agent can parse JSON and promote level=error into a label carefully

What it means: Only low-cardinality fields become labels.

Loki card

1 Loki 2 agent 3 DS 4 LogQL 5 cardinality 6 retention

Suppose: no logs

agent/labels/range.

Suppose: slow

high cardinality.

Ravindra Bagale's Tip

At Infosys interviews, explain: label filter first ({app=}), then |= text. Reviewers fail candidates who grep everything client-side.

Check yourself (quiz)

Q1. ship?

agent

Q2. LogQL?

labels first

Q3. high card?

request_id

Q4. empty?

agent/labels/range

Q5. metrics vs logs?

when/why

Q6. lab auth?

local only off

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

agent → Loki → Explore → LogQL → fix. Did you understand? Let us try it once more.

Frequently asked questions

Loki vs Elasticsearch?

Loki indexes labels heavily; cheaper for many teams.

Promtail vs Alloy?

Both can ship; follow team standard.

Why labels first?

Cheaper query path.

No logs?

Agent, labels, auth, time range.

JSON logs?

Parse in agent; promote only safe fields.

Auth in prod?

Yes — lab false is temporary.