SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
refactor(instrumentation): flatten code source into code.filepath, code.function, code.lineno (#10667)
Pandeya321ef8de8a5
diff --git a/pkg/instrumentation/logger.go b/pkg/instrumentation/logger.goindex 5663000a36..0f20b83ae1 100644--- a/pkg/instrumentation/logger.go+++ b/pkg/instrumentation/logger.go@@ -13,13 +13,7 @@ type zapToSlogConverter struct{}func NewLogger(config Config) *slog.Logger {logger := slog.New(loghandler.New(- slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: config.Logs.Level, AddSource: true, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {- // This is more in line with OpenTelemetry semantic conventions- if a.Key == slog.SourceKey {- a.Key = "code"- return a- }-+ slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: config.Logs.Level, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {if a.Key == slog.TimeKey {a.Key = "timestamp"return a@@ -27,6 +21,7 @@ func NewLogger(config Config) *slog.Logger {return a}}),+ loghandler.NewSource(),loghandler.NewCorrelation(),loghandler.NewFiltering(),loghandler.NewException(),diff --git a/pkg/instrumentation/loghandler/source.go b/pkg/instrumentation/loghandler/source.gonew file mode 100644index 0000000000..7d809d9cc4--- /dev/null+++ b/pkg/instrumentation/loghandler/source.go@@ -0,0 +1,28 @@+package loghandler++import (+ "context"+ "log/slog"+ "runtime"+)++type source struct{}++func NewSource() *source {+ return &source{}+}++func (h *source) Wrap(next LogHandler) LogHandler {+ return LogHandlerFunc(func(ctx context.Context, record slog.Record) error {+ if record.PC != 0 {+ frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()+ record.AddAttrs(+ slog.String("code.filepath", frame.File),+ slog.String("code.function", frame.Function),+ slog.Int("code.lineno", frame.Line),+ )+ }++ return next.Handle(ctx, record)+ })+}diff --git a/pkg/instrumentation/loghandler/source_test.go b/pkg/instrumentation/loghandler/source_test.gonew file mode 100644index 0000000000..e1c0418915--- /dev/null+++ b/pkg/instrumentation/loghandler/source_test.go@@ -0,0 +1,37 @@+package loghandler++import (+ "bytes"+ "context"+ "encoding/json"+ "log/slog"+ "testing"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func TestSource(t *testing.T) {+ src := NewSource()++ buf := bytes.NewBuffer(nil)+ logger := slog.New(&handler{base: slog.NewJSONHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug}), wrappers: []Wrapper{src}})++ logger.InfoContext(context.Background(), "test")++ m := make(map[string]any)+ err := json.Unmarshal(buf.Bytes(), &m)+ require.NoError(t, err)++ assert.Contains(t, m, "code.filepath")+ assert.Contains(t, m, "code.function")+ assert.Contains(t, m, "code.lineno")++ assert.Contains(t, m["code.filepath"], "source_test.go")+ assert.Contains(t, m["code.function"], "TestSource")+ assert.NotZero(t, m["code.lineno"])++ // Ensure the nested "source" key is not present.+ assert.NotContains(t, m, "source")+ assert.NotContains(t, m, "code")+}