-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathopencensus
49 lines (43 loc) · 1.57 KB
/
opencensus
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
import (
"go.opencensus.io/trace"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithTracing" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with opentracing spans
type {{$decorator}} struct {
{{.Interface.Type}}
_instance string
_spanDecorator func(span *trace.Span, params, results map[string]interface{})
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}}, instance string, spanDecorator ...func(span *trace.Span, params, results map[string]interface{})) {{$decorator}} {
d := {{$decorator}} {
{{.Interface.Name}}: base,
_instance: instance,
}
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
d._spanDecorator = spanDecorator[0]
}
return d
}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
ctx, _span := trace.StartSpan(ctx, _d._instance + ".{{$.Interface.Type}}.{{$method.Name}}")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, {{$method.ParamsMap}}, {{$method.ResultsMap}})
}{{- if $method.ReturnsError}} else if err != nil {
_span.AddAttributes(
trace.BoolAttribute("error", true),
trace.StringAttribute("event", "error"),
trace.StringAttribute("message", err.Error()),
)
}
{{end}}
_span.End()
}()
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}