forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitoring.go
173 lines (152 loc) · 5.18 KB
/
monitoring.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
// 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 client
import (
"context"
"strconv"
"strings"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"google.golang.org/grpc/codes"
)
const (
typeURL = "typeURL"
errorCode = "code"
errorStr = "error"
)
// StatsContext enables metric collection backed by OpenCensus.
type StatsContext struct {
requestAcksTotal *stats.Int64Measure
requestNacksTotal *stats.Int64Measure
sendFailuresTotal *stats.Int64Measure
recvFailuresTotal *stats.Int64Measure
streamCreateSuccessTotal *stats.Int64Measure
}
func (s *StatsContext) recordError(err error, code codes.Code, stat *stats.Int64Measure) {
ctx, ctxErr := tag.New(context.Background(),
tag.Insert(ErrorTag, err.Error()),
tag.Insert(ErrorCodeTag, strconv.FormatUint(uint64(code), 10)))
if ctxErr != nil {
scope.Errorf("MCP: error creating monitoring context. %v", ctxErr)
return
}
stats.Record(ctx, stat.M(1))
}
// RecordSendError records an error during a network send with its error
// string and code.
func (s *StatsContext) RecordSendError(err error, code codes.Code) {
s.recordError(err, code, s.sendFailuresTotal)
}
// RecordRecvError records an error during a network recv with its error
// string and code.
func (s *StatsContext) RecordRecvError(err error, code codes.Code) {
s.recordError(err, code, s.recvFailuresTotal)
}
// RecordRequestAck records an ACK message for a type URL on a connection.
func (s *StatsContext) RecordRequestAck(typeURL string) {
ctx, ctxErr := tag.New(context.Background(), tag.Insert(TypeURLTag, typeURL))
if ctxErr != nil {
scope.Errorf("MCP: error creating monitoring context. %v", ctxErr)
return
}
stats.Record(ctx, s.requestAcksTotal.M(1))
}
// RecordRequestNack records a NACK message for a type URL on a connection.
func (s *StatsContext) RecordRequestNack(typeURL string, err error) {
ctx, ctxErr := tag.New(context.Background(),
tag.Insert(TypeURLTag, typeURL),
tag.Insert(ErrorTag, err.Error()))
if ctxErr != nil {
scope.Errorf("MCP: error creating monitoring context. %v", ctxErr)
return
}
stats.Record(ctx, s.requestNacksTotal.M(1))
}
// RecordStreamCreateSuccess records a successful stream connection.
func (s *StatsContext) RecordStreamCreateSuccess() {
stats.Record(context.Background(), s.streamCreateSuccessTotal.M(1))
}
// NewStatsContext creates a new context for recording metrics using
// OpenCensus. The specified prefix is prepended to all metric names and must
// be a non-empty string.
func NewStatsContext(prefix string) *StatsContext {
if len(prefix) == 0 {
panic("must specify prefix for MCP client monitoring.")
} else if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
ctx := &StatsContext{
requestAcksTotal: stats.Int64(
prefix+"mcp/client/request_acks",
"The number of request acks sent by the client.",
stats.UnitDimensionless),
requestNacksTotal: stats.Int64(
prefix+"mcp/client/request_nacks",
"The number of request nacks sent by the client.",
stats.UnitDimensionless),
sendFailuresTotal: stats.Int64(
prefix+"mcp/client/send_failures",
"The number of send failures in the client.",
stats.UnitDimensionless),
recvFailuresTotal: stats.Int64(
prefix+"mcp/client/recv_failures",
"The number of recv failures in the client.",
stats.UnitDimensionless),
streamCreateSuccessTotal: stats.Int64(
prefix+"mcp/client/reconnections",
"The number of times the client has reconnected.",
stats.UnitDimensionless),
}
err := view.Register(
newView(ctx.requestAcksTotal, []tag.Key{TypeURLTag}, view.Count()),
newView(ctx.requestNacksTotal, []tag.Key{ErrorTag, TypeURLTag}, view.Count()),
newView(ctx.sendFailuresTotal, []tag.Key{ErrorCodeTag, ErrorTag}, view.Count()),
newView(ctx.recvFailuresTotal, []tag.Key{ErrorCodeTag, ErrorTag}, view.Count()),
newView(ctx.streamCreateSuccessTotal, []tag.Key{}, view.Count()),
)
if err != nil {
panic(err)
}
return ctx
}
var (
// TypeURLTag holds the type URL for the context.
TypeURLTag tag.Key
// ErrorCodeTag holds the gRPC error code for the context.
ErrorCodeTag tag.Key
// ErrorTag holds the error string for the context.
ErrorTag tag.Key
)
func newView(measure stats.Measure, keys []tag.Key, aggregation *view.Aggregation) *view.View {
return &view.View{
Name: measure.Name(),
Description: measure.Description(),
Measure: measure,
TagKeys: keys,
Aggregation: aggregation,
}
}
func init() {
var err error
if TypeURLTag, err = tag.NewKey(typeURL); err != nil {
panic(err)
}
if ErrorCodeTag, err = tag.NewKey(errorCode); err != nil {
panic(err)
}
if ErrorTag, err = tag.NewKey(errorStr); err != nil {
panic(err)
}
}