|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + |
| 24 | + "k8s.io/component-base/metrics" |
| 25 | + "k8s.io/klog" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + // envGKEPDCSIVersion is an environment variable set in the PDCSI controller manifest |
| 30 | + // with the current version of the GKE component. |
| 31 | + envGKEPDCSIVersion = "GKE_PDCSI_VERSION" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + // This metric is exposed only from the controller driver component when GKE_PDCSI_VERSION env variable is set. |
| 36 | + gkeComponentVersion = metrics.NewGaugeVec(&metrics.GaugeOpts{ |
| 37 | + Name: "component_version", |
| 38 | + Help: "Metric to expose the version of the PDCSI GKE component.", |
| 39 | + }, []string{"component_version"}) |
| 40 | +) |
| 41 | + |
| 42 | +type metricsManager struct { |
| 43 | + registry metrics.KubeRegistry |
| 44 | +} |
| 45 | + |
| 46 | +func NewMetricsManager() metricsManager { |
| 47 | + mm := metricsManager{ |
| 48 | + registry: metrics.NewKubeRegistry(), |
| 49 | + } |
| 50 | + return mm |
| 51 | +} |
| 52 | + |
| 53 | +func (mm *metricsManager) GetRegistry() metrics.KubeRegistry { |
| 54 | + return mm.registry |
| 55 | +} |
| 56 | + |
| 57 | +func (mm *metricsManager) registerComponentVersionMetric() { |
| 58 | + mm.registry.MustRegister(gkeComponentVersion) |
| 59 | +} |
| 60 | + |
| 61 | +func (mm *metricsManager) recordComponentVersionMetric() error { |
| 62 | + v := getEnvVar(envGKEPDCSIVersion) |
| 63 | + if v == "" { |
| 64 | + klog.V(2).Info("Skip emitting component version metric") |
| 65 | + return fmt.Errorf("Failed to register GKE component version metric, env variable %v not defined", envGKEPDCSIVersion) |
| 66 | + } |
| 67 | + |
| 68 | + gkeComponentVersion.WithLabelValues(v).Set(1.0) |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (mm *metricsManager) EmitGKEComponentVersion() error { |
| 73 | + mm.registerComponentVersionMetric() |
| 74 | + if err := mm.recordComponentVersionMetric(); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// Server represents any type that could serve HTTP requests for the metrics |
| 82 | +// endpoint. |
| 83 | +type Server interface { |
| 84 | + Handle(pattern string, handler http.Handler) |
| 85 | +} |
| 86 | + |
| 87 | +// RegisterToServer registers an HTTP handler for this metrics manager to the |
| 88 | +// given server at the specified address/path. |
| 89 | +func (mm *metricsManager) registerToServer(s Server, metricsPath string) { |
| 90 | + s.Handle(metricsPath, metrics.HandlerFor( |
| 91 | + mm.GetRegistry(), |
| 92 | + metrics.HandlerOpts{ |
| 93 | + ErrorHandling: metrics.ContinueOnError})) |
| 94 | +} |
| 95 | + |
| 96 | +// InitializeHttpHandler sets up a server and creates a handler for metrics. |
| 97 | +func (mm *metricsManager) InitializeHttpHandler(address, path string) { |
| 98 | + mux := http.NewServeMux() |
| 99 | + mm.registerToServer(mux, path) |
| 100 | + go func() { |
| 101 | + klog.Infof("Metric server listening at %q", address) |
| 102 | + if err := http.ListenAndServe(address, mux); err != nil { |
| 103 | + klog.Fatalf("Failed to start metric server at specified address (%q) and path (%q): %s", address, path, err) |
| 104 | + } |
| 105 | + }() |
| 106 | +} |
| 107 | + |
| 108 | +func getEnvVar(envVarName string) string { |
| 109 | + v, ok := os.LookupEnv(envVarName) |
| 110 | + if !ok { |
| 111 | + klog.Warningf("%q env not set", envVarName) |
| 112 | + return "" |
| 113 | + } |
| 114 | + |
| 115 | + return v |
| 116 | +} |
| 117 | + |
| 118 | +func IsGKEComponentVersionAvailable() bool { |
| 119 | + if getEnvVar(envGKEPDCSIVersion) == "" { |
| 120 | + return false |
| 121 | + } |
| 122 | + |
| 123 | + return true |
| 124 | +} |
0 commit comments