forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
94 lines (85 loc) · 3.49 KB
/
prometheus.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package metrics
import (
"errors"
"runtime"
"github.com/gravitational/trace"
om "github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/gravitational/teleport"
)
// RegisterPrometheusCollectors is a wrapper around prometheus.Register that
// - ignores equal or re-registered collectors
// - returns an error if a collector does not fulfill the consistency and
// uniqueness criteria
func RegisterPrometheusCollectors(collectors ...prometheus.Collector) error {
var errs []error
for _, c := range collectors {
if err := prometheus.Register(c); err != nil {
var alreadyRegisteredError prometheus.AlreadyRegisteredError
if errors.As(err, &alreadyRegisteredError) {
continue
}
errs = append(errs, err)
}
}
return trace.NewAggregate(errs...)
}
// BuildCollector provides a Collector that contains build information gauge
func BuildCollector() prometheus.Collector {
return prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Name: teleport.MetricBuildInfo,
Help: "Provides build information of Teleport including gitref (git describe --long --tags), Go version, and Teleport version. The value of this gauge will always be 1.",
ConstLabels: prometheus.Labels{
teleport.TagVersion: teleport.Version,
teleport.TagGitref: teleport.Gitref,
teleport.TagGoVersion: runtime.Version(),
},
},
func() float64 { return 1 },
)
}
// CreateGRPCServerMetrics creates server gRPC metrics configuration that is to be registered and used by the caller
// in an openmetrics unary and/or stream interceptor
func CreateGRPCServerMetrics(latencyEnabled bool, labels prometheus.Labels) *om.ServerMetrics {
serverOpts := []om.ServerMetricsOption{om.WithServerCounterOptions(om.WithConstLabels(labels))}
if latencyEnabled {
histOpts := grpcHistogramOpts(labels)
serverOpts = append(serverOpts, om.WithServerHandlingTimeHistogram(histOpts...))
}
return om.NewServerMetrics(serverOpts...)
}
// CreateGRPCClientMetrics creates client gRPC metrics configuration that is to be registered and used by the caller
// in an openmetrics unary and/or stream interceptor
func CreateGRPCClientMetrics(latencyEnabled bool, labels prometheus.Labels) *om.ClientMetrics {
clientOpts := []om.ClientMetricsOption{om.WithClientCounterOptions(om.WithConstLabels(labels))}
if latencyEnabled {
histOpts := grpcHistogramOpts(labels)
clientOpts = append(clientOpts, om.WithClientHandlingTimeHistogram(histOpts...))
}
return om.NewClientMetrics(clientOpts...)
}
func grpcHistogramOpts(labels prometheus.Labels) []om.HistogramOption {
return []om.HistogramOption{
om.WithHistogramBuckets(prometheus.ExponentialBuckets(0.001, 2, 16)),
om.WithHistogramConstLabels(labels),
}
}