slogotlp

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

slogoltp

Go Reference

OpenTelemetry oltp exporter support for slog.

  • Go 1.21+ support as we use the new stdlib slog.
  • Currently , only grpc otlp is supported.

Usage

import (
    "context"
    "log/slog"

    "github.com/bakins/slogotlp"
)

func main() {
    handler, err := slogotlp.NewHandler(context.Background())
    // handle err

    defer handler.Shutdown(context.Background())

    logger := slog.New(handler)

    // use logger
}

By default, slogotlp uses environment variables for configuration.

  • checks OTEL_EXPORTER_OTLP_LOGS_ENDPOINT then `OTEL_EXPORTER_OTLP_ENDPOINT`` for the exporter endpoint. This should be a url like "http://localhost:4317". There is no default.
  • If the scheme in the endpoint url is "http" or the environment variable OTEL_EXPORTER_OTLP_LOGS_INSECURE or OTEL_EXPORTER_OTLP_INSECURE is set to "true", then insecure communication is used.

Currently, no other OpenTelemetry environment variables are supported.

Options may also be set when creating the log handler.

LICENSE

see LICENSE

Documentation

Overview

Purpose: slogotlp is a slog.Handler that sends logs to an OpenTelemetry collector. Currently, only grpc is supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvSet

func EnvSet() bool

EnvSet returns if the OTLP environment variables are set.

Types

type Handler

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

Handler implements the slog.Handler interface.

func NewHandler

func NewHandler(ctx context.Context, options ...HandlerOption) (*Handler, error)

NewHandler creates a new slog.Handler.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

func (*Handler) Handle

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

func (*Handler) Shutdown

func (h *Handler) Shutdown(ctx context.Context) error

Shutdown shutsdown the handler. This should be called before the process exits to flush buffers and close conenctions.

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

WithGroup is a no-op, as we do not support groups yet.

type HandlerOption

type HandlerOption interface {
	// contains filtered or unexported methods
}

HandlerOption applies an option when creating a Handler.

func WithBundleCountThreshold

func WithBundleCountThreshold(count int) HandlerOption

WithBundleCountThreshold sets the bundle count threshold. The default is bundler.DefaultBundleCountThreshold.

func WithBundleDelayThreshold

func WithBundleDelayThreshold(delay time.Duration) HandlerOption

WithBundleDelayThreshold sets the bundle delay threshold. The default is bundler.DefaultDelayThreshold.

func WithDialOptions added in v1.2.0

func WithDialOptions(opts ...grpc.DialOption) HandlerOption

WithDialOptions sets the grpc dial options.

func WithEndpoint

func WithEndpoint(endpoint string) HandlerOption

WithEndpoint sets the endpoint. If not set, this will be read from the environment

func WithErrorHandler

func WithErrorHandler(handler func(error)) HandlerOption

WithErrorHandler sets the error handler. If not set, errors are logged to stderr.

func WithInsecure

func WithInsecure(insecure bool) HandlerOption

WithInsecure sets the insecure flag. If not set, this will be read from the environment

func WithLevel

func WithLevel(level slog.Level) HandlerOption

WithLevel sets the minimum level to log. The default is slog.LevelInfo.

func WithResource

func WithResource(resource *resource.Resource) HandlerOption

WithResource sets the resource. There is no default.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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