Skip to content

Commit

Permalink
feat: load attributes from context
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 25, 2024
1 parent a293106 commit 5ce086f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Option struct {

// optional: customize webhook event builder
Converter Converter
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr

// optional: see slog.HandlerOptions
AddSource bool
Expand Down Expand Up @@ -127,6 +129,41 @@ func main() {
}
```

### Tracing

Import the samber/slog-otel library.

```go
import (
slogloki "github.com/samber/slog-loki"
slogotel "github.com/samber/slog-otel"
"go.opentelemetry.io/otel/sdk/trace"
)

func main() {
tp := trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
)
tracer := tp.Tracer("hello/world")

ctx, span := tracer.Start(context.Background(), "foo")
defer span.End()

span.AddEvent("bar")

logger := slog.New(
slogloki.Option{
// ...
AttrFromContext: []func(ctx context.Context) []slog.Attr{
slogotel.ExtractOtelAttrFromContext([]string{"tracing"}, "trace_id", "span_id"),
},
}.NewLokiHandler(),
)

logger.ErrorContext(ctx, "a message")
}
```

## 🤝 Contributing

- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/grafana/loki-client-go v0.0.0-20230116142646-e7494d0ef70c
github.com/prometheus/common v0.34.0
github.com/samber/slog-common v0.15.2
github.com/samber/slog-common v0.16.0
go.uber.org/goleak v1.2.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiB
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-common v0.15.2 h1:jQQK2MzJ1kfEsUyxI/mpwkqB2xI0qCo9Ne1D1yziuP0=
github.com/samber/slog-common v0.15.2/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/samber/slog-common v0.16.0 h1:2/t1EcFd1Ru77mh2ab+8B6NBHnEXsBBHtOJc7PSH0aI=
github.com/samber/slog-common v0.16.0/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
Expand Down
9 changes: 8 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Option struct {

// optional: customize webhook event builder
Converter Converter
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr

// optional: see slog.HandlerOptions
AddSource bool
Expand All @@ -40,6 +42,10 @@ func (o Option) NewLokiHandler() slog.Handler {
o.Converter = DefaultConverter
}

if o.AttrFromContext == nil {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}

return &LokiHandler{
option: o,
attrs: []slog.Attr{},
Expand All @@ -60,7 +66,8 @@ func (h *LokiHandler) Enabled(_ context.Context, level slog.Level) bool {
}

func (h *LokiHandler) Handle(ctx context.Context, record slog.Record) error {
attrs := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, h.attrs, h.groups, &record)
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
attrs := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)

return h.option.Client.Handle(attrs, record.Time, record.Message)
}
Expand Down

0 comments on commit 5ce086f

Please sign in to comment.