instalogrus

package module
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 4 Imported by: 0

README

Instana instrumentation for github.com/sirupsen/logrus

This module contains instrumentation code for github.com/sirupsen/logrus logger.

PkgGoDev

Installation

To add the module to your go.mod file run the following command in your project directory:

$ go get github.com/instana/go-sensor/instrumentation/instalogrus

Usage

The instalogrus.NewHook() collects any warning or errors logged with logrus.Logger, associates them with the current span and sends to Instana.

// Create a sensor
sensor := instana.NewSensor("my-web-server")

// Register the instalogrus hook
logrus.AddHook(instalogrus.NewHook(sensor))

// ...

// Make sure that you provide context.Context while logging so that
// the hook could corellate log records to operations:
logrus.WithContext(ctx).
	Error("something went wrong")

Full example

Documentation

Overview

Example (GlobalLogger)

This example demostrates how to use instalogrus.NewHook() to instrument the global logrus logger with Instana. The instrumented logger instance will then send any ERROR and WARN log messages to Instana, associating them with the current operation span.

package main

import (
	"context"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalogrus"
	"github.com/sirupsen/logrus"
)

func main() {
	sensor := instana.NewSensor("my-service")
	ctx := context.Background()

	// Add instalogrus hook to instrument the logger instance
	logrus.AddHook(instalogrus.NewHook(sensor))

	// Start and inject a span into context. Normally our instrumentation code does it for you.
	sp := sensor.Tracer().StartSpan("entry")
	defer sp.Finish()

	ctx = instana.ContextWithSpan(ctx, sp)

	logrus.
		// Make sure to add context to the log entry, so that the hook could corellate
		// this log record to current operation.
		WithContext(ctx).
		// Use your instrumented logger as usual
		WithFields(logrus.Fields{"data": "..."}).
		Error("something went wrong")
}
Output:

Example (LoggerInstance)

This example demostrates how to use instalogrus.NewHook() to instrument a logrus.Logger instance with Instana. The instrumented logger instance will then send any ERROR and WARN log messages to Instana, associating them with the current operation span.

package main

import (
	"context"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalogrus"
	"github.com/sirupsen/logrus"
)

func main() {
	sensor := instana.NewSensor("my-service")
	ctx := context.Background()

	log := logrus.New()
	// Configure logger
	// ...

	// Add instalogrus hook to instrument the logger instance
	log.AddHook(instalogrus.NewHook(sensor))

	// Start and inject a span into context. Normally our instrumentation code does it for you.
	sp := sensor.Tracer().StartSpan("entry")
	defer sp.Finish()

	ctx = instana.ContextWithSpan(ctx, sp)

	log.
		// Make sure to add context to the log entry, so that the hook could corellate
		// this log record to current operation.
		WithContext(ctx).
		// Use your instrumented logger as usual
		WithFields(logrus.Fields{"data": "..."}).
		Error("something went wrong")
}
Output:

Index

Examples

Constants

View Source
const Version = "1.15.0"

Version is the instrumentation module semantic version

Variables

This section is empty.

Functions

func NewHook

func NewHook(sensor instana.TracerLogger) *hook

NewHook returns a new logrus.Hook to instrument logger with Instana

Types

This section is empty.

Jump to

Keyboard shortcuts

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