Skip to content

Commit

Permalink
splitFuncName
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Dec 10, 2024
1 parent 740fc74 commit 5af95a4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
17 changes: 16 additions & 1 deletion bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"log/slog"
"runtime"
"slices"
"strings"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
Expand Down Expand Up @@ -192,9 +193,11 @@ func (h *Handler) convertRecord(r slog.Record) log.Record {
if h.source {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
funcName, namespace := splitFuncName(f.Function)
record.AddAttributes(
log.String(string(semconv.CodeFilepathKey), f.File),
log.String(string(semconv.CodeFunctionKey), f.Function),
log.String(string(semconv.CodeFunctionKey), funcName),
log.String(string(semconv.CodeNamespaceKey), namespace),
log.Int(string(semconv.CodeLineNumberKey), f.Line),
)
}
Expand Down Expand Up @@ -476,3 +479,15 @@ func convert(v slog.Value) log.Value {
return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", v.Kind(), v.Any()))
}
}

// splitFuncName splits package path-qualified function name into
// function name and package full name (namespace). E.g. it splits
// "github.com/my/repo/pkg.foo" into
// "foo" and "github.com/my/repo/pkg".
func splitFuncName(f string) (string, string) {
i := strings.LastIndexByte(f, '.')
if i < 0 {
return "", ""
}
return f[i+1:], f[:i]
}
39 changes: 38 additions & 1 deletion bridges/otelslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (h *wrapper) Handle(ctx context.Context, r slog.Record) error {
func TestSLogHandler(t *testing.T) {
// Capture the PC of this line
pc, file, line, _ := runtime.Caller(0)
funcName := runtime.FuncForPC(pc).Name()
funcName, namespace := splitFuncName(runtime.FuncForPC(pc).Name())

cases := []testCase{
{
Expand Down Expand Up @@ -414,6 +414,7 @@ func TestSLogHandler(t *testing.T) {
checks: [][]check{{
hasAttr(string(semconv.CodeFilepathKey), file),
hasAttr(string(semconv.CodeFunctionKey), funcName),
hasAttr(string(semconv.CodeNamespaceKey), namespace),
hasAttr(string(semconv.CodeLineNumberKey), int64(line)),
}},
options: []Option{WithSource(true)},
Expand Down Expand Up @@ -510,6 +511,42 @@ func TestHandlerEnabled(t *testing.T) {
assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed")
}

func TestSplitFuncName(t *testing.T) {
testCases := []struct {
fullFuncName string
wantFuncName string
wantNamespace string
}{
{
fullFuncName: "github.com/my/repo/pkg.foo",
wantFuncName: "foo",
wantNamespace: "github.com/my/repo/pkg",
},
{
fullFuncName: "net/http.Get",
wantFuncName: "Get",
wantNamespace: "net/http",
},
{
fullFuncName: "invalid",
wantFuncName: "",
wantNamespace: "",
},
{
fullFuncName: ".",
wantFuncName: "",
wantNamespace: "",
},
}
for _, tc := range testCases {
t.Run(tc.fullFuncName, func(t *testing.T) {
gotFuncName, gotNamespace := splitFuncName(tc.fullFuncName)
assert.Equal(t, tc.wantFuncName, gotFuncName)
assert.Equal(t, tc.wantNamespace, gotNamespace)
})
}
}

func BenchmarkHandler(b *testing.B) {
var (
h slog.Handler
Expand Down

0 comments on commit 5af95a4

Please sign in to comment.