This document describes coding conventions for the Loki codebase. Most of these rules are enforced automatically by make lint (golangci-lint). Run it before submitting a pull request.
Imports must be grouped into three sections, separated by blank lines:
- Standard library
- External packages
- Internal packages (
github.com/grafana/loki/...)
import (
"context"
"fmt"
"github.com/go-kit/log"
"github.com/prometheus/common/model"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logql"
)Use goimports (called by make lint) to sort and group imports automatically.
- Always check returned errors. The linter (
errcheck) flags unchecked errors. - Wrap errors with context using
fmt.Errorf("doing X: %w", err)so callers can inspect them witherrors.Is/errors.As. - Do not swallow errors silently; if an error is intentionally ignored, document why.
Follow standard Go naming conventions:
- Use
MixedCapsfor exported names,mixedCapsfor unexported. - Acronyms are consistently cased:
HTTPServer, notHttpServer;userID, notuserId. - Interface names describing a single method typically end in
-er:Reader,Writer,Flusher. - Test helper functions that call
t.Fatalaccepttesting.TB, not*testing.T, so they can be reused in benchmarks.
- Prefer table-driven tests to reduce boilerplate and make adding cases easy.
- Place tests in a
_testpackage (e.g.,package logql_test) to test the public API by default. Use the same package only when testing unexported internals. - Use
require(notassert) for checks where a failure should stop the test immediately — this avoids misleading failures cascading from an initial error. - Integration tests must be gated with the
integrationbuild tag and live under./integration/. Run them withmake test-integration.
Use github.com/go-kit/log (not github.com/go-kit/kit/log, which is deprecated). The depguard linter enforces this.
Structured log lines use key-value pairs:
level.Info(logger).Log("msg", "starting ingester", "addr", addr, "component", "ingester")- Use
"msg"as the first key to describe the event. - Keep values scalar where possible; avoid embedding structured data in a single string value.
- Error log lines include
"err", err.
The linter enforces the following import restrictions:
| Forbidden | Use instead |
|---|---|
github.com/go-kit/kit/log |
github.com/go-kit/log |