otellogrus

package module
v0.0.0-...-7585d43 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: BSD-2-Clause Imports: 8 Imported by: 1

README

PkgGoDev

OpenTelemetry Go instrumentation for logrus logging

This instrumentation records logrus log messages as events on the existing span that is passed via a context.Context. It does not record anything if a context does not contain a span.

Installation

go get github.com/rlakhtakia/opentelemetry-go-extra/otellogrus

Usage

You need to install an otellogrus.Hook and use logrus.WithContext to propagate the active span.

import (
    "github.com/rlakhtakia/opentelemetry-go-extra/otellogrus"
    "github.com/sirupsen/logrus"
)

// Instrument logrus.
logrus.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
	logrus.PanicLevel,
	logrus.FatalLevel,
	logrus.ErrorLevel,
	logrus.WarnLevel,
)))

// Use ctx to pass the active span.
logrus.WithContext(ctx).
	WithError(errors.New("hello world")).
	WithField("foo", "bar").
	Error("something failed")

See example for details.

Options

otellogrus.NewHook accepts the following options:

  • otellogrus.WithLevels(logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel) sets the logrus logging levels on which the hook is fired.
  • WithErrorStatusLevel(logrus.ErrorLevel) sets the minimal logrus logging level on which the span status is set to codes.Error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version is the current release version.

Types

type Hook

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

Hook is a logrus hook that adds logs to the active span as events.

func NewHook

func NewHook(opts ...Option) *Hook

NewHook returns a logrus hook.

func (*Hook) Fire

func (hook *Hook) Fire(entry *logrus.Entry) error

Fire is a logrus hook that is fired on a new log entry.

func (*Hook) Levels

func (hook *Hook) Levels() []logrus.Level

Levels returns logrus levels on which this hook is fired.

type Option

type Option func(h *Hook)

Option applies a configuration to the given config.

func WithErrorStatusLevel

func WithErrorStatusLevel(level logrus.Level) Option

WithErrorStatusLevel sets the minimal logrus logging level on which the span status is set to codes.Error.

The default is <= logrus.ErrorLevel.

func WithLevels

func WithLevels(levels ...logrus.Level) Option

WithLevels sets the logrus logging levels on which the hook is fired.

The default is all levels between logrus.PanicLevel and logrus.WarnLevel inclusive.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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