/
reporter.go
119 lines (103 loc) · 3.6 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monitoring
import (
"sync"
"google.golang.org/grpc/codes"
)
type errorCodeKey struct {
error string
code codes.Code
}
type requestKey struct {
typeURL string
connectionID int64
}
type nackKey struct {
typeURL string
connectionID int64
code codes.Code
}
// InMemoryStatsContext enables MCP server metric collection which is
// stored in memory for testing purposes.
type InMemoryStatsContext struct {
mutex sync.Mutex
StreamTotal int64
RequestSizesBytes map[requestKey][]int64
RequestAcksTotal map[requestKey]int64
RequestNacksTotal map[nackKey]int64
SendFailuresTotal map[errorCodeKey]int64
RecvFailuresTotal map[errorCodeKey]int64
StreamCreateSuccessTotal int64
}
// SetStreamCount updates the current stream count to the given argument.
func (s *InMemoryStatsContext) SetStreamCount(clients int64) {
s.mutex.Lock()
s.StreamTotal = clients
s.mutex.Unlock()
}
// RecordSendError records an error during a network send with its error
// string and code.
func (s *InMemoryStatsContext) RecordSendError(err error, code codes.Code) {
s.mutex.Lock()
s.SendFailuresTotal[errorCodeKey{err.Error(), code}]++
s.mutex.Unlock()
}
// RecordRecvError records an error during a network recv with its error
// string and code.
func (s *InMemoryStatsContext) RecordRecvError(err error, code codes.Code) {
s.mutex.Lock()
s.RecvFailuresTotal[errorCodeKey{err.Error(), code}]++
s.mutex.Unlock()
}
// RecordRequestSize records the size of a request from a connection for a specific type URL.
func (s *InMemoryStatsContext) RecordRequestSize(typeURL string, connectionID int64, size int) {
key := requestKey{typeURL, connectionID}
s.mutex.Lock()
s.RequestSizesBytes[key] = append(s.RequestSizesBytes[key], int64(size))
s.mutex.Unlock()
}
// RecordRequestAck records an ACK message for a type URL on a connection.
func (s *InMemoryStatsContext) RecordRequestAck(typeURL string, connectionID int64) {
s.mutex.Lock()
s.RequestAcksTotal[requestKey{typeURL, connectionID}]++
s.mutex.Unlock()
}
// RecordRequestNack records a NACK message for a type URL on a connection.
func (s *InMemoryStatsContext) RecordRequestNack(typeURL string, connectionID int64, code codes.Code) {
s.mutex.Lock()
s.RequestNacksTotal[nackKey{typeURL, connectionID, code}]++
s.mutex.Unlock()
}
// RecordStreamCreateSuccess records a successful stream connection.
func (s *InMemoryStatsContext) RecordStreamCreateSuccess() {
s.mutex.Lock()
s.StreamCreateSuccessTotal++
s.mutex.Unlock()
}
// Close implements io.Closer.
func (s *InMemoryStatsContext) Close() error {
return nil
}
// NewInMemoryStatsContext creates a new context for tracking metrics
// in memory.
func NewInMemoryStatsContext() *InMemoryStatsContext {
return &InMemoryStatsContext{
RequestSizesBytes: make(map[requestKey][]int64),
RequestAcksTotal: make(map[requestKey]int64),
RequestNacksTotal: make(map[nackKey]int64),
SendFailuresTotal: make(map[errorCodeKey]int64),
RecvFailuresTotal: make(map[errorCodeKey]int64),
}
}