otelzerolog

package module
v0.0.0-...-0c3fcdf Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package otelzerolog provides a Hook, a zerolog.Hook implementation that can be used to bridge between the zerolog API and OpenTelemetry.

Record Conversion

The zerolog.Event records are converted to OpenTelemetry log.Record in the following way:

  • Message is set as the Body using a log.StringValue.
  • Level is transformed and set as the Severity. The SeverityText is also set.

The Level is transformed to the OpenTelemetry Severity types in the following way.

NOTE: Fields are not transformed because of https://github.com/rs/zerolog/issues/493.

Example
package main

import (
	"os"

	"github.com/rs/zerolog"

	"go.opentelemetry.io/contrib/bridges/otelzerolog"
	"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 a logger that emits logs to both STDOUT and the OTel Go SDK.
	hook := otelzerolog.NewHook("my/pkg/name", otelzerolog.WithLoggerProvider(provider))
	logger := zerolog.New(os.Stdout).With().Logger()
	logger = logger.Hook(hook)
}

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 zerolog.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 [Zerolog.Hook]. If WithLoggerProvider is not provided, the returned Hook will use the global LoggerProvider.

func (Hook) Run

func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string)

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

type Option

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

Option configures a Hook.

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