-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
122 lines (104 loc) · 3.49 KB
/
metrics.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
114
115
116
117
118
119
120
121
122
package totem
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
type MetricsExporter struct {
rxBytesCollector metric.Int64Counter
txBytesCollector metric.Int64Counter
rxRpcCollector metric.Int64Counter
txRpcCollector metric.Int64Counter
svcRxLatencyCollector metric.Int64Histogram
svcTxLatencyCollector metric.Int64Histogram
staticAttrs []attribute.KeyValue
}
func NewMetricsExporter(provider metric.MeterProvider, staticAttrs ...attribute.KeyValue) *MetricsExporter {
meter := provider.Meter("github.com/kralicky/totem/metrics")
rxBytes, err := meter.Int64Counter("stream_receive_bytes",
metric.WithDescription("Total number of bytes received on a stream"))
if err != nil {
panic(err)
}
txBytes, err := meter.Int64Counter("stream_transmit_bytes",
metric.WithDescription("Total number of bytes transmitted on a stream"))
if err != nil {
panic(err)
}
rxRpc, err := meter.Int64Counter("stream_receive_rpcs",
metric.WithDescription("Total number of requests and replies received on a stream"))
if err != nil {
panic(err)
}
txRpc, err := meter.Int64Counter("stream_transmit_rpcs",
metric.WithDescription("Total number of requests and replies transmitted on a stream"))
if err != nil {
panic(err)
}
svcRxLatency, err := meter.Int64Histogram("stream_local_service_latency",
metric.WithDescription("Incoming RPC request-response latency for services handled locally on a stream"),
metric.WithUnit("μs"),
)
if err != nil {
panic(err)
}
svcTxLatency, err := meter.Int64Histogram("stream_remote_service_latency",
metric.WithDescription("Outgoing RPC request-response latency for services handled remotely on a stream"),
metric.WithUnit("μs"),
)
if err != nil {
panic(err)
}
return &MetricsExporter{
rxBytesCollector: rxBytes,
txBytesCollector: txBytes,
rxRpcCollector: rxRpc,
txRpcCollector: txRpc,
svcRxLatencyCollector: svcRxLatency,
svcTxLatencyCollector: svcTxLatency,
staticAttrs: staticAttrs,
}
}
func (m *MetricsExporter) TrackRxBytes(service, method string, count int64) {
if m == nil {
return
}
attrs := append(m.staticAttrs,
attribute.Key("service").String(service),
attribute.Key("method").String(method),
)
m.rxRpcCollector.Add(context.Background(), 1, metric.WithAttributes(attrs...))
m.rxBytesCollector.Add(context.Background(), count, metric.WithAttributes(attrs...))
}
func (m *MetricsExporter) TrackTxBytes(service, method string, count int64) {
if m == nil {
return
}
attrs := append(m.staticAttrs,
attribute.Key("service").String(service),
attribute.Key("method").String(method),
)
m.txRpcCollector.Add(context.Background(), 1, metric.WithAttributes(attrs...))
m.txBytesCollector.Add(context.Background(), count, metric.WithAttributes(attrs...))
}
func (m *MetricsExporter) TrackSvcRxLatency(service, method string, latency time.Duration) {
if m == nil {
return
}
attrs := append(m.staticAttrs,
attribute.Key("service").String(service),
attribute.Key("method").String(method),
)
m.svcRxLatencyCollector.Record(context.Background(), latency.Microseconds(), metric.WithAttributes(attrs...))
}
func (m *MetricsExporter) TrackSvcTxLatency(service, method string, latency time.Duration) {
if m == nil {
return
}
attrs := append(m.staticAttrs,
attribute.Key("service").String(service),
attribute.Key("method").String(method),
)
m.svcTxLatencyCollector.Record(context.Background(), latency.Microseconds(), metric.WithAttributes(attrs...))
}