apmzerolog

package module
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package apmzerolog provides an implementaton of zerolog.LevelWriter for sending error records to Elastic APM, as well as functions for adding trace context and detailed error stack traces to log records.

Index

Examples

Constants

View Source
const (
	// SpanIDFieldName is the field name for the span ID.
	SpanIDFieldName = "span.id"

	// TraceIDFieldName is the field name for the trace ID.
	TraceIDFieldName = "trace.id"

	// TransactionIDFieldName is the field name for the transaction ID.
	TransactionIDFieldName = "transaction.id"
)
View Source
const (
	// DefaultFatalFlushTimeout is the default value for Writer.FatalFlushTimeout.
	DefaultFatalFlushTimeout = 5 * time.Second

	// StackSourceLineName is the key for the line number of a stack frame.
	StackSourceLineName = "line"

	// StackSourceFunctionName is the key for the function name of a stack frame.
	StackSourceFunctionName = "func"
)

Variables

This section is empty.

Functions

func MarshalErrorStack

func MarshalErrorStack(err error) interface{}

MarshalErrorStack marshals the stack trace in err, if err was produced (or wrapped) by github.com/pkg/errors.

This is similar to github.com/rs/zerolog/pkgerrors.MarshalStack, with the following differences:

  • the "source" field value may be an absolute path
  • the "func" field value will be fully qualified
Example
package main

import (
	"os"

	"github.com/pkg/errors"
	"github.com/rs/zerolog"

	"go.elastic.co/apm/module/apmzerolog"
)

func main() {
	zerolog.ErrorStackMarshaler = apmzerolog.MarshalErrorStack

	logger := zerolog.New(os.Stdout)
	logger.Error().Stack().Err(errors.New("aieee")).Msg("nope nope nope")
}
Output:

func TraceContextHook

func TraceContextHook(ctx context.Context) zerolog.Hook

TraceContextHook returns a zerolog.Hook that will add any trace context contained in ctx to log events.

Example
package main

import (
	"net/http"

	"github.com/rs/zerolog"

	"go.elastic.co/apm/module/apmzerolog"
)

func main() {
	handleRequest := func(w http.ResponseWriter, req *http.Request) {
		logger := zerolog.Ctx(req.Context()).Hook(apmzerolog.TraceContextHook(req.Context()))
		logger.Error().Msg("message")
	}
	http.HandleFunc("/", handleRequest)
}
Output:

Types

type Writer

type Writer struct {
	// Tracer is the apm.Tracer to use for reporting errors.
	// If Tracer is nil, then apm.DefaultTracer will be used.
	Tracer *apm.Tracer

	// FatalFlushTimeout is the amount of time to wait while
	// flushing a fatal log message to the APM Server before
	// the process is exited. If this is 0, then
	// DefaultFatalFlushTimeout will be used. If the timeout
	// is a negative value, then no flushing will be performed.
	FatalFlushTimeout time.Duration

	// MinLevel holds the minimum level of logs to send to
	// Elastic APM as errors.
	//
	// MinLevel must be greater than or equal to zerolog.ErrorLevel.
	// If it is less than this, zerolog.ErrorLevel will be used as
	// the minimum instead.
	MinLevel zerolog.Level
}

Writer is an implementation of zerolog.LevelWriter, reporting log records as errors to the APM Server. If TraceContext is used to add trace IDs to the log records, the errors reported will be associated with them.

Because we only have access to the serialised form of the log record, we must rely on enough information being encoded into the events. For error stack traces, you must use zerolog's Stack() method, and set zerolog.ErrorStackMarshaler either to github.com/rs/zerolog/pkgerrors.MarshalStack, or to the function apmzerolog.MarshalErrorStack in this package. The pkgerrors.MarshalStack implementation omits some information, whereas apmzerolog is designed to convey the complete file location and fully qualified function name.

Example
package main

import (
	"os"

	"github.com/rs/zerolog"

	"go.elastic.co/apm/module/apmzerolog"
)

func main() {
	zerolog.ErrorStackMarshaler = apmzerolog.MarshalErrorStack

	logger := zerolog.New(zerolog.MultiLevelWriter(os.Stdout, &apmzerolog.Writer{}))
	logger.Error().Msg("boom")
}
Output:

func (*Writer) Write

func (*Writer) Write(p []byte) (int, error)

Write is a no-op.

func (*Writer) WriteLevel

func (w *Writer) WriteLevel(level zerolog.Level, p []byte) (int, error)

WriteLevel decodes the JSON-encoded log record in p, and reports it as an error using w.Tracer.

Jump to

Keyboard shortcuts

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