-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package tracer | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"strings" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
// slogHandler implements the slog.Handler interface to dispatch messages to our | ||
// internal logger. | ||
type slogHandler struct { | ||
attrs []string | ||
groups []string | ||
} | ||
|
||
func (h slogHandler) Enabled(ctx context.Context, lvl slog.Level) bool { | ||
if lvl <= slog.LevelDebug { | ||
return log.DebugEnabled() | ||
} | ||
// TODO(fg): Implement generic log level checking in the internal logger. | ||
// But we're we're not concerned with slog perf, so this is okay for now. | ||
return true | ||
} | ||
|
||
func (h slogHandler) Handle(ctx context.Context, r slog.Record) error { | ||
parts := make([]string, 0, 1+len(h.attrs)+r.NumAttrs()) | ||
parts = append(parts, r.Message) | ||
parts = append(parts, h.attrs...) | ||
r.Attrs(func(a slog.Attr) bool { | ||
parts = append(parts, formatAttr(a, h.groups)) | ||
return true | ||
}) | ||
|
||
msg := strings.Join(parts, " ") | ||
switch r.Level { | ||
case slog.LevelDebug: | ||
log.Debug(msg) | ||
case slog.LevelInfo: | ||
log.Info(msg) | ||
case slog.LevelWarn: | ||
log.Warn(msg) | ||
case slog.LevelError: | ||
log.Error(msg) | ||
} | ||
return nil | ||
} | ||
|
||
func (h slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
for _, a := range attrs { | ||
h.attrs = append(h.attrs, formatAttr(a, h.groups)) | ||
} | ||
return h | ||
} | ||
|
||
func (h slogHandler) WithGroup(name string) slog.Handler { | ||
h.groups = append(h.groups, name) | ||
return h | ||
} | ||
|
||
func formatAttr(a slog.Attr, groups []string) string { | ||
return strings.Join(append(groups, a.String()), ".") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tracer | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
func Test_slogHandler(t *testing.T) { | ||
// Create a record logger to capture the logs and restore the original | ||
// logger at the end. | ||
rl := &log.RecordLogger{} | ||
defer log.UseLogger(rl)() | ||
|
||
// Log a few messages at different levels. The debug message gets discarded | ||
// because the internal logger does not have debug enabled by default. | ||
l := slog.New(slogHandler{}) | ||
l = l.With("foo", "bar") | ||
l = l.WithGroup("a").WithGroup("b") | ||
l.Debug("debug test", "n", 0) | ||
l.Info("info test", "n", 1) | ||
l.Warn("warn test", "n", 2) | ||
l.Error("error test", "n", 3) | ||
log.Flush() // needed to get the error log flushed | ||
|
||
// Check that the logs were written correctly. | ||
require.Len(t, rl.Logs(), 3) | ||
require.Contains(t, rl.Logs()[0], "info test foo=bar a.b.n=1") | ||
require.Contains(t, rl.Logs()[1], "warn test foo=bar a.b.n=2") | ||
require.Contains(t, rl.Logs()[2], "error test foo=bar a.b.n=3") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package tracer | |
import ( | ||
gocontext "context" | ||
"encoding/binary" | ||
"log/slog" | ||
"math" | ||
"os" | ||
"runtime/pprof" | ||
|
@@ -32,6 +33,7 @@ import ( | |
"gopkg.in/DataDog/dd-trace-go.v1/internal/traceprof" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/obfuscate" | ||
"github.com/DataDog/go-runtime-metrics-internal/pkg/runtimemetrics" | ||
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-core
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-core
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-core
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-contrib
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-contrib
Check failure on line 36 in ddtrace/tracer/tracer.go GitHub Actions / PR Unit and Integration Tests / test-contrib
|
||
) | ||
|
||
var _ ddtrace.Tracer = (*tracer)(nil) | ||
|
@@ -307,6 +309,14 @@ func newTracer(opts ...StartOption) *tracer { | |
t.reportRuntimeMetrics(defaultMetricsReportInterval) | ||
}() | ||
} | ||
if c.runtimeMetricsV2 { | ||
l := slog.New(slogHandler{}) | ||
if err := runtimemetrics.Start(t.statsd, l); err == nil { | ||
l.Debug("Runtime metrics v2 enabled.") | ||
} else { | ||
l.Error("Failed to enable runtime metrics v2", "err", err.Error()) | ||
} | ||
} | ||
if c.debugAbandonedSpans { | ||
log.Info("Abandoned spans logs enabled.") | ||
t.abandonedSpansDebugger = newAbandonedSpansDebugger() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters