summaryrefslogtreecommitdiff
path: root/plugins/metrics/rpc.go
blob: d7c90d390a7007517dbf0b86ade17064e99e2e05 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

type rpcServer struct {
	svc *Plugin
	log logger.Logger
}

// Metric represent single metric produced by the application.
type Metric struct {
	// Collector name.
	Name string

	// Collector value.
	Value float64

	// Labels associated with metric. Only for vector metrics. Must be provided in a form of label values.
	Labels []string
}

// Add new metric to the designated collector.
func (rpc *rpcServer) Add(m *Metric, ok *bool) error {
	const op = errors.Op("metrics_plugin_add")
	rpc.log.Info("Adding metric", "name", m.Name, "value", m.Value, "labels", m.Labels)
	c, exist := rpc.svc.collectors.Load(m.Name)
	if !exist {
		rpc.log.Error("undefined collector", "collector", m.Name)
		return errors.E(op, errors.Errorf("undefined collector %s, try first Declare the desired collector", m.Name))
	}

	switch c := c.(type) {
	case prometheus.Gauge:
		c.Add(m.Value)

	case *prometheus.GaugeVec:
		if len(m.Labels) == 0 {
			rpc.log.Error("required labels for collector", "collector", m.Name)
			return errors.E(op, errors.Errorf("required labels for collector %s", m.Name))
		}

		gauge, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			rpc.log.Error("failed to get metrics with label values", "collector", m.Name, "labels", m.Labels)
			return errors.E(op, err)
		}
		gauge.Add(m.Value)
	case prometheus.Counter:
		c.Add(m.Value)

	case *prometheus.CounterVec:
		if len(m.Labels) == 0 {
			return errors.E(op, errors.Errorf("required labels for collector `%s`", m.Name))
		}

		gauge, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			rpc.log.Error("failed to get metrics with label values", "collector", m.Name, "labels", m.Labels)
			return errors.E(op, err)
		}
		gauge.Add(m.Value)

	default:
		return errors.E(op, errors.Errorf("collector %s does not support method `Add`", m.Name))
	}

	// RPC, set ok to true as return value. Need by rpc.Call reply argument
	*ok = true
	rpc.log.Info("new metric successfully added", "name", m.Name, "labels", m.Labels, "value", m.Value)
	return nil
}

// Sub subtract the value from the specific metric (gauge only).
func (rpc *rpcServer) Sub(m *Metric, ok *bool) error {
	const op = errors.Op("metrics_plugin_sub")
	rpc.log.Info("Subtracting value from metric", "name", m.Name, "value", m.Value, "labels", m.Labels)
	c, exist := rpc.svc.collectors.Load(m.Name)
	if !exist {
		rpc.log.Error("undefined collector", "name", m.Name, "value", m.Value, "labels", m.Labels)
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}
	if c == nil {
		// can it be nil ??? I guess can't
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}

	switch c := c.(type) {
	case prometheus.Gauge:
		c.Sub(m.Value)

	case *prometheus.GaugeVec:
		if len(m.Labels) == 0 {
			rpc.log.Error("required labels for collector, but none was provided", "name", m.Name, "value", m.Value)
			return errors.E(op, errors.Errorf("required labels for collector %s", m.Name))
		}

		gauge, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			rpc.log.Error("failed to get metrics with label values", "collector", m.Name, "labels", m.Labels)
			return errors.E(op, err)
		}
		gauge.Sub(m.Value)
	default:
		return errors.E(op, errors.Errorf("collector `%s` does not support method `Sub`", m.Name))
	}
	rpc.log.Info("Subtracting operation applied successfully", "name", m.Name, "labels", m.Labels, "value", m.Value)

	*ok = true
	return nil
}

// Observe the value (histogram and summary only).
func (rpc *rpcServer) Observe(m *Metric, ok *bool) error {
	const op = errors.Op("metrics_plugin_observe")
	rpc.log.Info("Observing metric", "name", m.Name, "value", m.Value, "labels", m.Labels)

	c, exist := rpc.svc.collectors.Load(m.Name)
	if !exist {
		rpc.log.Error("undefined collector", "name", m.Name, "value", m.Value, "labels", m.Labels)
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}
	if c == nil {
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}

	switch c := c.(type) {
	case *prometheus.SummaryVec:
		if len(m.Labels) == 0 {
			return errors.E(op, errors.Errorf("required labels for collector `%s`", m.Name))
		}

		observer, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			return errors.E(op, err)
		}
		observer.Observe(m.Value)

	case prometheus.Histogram:
		c.Observe(m.Value)

	case *prometheus.HistogramVec:
		if len(m.Labels) == 0 {
			return errors.E(op, errors.Errorf("required labels for collector `%s`", m.Name))
		}

		observer, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			rpc.log.Error("failed to get metrics with label values", "collector", m.Name, "labels", m.Labels)
			return errors.E(op, err)
		}
		observer.Observe(m.Value)
	default:
		return errors.E(op, errors.Errorf("collector `%s` does not support method `Observe`", m.Name))
	}

	rpc.log.Info("observe operation finished successfully", "name", m.Name, "labels", m.Labels, "value", m.Value)

	*ok = true
	return nil
}

