-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_reporter.go
86 lines (73 loc) · 2.25 KB
/
server_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
// 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 serverReporter struct {
metrics *ServerMetrics
rpcType grpcType
serviceName string
methodName string
startTime time.Time
}
func newServerReporter(ctx context.Context, m *ServerMetrics, rpcType grpcType, fullMethod string) *serverReporter {
r := &serverReporter{
metrics: m,
rpcType: rpcType,
}
if r.metrics.serverHandledHistogramEnabled {
r.startTime = time.Now()
}
r.serviceName, r.methodName = splitMethodName(fullMethod)
r.metrics.counters[serverStartedCounter].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 *serverReporter) ReceivedMessage(ctx context.Context) {
r.metrics.counters[serverStreamMsgReceived].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 *serverReporter) SentMessage(ctx context.Context) {
r.metrics.counters[serverStreamMsgSent].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 *serverReporter) Handled(ctx context.Context, code codes.Code) {
r.metrics.counters[serverHandledCounter].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.serverHandledHistogramEnabled {
dur := float64(time.Since(r.startTime).Milliseconds())
opt := bucketSelect(r.metrics.bucket, dur)
r.metrics.valueRecorders[serverHandledHistogram].Record(ctx, opt,
append(r.metrics.labels,
attribute.String(AttrType, string(r.rpcType)),
attribute.String(AttrService, r.serviceName),
attribute.String(AttrMethod, r.methodName),
)...,
)
}
}