Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/SigNoz/zap_otlp
Zap Logger with OpenTelemetry support
https://github.com/SigNoz/zap_otlp
golang logging zap
Last synced: 9 days ago
JSON representation
Zap Logger with OpenTelemetry support
- Host: GitHub
- URL: https://github.com/SigNoz/zap_otlp
- Owner: SigNoz
- License: mit
- Created: 2023-05-08T10:44:49.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-08T13:20:59.000Z (4 months ago)
- Last Synced: 2024-11-30T17:54:44.887Z (12 days ago)
- Topics: golang, logging, zap
- Language: Go
- Homepage: https://signoz.io
- Size: 94.7 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-repositories - SigNoz/zap_otlp - Zap Logger with OpenTelemetry support (Go)
README
# Zap OTLP
inspired from https://github.com/MrAlias/otlpr
This plugin helps you send logs from [zap](https://github.com/uber-go/zap) logger to a OTLP endpoint in your Go application.
## Getting Started
* Create two encoders. One for console and the other for OTLP.
```
config := zap.NewProductionEncoderConfig()
otlpEncoder := zapotlpencoder.NewOTLPEncoder(config)
consoleEncoder := zapcore.NewConsoleEncoder(config)
```
* Initialize the OTLP sync to send data to OTLP endpoint
```
var targetPtr = flag.String("target", "127.0.0.1:4317", "OTLP target")
var grpcInsecure = os.Getenv("OTEL_EXPORTER_OTLP_INSECURE")...
var secureOption grpc.DialOption
if strings.ToLower(grpcInsecure) == "false" || grpcInsecure == "0" || strings.ToLower(grpcInsecure) == "f" {
secureOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))
} else {
secureOption = grpc.WithTransportCredentials(insecure.NewCredentials())
}
conn, err := grpc.DialContext(ctx, *targetPtr, grpc.WithBlock(), secureOption, grpc.WithTimeout(time.Duration(5)*time.Second))
if err != nil {
log.Fatal(err)
}otlpSync := zapotlpsync.NewOtlpSyncer(conn, zapotlpsync.Options{
BatchSize: 100,
ResourceSchema: semconv.SchemaURL,
Resource: resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("example application"),
),
})
```
* Configure zap to use the encoders and sync.
```
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, os.Stdout, defaultLogLevel),
zapcore.NewCore(otlpEncoder, otlpSync, defaultLogLevel),
)
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
```
See the [example](./example/main.go) for a working example application.## Configurations
1) Batching
The default batch size is 100, you can change the batch size in `zapotlpsync.Options`
```
zapotlpsync.Options{
BatchSize: ,
.....
```
2) BatchInterval:
Time duration after which a batch will be sent regardless of size. Default is 5seconds. You can
change it in `zapotlpsync.Options`
```
zapotlpsync.Options{
BatchInterval: ,
.....
```## Send Trace Details
* The trace details are not populated automatically. You will have to add it in your log lines by passing the context to `zapotlp.SpanCtx()`
```
a.logger.Named("test_logger").Info("trace_test", zapotlp.SpanCtx(ctx))
```## To send data to signoz cloud
Set these environment variables
```
OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=
OTEL_EXPORTER_OTLP_INSECURE=false
```Replace `` with your ingestion token
target = ingest.{region}.signoz.cloud:443