apm-agent-go: github.com/elastic/apm-agent-go/module/apmzerolog Index | Examples | Files

package apmzerolog

import "github.com/elastic/apm-agent-go/module/apmzerolog"

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

Package Files

context.go doc.go stack.go writer.go

Constants

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"
)
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"
)

func MarshalErrorStack Uses

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

Code:

zerolog.ErrorStackMarshaler = apmzerolog.MarshalErrorStack

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

func TraceContextHook Uses

func TraceContextHook(ctx context.Context) zerolog.Hook

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

Code:

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)

type Writer Uses

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.

Code:

zerolog.ErrorStackMarshaler = apmzerolog.MarshalErrorStack

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

func (*Writer) Write Uses

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

Write is a no-op.

func (*Writer) WriteLevel Uses

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.

Package apmzerolog imports 13 packages (graph). Updated 2020-05-21. Refresh now. Tools for package owners.