-
Notifications
You must be signed in to change notification settings - Fork 443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ddtrace/tracer: runtime metrics v2 (exclude from release notes) #2772
Merged
felixge
merged 17 commits into
main
from
felix.geisendoerfer/PROF-8665-experimental-runtime-v2-metrics
Nov 7, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a7e5c8d
ddtrace/tracer: runtime metrics v2
felixge 0f5b5f6
Merge remote-tracking branch 'origin/main' into felix.geisendoerfer/P…
felixge fdd5e08
fix: test failure b/c of global log level leak
felixge 1276984
fix internal apps
felixge b070f10
copyright headers
felixge 2b9ebd0
fix one more log level leak
felixge 160885f
one more go.mod
felixge 17d94f1
Merge remote-tracking branch 'origin/main' into felix.geisendoerfer/P…
felixge 535a60a
fix global state pollution in a better way
felixge 7fcc908
Merge remote-tracking branch 'origin/main' into felix.geisendoerfer/P…
felixge 42701f7
statsdtest: implement new mock methods
felixge 110e409
add startup log
felixge a4be8ae
fix startup logs
felixge a5e1eba
fix: use BoolEnv instead of BoolVal
felixge f16058f
Upgrade go modules
felixge b1a595b
tidy go.mod
felixge bae27e7
Merge branch 'main' into felix.geisendoerfer/PROF-8665-experimental-r…
felixge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,69 @@ | ||
// 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 2024 Datadog, Inc. | ||
|
||
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,45 @@ | ||
// 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 2024 Datadog, Inc. | ||
|
||
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)() | ||
|
||
// Ensure the logger is set to the default level. This may not be the case | ||
// when previous tests pollute the global state. We leave the logger in the | ||
// state we found it to not contribute to this pollution ourselves. | ||
oldLevel := log.GetLevel() | ||
log.SetLevel(log.LevelWarn) | ||
defer log.SetLevel(oldLevel) | ||
|
||
// 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT from @Gandem: We should consider adding a test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolution: Will probably add one in a follow-up PR.