// Declare is used to register new collector in prometheus
// THE TYPES ARE:
// 	NamedCollector -> Collector with the name
// 	bool -> RPC reply value
// RETURNS:
// 	error
func (rpc *rpcServer) Declare(nc *NamedCollector, ok *bool) error {
	const op = errors.Op("metrics_plugin_declare")
	rpc.log.Info("Declaring new metric", "name", nc.Name, "type", nc.Type, "namespace", nc.Namespace)
	_, exist := rpc.svc.collectors.Load(nc.Name)
	if exist {
		rpc.log.Error("metric with provided name already exist", "name", nc.Name, "type", nc.Type, "namespace", nc.Namespace)
		return errors.E(op, errors.Errorf("tried to register existing collector with the name `%s`", nc.Name))
	}

	var collector prometheus.Collector
	switch nc.Type {
	case Histogram:
		opts := prometheus.HistogramOpts{
			Name:      nc.Name,
			Namespace: nc.Namespace,
			Subsystem: nc.Subsystem,
			Help:      nc.Help,
			Buckets:   nc.Buckets,
		}

		if len(nc.Labels) != 0 {
			collector = prometheus.NewHistogramVec(opts, nc.Labels)
		} else {
			collector = prometheus.NewHistogram(opts)
		}
	case Gauge:
		opts := prometheus.GaugeOpts{
			Name:      nc.Name,
			Namespace: nc.Namespace,
			Subsystem: nc.Subsystem,
			Help:      nc.Help,
		}

		if len(nc.Labels) != 0 {
			collector = prometheus.NewGaugeVec(opts, nc.Labels)
		} else {
			collector = prometheus.NewGauge(opts)
		}
	case Counter:
		opts := prometheus.CounterOpts{
			Name:      nc.Name,
			Namespace: nc.Namespace,
			Subsystem: nc.Subsystem,
			Help:      nc.Help,
		}

		if len(nc.Labels) != 0 {
			collector = prometheus.NewCounterVec(opts, nc.Labels)
		} else {
			collector = prometheus.NewCounter(opts)
		}
	case Summary:
		opts := prometheus.SummaryOpts{
			Name:      nc.Name,
			Namespace: nc.Namespace,
			Subsystem: nc.Subsystem,
			Help:      nc.Help,
		}

		if len(nc.Labels) != 0 {
			collector = prometheus.NewSummaryVec(opts, nc.Labels)
		} else {
			collector = prometheus.NewSummary(opts)
		}

	default:
		return errors.E(op, errors.Errorf("unknown collector type %s", nc.Type))
	}

	// add collector to sync.Map
	rpc.svc.collectors.Store(nc.Name, collector)
	// that method might panic, we handle it by recover
	err := rpc.svc.Register(collector)
	if err != nil {
		*ok = false
		return errors.E(op, err)
	}

	rpc.log.Info("metric successfully added", "name", nc.Name, "type", nc.Type, "namespace", nc.Namespace)

	*ok = true
	return nil
}

// Set the metric value (only for gaude).
func (rpc *rpcServer) Set(m *Metric, ok *bool) (err error) {
	const op = errors.Op("metrics_plugin_set")
	rpc.log.Info("Observing metric", "name", m.Name, "value", m.Value, "labels", m.Labels)

	c, exist := rpc.svc.collectors.Load(m.Name)
	if !exist {
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}
	if c == nil {
		return errors.E(op, errors.Errorf("undefined collector %s", m.Name))
	}

	switch c := c.(type) {
	case prometheus.Gauge:
		c.Set(m.Value)

	case *prometheus.GaugeVec:
		if len(m.Labels) == 0 {
			rpc.log.Error("required labels for collector", "collector", m.Name)
			return errors.E(op, errors.Errorf("required labels for collector %s", m.Name))
		}

		gauge, err := c.GetMetricWithLabelValues(m.Labels...)
		if err != nil {
			rpc.log.Error("failed to get metrics with label values", "collector", m.Name, "labels", m.Labels)
			return errors.E(op, err)
		}
		gauge.Set(m.Value)

	default:
		return errors.E(op, errors.Errorf("collector `%s` does not support method Set", m.Name))
	}

	rpc.log.Info("set operation finished successfully", "name", m.Name, "labels", m.Labels, "value", m.Value)

	*ok = true
	return nil
}