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

Adding new documentation for Zap logger #1027

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
5 changes: 5 additions & 0 deletions constants/docsSideNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ const docsSideNav = [
route: '/docs/logs-management/send-logs/vector-logs-to-signoz',
label: 'Vector',
},
{
type: 'doc',
route: '/docs/logs-management/send-logs/zap-to-signoz',
label: 'Zap',
},
{
type: 'doc',
route: '/docs/logs-management/send-logs/windows-events-log',
Expand Down
364 changes: 364 additions & 0 deletions data/docs/logs-management/send-logs/zap-to-signoz.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,364 @@
---
date: 2024-12-06
title: Send Logs from Zap to SigNoz
id: zap-to-signoz
---

This guide explains how to forward logs from [Zap](https://github.com/uber-go/zap) to SigNoz.


<Tabs>

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

### Requirements

- Go Modules: Ensure your project is using the following Go modules.

```bash
go get go.opentelemetry.io/contrib/bridges/otelzap
go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
go get go.uber.org/zap
```

We import the required packages:

- `otelzap`: The bridge for Zap logger to work with OpenTelemetry.
- `otlploghttp`: The OTLP exporter for logs over HTTP.
- `zap`: The high-performance logging library.

### Define Endpoint and Access Token

You need to provide

- `otlpEndpoint`: The URL of your SigNoz Cloud OTLP collector endpoint.
- `accessToken`: The token required to authenticate against SigNoz Cloud. This is typically provided in your account dashboard.

```go
otlpEndpoint := "<signoz-client-url>"
accessToken := "<my-access-token>"
```

### Create HTTP Headers for Authorization

To send data securely to the SigNoz Cloud, the access token is included in the `Authorization` header

```go
headers := map[string]string{
"Authorization": "Bearer " + accessToken,
}
```

### Create Exporter

```go
ctx := context.Background()
exporter, err := otlploghttp.New(ctx,
otlploghttp.WithEndpoint(otlpEndpoint),
otlploghttp.WithHeaders(headers),
)
if err != nil {
fmt.Println(err)
}
```

Here, we create an OTLP HTTP exporter. This is responsible for exporting log records to an OTLP endpoint (like the OpenTelemetry collector).

### Log Processor

```go
processor := log.NewBatchProcessor(exporter)
```

A batch processor is used to efficiently group and export log records. The processor ensures that log records are buffered and sent in batches to the OTLP endpoint.

### Logger Provider

```go
provider := log.NewLoggerProvider(
log.WithProcessor(processor),
)

defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
fmt.Println(err)
}
}()
```

The `LoggerProvider` serves as the core log provider, which connects the log pipeline (processor) to the Zap logger. This ensures that logs generated by your application will pass through OpenTelemetry’s logging mechanisms.

It's important to properly shut down the `LoggerProvider` to ensure that no logs are left unprocessed before the application exits.

Zap Logger Initialisation

```go
logger := zap.New(otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)))
defer logger.Sync()
```

We initialise a Zap logger, but instead of using Zap’s native logging core, we create an OpenTelemetry-based core (`otelzap.NewCore`). This core connects Zap’s logging functionality to the OpenTelemetry log pipeline, ensuring logs are exported.

- `"my/pkg/name"`: Specifies the package name or source of the logs, useful for debugging.
- `otelzap.WithLoggerProvider(provider)`: Connects the previously created OpenTelemetry `LoggerProvider`.

### Logging Messages

```go
logger.Info("something really cool")
```

The `logger.Info` method logs messages. In this example, instead of writing logs to standard output, they are sent to the OTLP endpoint.

### Trace With Context

```go
logger.Info("setting context", zap.Any("context", ctx))
```

You can include the context for more contextual tracing of the log messages

logging.go

```go
package logging

import (
"context"
"fmt"

"go.opentelemetry.io/contrib/bridges/otelzap"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/sdk/log"
"go.uber.org/zap"
)

func Example() {

otlpEndpoint := "<signoz-client-url>"
accessToken := "<my-access-token>"
ctx := context.Background()

headers := map[string]string{
"Authorization": "Bearer " + accessToken,
}

// Create an exporter that will emit log records.
exporter, err := otlploghttp.New(ctx,
otlploghttp.WithEndpoint(otlpEndpoint),
otlploghttp.WithHeaders(headers),
)
if err != nil {
fmt.Println(err)
}
// Create a log record processor pipeline.
processor := log.NewBatchProcessor(exporter)

// Create a logger provider.
// You can pass this instance directly when creating a log bridge.
provider := log.NewLoggerProvider(
log.WithProcessor(processor),
)

defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
fmt.Println(err)
}
}()

// Initialize a zap logger with the otelzap bridge core.
logger := zap.New(otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)))
defer logger.Sync()

// You can now use your logger in your code.
logger.Info("something really cool")

// You can set context for trace correlation using zap.Any or zap.Reflect
logger.Info("setting context", zap.Any("context", ctx))

logger.Info("Hello from Zap logger!")
}

```

## Everything
## Output
For looking at logs, login into signoz cloud and navigate into logs tab to view the logs.
<figure data-zoomable align='center'>
<img src="/img/docs/logs-management/send-logs/zap-to-signoz.png" alt="Send Zap logs to Signoz"/>
<figcaption><i>Zap Logs in SigNoz</i></figcaption>
</figure>

</TabItem>



<TabItem value="self-host" label="Self-Host" default>

### Requirements

- Go Modules: Ensure your project is using the following Go modules.

```bash
go get go.opentelemetry.io/contrib/bridges/otelzap
go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
go get go.uber.org/zap
```

We import the required packages:

- `otelzap`: The bridge for Zap logger to work with OpenTelemetry.
- `otlploghttp`: The OTLP exporter for logs over HTTP.
- `zap`: The high-performance logging library.

### Define Endpoint and Access Token

You need to provide

- `otlpEndpoint`: The URL of your SigNoz Cloud OTLP collector endpoint.

```go
otlpEndpoint := "<signoz-client-url>" // For local this is usually 0.0.0.0:4318
```

### Create Exporter

```go
ctx := context.Background()
exporter, err := otlploghttp.New(ctx,
otlploghttp.WithEndpoint(otlpEndpoint),
otlploghttp.WithInsecure(),
)
if err != nil {
fmt.Println(err)
}
```

Here, we create an OTLP HTTP exporter. This is responsible for exporting log records to an OTLP endpoint (like the OpenTelemetry collector).

### Log Processor

```go
processor := log.NewBatchProcessor(exporter)
```

A batch processor is used to efficiently group and export log records. The processor ensures that log records are buffered and sent in batches to the OTLP endpoint.

### Logger Provider

```go
provider := log.NewLoggerProvider(
log.WithProcessor(processor),
)

defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
fmt.Println(err)
}
}()
```

The `LoggerProvider` serves as the core log provider, which connects the log pipeline (processor) to the Zap logger. This ensures that logs generated by your application will pass through OpenTelemetry’s logging mechanisms.

It's important to properly shut down the `LoggerProvider` to ensure that no logs are left unprocessed before the application exits.

Zap Logger Initialisation

```go
logger := zap.New(otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)))
defer logger.Sync()
```

We initialise a Zap logger, but instead of using Zap’s native logging core, we create an OpenTelemetry-based core (`otelzap.NewCore`). This core connects Zap’s logging functionality to the OpenTelemetry log pipeline, ensuring logs are exported.

- `"my/pkg/name"`: Specifies the package name or source of the logs, useful for debugging.
- `otelzap.WithLoggerProvider(provider)`: Connects the previously created OpenTelemetry `LoggerProvider`.

### Logging Messages

```go
logger.Info("something really cool")
```

The `logger.Info` method logs messages. In this example, instead of writing logs to standard output, they are sent to the OTLP endpoint.

### Trace With Context

```go
logger.Info("setting context", zap.Any("context", ctx))
```

You can include the context for more contextual tracing of the log messages

logging.go

```go
package logging

import (
"context"
"fmt"

"go.opentelemetry.io/contrib/bridges/otelzap"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/sdk/log"
"go.uber.org/zap"
)

func Example() {

otlpEndpoint := "<signoz-client-url>" // For local instance, this is usually 0.0.0.0:4318
ctx := context.Background()

// Create an exporter that will emit log records.
exporter, err := otlploghttp.New(ctx,
otlploghttp.WithEndpoint(otlpEndpoint),
otlploghttp.WithInsecure(),
)
if err != nil {
fmt.Println(err)
}
// Create a log record processor pipeline.
processor := log.NewBatchProcessor(exporter)

// Create a logger provider.
// You can pass this instance directly when creating a log bridge.
provider := log.NewLoggerProvider(
log.WithProcessor(processor),
)

defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
fmt.Println(err)
}
}()

// Initialize a zap logger with the otelzap bridge core.
logger := zap.New(otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(provider)))
defer logger.Sync()

// You can now use your logger in your code.
logger.Info("something really cool")

// You can set context for trace correlation using zap.Any or zap.Reflect
logger.Info("setting context", zap.Any("context", ctx))

logger.Info("Hello from Zap logger!")
}

```


## Output
For looking at logs, login into signoz cloud and navigate into logs tab to view the logs.
<figure data-zoomable align='center'>
<img src="/img/docs/logs-management/send-logs/zap-to-signoz-local.png" alt="Send Zap logs to Signoz"/>
<figcaption><i>Zap Logs in SigNoz</i></figcaption>
</figure>
</TabItem>

</Tabs>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.