loki

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package loki provides a client implementation for sending log entries to a Loki server. It supports batching, retries with backoff, and metrics for monitoring the client's behavior.

The Client struct is the main component of this package, responsible for managing log entries, batching them, and sending them to the Loki server. It also provides support for multi-tenancy and external labels.

Metrics: - encoded_bytes_total: Number of bytes encoded and ready to send. - sent_bytes_total: Number of bytes successfully sent. - dropped_bytes_total: Number of bytes dropped due to failed retries. - sent_entries_total: Number of log entries successfully sent. - dropped_entries_total: Number of log entries dropped due to failed retries. - request_duration_seconds: Duration of send requests. - batch_retries_total: Number of times batches have been retried. - promtail_stream_lag_seconds: Difference between current time and last batch timestamp for successful sends.

Key Features: - Batching: Log entries are batched based on size and time constraints. - Retry with Backoff: Failed requests are retried with exponential backoff. - Multi-Tenancy: Supports sending logs to multiple tenants using the X-Scope-OrgID header. - External Labels: Allows adding external labels to log entries. - Metrics: Exposes Prometheus metrics for monitoring the client's behavior.

Usage: - Use New or NewWithLogger to create a new Client instance. - Call Handle or HandleWithMetadata to add log entries to the client. - Call Stop to gracefully stop the client and send any remaining batches.

Configuration:

  • Config struct allows customization of the client's behavior, including batch size, batch wait time, timeout, and backoff settings. Defaults are applied if not explicitly set.

Example:

cfg := loki.Config{
    URL: &url.URL{Host: "loki.example.com"},
    BatchSize: 1024 * 1024, // 1MB
    BatchWait: 1 * time.Second,
}
client, err := loki.New(cfg)
if err != nil {
    log.Fatalf("failed to create loki client: %v", err)
}
defer client.Stop()

labels := model.LabelSet{"job": "example"}
client.Handle(labels, time.Now(), "This is a log entry")

Index

Constants

View Source
const (
	JSONContentType = "application/json"

	// Label reserved to override the tenant ID while processing
	// pipeline stages
	ReservedLabelTenantID = "__tenant_id__"

	LatencyLabel = "filename"
	HostLabel    = "host"
)
View Source
const (
	BatchWait      = 1 * time.Second
	BatchSize  int = 1024 * 1024
	MinBackoff     = 500 * time.Millisecond
	MaxBackoff     = 5 * time.Minute
	MaxRetries int = 10
	Timeout        = 10 * time.Second
)

NOTE the helm chart for promtail and fluent-bit also have defaults for these values, please update to match if you make changes here.

Variables

View Source
var (
	UserAgent = fmt.Sprintf("promtail/%s", version.Version)
)

Functions

This section is empty.

Types

type Client

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

func New

func New(cfg Config) (*Client, error)

New makes a new Client from config

func NewWithDefault

func NewWithDefault(url string) (*Client, error)

NewWithDefault creates a new client with default configuration.

func NewWithLogger

func NewWithLogger(cfg Config, logger *slog.Logger) (*Client, error)

NewWithLogger makes a new Client from a logger and a config

func (*Client) Handle

func (c *Client) Handle(ls model.LabelSet, t time.Time, s string) error

Handle implement EntryHandler; adds a new line to the next batch; send is async.

func (*Client) HandleWithMetadata

func (c *Client) HandleWithMetadata(ls model.LabelSet, t time.Time, s string, m push.LabelsAdapter) error

Handle implement EntryHandler; adds a new line to the next batch; send is async.

func (*Client) Stop

func (c *Client) Stop()

Stop the client.

func (*Client) UnregisterLatencyMetric

func (c *Client) UnregisterLatencyMetric(labels model.LabelSet)

type Config

type Config struct {
	URL       urlutil.URLValue
	BatchWait time.Duration
	BatchSize int

	Client config.HTTPClientConfig `yaml:",inline"`

	BackoffConfig backoff.BackoffConfig `yaml:"backoff_config"`
	// The labels to add to any time series or alerts when communicating with loki
	ExternalLabels labelutil.LabelSet `yaml:"external_labels,omitempty"`
	Timeout        time.Duration      `yaml:"timeout"`

	// The tenant ID to use when pushing logs to Loki (empty string means
	// single tenant mode)
	TenantID string `yaml:"tenant_id"`

	// Use Loki JSON api as opposed to the snappy protobuf.
	EncodeJson bool `yaml:"encode_json"`
}

Config describes configuration for a HTTP pusher client.

func NewDefaultConfig

func NewDefaultConfig(url string) (Config, error)

NewDefaultConfig creates a default configuration for a given target Loki URL.

func (*Config) RegisterFlags

func (c *Config) RegisterFlags(flags *flag.FlagSet)

RegisterFlags registers flags.

func (*Config) RegisterFlagsWithPrefix

func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

RegisterFlags with prefix registers flags where every name is prefixed by prefix. If prefix is a non-empty string, prefix should end with a period.

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implement Yaml Unmarshaler

type LokiHandler

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

func NewLokiHandler

func NewLokiHandler(client *Client, level slog.Level, labels model.LabelSet) *LokiHandler

func (*LokiHandler) Enabled

func (h *LokiHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*LokiHandler) Handle

func (h *LokiHandler) Handle(ctx context.Context, record slog.Record) error

func (*LokiHandler) WithAttrs

func (h *LokiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LokiHandler) WithGroup

func (h *LokiHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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