-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumenter.go
113 lines (91 loc) · 2.83 KB
/
instrumenter.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
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
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package instrumenter
import (
"context"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/trace"
"github.com/insolar/insolar/insolar/utils"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/insmetrics"
"github.com/insolar/insolar/instrumentation/instracer"
)
type methodInstrumenterKey struct{}
type MethodInstrumenter struct {
ctx context.Context
methodName string
subMethodName *string
startTime time.Time
errorLink *error
errorShort string
span *trace.Span
traceID string
}
func NewMethodInstrument(methodName string) (context.Context, *MethodInstrumenter) {
traceID := utils.RandTraceID()
ctx, _ := inslogger.WithTraceField(context.Background(), traceID)
ctx, span := instracer.StartSpan(ctx, methodName)
ctx = insmetrics.InsertTag(ctx, tagMethod, methodName)
stats.Record(ctx, incomingRequests.M(1))
instrumenter := &MethodInstrumenter{
ctx: ctx,
startTime: time.Now(),
methodName: methodName,
span: span,
traceID: traceID,
}
ctx = context.WithValue(ctx, methodInstrumenterKey{}, instrumenter)
return ctx, instrumenter
}
func (mi *MethodInstrumenter) SetCallSite(callSite string) {
mi.subMethodName = &callSite
}
func (mi *MethodInstrumenter) SetError(err error, errShort string) {
mi.errorLink = &err
mi.errorShort = errShort
}
func (mi MethodInstrumenter) TraceID() string {
return mi.traceID
}
func (mi MethodInstrumenter) Annotate(text string) {
mi.span.Annotate(nil, text)
}
func (mi *MethodInstrumenter) End() {
latency := time.Since(mi.startTime)
ctx := mi.ctx
if mi.errorLink != nil && *mi.errorLink != nil {
instracer.AddError(mi.span, *mi.errorLink)
}
if mi.errorShort != "" {
ctx = insmetrics.InsertTag(ctx, tagError, mi.errorShort)
}
if mi.subMethodName != nil {
ctx = insmetrics.InsertTag(ctx, tagSubMethod, *mi.subMethodName)
}
stats.Record(ctx, statLatency.M(latency.Nanoseconds()/1e6))
mi.span.End()
}
func GetInstrumenter(ctx context.Context) *MethodInstrumenter {
return ctx.Value(methodInstrumenterKey{}).(*MethodInstrumenter)
}
func GetTraceID(ctx context.Context) string {
instrumenter := GetInstrumenter(ctx)
if instrumenter != nil {
return instrumenter.traceID
}
return ""
}