Skip to content

Commit

Permalink
contrib: add log/slog instrumentation (#2765)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored Jul 1, 2024
1 parent bdc072c commit 974aea6
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 3 deletions.
48 changes: 48 additions & 0 deletions contrib/log/slog/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

package slog_test

import (
"context"
"log/slog"
"os"

slogtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/log/slog"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func ExampleNewJSONHandler() {
// start the DataDog tracer
tracer.Start()
defer tracer.Stop()

// create the application logger
logger := slog.New(slogtrace.NewJSONHandler(os.Stdout, nil))

// start a new span
span, ctx := tracer.StartSpanFromContext(context.Background(), "ExampleNewJSONHandler")
defer span.Finish()

// log a message using the context containing span information
logger.Log(ctx, slog.LevelInfo, "this is a log with tracing information")
}

func ExampleWrapHandler() {
// start the DataDog tracer
tracer.Start()
defer tracer.Stop()

// create the application logger
myHandler := slog.NewJSONHandler(os.Stdout, nil)
logger := slog.New(slogtrace.WrapHandler(myHandler))

// start a new span
span, ctx := tracer.StartSpanFromContext(context.Background(), "ExampleWrapHandler")
defer span.Finish()

// log a message using the context containing span information
logger.Log(ctx, slog.LevelInfo, "this is a log with tracing information")
}
51 changes: 51 additions & 0 deletions contrib/log/slog/slog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

// Package slog provides functions to correlate logs and traces using log/slog package (https://pkg.go.dev/log/slog).
package slog // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/log/slog"

import (
"context"
"io"
"log/slog"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
)

const componentName = "log/slog"

func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported("log/slog")
}

// NewJSONHandler is a convenience function that returns a *slog.JSONHandler logger enhanced with
// tracing information.
func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return WrapHandler(slog.NewJSONHandler(w, opts))
}

// WrapHandler enhances the given logger handler attaching tracing information to logs.
func WrapHandler(h slog.Handler) slog.Handler {
return &handler{h}
}

type handler struct {
slog.Handler
}

// Handle handles the given Record, attaching tracing information if found.
func (h *handler) Handle(ctx context.Context, rec slog.Record) error {
span, ok := tracer.SpanFromContext(ctx)
if ok {
rec.Add(
slog.Uint64(ext.LogKeyTraceID, span.Context().TraceID()),
slog.Uint64(ext.LogKeySpanID, span.Context().SpanID()),
)
}
return h.Handler.Handle(ctx, rec)
}
76 changes: 76 additions & 0 deletions contrib/log/slog/slog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

package slog

import (
"bytes"
"context"
"encoding/json"
"log/slog"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
internallog "gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

func assertLogEntry(t *testing.T, rawEntry, wantMsg, wantLevel string) {
t.Helper()

var data map[string]interface{}
err := json.Unmarshal([]byte(rawEntry), &data)
require.NoError(t, err)
require.NotEmpty(t, data)

assert.Equal(t, wantMsg, data["msg"])
assert.Equal(t, wantLevel, data["level"])
assert.NotEmpty(t, data["time"])
assert.NotEmpty(t, data[ext.LogKeyTraceID])
assert.NotEmpty(t, data[ext.LogKeySpanID])
}

func testLogger(t *testing.T, createHandler func(b *bytes.Buffer) slog.Handler) {
tracer.Start(tracer.WithLogger(internallog.DiscardLogger{}))
defer tracer.Stop()

// create the application logger
var b bytes.Buffer
h := createHandler(&b)
logger := slog.New(h)

// start a new span
span, ctx := tracer.StartSpanFromContext(context.Background(), "test")
defer span.Finish()

// log a message using the context containing span information
logger.Log(ctx, slog.LevelInfo, "this is an info log with tracing information")
logger.Log(ctx, slog.LevelError, "this is an error log with tracing information")

logs := strings.Split(
strings.TrimRight(b.String(), "\n"),
"\n",
)
// assert log entries contain trace information
require.Len(t, logs, 2)
assertLogEntry(t, logs[0], "this is an info log with tracing information", "INFO")
assertLogEntry(t, logs[1], "this is an error log with tracing information", "ERROR")
}

func TestNewJSONHandler(t *testing.T) {
testLogger(t, func(b *bytes.Buffer) slog.Handler {
return NewJSONHandler(b, nil)
})
}

func TestWrapHandler(t *testing.T) {
testLogger(t, func(b *bytes.Buffer) slog.Handler {
return WrapHandler(slog.NewJSONHandler(b, nil))
})
}
5 changes: 3 additions & 2 deletions contrib/sirupsen/logrus/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package logrus

import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"

Expand Down Expand Up @@ -34,7 +35,7 @@ func (d *DDContextLogHook) Fire(e *logrus.Entry) error {
if !found {
return nil
}
e.Data["dd.trace_id"] = span.Context().TraceID()
e.Data["dd.span_id"] = span.Context().SpanID()
e.Data[ext.LogKeyTraceID] = span.Context().TraceID()
e.Data[ext.LogKeySpanID] = span.Context().SpanID()
return nil
}
13 changes: 13 additions & 0 deletions ddtrace/ext/log_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

package ext

const (
// LogKeyTraceID is used by log integrations to correlate logs with a given trace.
LogKeyTraceID = "dd.trace_id"
// LogKeySpanID is used by log integrations to correlate logs with a given span.
LogKeySpanID = "dd.span_id"
)
1 change: 1 addition & 0 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var contribIntegrations = map[string]struct {
"github.com/urfave/negroni": {"Negroni", false},
"github.com/valyala/fasthttp": {"FastHTTP", false},
"github.com/zenazn/goji": {"Goji", false},
"log/slog": {"log/slog", false},
}

var (
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestAgentIntegration(t *testing.T) {
defer clearIntegrationsForTests()

cfg.loadContribIntegrations(nil)
assert.Equal(t, len(cfg.integrations), 55)
assert.Equal(t, 56, len(cfg.integrations))
for integrationName, v := range cfg.integrations {
assert.False(t, v.Instrumented, "integrationName=%s", integrationName)
}
Expand Down

0 comments on commit 974aea6

Please sign in to comment.