forked from go-eagle/eagle
/
tracer.go
58 lines (46 loc) · 1.34 KB
/
tracer.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
54
55
56
57
58
package function
import (
"context"
"fmt"
"path"
"runtime"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/contrib"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
const (
// TraceName function trance name
TraceName = "github.com/willieso/baby-univ-biz-service/trace/plugins/function"
// PluginName plugin category name
PluginName = "function"
)
var tracer trace.Tracer
func init() {
tracer = otel.GetTracerProvider().Tracer(TraceName, trace.WithInstrumentationVersion(contrib.SemVersion()))
}
// StartFromContext create a new context
func StartFromContext(ctx context.Context) (context.Context, trace.Span) {
var spanAttrs []trace.SpanStartOption
fileName, lineNo, funcName := getCallerInfo(2)
caller := fmt.Sprintf("FuncName: %s, file: %s, line: %d", funcName, fileName, lineNo)
if caller != "" {
spanAttrs = append(spanAttrs, trace.WithAttributes(attribute.String("caller", caller)))
}
spanName := fmt.Sprintf("%s: %s", PluginName, funcName)
return tracer.Start(ctx, spanName, spanAttrs...)
}
func getCallerInfo(skip int) (string, int, string) {
var (
fileName string
lineNo int
funcName string
)
pc, file, lineNo, ok := runtime.Caller(skip)
if !ok {
return fileName, lineNo, funcName
}
funcName = runtime.FuncForPC(pc).Name()
fileName = path.Base(file)
return fileName, lineNo, funcName
}