Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenTelemetry Bridge for Logrus #1036

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions constants/docsSideNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ const docsSideNav = [
type: 'doc',
route: '/docs/userguide/collecting_syslogs',
label: 'Syslogs',
}, {
type: 'doc',
route: '/docs/logs-management/send-logs/logrus-to-signoz',
label: 'OpenTelemetry Bridge for Logrus',
},
{
type: 'category',
Expand Down
177 changes: 177 additions & 0 deletions data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
---
date: 2024-12-08
title: OpenTelemetry Bridge for Logrus
id: logrus-to-signoz
---

## Overview

This guide will teach you how to collect log data from [Logrus](https://github.com/sirupsen/logrus) to [SigNoz](https://signoz.io/) using the [OpenTelemetry Logrus Log Bridge (Otellogrus)](https://www.notion.so/Complete-guide-to-implementing-OpenTelemetry-Logrus-Log-Bridge-in-Golang-applications-12b983ed03288007ace3c0bcd379bed6?pvs=4).

## Prerequisites

- [Self-hosted SigNoz](https://signoz.io/docs/install/docker/) or [SigNoz cloud account](https://signoz.io/).
- Golang installed. You can download and install it from the official [Go website](https://golang.org/).
- Familiarity with [Golang Logging](https://signoz.io/guides/golang-log/).

## 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.

```bash
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.

```bash
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

<Tabs>

<TabItem value="cloud" label="SigNoz Cloud" default>

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.
```bash
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.
</TabItem>

<TabItem value="self" label="Self-hosted SigNoz">

To send Logrus logs to your Self-hosted SigNoz dashboard:

1. Add logs to your application.
```
logrus.WithFields(LogrusFields(span)).Info("Logrus logs message")
```
2. Run your Golang application with your Signoz credentials.
```bash
INSECURE_MODE=true OTEL_EXPORTER_OTLP_ENDPOINT=<SIGNOZ_OPENTELEMETRY_ENDPOINT> go run main.go
```

Change `<SIGNOZ_OPENTELEMETRY_ENDPOINT>` to your SigNoz `otlp` receiver endpoint, change `INSECURE_MODE` to `true` or `false` depending on if your connection is secured or not.

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

</Tabs>
Loading