-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspanprocessor.go
53 lines (43 loc) · 1.24 KB
/
spanprocessor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package otelfuncmeta
import (
"context"
"fmt"
"runtime"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
type SpanProcessor struct {
callerDepth int
}
func NewSpanProcessor() *SpanProcessor {
return &SpanProcessor{
callerDepth: 4,
}
}
func NewSpanProcessorWithCallerDepth(depth int) *SpanProcessor {
return &SpanProcessor{
callerDepth: depth,
}
}
// OnStart is called when a span is started.
func (s *SpanProcessor) OnStart(parent context.Context, span sdktrace.ReadWriteSpan) {
pc, funcName, file, line := funcMetadata(s.callerDepth)
span.SetAttributes(
attribute.String("function.pc", fmt.Sprintf("0x%x", pc)),
attribute.String("function.name", funcName),
attribute.String("function.file", file),
attribute.Int("function.line", line),
)
}
func funcMetadata(depth int) (uintptr, string, string, int) {
var pcbuf [1]uintptr
runtime.Callers(depth, pcbuf[:])
pc := pcbuf[0]
f := runtime.FuncForPC(pc)
funcName := f.Name()
file, line := f.FileLine(pc)
return pc, funcName, file, line
}
func (s *SpanProcessor) OnEnd(_ sdktrace.ReadOnlySpan) {}
func (s *SpanProcessor) Shutdown(_ context.Context) error { return nil }
func (s *SpanProcessor) ForceFlush(_ context.Context) error { return nil }