apmlogrus

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: 5 Imported by: 24

Documentation

Index

Examples

Constants

View Source
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"
)
View Source
const (
	// DefaultFatalFlushTimeout is the default value for Hook.FatalFlushTimeout.
	DefaultFatalFlushTimeout = 5 * time.Second
)

Variables

View Source
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,
	}
)

Functions

func TraceContext

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.

Example
package main

import (
	"context"

	"github.com/sirupsen/logrus"

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

func main() {
	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")
}
Output:

Types

type Hook

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.

Example
package main

import (
	"github.com/sirupsen/logrus"

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

func main() {
	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},
	})
}
Output:

func (*Hook) Fire

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

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

func (*Hook) Levels

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

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

Jump to

Keyboard shortcuts

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