/
mmlogic_stats.go
139 lines (124 loc) · 5.31 KB
/
mmlogic_stats.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
/*
Copyright 2018 Google LLC
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
https://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 apisrv
import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
// OpenCensus Measures. These are exported as metrics to your monitoring system
// https://godoc.org/go.opencensus.io/stats
//
// When making opencensus stats, the 'name' param, with forward slashes changed
// to underscores, is appended to the 'namespace' value passed to the
// prometheus exporter to become the Prometheus metric name. You can also look
// into having Prometheus rewrite your metric names on scrape.
//
// For example:
// - defining the promethus export namespace "open_match" when instanciating the exporter:
// pe, err := promethus.NewExporter(promethus.Options{Namespace: "open_match"})
// - and naming the request counter "mmlogic/requests_total":
// MGrpcRequests := stats.Int64("mmlogicapi/requests_total", ...
// - results in the prometheus metric name:
// open_match_mmlogicapi_requests_total
// - [note] when using opencensus views to aggregate the metrics into
// distribution buckets and such, multiple metrics
// will be generated with appended types ("<metric>_bucket",
// "<metric>_count", "<metric>_sum", for example)
//
// In addition, OpenCensus stats propogated to Prometheus have the following
// auto-populated labels pulled from kubernetes, which we should avoid to
// prevent overloading and having to use the HonorLabels param in Prometheus.
//
// - Information about the k8s pod being monitored:
// "pod" (name of the monitored k8s pod)
// "namespace" (k8s namespace of the monitored pod)
// - Information about how promethus is gathering the metrics:
// "instance" (IP and port number being scraped by prometheus)
// "job" (name of the k8s service being scraped by prometheus)
// "endpoint" (name of the k8s port in the k8s service being scraped by prometheus)
//
var (
// API instrumentation
MlGrpcRequests = stats.Int64("mmlogicapi/requests_total", "Number of requests to the gRPC Frontend API endpoints", "1")
MlGrpcErrors = stats.Int64("mmlogicapi/errors_total", "Number of errors generated by the gRPC Frontend API endpoints", "1")
MlGrpcLatencySecs = stats.Float64("mmlogicapi/latency_seconds", "Latency in seconds of the gRPC Frontend API endpoints", "1")
// Logging instrumentation
// There's no need to record this measurement directly if you use
// the logrus hook provided in metrics/helper.go after instantiating the
// logrus instance in your application code.
// https://godoc.org/github.com/sirupsen/logrus#LevelHooks
MlLogLines = stats.Int64("mmlogicapi/logs_total", "Number of Frontend API lines logged", "1")
// Failure instrumentation
MlFailures = stats.Int64("mmlogicapi/failures_total", "Number of Frontend API failures", "1")
)
var (
// KeyMethod is used to tag a measure with the currently running API method.
KeyMethod, _ = tag.NewKey("method")
KeySeverity, _ = tag.NewKey("severity")
)
var (
// Latency in buckets:
// [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, >=1s, >=2s, >=4s, >=6s]
latencyDistribution = view.Distribution(0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000, 6000)
)
// Package metrics provides some convience views.
// You need to register the views for the data to actually be collected.
// Note: The OpenCensus View 'Description' is exported to Prometheus as the HELP string.
// Note: If you get a "Failed to export to Prometheus: inconsistent label
// cardinality" error, chances are you forgot to set the tags specified in the
// view for a given measure when you tried to do a stats.Record()
var (
MlLatencyView = &view.View{
Name: "mmlogic/latency",
Measure: MlGrpcLatencySecs,
Description: "The distribution of mmlogic latencies",
Aggregation: latencyDistribution,
TagKeys: []tag.Key{KeyMethod},
}
MlRequestCountView = &view.View{
Name: "mmlogic/grpc/requests",
Measure: MlGrpcRequests,
Description: "The number of successful mmlogic gRPC requests",
Aggregation: view.Count(),
TagKeys: []tag.Key{KeyMethod},
}
MlErrorCountView = &view.View{
Name: "mmlogic/grpc/errors",
Measure: MlGrpcErrors,
Description: "The number of gRPC errors",
Aggregation: view.Count(),
TagKeys: []tag.Key{KeyMethod},
}
MlLogCountView = &view.View{
Name: "log_lines/total",
Measure: MlLogLines,
Description: "The number of lines logged",
Aggregation: view.Count(),
TagKeys: []tag.Key{KeySeverity},
}
MlFailureCountView = &view.View{
Name: "failures",
Measure: MlFailures,
Description: "The number of failures",
Aggregation: view.Count(),
}
)
// DefaultMmlogicAPIViews are the default mmlogic API OpenCensus measure views.
var DefaultMmlogicAPIViews = []*view.View{
MlLatencyView,
MlRequestCountView,
MlErrorCountView,
MlLogCountView,
MlFailureCountView,
}