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 26, 2024
1 parent 273df52 commit 05fca7a
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 json payload 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 @@ -187,6 +189,41 @@ attributes BINARY F 2 155.50 B "0x7B2263617465676F
source BINARY F 2 39.50 B "samber/slog-parquet" / "samber/slog-parquet"
```

### Tracing

Import the samber/slog-otel library.

```go
import (
slogparquet "github.com/samber/slog-parquet"
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(
slogparquet.Option{
// ...
AttrFromContext: []func(ctx context.Context) []slog.Attr{
slogotel.ExtractOtelAttrFromContext([]string{"tracing"}, "trace_id", "span_id"),
},
}.NewParquetHandler(),
)

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 @@ -6,7 +6,7 @@ require (
github.com/gofrs/uuid v4.4.0+incompatible
github.com/parquet-go/parquet-go v0.17.0
github.com/samber/lo v1.38.1
github.com/samber/slog-common v0.15.2
github.com/samber/slog-common v0.16.0
github.com/thanos-io/objstore v0.0.0-20230816175749-20395bffdf26
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 @@ -211,8 +211,8 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
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/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
Expand Down
9 changes: 8 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Option struct {

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

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

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

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

func (h *ParquetHandler) 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.Buffer.Append(
record.Time,
Expand Down

0 comments on commit 05fca7a

Please sign in to comment.