Skip to content

Commit 43b5384

Browse files
committed
Add new metric to expose GKE component version
1 parent ac52f17 commit 43b5384

File tree

119 files changed

+27015
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+27015
-0
lines changed

cmd/gce-pd-csi-driver/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
2929
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
3030
driver "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-pd-csi-driver"
31+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/metrics"
3132
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
3233
)
3334

@@ -36,6 +37,8 @@ var (
3637
endpoint = flag.String("endpoint", "unix:/tmp/csi.sock", "CSI endpoint")
3738
runControllerService = flag.Bool("run-controller-service", true, "If set to false then the CSI driver does not activate its controller service (default: true)")
3839
runNodeService = flag.Bool("run-node-service", true, "If set to false then the CSI driver does not activate its node service (default: true)")
40+
httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.")
41+
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
3942
extraVolumeLabels map[string]string
4043
version string
4144
)
@@ -70,6 +73,11 @@ func handle() {
7073
}
7174
klog.V(2).Infof("Driver vendor version %v", version)
7275

76+
if *runControllerService && *httpEndpoint != "" && metrics.IsComponentVersionAvailable() {
77+
metrics.InitializeHandler(*httpEndpoint, *metricsPath)
78+
metrics.RegisterComponentVersionMetric()
79+
}
80+
7381
gceDriver := driver.GetGCEDriver()
7482

7583
//Initialize GCE Driver

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/kubernetes-csi/csi-test/v3 v3.0.0
1414
github.com/onsi/ginkgo v1.11.0
1515
github.com/onsi/gomega v1.7.1
16+
github.com/prometheus/client_golang v1.0.0
1617
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
1718
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
1819
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f

pkg/metrics/metrics.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
"net/http"
21+
"os"
22+
23+
"github.com/prometheus/client_golang/prometheus"
24+
"github.com/prometheus/client_golang/prometheus/promhttp"
25+
"k8s.io/klog"
26+
)
27+
28+
const (
29+
// envPDCSIVersion is an environment variable set in the PDCSI controller manifest
30+
// with the current version of the GKE component.
31+
envPDCSIVersion = "PDCSI_VERSION"
32+
)
33+
34+
var (
35+
// This metric is exposed only from the controller driver component when PDCSI_VERSION env variable is set.
36+
componentVersion = prometheus.NewGaugeVec(
37+
prometheus.GaugeOpts{
38+
Name: "component_version",
39+
Help: "Metric to expose the version of the PDCSI GKE component.",
40+
},
41+
[]string{"component_version"},
42+
)
43+
)
44+
45+
func InitializeHandler(address, path string) {
46+
go func() {
47+
http.Handle("/metrics", promhttp.Handler())
48+
err := http.ListenAndServe(address, nil)
49+
klog.Fatalf("Failed to start metrics handler: %v", err)
50+
}()
51+
}
52+
53+
func RegisterComponentVersionMetric() {
54+
v := getEnvVar(envPDCSIVersion)
55+
if v == "" {
56+
klog.V(2).Info("Skip emitting component version metric")
57+
return
58+
}
59+
60+
prometheus.MustRegister(componentVersion)
61+
recordMetric(componentVersion, v)
62+
}
63+
64+
func recordMetric(metric *prometheus.GaugeVec, value string) {
65+
metric.WithLabelValues(value).Set(1.0)
66+
}
67+
68+
func getEnvVar(envVarName string) string {
69+
v, ok := os.LookupEnv(envVarName)
70+
if !ok {
71+
klog.Warningf("%q env not set", envVarName)
72+
return ""
73+
}
74+
75+
return v
76+
}
77+
78+
func IsComponentVersionAvailable() bool {
79+
if getEnvVar(envPDCSIVersion) == "" {
80+
return false
81+
}
82+
83+
return true
84+
}

vendor/github.com/beorn7/perks/LICENSE

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)