-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto.go
More file actions
121 lines (113 loc) · 3.65 KB
/
auto.go
File metadata and controls
121 lines (113 loc) · 3.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package stepmark
import (
"context"
"runtime"
"strings"
)
var noop = func() {}
// Step records an unscoped event, automatically using the calling
// function's name as the stage.
//
// func ProcessOrder(ctx context.Context) {
// stepmark.Step(ctx, "started", nil) // stage = "ProcessOrder"
// stepmark.Step(ctx, "validated", nil) // stage = "ProcessOrder"
// }
func Step(ctx context.Context, action string, meta map[string]any) {
t, _ := ctx.Value(contextKey{}).(*tracer)
if t == nil {
return
}
t.record(resolveCallerName(2), action, meta)
}
// StepEntity records an entity event, automatically using the calling
// function's name as the stage. Respects [WithEntityFilter].
//
// func ScoreProduct(ctx context.Context, productID string, score float64) {
// stepmark.StepEntity(ctx, productID, "scored", map[string]any{"score": score})
// // stage = "ScoreProduct"
// }
func StepEntity(ctx context.Context, entityID, action string, meta map[string]any) {
t, _ := ctx.Value(contextKey{}).(*tracer)
if t == nil {
return
}
if t.entityFilter != nil && !t.entityFilter(entityID) {
return
}
t.recordEntity(entityID, resolveCallerName(2), action, meta)
}
// Enter records a function entry event and returns a function that
// records the exit with duration. Typically used with defer:
//
// func ValidateOrder(ctx context.Context) error {
// defer stepmark.Enter(ctx, nil)()
// // stage = "ValidateOrder", action = "entered" ... then "exited"
// }
func Enter(ctx context.Context, meta map[string]any) func() {
t, _ := ctx.Value(contextKey{}).(*tracer)
if t == nil {
return noop
}
stage := resolveCallerName(2)
t.record(stage, "entered", meta)
start := t.clock()
return func() {
elapsed := t.clock().Sub(start)
t.record(stage, "exited", map[string]any{
"duration_ms": float64(elapsed.Microseconds()) / 1000.0,
})
}
}
// EnterEntity records a function entry for a specific entity and returns
// a function that records the exit with duration. Respects [WithEntityFilter].
//
// func ChargePayment(ctx context.Context, orderID string) error {
// defer stepmark.EnterEntity(ctx, orderID, nil)()
// // Records entered/exited events on orderID with stage = "ChargePayment"
// }
func EnterEntity(ctx context.Context, entityID string, meta map[string]any) func() {
t, _ := ctx.Value(contextKey{}).(*tracer)
if t == nil {
return noop
}
if t.entityFilter != nil && !t.entityFilter(entityID) {
return noop
}
stage := resolveCallerName(2)
t.recordEntity(entityID, stage, "entered", meta)
start := t.clock()
return func() {
elapsed := t.clock().Sub(start)
t.recordEntity(entityID, stage, "exited", map[string]any{
"duration_ms": float64(elapsed.Microseconds()) / 1000.0,
})
}
}
// resolveCallerName returns the short function name of the caller
// at the given stack depth. skip=2 means: resolveCallerName → public
// function (Step/Enter/etc.) → user's function.
func resolveCallerName(skip int) string {
pc, _, _, ok := runtime.Caller(skip)
if !ok {
return "unknown"
}
return cleanFuncName(runtime.FuncForPC(pc).Name())
}
// cleanFuncName extracts a readable function name from the fully
// qualified name returned by runtime.FuncForPC.
//
// "github.com/user/pkg.Function" → "Function"
// "github.com/user/pkg.(*Type).Method" → "Type.Method"
// "github.com/user/pkg.Function.func1" → "Function.func1"
func cleanFuncName(full string) string {
if i := strings.LastIndexByte(full, '/'); i >= 0 {
full = full[i+1:]
}
if i := strings.IndexByte(full, '.'); i >= 0 {
full = full[i+1:]
}
if after, ok := strings.CutPrefix(full, "(*"); ok {
return strings.Replace(after, ").", ".", 1)
}
return full
}