-
Notifications
You must be signed in to change notification settings - Fork 229
/
metrics.go
212 lines (182 loc) · 6.33 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Apache License 2.0.
* See the file "LICENSE" for details.
*/
package reporter
import (
"context"
"fmt"
"sync/atomic"
"google.golang.org/grpc/stats"
"github.com/elastic/otel-profiling-agent/libpf/xsync"
)
type statsHandlerImpl struct {
// Total number of uncompressed bytes in/out
numRPCBytesOut atomic.Int64
numRPCBytesIn atomic.Int64
// Total number of on-the-wire (post-compression) bytes in/out
numWireBytesOut atomic.Int64
numWireBytesIn atomic.Int64
// These two maps aggregate total in/out byte counts under each RPC method name
rpcBytesOut xsync.RWMutex[map[string]uint64]
rpcBytesIn xsync.RWMutex[map[string]uint64]
// These two maps aggregate total in/out byte counts under each RPC method name
wireBytesOut xsync.RWMutex[map[string]uint64]
wireBytesIn xsync.RWMutex[map[string]uint64]
}
// Make sure that the handler implements stats.Handler.
var _ stats.Handler = (*statsHandlerImpl)(nil)
// keyRPCTagInfo is the context key for our state.
//
// This is in a global to avoid having to allocate a new string on every call.
var keyRPCTagInfo = "RPCTagInfo"
// newStatsHandler creates a new statistics handler.
func newStatsHandler() *statsHandlerImpl {
return &statsHandlerImpl{
rpcBytesOut: xsync.NewRWMutex(map[string]uint64{}),
rpcBytesIn: xsync.NewRWMutex(map[string]uint64{}),
wireBytesOut: xsync.NewRWMutex(map[string]uint64{}),
wireBytesIn: xsync.NewRWMutex(map[string]uint64{}),
}
}
// TagRPC implements the stats.Handler interface.
func (sh *statsHandlerImpl) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return context.WithValue(ctx, &keyRPCTagInfo, info)
}
// TagConn implements the stats.Handler interface.
func (sh *statsHandlerImpl) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
return ctx
}
// HandleConn implements the stats.Handler interface.
func (sh *statsHandlerImpl) HandleConn(context.Context, stats.ConnStats) {
}
func rpcMethodFromContext(ctx context.Context) (string, error) {
tagInfo, ok := ctx.Value(&keyRPCTagInfo).(*stats.RPCTagInfo)
if !ok {
return "", fmt.Errorf("missing context key: %v", keyRPCTagInfo)
}
return tagInfo.FullMethodName, nil
}
// HandleRPC implements the stats.Handler interface.
func (sh *statsHandlerImpl) HandleRPC(ctx context.Context, s stats.RPCStats) {
var wireBytesIn, wireBytesOut, rpcBytesIn, rpcBytesOut int64
switch s := s.(type) {
case *stats.InPayload:
// WireLength is the length of data on wire (compressed, signed, encrypted,
// with gRPC framing).
wireBytesIn = int64(s.WireLength)
// Length is the uncompressed payload data
rpcBytesIn = int64(s.Length)
case *stats.OutPayload:
wireBytesOut = int64(s.WireLength)
rpcBytesOut = int64(s.Length)
default:
return
}
method, err := rpcMethodFromContext(ctx)
if err != nil {
// If this happens, it's a bug and we should visibly exit (context must contain tag)
panic(err)
}
if wireBytesIn != 0 {
sh.numWireBytesIn.Add(wireBytesIn)
sh.numRPCBytesIn.Add(rpcBytesIn)
wireIn := sh.wireBytesIn.WLock()
rpcIn := sh.rpcBytesIn.WLock()
defer sh.wireBytesIn.WUnlock(&wireIn)
defer sh.rpcBytesIn.WUnlock(&rpcIn)
(*wireIn)[method] += uint64(wireBytesIn)
(*rpcIn)[method] += uint64(rpcBytesIn)
}
if wireBytesOut != 0 {
sh.numWireBytesOut.Add(wireBytesOut)
wireOut := sh.wireBytesOut.WLock()
rpcOut := sh.rpcBytesOut.WLock()
defer sh.wireBytesOut.WUnlock(&wireOut)
defer sh.rpcBytesOut.WUnlock(&rpcOut)
(*wireOut)[method] += uint64(wireBytesOut)
(*rpcOut)[method] += uint64(rpcBytesOut)
}
}
func (sh *statsHandlerImpl) getWireBytesOut() int64 {
return sh.numWireBytesOut.Swap(0)
}
func (sh *statsHandlerImpl) getWireBytesIn() int64 {
return sh.numWireBytesIn.Swap(0)
}
func (sh *statsHandlerImpl) getRPCBytesOut() int64 {
return sh.numRPCBytesOut.Swap(0)
}
func (sh *statsHandlerImpl) getRPCBytesIn() int64 {
return sh.numRPCBytesIn.Swap(0)
}
// nolint:unused
func (sh *statsHandlerImpl) getMethodRPCBytesOut() map[string]uint64 {
rpcOut := sh.rpcBytesOut.RLock()
defer sh.rpcBytesOut.RUnlock(&rpcOut)
res := make(map[string]uint64, len(*rpcOut))
for k, v := range *rpcOut {
res[k] = v
}
return res
}
// nolint:unused
func (sh *statsHandlerImpl) getMethodRPCBytesIn() map[string]uint64 {
rpcIn := sh.rpcBytesIn.RLock()
defer sh.rpcBytesIn.RUnlock(&rpcIn)
res := make(map[string]uint64, len(*rpcIn))
for k, v := range *rpcIn {
res[k] = v
}
return res
}
// nolint:unused
func (sh *statsHandlerImpl) getMethodWireBytesOut() map[string]uint64 {
wireOut := sh.wireBytesOut.RLock()
defer sh.wireBytesOut.RUnlock(&wireOut)
res := make(map[string]uint64, len(*wireOut))
for k, v := range *wireOut {
res[k] = v
}
return res
}
// nolint:unused
func (sh *statsHandlerImpl) getMethodWireBytesIn() map[string]uint64 {
wireIn := sh.wireBytesIn.RLock()
defer sh.wireBytesIn.RUnlock(&wireIn)
res := make(map[string]uint64, len(*wireIn))
for k, v := range *wireIn {
res[k] = v
}
return res
}
// Metrics holds the metric counters for the reporter package.
type Metrics struct {
CountsForTracesOverwriteCount uint32
ExeMetadataOverwriteCount uint32
FrameMetadataOverwriteCount uint32
FramesForTracesOverwriteCount uint32
HostMetadataOverwriteCount uint32
MetricsOverwriteCount uint32
FallbackSymbolsOverwriteCount uint32
RPCBytesOutCount int64
RPCBytesInCount int64
WireBytesOutCount int64
WireBytesInCount int64
}
func (r *GRPCReporter) GetMetrics() Metrics {
return Metrics{
CountsForTracesOverwriteCount: r.countsForTracesQueue.getOverwriteCount(),
ExeMetadataOverwriteCount: r.execMetadataQueue.getOverwriteCount(),
FrameMetadataOverwriteCount: r.frameMetadataQueue.getOverwriteCount(),
FramesForTracesOverwriteCount: r.framesForTracesQueue.getOverwriteCount(),
HostMetadataOverwriteCount: r.hostMetadataQueue.getOverwriteCount(),
MetricsOverwriteCount: r.metricsQueue.getOverwriteCount(),
FallbackSymbolsOverwriteCount: r.fallbackSymbolsQueue.getOverwriteCount(),
RPCBytesOutCount: r.rpcStats.getRPCBytesOut(),
RPCBytesInCount: r.rpcStats.getRPCBytesIn(),
WireBytesOutCount: r.rpcStats.getWireBytesOut(),
WireBytesInCount: r.rpcStats.getWireBytesIn(),
}
}