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

feat: slog baggage option #21

Merged
merged 2 commits into from
Sep 30, 2024
Merged
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
6 changes: 6 additions & 0 deletions otelslog/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

## [v0.1.2] 2024-09-30

### Added

- `AddBaggage` option to add baggage attributes as log attributes

## [v0.1.1] 2023-12-24

### Added
Expand Down
10 changes: 10 additions & 0 deletions otelslog/otel_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
otel "github.com/agoda-com/opentelemetry-logs-go/logs"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/sdk/instrumentation"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
Expand All @@ -41,6 +42,7 @@ type OtelHandler struct {
// A zero HandlerOptions consists entirely of default values.
type HandlerOptions struct {
Level slog.Leveler
AddBaggage bool
}

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

var attributes []attribute.KeyValue

if o.opts.AddBaggage {
b := baggage.FromContext(ctx)
// Iterate over baggage items and add them to log attributes
for _, i := range b.Members() {
attributes = append(attributes, attribute.String(i.Key(), i.Value()))
}
}
for _, attr := range o.attrs {
attributes = append(attributes, otelAttribute(attr)...)
}


record.Attrs(func(attr slog.Attr) bool {
attributes = append(attributes, otelAttribute(withGroupPrefix(o.groupPrefix, attr))...)
return true
Expand Down
10 changes: 8 additions & 2 deletions otelslog/otel_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otelslog
import (
"bytes"
"context"
"go.opentelemetry.io/otel/baggage"
"log/slog"
"os"
"testing"
Expand Down Expand Up @@ -44,7 +45,8 @@ func TestNewOtelHandler(t *testing.T) {
)

handler := NewOtelHandler(loggerProvider, &HandlerOptions{
Level: slog.LevelInfo,
Level: slog.LevelInfo,
AddBaggage: true,
}).
WithAttrs([]slog.Attr{slog.String("first", "value1")}).
WithGroup("group1").
Expand All @@ -54,12 +56,16 @@ func TestNewOtelHandler(t *testing.T) {
otelLogger := slog.New(handler)
slog.SetDefault(otelLogger)

member, _ := baggage.NewMember("baggage.key", "true")
bag, _ := baggage.New(member)
ctx = baggage.ContextWithBaggage(ctx, bag)

doSomething(ctx)

loggerProvider.Shutdown(ctx)

actual := buf.String()

assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.0.1] {host.name=")
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}")
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}")
}