OpenTelemetry Bridge for Logrus

Overview

This guide will teach you how to collect log data from Logrus to SigNoz using the OpenTelemetry Logrus Log Bridge (Otellogrus).

Prerequisites

Instrument your application with the OpenTelemetry Logrus Log Bridge

Step 1: Get the sample Golang application.

Navigate to https://github.com/SigNoz/sample-golang-app to clone the SigNoz sample-golang-app application.

After cloning the repository, switch to the without-instrumentation branch to complete the tutorial.

git checkout without-instrumentation

Step 2: Install dependencies

The next step is to install all the required dependencies to implement the OpenTelemetry Logrus Log Bridge.

Navigate to the sample-golang app root folder and run the following command to install the required dependencies.

go get \
go.opentelemetry.io/contrib/bridges/otellogrus \
go.opentelemetry.io/otel/sdk/log \
go.opentelemetry.io/otel/sdk/trace \
go.opentelemetry.io/otel/trace \
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp \
github.com/sirupsen/logrus

Step 3: Instrument OpenTelemetry Logrus Log Bridge in your Golang application

To instrument your Go application with the OpenTelemetry Logrus Log Bridge, add the following code to your sample-golang-app main.go file.

import (
   "context"
   "log"
   
   "github.com/sirupsen/logrus"
   "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
   otel_log"go.opentelemetry.io/otel/sdk/log"
   "go.opentelemetry.io/contrib/bridges/otellogrus"
)


func main() {
   logExporter, err := otlploghttp.New(context.Background())
   if err != nil {
       // handle error
       log.Fatal(err)
   }


   // create log provider
   log_provider := otel_log.NewLoggerProvider(
       otel_log.WithProcessor(
           otel_log.NewBatchProcessor(logExporter),
       ),
   )


   defer log_provider.Shutdown(context.Background())


   // Create an *otellogrus.Hook and use it in your application.
   hook := otellogrus.NewHook("<signoz-golang>", otellogrus.WithLoggerProvider(log_provider))


   // Set the newly created hook as a global logrus hook
   logrus.AddHook(hook)
   
   ......
}

Inject Trace and Span ID fields to your logs

To add the Span and Trace ID fields to your structured logs:

Step 1: Import the OpenTelemetry Trace library

import (
   "go.opentelemetry.io/otel"
   sdktrace "go.opentelemetry.io/otel/sdk/trace"
   oteltrace "go.opentelemetry.io/otel/trace"
)

Step 2: Fetch the current Span and Trace ID from context with the OpenTelemetry Trace library

   
   otel.SetTracerProvider(
       sdktrace.NewTracerProvider(
           sdktrace.WithSampler(sdktrace.AlwaysSample()),
       ),
   )


   tracer := otel.GetTracerProvider().Tracer("signoz-tracer")
   _, span := tracer.Start(context.Background(), "signoz-tracer")
   defer span.End()
   

Step 3: Create a function to add the Span and Trace ID values to your Logrus fields

func LogrusFields(span oteltrace.Span) logrus.Fields {
   return logrus.Fields{
       "span_id":  span.SpanContext().SpanID().String(),
       "trace_id": span.SpanContext().TraceID().String(),
   }
}

Send Logrus logs to SigNoz

To send Logrus logs to your SigNoz cloud account:

  1. Add logs to your application.

    logrus.WithFields(LogrusFields(span)).Info("Logrus logs message")
    
  2. Run your Golang application with your Signoz credentials.

    INSECURE_MODE=false OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_ACCESS_TOKEN> OTEL_EXPORTER_OTLP_ENDPOINT=<SIGNOZ_OPENTELEMETRY_ENDPOINT> go run main.go
    

    Input the following values:

    • <SIGNOZ_ACCESS_TOKEN>: Your SigNoz cloud ingestion token.
    • <SIGNOZ_OPENTELEMETRY_ENDPOINT>: Your SigNoz cloud ingestion URL. The value is https://ingest.{region}.signoz.cloud:443 where {region} is the choice region in SigNoz cloud.

Run your Golang application as specified above and wait some minutes for the logs to be sent to your SigNoz cloud dashboard. You can view the logs in the Logs section of your dashboard.

Was this page helpful?