-
Notifications
You must be signed in to change notification settings - Fork 3
/
telemetry.go
127 lines (103 loc) · 4.86 KB
/
telemetry.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
123
124
125
126
127
package zikade
import (
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"github.com/probe-lab/zikade/tele"
)
// Telemetry is the struct that holds a reference to all metrics and the tracer.
// Initialize this struct with [NewTelemetry]. Make sure
// to also register the [MeterProviderOpts] with your custom or the global
// [metric.MeterProvider].
//
// To see the documentation for each metric below, check out [NewTelemetry] and the
// metric.WithDescription() calls when initializing each metric.
type Telemetry struct {
Tracer trace.Tracer
ReceivedMessages metric.Int64Counter
ReceivedMessageErrors metric.Int64Counter
ReceivedBytes metric.Int64Histogram
InboundRequestLatency metric.Float64Histogram
OutboundRequestLatency metric.Float64Histogram
SentMessages metric.Int64Counter // number of messages sent that did not expect a response
SentMessageErrors metric.Int64Counter
SentRequests metric.Int64Counter // number of messages sent that expected a response
SentRequestErrors metric.Int64Counter
SentBytes metric.Int64Histogram
LRUCache metric.Int64Counter
NetworkSize metric.Int64Counter
}
// NewWithGlobalProviders uses the global meter and tracer providers from
// opentelemetry. Check out the documentation of [MeterProviderOpts] for
// implications of using this constructor.
func NewWithGlobalProviders() (*Telemetry, error) {
return NewTelemetry(otel.GetMeterProvider(), otel.GetTracerProvider())
}
// NewTelemetry initializes a Telemetry struct with the given meter and tracer providers.
// It constructs the different metric counters and histograms. The histograms
// have custom boundaries. Therefore, the given [metric.MeterProvider] should
// have the custom view registered that [MeterProviderOpts] returns.
func NewTelemetry(meterProvider metric.MeterProvider, tracerProvider trace.TracerProvider) (*Telemetry, error) {
var err error
if meterProvider == nil {
meterProvider = otel.GetMeterProvider()
}
if tracerProvider == nil {
tracerProvider = otel.GetTracerProvider()
}
t := &Telemetry{
Tracer: tracerProvider.Tracer(tele.TracerName),
}
meter := meterProvider.Meter(tele.MeterName)
// Initalize metrics for the DHT
t.ReceivedMessages, err = meter.Int64Counter("received_messages", metric.WithDescription("Total number of messages received per RPC"))
if err != nil {
return nil, fmt.Errorf("received_messages counter: %w", err)
}
t.ReceivedMessageErrors, err = meter.Int64Counter("received_message_errors", metric.WithDescription("Total number of errors for messages received per RPC"))
if err != nil {
return nil, fmt.Errorf("received_message_errors counter: %w", err)
}
t.ReceivedBytes, err = meter.Int64Histogram("received_bytes", metric.WithDescription("Total received bytes per RPC"), metric.WithUnit("By"))
if err != nil {
return nil, fmt.Errorf("received_bytes histogram: %w", err)
}
t.InboundRequestLatency, err = meter.Float64Histogram("inbound_request_latency", metric.WithDescription("Latency per RPC"), metric.WithUnit("ms"))
if err != nil {
return nil, fmt.Errorf("inbound_request_latency histogram: %w", err)
}
t.OutboundRequestLatency, err = meter.Float64Histogram("outbound_request_latency", metric.WithDescription("Latency per RPC"), metric.WithUnit("ms"))
if err != nil {
return nil, fmt.Errorf("outbound_request_latency histogram: %w", err)
}
t.SentMessages, err = meter.Int64Counter("sent_messages", metric.WithDescription("Total number of messages sent per RPC"))
if err != nil {
return nil, fmt.Errorf("sent_messages counter: %w", err)
}
t.SentMessageErrors, err = meter.Int64Counter("sent_message_errors", metric.WithDescription("Total number of errors for messages sent per RPC"))
if err != nil {
return nil, fmt.Errorf("sent_message_errors counter: %w", err)
}
t.SentRequests, err = meter.Int64Counter("sent_requests", metric.WithDescription("Total number of requests sent per RPC"))
if err != nil {
return nil, fmt.Errorf("sent_requests counter: %w", err)
}
t.SentRequestErrors, err = meter.Int64Counter("sent_request_errors", metric.WithDescription("Total number of errors for requests sent per RPC"))
if err != nil {
return nil, fmt.Errorf("sent_request_errors counter: %w", err)
}
t.SentBytes, err = meter.Int64Histogram("sent_bytes", metric.WithDescription("Total sent bytes per RPC"), metric.WithUnit("By"))
if err != nil {
return nil, fmt.Errorf("sent_bytes histogram: %w", err)
}
t.LRUCache, err = meter.Int64Counter("lru_cache", metric.WithDescription("Cache hit or miss counter"))
if err != nil {
return nil, fmt.Errorf("lru_cache counter: %w", err)
}
t.NetworkSize, err = meter.Int64Counter("network_size", metric.WithDescription("Network size estimation"))
if err != nil {
return nil, fmt.Errorf("network_size counter: %w", err)
}
return t, nil
}