Skip to content

Commit

Permalink
feat(logger): Adding otel attribute to logger
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Dec 9, 2023
1 parent 193f110 commit 0286585
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
35 changes: 25 additions & 10 deletions pkg/telemetry/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ import (
"log/slog"
"strconv"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

func (s Service) AddTraceToLogHandler(handler slog.Handler) slog.Handler {
return OtlpLogger{
Handler: handler,
Uint64: s.TraceUint64,
Handler: handler,
Uint64: s.TraceUint64,
Attributes: otelAttributeToSlogAttr(s.resource.Attributes()),
}
}

type OtlpLogger struct {
slog.Handler
Uint64 bool
}
func otelAttributeToSlogAttr(attributes []attribute.KeyValue) []slog.Attr {
output := make([]slog.Attr, len(attributes))

func (tl OtlpLogger) getTraceID(id string) string {
if tl.Uint64 {
return uint64TraceId(id)
for index, attribute := range attributes {
output[index] = slog.String(string(attribute.Key), attribute.Value.AsString())
}

return id
return output
}

type OtlpLogger struct {
slog.Handler
Attributes []slog.Attr
Uint64 bool
}

func (tl OtlpLogger) Handle(ctx context.Context, r slog.Record) error {
Expand All @@ -39,9 +44,19 @@ func (tl OtlpLogger) Handle(ctx context.Context, r slog.Record) error {
r.AddAttrs(slog.String("span_id", tl.getTraceID(spanCtx.SpanID().String())))
}

r.AddAttrs(tl.Attributes...)

return tl.Handler.Handle(ctx, r)
}

func (tl OtlpLogger) getTraceID(id string) string {
if tl.Uint64 {
return uint64TraceId(id)
}

return id
}

func uint64TraceId(id string) string {
if len(id) < 16 {
return ""
Expand Down
6 changes: 4 additions & 2 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
tr "go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)

type Service struct {
resource *resource.Resource
tracerProvider *trace.TracerProvider
meterProvider *metric.MeterProvider
TraceUint64 bool
Expand Down Expand Up @@ -103,6 +105,7 @@ func New(ctx context.Context, config *Config) (Service, error) {
}

return Service{
resource: otelResource,
tracerProvider: tracerProvider,
meterProvider: meterProvider,
}, nil
Expand Down Expand Up @@ -183,8 +186,7 @@ func newResource(ctx context.Context) (*resource.Resource, error) {
newResource, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithAttributes(
attribute.String("version", model.Version()),
attribute.String("language", "go"),
semconv.ServiceVersion(model.Version()),
),
)
if err != nil {
Expand Down

0 comments on commit 0286585

Please sign in to comment.