/
opencensus_annotator.go
65 lines (57 loc) · 1.43 KB
/
opencensus_annotator.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
59
60
61
62
63
64
65
package profiler
import (
"context"
"errors"
"github.com/luthersystems/elps/lisp"
"go.opencensus.io/trace"
)
type ocAnnotator struct {
profiler
currentContext context.Context
currentSpan *trace.Span
}
var _ lisp.Profiler = &ocAnnotator{}
func NewOpenCensusAnnotator(runtime *lisp.Runtime, parentContext context.Context, opts ...Option) *ocAnnotator {
p := &ocAnnotator{
profiler: profiler{
runtime: runtime,
},
currentContext: parentContext,
}
p.profiler.applyConfigs(opts...)
return p
}
func (p *ocAnnotator) Enable() error {
p.runtime.Profiler = p
if p.currentContext == nil {
return errors.New("we can only append spans to a context that is linked to opencensus")
}
return p.profiler.Enable()
}
func (p *ocAnnotator) Complete() error {
if p.currentSpan != nil {
p.currentSpan.End()
}
return nil
}
func (p *ocAnnotator) Start(fun *lisp.LVal) func() {
if p.skipTrace(fun) {
return func() {}
}
oldContext := p.currentContext
prettyLabel, _ := p.prettyFunName(fun)
p.currentContext, p.currentSpan = trace.StartSpan(p.currentContext, prettyLabel)
loc := getSourceLoc(fun)
if loc != nil {
p.currentSpan.Annotate([]trace.Attribute{
trace.StringAttribute("file", loc.File),
trace.Int64Attribute("line", int64(loc.Line)),
}, "source")
}
return func() {
p.currentSpan.End()
// And pop the current context back
p.currentContext = oldContext
p.currentSpan = trace.FromContext(p.currentContext)
}
}