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

Merged
merged 85 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
492a08d
signoz-to-logrus
Quadrisheriff Dec 8, 2024
c4e0d83
signoz-to-logrus
Quadrisheriff Dec 8, 2024
76e17c2
fix preview
Quadrisheriff Dec 9, 2024
5c4cdf7
Update constants/docsSideNav.ts
Quadrisheriff Jan 9, 2025
ea3c14c
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
b9a5c5d
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
7bd8759
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
dd44a5f
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
133fbc7
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
81edf1b
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
7dcbca1
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
368fef6
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
c75aec4
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
d003793
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
70fc30d
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
66eea5b
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
c0199cf
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
ed2fde5
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
192df8f
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
9714c63
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
010c370
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
c07a1d3
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
055183c
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
4dc2ae2
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
ccee809
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
146ec91
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
495dc67
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
88d897a
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
879f9a9
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
7ce2e47
implement review changes
Quadrisheriff Jan 9, 2025
1e972bc
implement review changes
Quadrisheriff Jan 9, 2025
af2a889
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
b4f609b
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
c383b4e
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
862c1cc
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
479bdff
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
c6594c9
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
9873a6f
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
9cf9c27
fix second review
Quadrisheriff Jan 10, 2025
2a4f103
signoz-to-logrus
Quadrisheriff Dec 8, 2024
a4330d9
signoz-to-logrus
Quadrisheriff Dec 8, 2024
ead3175
fix preview
Quadrisheriff Dec 9, 2024
30191fe
Update constants/docsSideNav.ts
Quadrisheriff Jan 9, 2025
8a92308
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
c2745dc
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
e40b458
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
499b233
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
f514fc5
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
ac9b061
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
03f4e35
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
2249656
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
d87d5f2
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
23deb85
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
170e108
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
cf5c16f
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
b98f0fb
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
8fc7bd7
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
bf7b270
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
e7ae8e3
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
374bc3f
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
343bf06
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
e30b6c5
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
69e1f10
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
9090ac4
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
60da85b
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
59b9dc5
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
c765e6a
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
ab2bbdd
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 9, 2025
97dc64e
implement review changes
Quadrisheriff Jan 9, 2025
bf420fa
implement review changes
Quadrisheriff Jan 9, 2025
9989dce
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
722d65e
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
bb25b07
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
1abda01
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
20b1ccd
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
cbb1344
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
f78ff26
Update data/docs/logs-management/send-logs/logrus-to-signoz.mdx
Quadrisheriff Jan 10, 2025
e17518c
fix second review
Quadrisheriff Jan 10, 2025
f9a62b4
Merge branch 'logrus-to-signoz' of https://github.com/Quadrisheriff/s…
Quadrisheriff Jan 10, 2025
e3bb84e
Merge branch 'main' into logrus-to-signoz
Quadrisheriff Jan 10, 2025
81449e1
Merge branch 'logrus-to-signoz' of https://github.com/Quadrisheriff/s…
Quadrisheriff Jan 10, 2025
3f56f78
merge conflicts
Quadrisheriff Jan 10, 2025
ae3aa3f
merge conflicts
Quadrisheriff Jan 10, 2025
7456f93
merge conflicts
Quadrisheriff Jan 10, 2025
9068e09
change images
Quadrisheriff Jan 10, 2025
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 @@ -529,6 +529,10 @@ const docsSideNav = [
type: 'doc',
route: '/docs/userguide/collecting_syslogs',
label: 'Syslogs',
}, {
type: 'doc',
route: '/docs/logs-management/send-logs/logrus-to-signoz',
label: '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 document explains how to collect log data from [Logrus](https://github.com/sirupsen/logrus) and visualise it in [SigNoz](https://signoz.io/).

## Prerequisites

- A running Golang application

## Setup

### Step 1: Install dependencies

Navigate to the root folder of your golang application and run the below 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 2: 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 golang application `main.go` file.

```go
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)

......
}
```

In this code, we initialized an OpenTelemetry Protocol (OTLP) Log provider using the OTLP log exporter and OTLP Batch processor. The OTLP Log provider is then used to create a global Logrus hook using the `otellogrus` package.

### Step 3 (Optional): Inject Trace and Span ID fields to your logs

Trace and span IDs in structured logs provide a way to correlate logs, track requests across services, and diagnose issues effectively. To add the Span and Trace ID fields to your structured logs:

**Import the OpenTelemetry Trace library to your code file**

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

**Fetch current Span and Trace ID from context using the OTLP TracerProvider**

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

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

```
This code implements an OTLP TracerProvider. The OTLP TracerProvider is responsible for creating Spans which help to trace an operation.

**Create a function to add Span and Trace ID values to your Logrus fields**

```go
func LogrusFields(span oteltrace.Span) logrus.Fields {
return logrus.Fields{
"span_id": span.SpanContext().SpanID().String(),
"trace_id": span.SpanContext().TraceID().String(),
}
}
```
This function extracts the context span and trace IDs and adds the IDs as fields to the Logrus structured log.

### Step 4: Send Logrus logs to SigNoz

<Tabs entityName="plans">

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

To send Logrus logs to your SigNoz cloud account:

1. Add logs to your code.
```go
# with span and trace ids
logrus.WithFields(LogrusFields(span)).Info("Logrus logs with span id and trace id 004")

# without span and trace ids
logrus.Info("Logrus logs with span id and trace id 004")
```
2. Run your Golang application with your SigNoz credentials
```bash
INSECURE_MODE=false OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<your-ingestion-key> OTEL_EXPORTER_OTLP_ENDPOINT=ingest.<region>.signoz.cloud:443 go run main.go
```

Input the following values:

- Replace `<your-ingestion-key>` with your SigNoz [ingestion key](https://signoz.io/docs/ingestion/signoz-cloud/keys/)
- Set the `<region>` to match your SigNoz Cloud [region](https://signoz.io/docs/ingestion/signoz-cloud/overview/#endpoint)

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-host" label="Self-hosted SigNoz">

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

1. Add logs to your code.
```go
# with span and trace ids
logrus.WithFields(LogrusFields(span)).Info("Logrus logs with span id and trace id 004")

# without span and trace ids
logrus.Info("Logrus logs with span id and trace id 004")
```
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>

<figure data-zoomable align='center'>
<img src="/img/docs/logs-management/send-logs/logrus-logs.webp" alt="Logrus logs" />
<figcaption><i>Logrus logs</i></figcaption>
</figure>
Loading