Skip to content

Commit

Permalink
prometheus metrics #231
Browse files Browse the repository at this point in the history
  • Loading branch information
aide-cloud committed Jul 30, 2023
1 parent 743e83a commit 3fa1bdc
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ This driver allows Kubernetes to access LocalStorage on Linux node.
test-pvc Bound pvc-2b2c809f-33b5-437f-a4b8-61906c10a3e1 1Mi RWX pixiu-localstorage 5s
```

## Metrics
- [metrics](./docs/metrics.md)

## Feature
- Schedule with volume status
- Volume metrics
Expand Down
27 changes: 22 additions & 5 deletions cmd/localstorage-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/caoyingjunz/csi-driver-localstorage/pkg/client/clientset/versioned"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/client/informers/externalversions"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/controller/storage"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/metrics"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/runtime"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/signals"
"github.com/caoyingjunz/csi-driver-localstorage/pkg/util"
Expand Down Expand Up @@ -79,6 +80,10 @@ var (
resourceNamespace = flag.String("leader-elect-resource-namespace", ResourceNamespace, "The namespace of resource object that is used for locking during leader election.")
leaseDuration = flag.Int("leader-elect-lease-duration", LeaseDuration, "The duration that non-leader candidates will wait")
renewDeadline = flag.Int("leader-elect-renew-deadline", RenewDeadline, "The interval between attempts by the acting master to renew a leadership slot before it stops leading.")

// metrics
metricsPort = flag.Int("metrics-port", 0, "metricsPort is the port of the localhost metrics endpoint (set to 0 to disable)")
interval = flag.Duration("metrics-interval", 10, "metricsInterval is the interval of the metrics collection, in seconds (default 10s)")
)

func init() {
Expand Down Expand Up @@ -127,12 +132,13 @@ func main() {
if err != nil {
klog.Fatalf("Failed to build kube clientSet: %v", err)
}
run := func(ctx context.Context) {
lsClientSet, err := versioned.NewForConfig(kubeConfig)
if err != nil {
klog.Fatalf("Failed to new localstorage clientSet: %v", err)
}

lsClientSet, err := versioned.NewForConfig(kubeConfig)
if err != nil {
klog.Fatalf("Failed to new localstorage clientSet: %v", err)
}

run := func(ctx context.Context) {
sharedInformer := externalversions.NewSharedInformerFactory(lsClientSet, 300*time.Second)
sc, err := storage.NewStorageController(ctx,
sharedInformer.Storage().V1().LocalStorages(),
Expand Down Expand Up @@ -164,6 +170,17 @@ func main() {
}, 5*time.Second, wait.NeverStop)
}

if *metricsPort > 0 {
mux := http.NewServeMux()
metrics.InstallHandler(mux, "/metrics")
metrics.TimingAcquisition(ctx, lsClientSet, *interval*time.Second)
go func() {
if err = http.ListenAndServe(net.JoinHostPort("", strconv.Itoa(*metricsPort)), mux); err != nil {
klog.ErrorS(err, "Failed to start metrics server")
}
}()
}

if !*leaderElect {
run(ctx)
klog.Fatalf("unreachable")
Expand Down
3 changes: 3 additions & 0 deletions deploy/latest/ls-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ spec:
- -v=2
# port's value equals 0 means health check func disabled
- --healthz-port=10258
- --metrics-port=10259
# default metrics interval is 10s
- --metrics-interval=10
- --cert-dir=/tmp/cert
- --port=8443
image: harbor.powerlaw.club/pixiuio/localstorage-controller:latest
Expand Down
Loading

0 comments on commit 3fa1bdc

Please sign in to comment.