Skip to content

Commit bfa2f10

Browse files
chameleon82Aleksandr Nekrasov
and
Aleksandr Nekrasov
authored
feat: slog baggage option (#21)
Co-authored-by: Aleksandr Nekrasov <aleksandr.nekrasov@agoda.com>
1 parent 5ecb4b6 commit bfa2f10

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

otelslog/CHANGELOG

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
## [v0.1.2] 2024-09-30
12+
13+
### Added
14+
15+
- `AddBaggage` option to add baggage attributes as log attributes
16+
1117
## [v0.1.1] 2023-12-24
1218

1319
### Added

otelslog/otel_handler.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
otel "github.com/agoda-com/opentelemetry-logs-go/logs"
2222
"go.opentelemetry.io/otel/attribute"
23+
"go.opentelemetry.io/otel/baggage"
2324
"go.opentelemetry.io/otel/sdk/instrumentation"
2425
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2526
"go.opentelemetry.io/otel/trace"
@@ -41,6 +42,7 @@ type OtelHandler struct {
4142
// A zero HandlerOptions consists entirely of default values.
4243
type HandlerOptions struct {
4344
Level slog.Leveler
45+
AddBaggage bool
4446
}
4547

4648
type otelHandler struct {
@@ -88,10 +90,18 @@ func (o otelHandler) Handle(ctx context.Context, record slog.Record) error {
8890

8991
var attributes []attribute.KeyValue
9092

93+
if o.opts.AddBaggage {
94+
b := baggage.FromContext(ctx)
95+
// Iterate over baggage items and add them to log attributes
96+
for _, i := range b.Members() {
97+
attributes = append(attributes, attribute.String(i.Key(), i.Value()))
98+
}
99+
}
91100
for _, attr := range o.attrs {
92101
attributes = append(attributes, otelAttribute(attr)...)
93102
}
94103

104+
95105
record.Attrs(func(attr slog.Attr) bool {
96106
attributes = append(attributes, otelAttribute(withGroupPrefix(o.groupPrefix, attr))...)
97107
return true

otelslog/otel_handler_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package otelslog
33
import (
44
"bytes"
55
"context"
6+
"go.opentelemetry.io/otel/baggage"
67
"log/slog"
78
"os"
89
"testing"
@@ -44,7 +45,8 @@ func TestNewOtelHandler(t *testing.T) {
4445
)
4546

4647
handler := NewOtelHandler(loggerProvider, &HandlerOptions{
47-
Level: slog.LevelInfo,
48+
Level: slog.LevelInfo,
49+
AddBaggage: true,
4850
}).
4951
WithAttrs([]slog.Attr{slog.String("first", "value1")}).
5052
WithGroup("group1").
@@ -54,12 +56,16 @@ func TestNewOtelHandler(t *testing.T) {
5456
otelLogger := slog.New(handler)
5557
slog.SetDefault(otelLogger)
5658

59+
member, _ := baggage.NewMember("baggage.key", "true")
60+
bag, _ := baggage.New(member)
61+
ctx = baggage.ContextWithBaggage(ctx, bag)
62+
5763
doSomething(ctx)
5864

5965
loggerProvider.Shutdown(ctx)
6066

6167
actual := buf.String()
6268

6369
assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.0.1] {host.name=")
64-
assert.Contains(t, actual, "service.name=otelslog-example, service.version=1.0.0, first=value1, group1.second=value2, group1.group2.myKey=myValue, group1.group2.myGroup.groupKey=groupValue}")
70+
assert.Contains(t, actual, "service.name=otelslog-example, service.version=1.0.0, baggage.key=true, first=value1, group1.second=value2, group1.group2.myKey=myValue, group1.group2.myGroup.groupKey=groupValue}")
6571
}

0 commit comments

Comments
 (0)