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

package apmlogrus

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

Index

Examples

Package Files

fields.go hook.go

Constants

const (
    // FieldKeyTraceID is the field key for the trace ID.
    FieldKeyTraceID = "trace.id"

    // FieldKeyTransactionID is the field key for the transaction ID.
    FieldKeyTransactionID = "transaction.id"

    // FieldKeySpanID is the field key for the span ID.
    FieldKeySpanID = "span.id"
)
const (
    // DefaultFatalFlushTimeout is the default value for Hook.FatalFlushTimeout.
    DefaultFatalFlushTimeout = 5 * time.Second
)

Variables

var (
    // DefaultLogLevels is the log levels for which errors are reported by Hook, if Hook.LogLevels is not set.
    DefaultLogLevels = []logrus.Level{
        logrus.PanicLevel,
        logrus.FatalLevel,
        logrus.ErrorLevel,
    }
)

func TraceContext Uses

func TraceContext(ctx context.Context) logrus.Fields

TraceContext returns a logrus.Fields containing the trace context of the transaction and span contained in ctx, if any.

Code:

logger := logrus.New()

tx := apm.DefaultTracer.StartTransaction("name", "type")
defer tx.End()

ctx := apm.ContextWithTransaction(context.Background(), tx)
span, ctx := apm.StartSpan(ctx, "name", "type")
defer span.End()

// apmlogrus.TraceContext returns fields including the trace ID,
// transaction ID, and span ID, for the transaction and span in
// the given context.
logger.WithFields(apmlogrus.TraceContext(ctx)).Fatal("ohhh, what a world")

type Hook Uses

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

    // LogLevels holds the log levels to report as errors.
    // If LogLevels is nil, then the DefaultLogLevels will
    // be used.
    LogLevels []logrus.Level

    // 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
}

Hook implements logrus.Hook, 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.

Code:

logger := logrus.New()

// Report "error", "panic", and "fatal" log messages
// to Elastic APM using apm.DefaultTracer.
logger.AddHook(&apmlogrus.Hook{})

// Report "error", "panic", and "fatal" log messages
// to Elastic APM using a specific tracer.
var tracer *apm.Tracer
logger.AddHook(&apmlogrus.Hook{
    Tracer: tracer,
})

// Report only "panic" log messages to Elastic APM
// using apm.DefaultTracer.
logger.AddHook(&apmlogrus.Hook{
    LogLevels: []logrus.Level{logrus.PanicLevel},
})

func (*Hook) Fire Uses

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

Fire reports the log entry as an error to the APM Server.

func (*Hook) Levels Uses

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

Levels returns h.LogLevels, satisfying the logrus.Hook interface.

Package apmlogrus imports 5 packages (graph). Updated 2020-05-29. Refresh now. Tools for package owners.