loki

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package loki implements providers.LogsProvider against Grafana Loki, querying with LogQL over /loki/api/v1/* and normalizing the streams response into log lines. It mirrors internal/logs/victorialogs: same construction/auth shape, same optional LogStats/LogFields capabilities, same truncation sentinel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client queries a Grafana Loki backend.

func New

func New(baseURL string) *Client

New builds a client for a Loki base URL, unauthenticated.

func NewWithAuth

func NewWithAuth(baseURL, tokenEnv string, headers map[string]string) *Client

NewWithAuth builds a client that adds optional auth to every request; the semantics are identical to victorialogs.NewWithAuth (token read from the env at request-build time, never logged).

func (*Client) FieldNames

func (c *Client) FieldNames(ctx context.Context, query string, w providers.TimeWindow) ([]providers.FieldCount, error)

FieldNames lists the field names present in the logs matching query over the window — the log-schema discovery path (providers.LogFields). Loki splits the answer across two endpoints, so both are merged: STREAM labels from /loki/api/v1/labels (query-scoped; these are what a selector is built from, so they come first, Hits=0 — Loki reports no per-label hit count) and parsed body fields from /loki/api/v1/detected_fields (Loki 3.x; Hits carries the reported value cardinality). A missing detected_fields endpoint (older Loki) degrades to labels-only; only both failing is an error.

func (*Client) Hits

func (c *Client) Hits(ctx context.Context, query string, w providers.TimeWindow, step time.Duration) ([]providers.Bucket, error)

Hits returns the per-step match count over the window, split by severity, by wrapping the log query in the LogQL metric form `sum by (<level>) (count_over_time(<q> [<step>]))` — the Loki analogue of VictoriaLogs' /select/logsql/hits, powering the logs_error_summary histogram. The level label defaults to detected_level (Loki 3.x structured metadata); series without the label fold into one Level=="" bucket series, which the renderer already handles. A step <= 0 defaults to one minute.

func (*Client) Query

Query runs a LogQL query over the window and returns normalized log lines, newest first (matching the VictoriaLogs provider's ordering). It sends one request with limit=maxLines and direction=backward; when the server returns exactly the limit, more entries likely matched and the shared truncation sentinel is appended (providers.TruncationLine).

func (*Client) TopMessages

func (c *Client) TopMessages(ctx context.Context, query string, w providers.TimeWindow, k int) ([]providers.MsgCount, error)

TopMessages returns up to k dominant messages over the window. LogQL cannot group by message content (no `stats by (_msg)` equivalent; the /patterns endpoint is experimental and needs the pattern ingester), so this aggregates CLIENT-SIDE over the capped Query sample: numeric tokens collapse so near-identical lines group (mirroring collapse_nums), counts/first/last are per-sample (up to maxLines newest lines), which is enough to NAME what floods the logs — the only question logs_error_summary asks of it. k <= 0 defaults to 10.

func (*Client) WithLevelField

func (c *Client) WithLevelField(field string) *Client

WithLevelField overrides the severity label Hits splits by. Empty is a no-op so an unset config keeps the default; returns the client for chaining.

Jump to

Keyboard shortcuts

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