github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/daemon/metrics.go (about) 1 // Copyright 2018-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "fmt" 19 "time" 20 21 restapi "github.com/cilium/cilium/api/v1/server/restapi/metrics" 22 "github.com/cilium/cilium/pkg/api" 23 "github.com/cilium/cilium/pkg/metrics" 24 "github.com/cilium/cilium/pkg/option" 25 "github.com/cilium/cilium/pkg/spanstat" 26 27 "github.com/go-openapi/runtime/middleware" 28 "github.com/prometheus/client_golang/prometheus" 29 ) 30 31 type getMetrics struct { 32 daemon *Daemon 33 } 34 35 // NewGetMetricsHandler returns the metrics handler 36 func NewGetMetricsHandler(d *Daemon) restapi.GetMetricsHandler { 37 return &getMetrics{daemon: d} 38 } 39 40 func (h *getMetrics) Handle(params restapi.GetMetricsParams) middleware.Responder { 41 metrics, err := metrics.DumpMetrics() 42 if err != nil { 43 return api.Error( 44 restapi.GetMetricsInternalServerErrorCode, 45 fmt.Errorf("Cannot gather metrics from daemon")) 46 } 47 48 return restapi.NewGetMetricsOK().WithPayload(metrics) 49 } 50 51 func initMetrics() <-chan error { 52 var errs <-chan error 53 54 if option.Config.PrometheusServeAddr != "" { 55 log.Infof("Serving prometheus metrics on %s", option.Config.PrometheusServeAddr) 56 errs = metrics.Enable(option.Config.PrometheusServeAddr) 57 } 58 59 return errs 60 } 61 62 type bootstrapStatistics struct { 63 overall spanstat.SpanStat 64 earlyInit spanstat.SpanStat 65 k8sInit spanstat.SpanStat 66 restore spanstat.SpanStat 67 healthCheck spanstat.SpanStat 68 initAPI spanstat.SpanStat 69 initDaemon spanstat.SpanStat 70 cleanup spanstat.SpanStat 71 bpfBase spanstat.SpanStat 72 clusterMeshInit spanstat.SpanStat 73 ipam spanstat.SpanStat 74 daemonInit spanstat.SpanStat 75 mapsInit spanstat.SpanStat 76 workloadsInit spanstat.SpanStat 77 proxyStart spanstat.SpanStat 78 fqdn spanstat.SpanStat 79 enableConntrack spanstat.SpanStat 80 } 81 82 func (b *bootstrapStatistics) updateMetrics() { 83 for scope, stat := range b.getMap() { 84 if stat.SuccessTotal() != time.Duration(0) { 85 metricBootstrapTimes.WithLabelValues(scope, metrics.LabelValueOutcomeSuccess).Observe(stat.SuccessTotal().Seconds()) 86 } 87 if stat.FailureTotal() != time.Duration(0) { 88 metricBootstrapTimes.WithLabelValues(scope, metrics.LabelValueOutcomeFail).Observe(stat.FailureTotal().Seconds()) 89 } 90 } 91 } 92 93 func (b *bootstrapStatistics) getMap() map[string]*spanstat.SpanStat { 94 return map[string]*spanstat.SpanStat{ 95 "overall": &b.overall, 96 "earlyInit": &b.earlyInit, 97 "k8sInit": &b.k8sInit, 98 "restore": &b.restore, 99 "healthCheck": &b.healthCheck, 100 "initAPI": &b.initAPI, 101 "initDaemon": &b.initDaemon, 102 "cleanup": &b.cleanup, 103 "bpfBase": &b.bpfBase, 104 "clusterMeshInit": &b.clusterMeshInit, 105 "ipam": &b.ipam, 106 "daemonInit": &b.daemonInit, 107 "mapsInit": &b.mapsInit, 108 "workloadsInit": &b.workloadsInit, 109 "proxyStart": &b.proxyStart, 110 "fqdn": &b.fqdn, 111 "enableConntrack": &b.enableConntrack, 112 } 113 } 114 115 var ( 116 metricBootstrapTimes prometheus.ObserverVec 117 ) 118 119 func init() { 120 metricBootstrapTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{ 121 Namespace: metrics.Namespace, 122 Subsystem: metrics.SubsystemAgent, 123 Name: "bootstrap_seconds", 124 Help: "Duration of bootstrap sequence", 125 }, []string{metrics.LabelScope, metrics.LabelOutcome}) 126 127 if err := metrics.Register(metricBootstrapTimes); err != nil { 128 log.WithError(err).Fatal("unable to register prometheus metric") 129 } 130 }