-
Notifications
You must be signed in to change notification settings - Fork 0
/
otel.tmpl
46 lines (39 loc) · 1.31 KB
/
otel.tmpl
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
// The body from the function `main` will be injected at the start of each function.
//
// The comment `//otelinji:check-indents` specifies indents that indicate
// which injections should be skipped.
//
// Specified imports will be added to target files.
//
// Template support next arguments:
// - `CtxParamName` - `ctx` variable name or empty.
// - `FuncName` - current function name.
// - `PackageName` - current package name.
// - `ReceiverType` - current receiver type name or empty.
// - `IsContextUsed` - indicates either context is used in the function or not.
// - `ErrResultName` - result name of `err` of empty string.
//
// Functions:
// - `joinWithDot` joins to arguments by a dot, if one of them is empty,
// then the dot will be skipped.
//
package main
import (
"go.opentelemetry.io/otel"
"github.com/hedhyw/otelinji/pkg/otelinji"
)
{{$MethodName := joinWithDot .ReceiverType .FuncName }}
//otelinji:check-indents otel,Tracer
func main() {
{{if .CtxParamName -}}
{{.CtxParamName}}, span := otel.Tracer("{{.PackageName}}").Start({{.CtxParamName}}, "{{$MethodName}}")
{{if .ErrResultName}}
defer func(){ otelinji.EndSpanWithErr(span, {{.ErrResultName}}) }()
{{else}}
defer span.End()
{{end}}
{{if not .IsContextUsed }}
_ = {{.CtxParamName}}
{{end}}
{{- end}}
}