forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_reporter.go
185 lines (165 loc) · 5.66 KB
/
prometheus_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
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
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package metrics
import (
"fmt"
"log"
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/topfreegames/pitaya/constants"
)
var (
prometheusReporter *PrometheusReporter
once sync.Once
)
// PrometheusReporter reports metrics to prometheus
type PrometheusReporter struct {
serverType string
game string
countReportersMap map[string]*prometheus.CounterVec
summaryReportersMap map[string]*prometheus.SummaryVec
gaugeReportersMap map[string]*prometheus.GaugeVec
}
func (p *PrometheusReporter) registerMetrics(constLabels map[string]string) {
constLabels["game"] = p.game
constLabels["serverType"] = p.serverType
// HandlerResponseTimeMs summary
p.summaryReportersMap[ResponseTime] = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "pitaya",
Subsystem: "handler",
Name: ResponseTime,
Help: "the time to process a msg in nanoseconds",
Objectives: map[float64]float64{0.7: 0.02, 0.95: 0.005, 0.99: 0.001},
ConstLabels: constLabels,
},
[]string{"route", "status", "type"},
)
// ProcessDelay summary
p.summaryReportersMap[ProcessDelay] = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "pitaya",
Subsystem: "handler",
Name: ProcessDelay,
Help: "the delay to start processing a msg in nanoseconds",
Objectives: map[float64]float64{0.7: 0.02, 0.95: 0.005, 0.99: 0.001},
ConstLabels: constLabels,
},
[]string{"type"},
)
// ConnectedClients gauge
p.gaugeReportersMap[ConnectedClients] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "acceptor",
Name: ConnectedClients,
Help: "the number of clients connected right now",
ConstLabels: constLabels,
},
[]string{},
)
p.gaugeReportersMap[CountServers] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "service_discovery",
Name: CountServers,
Help: "the number of discovered servers by service discovery",
ConstLabels: constLabels,
},
[]string{"type"},
)
p.gaugeReportersMap[ChannelCapacity] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "channel",
Name: ChannelCapacity,
Help: "the available capacity of the channel",
ConstLabels: constLabels,
},
[]string{"channel"},
)
p.gaugeReportersMap[DroppedMessages] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "rpc_server",
Name: DroppedMessages,
Help: "the number of rpc server dropped messages (messages that are not handled)",
ConstLabels: constLabels,
},
[]string{},
)
toRegister := make([]prometheus.Collector, 0)
for _, c := range p.countReportersMap {
toRegister = append(toRegister, c)
}
for _, c := range p.gaugeReportersMap {
toRegister = append(toRegister, c)
}
for _, c := range p.summaryReportersMap {
toRegister = append(toRegister, c)
}
prometheus.MustRegister(toRegister...)
}
// GetPrometheusReporter gets the prometheus reporter singleton
func GetPrometheusReporter(serverType string, game string, port int, constLabels map[string]string) *PrometheusReporter {
once.Do(func() {
prometheusReporter = &PrometheusReporter{
serverType: serverType,
game: game,
countReportersMap: make(map[string]*prometheus.CounterVec),
summaryReportersMap: make(map[string]*prometheus.SummaryVec),
gaugeReportersMap: make(map[string]*prometheus.GaugeVec),
}
prometheusReporter.registerMetrics(constLabels)
http.Handle("/metrics", prometheus.Handler())
go (func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
})()
})
return prometheusReporter
}
// ReportSummary reports a summary metric
func (p *PrometheusReporter) ReportSummary(metric string, labels map[string]string, value float64) error {
sum := p.summaryReportersMap[metric]
if sum != nil {
sum.With(labels).Observe(value)
return nil
}
return constants.ErrMetricNotKnown
}
// ReportCount reports a summary metric
func (p *PrometheusReporter) ReportCount(metric string, labels map[string]string, count float64) error {
cnt := p.countReportersMap[metric]
if cnt != nil {
cnt.With(labels).Add(count)
return nil
}
return constants.ErrMetricNotKnown
}
// ReportGauge reports a gauge metric
func (p *PrometheusReporter) ReportGauge(metric string, labels map[string]string, value float64) error {
g := p.gaugeReportersMap[metric]
if g != nil {
g.With(labels).Set(value)
return nil
}
return constants.ErrMetricNotKnown
}