loki

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package loki talks to the Loki HTTP API: labels and their values, log queries and the live tail.

Index

Constants

This section is empty.

Variables

View Source
var ErrTailClosed = errors.New("the server closed the tail")

ErrTailClosed is returned when the server ends the tail, which it does once tail_max_duration has passed.

Functions

func Describe

func Describe(err error) string

Describe sums up a query error for the interface: Loki wraps parse errors in a lot of detail.

func Format

func Format(line string) string

Format names how a line is structured.

func IsStatus

func IsStatus(err error, codes ...int) bool

IsStatus reports whether err is a response with one of the given status codes.

func Pretty

func Pretty(line string) (string, bool)

Pretty indents a JSON line, or returns the line as it is.

func Selector

func Selector(matchers ...Matcher) string

Selector renders matchers as a stream selector, in the order given, leaving out the ones without values. It returns "" when none has a value.

func Selects

func Selects(matchers ...Matcher) bool

Selects reports whether a selector picks streams by itself. Loki refuses selectors whose every matcher also matches the empty value, like {app!="api"}, so at least one must include a value.

func StreamSelector

func StreamSelector(labels Labels) string

StreamSelector is the selector that picks exactly the stream with these labels.

func VaryingLabels

func VaryingLabels(streams []Labels) []string

VaryingLabels returns the names of the labels whose values differ between the streams, or that some streams lack, sorted. Labels every stream shares say nothing about a line.

func WithLineFilter

func WithLineFilter(query, text string) string

WithLineFilter appends a line filter for text to a query, or returns it as is for empty text.

Types

type BuildInfo

type BuildInfo struct {
	Version   string `json:"version"`
	Revision  string `json:"revision"`
	Branch    string `json:"branch"`
	GoVersion string `json:"goVersion"`
}

BuildInfo is what the server reports about its build.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client calls the API of one Loki server or gateway.

func NewClient

func NewClient(addr string, httpClient *http.Client, header http.Header) (*Client, error)

NewClient returns a client for the server at addr. Every request carries header, which holds the authentication and the tenant. The http client carries the tls configuration.

func (*Client) Addr

func (c *Client) Addr() string

Addr is the address of the server, without credentials.

func (*Client) BuildInfo

func (c *Client) BuildInfo(ctx context.Context) (BuildInfo, error)

BuildInfo asks the server for its version.

func (*Client) Follow

func (c *Client) Follow(ctx context.Context, query string, start time.Time, limit int, events chan<- FollowEvent)

Follow tails query for as long as ctx lives, dialing again when the connection drops, and sends what happens on events. It starts with up to limit entries since start. A tail opened again resumes after the last entry seen, so entries are neither missed, up to resumeLimit, nor sent twice. Follow closes events when it returns.

func (*Client) LabelValues

func (c *Client) LabelValues(ctx context.Context, name, selector string, start, end time.Time) ([]string, error)

LabelValues lists the values of the label seen between start and end, of the streams selector matches, or of all streams when it is empty.

func (*Client) Labels

func (c *Client) Labels(ctx context.Context, selector string, start, end time.Time) ([]string, error)

Labels lists the label names seen between start and end. With a selector set, only the names of the streams it matches are listed; servers before Loki 3 ignore it.

func (*Client) QueryRange

func (c *Client) QueryRange(ctx context.Context, query string, start, end time.Time, limit int, backward bool) ([]Entry, error)

QueryRange runs a log query between start and end and returns at most limit entries, the newest when backward is set, oldest first either way.

func (*Client) Tail

func (c *Client) Tail(ctx context.Context, query string, start time.Time, limit int, opened func(), fn func(TailBatch)) error

Tail follows the entries query selects, starting with up to limit entries since start, and calls fn with every batch until ctx is done or the connection fails. opened, when set, is called once the server accepted the tail. It needs a websocket to the server; a gateway that does not pass them on answers the upgrade with an error status.

func (*Client) Volume

func (c *Client) Volume(ctx context.Context, selector, target string, start, end time.Time) (map[string]uint64, error)

Volume reports the bytes ingested between start and end per value of the label target, for the streams selector matches. It needs Loki 3 with volume_enabled, other servers answer 404 or 400.

type Dropped

type Dropped struct {
	Time   time.Time
	Labels Labels
}

Dropped is an entry the server left out of the tail because the client did not keep up.

type Entry

type Entry struct {
	Time   time.Time
	Line   string
	Labels Labels // the labels of the stream, as indexed
	// Stream is Labels as a selector, worked out once for all the entries of a stream. Entries
	// made elsewhere may leave it empty, StreamKey then works it out.
	Stream string
	// Metadata and Parsed are only told apart from the stream labels when the server categorizes
	// them, which Loki 3 does. Older servers fold them into Labels.
	Metadata Labels // structured metadata attached to the line
	Parsed   Labels // labels extracted by a parser stage of the query
}

Entry is one log line with the stream it belongs to.

func (Entry) Key

func (e Entry) Key() string

Key identifies an entry: two entries with the same time, line and stream are the same entry.

func (Entry) StreamKey added in v1.1.0

func (e Entry) StreamKey() string

StreamKey identifies the stream of the entry: its labels as a selector.

type Field

type Field struct {
	Key   string
	Value string
}

Field is a key and value read from a structured log line.

func Fields

func Fields(line string) []Field

Fields reads the top level fields of a JSON object or a logfmt line, in the order they are written. It returns nil for a line that is neither.

type FollowEvent

type FollowEvent struct {
	State   FollowState
	Batch   TailBatch
	Err     error     // why the tail failed, when Retrying or Failed
	RetryAt time.Time // when the tail is dialed again, when Retrying
}

FollowEvent is what Follow reports: entries, or a change of state.

type FollowState

type FollowState int

FollowState is where a follow stands with the server.

const (
	Connecting FollowState = iota // dialing, the first time
	Live                          // the tail is open
	Retrying                      // the tail failed and is dialed again at RetryAt
	Failed                        // the server refused the tail, it is not tried again
)

type Labels

type Labels map[string]string

Labels is a set of label names and values, of a stream or of an entry.

func (Labels) String

func (l Labels) String() string

String renders the labels as a LogQL stream selector, sorted by name.

type Level

type Level int

Level is the severity of a log line, as far as it can be told.

const (
	LevelUnknown Level = iota
	LevelTrace
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

func DetectLevel

func DetectLevel(e Entry) Level

DetectLevel finds the level of an entry: in its labels, its structured metadata, its fields, or a level word near the start of the line.

func ParseLevel

func ParseLevel(s string) Level

ParseLevel reads a level as loggers write it: "WARNING", "err", "I", "50", ...

func (Level) String

func (l Level) String() string

type Matcher

type Matcher struct {
	Label   string
	Values  []string
	Exclude bool // match streams without these values
}

Matcher picks values of one label for a stream selector.

func (Matcher) String

func (m Matcher) String() string

String renders the matcher in LogQL: = or != for one value, =~ or !~ for several.

type RefusedError

type RefusedError struct {
	Reason string
}

RefusedError is the server closing a tail with a reason before it sent anything, which is how Loki turns down a query it accepted the websocket for, like a metric query.

func (*RefusedError) Error

func (e *RefusedError) Error() string

type StatusError

type StatusError struct {
	Code    int
	Message string
}

StatusError is a response the server answered with an error status.

func (*StatusError) Error

func (e *StatusError) Error() string

type TailBatch

type TailBatch struct {
	Entries []Entry
	Dropped []Dropped
}

TailBatch is one message of the live tail: the entries it carries, oldest first, and the ones the server dropped because the client fell behind.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL