/
metrics.go
130 lines (113 loc) · 3.91 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
// Copyright 2018-2019 Authors of Cilium
//
// 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
//
// http://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 main
import (
"fmt"
"time"
restapi "github.com/cilium/cilium/api/v1/server/restapi/metrics"
"github.com/cilium/cilium/pkg/api"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/spanstat"
"github.com/go-openapi/runtime/middleware"
"github.com/prometheus/client_golang/prometheus"
)
type getMetrics struct {
daemon *Daemon
}
// NewGetMetricsHandler returns the metrics handler
func NewGetMetricsHandler(d *Daemon) restapi.GetMetricsHandler {
return &getMetrics{daemon: d}
}
func (h *getMetrics) Handle(params restapi.GetMetricsParams) middleware.Responder {
metrics, err := metrics.DumpMetrics()
if err != nil {
return api.Error(
restapi.GetMetricsInternalServerErrorCode,
fmt.Errorf("Cannot gather metrics from daemon"))
}
return restapi.NewGetMetricsOK().WithPayload(metrics)
}
func initMetrics() <-chan error {
var errs <-chan error
if option.Config.PrometheusServeAddr != "" {
log.Infof("Serving prometheus metrics on %s", option.Config.PrometheusServeAddr)
errs = metrics.Enable(option.Config.PrometheusServeAddr)
}
return errs
}
type bootstrapStatistics struct {
overall spanstat.SpanStat
earlyInit spanstat.SpanStat
k8sInit spanstat.SpanStat
restore spanstat.SpanStat
healthCheck spanstat.SpanStat
initAPI spanstat.SpanStat
initDaemon spanstat.SpanStat
cleanup spanstat.SpanStat
bpfBase spanstat.SpanStat
clusterMeshInit spanstat.SpanStat
ipam spanstat.SpanStat
daemonInit spanstat.SpanStat
mapsInit spanstat.SpanStat
workloadsInit spanstat.SpanStat
proxyStart spanstat.SpanStat
fqdn spanstat.SpanStat
enableConntrack spanstat.SpanStat
}
func (b *bootstrapStatistics) updateMetrics() {
for scope, stat := range b.getMap() {
if stat.SuccessTotal() != time.Duration(0) {
metricBootstrapTimes.WithLabelValues(scope, metrics.LabelValueOutcomeSuccess).Observe(stat.SuccessTotal().Seconds())
}
if stat.FailureTotal() != time.Duration(0) {
metricBootstrapTimes.WithLabelValues(scope, metrics.LabelValueOutcomeFail).Observe(stat.FailureTotal().Seconds())
}
}
}
func (b *bootstrapStatistics) getMap() map[string]*spanstat.SpanStat {
return map[string]*spanstat.SpanStat{
"overall": &b.overall,
"earlyInit": &b.earlyInit,
"k8sInit": &b.k8sInit,
"restore": &b.restore,
"healthCheck": &b.healthCheck,
"initAPI": &b.initAPI,
"initDaemon": &b.initDaemon,
"cleanup": &b.cleanup,
"bpfBase": &b.bpfBase,
"clusterMeshInit": &b.clusterMeshInit,
"ipam": &b.ipam,
"daemonInit": &b.daemonInit,
"mapsInit": &b.mapsInit,
"workloadsInit": &b.workloadsInit,
"proxyStart": &b.proxyStart,
"fqdn": &b.fqdn,
"enableConntrack": &b.enableConntrack,
}
}
var (
metricBootstrapTimes prometheus.ObserverVec
)
func init() {
metricBootstrapTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.SubsystemAgent,
Name: "bootstrap_seconds",
Help: "Duration of bootstrap sequence",
}, []string{metrics.LabelScope, metrics.LabelOutcome})
if err := metrics.Register(metricBootstrapTimes); err != nil {
log.WithError(err).Fatal("unable to register prometheus metric")
}
}