otellogrus

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package otellogrus provides a Hook, a logrus.Hook implementation that can be used to bridge between the github.com/sirupsen/logrus API and OpenTelemetry.

Record Conversion

The logrus.Entry records are converted to OpenTelemetry log.Record in the following way:

  • Time is set as the Timestamp.
  • Message is set as the Body using a log.StringValue.
  • Level is transformed and set as the Severity. The SeverityText is not set.
  • Fields are transformed and set as the attributes.

The Level is transformed by using the static offset to the OpenTelemetry Severity types. For example:

Attribute values are transformed based on their type into log attributes, or into a string value if there is no matching type.

Example
package main

import (
	"github.com/sirupsen/logrus"

	"go.opentelemetry.io/contrib/bridges/otellogrus"
	"go.opentelemetry.io/otel/log/noop"
)

func main() {
	// Use a working LoggerProvider implementation instead e.g. using go.opentelemetry.io/otel/sdk/log.
	provider := noop.NewLoggerProvider()

	// Create an *otellogrus.Hook and use it in your application.
	hook := otellogrus.NewHook("my/pkg/name", otellogrus.WithLoggerProvider(provider))

	// Set the newly created hook as a global logrus hook
	logrus.AddHook(hook)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

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

Hook is a logrus.Hook that sends all logging records it receives to OpenTelemetry. See package documentation for how conversions are made.

func NewHook

func NewHook(name string, options ...Option) *Hook

NewHook returns a new Hook to be used as a logrus.Hook.

If WithLoggerProvider is not provided, the returned Hook will use the global LoggerProvider.

func (*Hook) Fire

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

Fire handles the passed record, and sends it to OpenTelemetry.

func (*Hook) Levels

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

Levels returns the list of log levels we want to be sent to OpenTelemetry.

type Option

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

Option configures a Hook.

func WithLevels

func WithLevels(l []logrus.Level) Option

WithLevels returns an Option that configures the log levels that will fire the configured Hook.

By default if this Option is not provided, the Hook will fire for all levels. LoggerProvider.

func WithLoggerProvider

func WithLoggerProvider(provider log.LoggerProvider) Option

WithLoggerProvider returns an Option that configures log.LoggerProvider used by a Hook.

By default if this Option is not provided, the Hook will use the global LoggerProvider.

func WithSchemaURL

func WithSchemaURL(schemaURL string) Option

WithSchemaURL returns an Option that configures the semantic convention schema URL of the log.Logger used by a Hook. The schemaURL should be the schema URL for the semantic conventions used in log records.

func WithVersion

func WithVersion(version string) Option

WithVersion returns an Option that configures the version of the log.Logger used by a Hook. The version should be the version of the package that is being logged.

Jump to

Keyboard shortcuts

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