/
metrics.go
163 lines (132 loc) · 4.32 KB
/
metrics.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
// Copyright (c) Edgeless Systems GmbH.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package core
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
dto "github.com/prometheus/client_model/go"
)
type coreMetrics struct {
coordinatorState prometheus.GaugeFunc
marbleAPI *marbleAPIMetrics
}
func newCoreMetrics(factory *promauto.Factory, core *Core, namespace string) *coreMetrics {
if factory == nil {
return &coreMetrics{
coordinatorState: nil,
marbleAPI: newNullMarbleAPIMetrics(),
}
}
return &coreMetrics{
coordinatorState: factory.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "state",
Help: "State of the Coordinator.",
},
func() float64 {
state, _, err := core.GetState(context.Background())
if err != nil {
return float64(0)
}
return float64(state)
}),
marbleAPI: newMarbleAPIMetrics(factory, namespace),
}
}
type marbleAPIMetrics struct {
activation CounterVec
activationSuccess CounterVec
}
func newMarbleAPIMetrics(factory *promauto.Factory, namespace string) *marbleAPIMetrics {
return &marbleAPIMetrics{
activation: factory.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "marble_activations_total",
Help: "Number of Marble activation attempts.",
},
[]string{"type", "uuid"},
),
activationSuccess: factory.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "marble_activations_success_total",
Help: "Number of successful Marble activations.",
},
[]string{"type", "uuid"},
),
}
}
func newNullMarbleAPIMetrics() *marbleAPIMetrics {
return &marbleAPIMetrics{
activation: NullCounterVec{},
activationSuccess: NullCounterVec{},
}
}
// NullCollector implements prometheus.Collector but does nothing.
type NullCollector struct{}
// Describe implements prometheus.Collector.
func (NullCollector) Describe(chan<- *prometheus.Desc) {}
// Collect implements prometheus.Collector.
func (NullCollector) Collect(chan<- prometheus.Metric) {}
// NullMetric implements prometheus.Metric but does nothing.
type NullMetric struct{}
// Desc implements prometheus.Metric.
func (NullMetric) Desc() *prometheus.Desc { return nil }
// Write implements prometheus.Metric.
func (NullMetric) Write(*dto.Metric) error { return nil }
// NullCounter implements prometheus.Counter but does nothing.
type NullCounter struct {
NullMetric
NullCollector
}
// Inc implements prometheus.Counter.
func (NullCounter) Inc() {}
// Add implements prometheus.Counter.
func (NullCounter) Add(float64) {}
// BaseVec is a vector of metrics.
type BaseVec interface {
prometheus.Collector
Delete(labels prometheus.Labels) bool
DeleteLabelValues(lvs ...string) bool
Reset()
}
// CounterVec is a vector of metrics.
type CounterVec interface {
BaseVec
GetMetricWith(labels prometheus.Labels) (prometheus.Counter, error)
GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error)
With(labels prometheus.Labels) prometheus.Counter
WithLabelValues(lvs ...string) prometheus.Counter
}
// NullBaseVec implements BaseVec but does nothing.
type NullBaseVec struct {
NullCollector
}
// Delete implements BaseVec.
func (NullBaseVec) Delete(_ prometheus.Labels) bool { return false }
// DeleteLabelValues implements BaseVec.
func (NullBaseVec) DeleteLabelValues(_ ...string) bool { return false }
// Reset implements BaseVec.
func (NullBaseVec) Reset() {}
// NullCounterVec implements CounterVec but does nothing.
type NullCounterVec struct {
NullBaseVec
}
// GetMetricWith implements CounterVec.
func (NullCounterVec) GetMetricWith(_ prometheus.Labels) (prometheus.Counter, error) {
return NullCounter{}, nil
}
// GetMetricWithLabelValues implements CounterVec.
func (NullCounterVec) GetMetricWithLabelValues(_ ...string) (prometheus.Counter, error) {
return NullCounter{}, nil
}
// With implements CounterVec.
func (NullCounterVec) With(_ prometheus.Labels) prometheus.Counter { return NullCounter{} }
// WithLabelValues implements CounterVec.
func (NullCounterVec) WithLabelValues(_ ...string) prometheus.Counter { return NullCounter{} }