Documentation
¶
Overview ¶
Package loki talks to the Loki HTTP API: labels and their values, log queries and the live tail.
Index ¶
- Variables
- func Describe(err error) string
- func Format(line string) string
- func IsStatus(err error, codes ...int) bool
- func Pretty(line string) (string, bool)
- func Selector(matchers ...Matcher) string
- func Selects(matchers ...Matcher) bool
- func StreamSelector(labels Labels) string
- func VaryingLabels(streams []Labels) []string
- func WithLineFilter(query, text string) string
- type BuildInfo
- type Client
- func (c *Client) Addr() string
- func (c *Client) BuildInfo(ctx context.Context) (BuildInfo, error)
- func (c *Client) Follow(ctx context.Context, query string, start time.Time, limit int, ...)
- func (c *Client) LabelValues(ctx context.Context, name, selector string, start, end time.Time) ([]string, error)
- func (c *Client) Labels(ctx context.Context, selector string, start, end time.Time) ([]string, error)
- func (c *Client) QueryRange(ctx context.Context, query string, start, end time.Time, limit int, ...) ([]Entry, error)
- func (c *Client) Tail(ctx context.Context, query string, start time.Time, limit int, opened func(), ...) error
- func (c *Client) Volume(ctx context.Context, selector, target string, start, end time.Time) (map[string]uint64, error)
- type Dropped
- type Entry
- type Field
- type FollowEvent
- type FollowState
- type Labels
- type Level
- type Matcher
- type RefusedError
- type StatusError
- type TailBatch
Constants ¶
This section is empty.
Variables ¶
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 ¶
Describe sums up a query error for the interface: Loki wraps parse errors in a lot of detail.
func Selector ¶
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 ¶
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 ¶
StreamSelector is the selector that picks exactly the stream with these labels.
func VaryingLabels ¶
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 ¶
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 ¶
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) 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 ¶
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.
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 Level ¶
type Level int
Level is the severity of a log line, as far as it can be told.
func DetectLevel ¶
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 ¶
ParseLevel reads a level as loggers write it: "WARNING", "err", "I", "50", ...
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.
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 ¶
StatusError is a response the server answered with an error status.
func (*StatusError) Error ¶
func (e *StatusError) Error() string