Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/github.com/hashicorp/g…
Browse files Browse the repository at this point in the history
…o-retryablehttp-0.7.7
  • Loading branch information
nsrip-dd authored Jul 2, 2024
2 parents 82a7f43 + 5525e1a commit c9ce2bc
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 102 deletions.
8 changes: 8 additions & 0 deletions contrib/gocql/gocql/gocql.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ func (tq *Query) MapScan(m map[string]interface{}) error {
return err
}

// MapScanCAS wraps in a span query.MapScanCAS call.
func (tq *Query) MapScanCAS(m map[string]interface{}) (applied bool, err error) {
span := tq.newChildSpan(tq.ctx)
applied, err = tq.Query.MapScanCAS(m)
tq.finishSpan(span, err)
return applied, err
}

// Scan wraps in a span query.Scan call.
func (tq *Query) Scan(dest ...interface{}) error {
span := tq.newChildSpan(tq.ctx)
Expand Down
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"
)
95 changes: 0 additions & 95 deletions ddtrace/tracer/exec_tracer_test.go

This file was deleted.

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
2 changes: 0 additions & 2 deletions internal/apps/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/gotraceui v0.2.0 h1:dmNsfQ9Vl3GwbiVD7Z8d/osC6WtGGrasyrC2suc4ZIQ=
honnef.co/go/gotraceui v0.2.0/go.mod h1:qHo4/W75cA3bX0QQoSvDjbJa4R8mAyyFjbWAj63XElc=
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
Expand Down
Loading

0 comments on commit c9ce2bc

Please sign in to comment.