-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_reporter.go
103 lines (86 loc) · 2.72 KB
/
client_reporter.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
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package otelgrpc
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"google.golang.org/grpc/codes"
)
type clientReporter struct {
metrics *ClientMetrics
rpcType grpcType
serviceName string
methodName string
startTime time.Time
}
func newClientReporter(ctx context.Context, m *ClientMetrics, rpcType grpcType, fullMethod string) *clientReporter {
r := &clientReporter{
metrics: m,
rpcType: rpcType,
}
if r.metrics.clientHandledHistogramEnabled {
r.startTime = time.Now()
}
r.serviceName, r.methodName = splitMethodName(fullMethod)
r.metrics.counters[clientStartedCounter].Add(ctx, 1,
append(
r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
)...,
)
return r
}
func (r *clientReporter) ReceiveMessageTimer(ctx context.Context, startTime time.Time) {
r.startTimer(ctx, clientStreamRecvHistogram, startTime)
}
func (r *clientReporter) ReceivedMessage(ctx context.Context) {
r.metrics.counters[clientStreamMsgReceived].Add(ctx, 1,
append(r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
)...,
)
}
func (r *clientReporter) SendMessageTimer(ctx context.Context, startTime time.Time) {
r.startTimer(ctx, clientStreamSendHistogram, startTime)
}
func (r *clientReporter) SentMessage(ctx context.Context) {
r.metrics.counters[clientStreamMsgSent].Add(ctx, 1,
append(r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
)...,
)
}
func (r *clientReporter) Handled(ctx context.Context, code codes.Code) {
r.metrics.counters[clientHandledCounter].Add(ctx, 1,
append(r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
attribute.String(AttrCode, code.String()),
)...,
)
if r.metrics.clientHandledHistogramEnabled {
r.startTimer(ctx, clientHandledHistogram, r.startTime)
}
}
func (r *clientReporter) startTimer(ctx context.Context, metric string, startTime time.Time) {
if !r.metrics.clientHandledHistogramEnabled {
return
}
dur := float64(time.Since(startTime).Milliseconds())
opt := bucketSelect(r.metrics.bucket, dur)
r.metrics.valueRecorders[metric].Record(ctx, opt,
append(r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
)...,
)
}