Skip to content

Commit

Permalink
feat: Add Prometheus Metrics
Browse files Browse the repository at this point in the history
adds Prometheus Metrics support, improving monitoring and performance analysis.

Fix #50
  • Loading branch information
dierbei authored and 和兑 committed Jun 2, 2023
1 parent 48e2f3e commit a86579f
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cmd/localstorageplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package main

import (
"flag"
"fmt"
"net/http"
"time"
// import pprof for performance diagnosed
_ "net/http/pprof"

"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"

"github.com/caoyingjunz/csi-driver-localstorage/pkg/client/informers/externalversions"
Expand All @@ -42,6 +44,8 @@ var (
pprofPort = flag.String("pprof-port", "6060", "The port of pprof to listen on")

kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Needs to be set if the plugin is being run out of cluster.")

metricsPort = flag.Int("metricsport", 8080, "metrics port")
)

func init() {
Expand Down Expand Up @@ -77,6 +81,15 @@ func main() {
// set up signals so we handle the shutdown signal gracefully
ctx := signals.SetupSignalHandler()

// Start prometheus metrics
go func() {
klog.Infof("Listening for Prometheus metrics on port: %v", *metricsPort)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(fmt.Sprintf(":%d", *metricsPort), nil); err != nil {
klog.Fatalf("Failed to initialize prometheus metrics, %v", err)
}
}()

kubeConfig, err := util.BuildClientConfig(*kubeconfig)
if err != nil {
klog.Fatalf("Failed to build kube config: %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/imdario/mergo v0.3.15 // indirect
github.com/prometheus/client_golang v1.15.1
golang.org/x/net v0.8.0
google.golang.org/grpc v1.54.0
k8s.io/api v0.24.8
Expand Down
Loading

0 comments on commit a86579f

Please sign in to comment.