Skip to content

Commit

Permalink
reuse collectors and registry by reseting each matrics
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani committed Nov 18, 2024
1 parent 3c12230 commit e0cb583
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
flagKubeConfigPath = flag.String("kubeconfig", "", "Path of a kubeconfig file, if not provided the app will try $KUBECONFIG, $HOME/.kube/config or in cluster config")
flagListenAddress = flag.String("listen-address", ":9779", "Listen address")
metricsNamespace = "kube_summary"

registry *prometheus.Registry
collectors *Collectors
)

type Collectors struct {
Expand Down Expand Up @@ -209,6 +212,33 @@ func (c *Collectors) register(registry *prometheus.Registry) {
)
}

func (c *Collectors) reset() {
c.containerLogsInodesFree.Reset()
c.containerLogsInodes.Reset()
c.containerLogsInodesUsed.Reset()
c.containerLogsAvailableBytes.Reset()
c.containerLogsCapacityBytes.Reset()
c.containerLogsUsedBytes.Reset()
c.containerRootFsInodesFree.Reset()
c.containerRootFsInodes.Reset()
c.containerRootFsInodesUsed.Reset()
c.containerRootFsAvailableBytes.Reset()
c.containerRootFsCapacityBytes.Reset()
c.containerRootFsUsedBytes.Reset()
c.podEphemeralStorageAvailableBytes.Reset()
c.podEphemeralStorageCapacityBytes.Reset()
c.podEphemeralStorageUsedBytes.Reset()
c.podEphemeralStorageInodesFree.Reset()
c.podEphemeralStorageInodes.Reset()
c.podEphemeralStorageInodesUsed.Reset()
c.nodeRuntimeImageFSAvailableBytes.Reset()
c.nodeRuntimeImageFSCapacityBytes.Reset()
c.nodeRuntimeImageFSUsedBytes.Reset()
c.nodeRuntimeImageFSInodesFree.Reset()
c.nodeRuntimeImageFSInodes.Reset()
c.nodeRuntimeImageFSInodesUsed.Reset()
}

// collectSummaryMetrics collects metrics from a /stats/summary response
func collectSummaryMetrics(summary *stats.Summary, collectors *Collectors) {
nodeName := summary.Node.NodeName
Expand Down Expand Up @@ -314,9 +344,7 @@ func nodeHandler(w http.ResponseWriter, r *http.Request, kubeClient *kubernetes.
return
}

collectors := newCollectors()
registry := prometheus.NewRegistry()
collectors.register(registry)
collectors.reset()
collectSummaryMetrics(summary, collectors)

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand All @@ -334,9 +362,7 @@ func allNodesHandler(w http.ResponseWriter, r *http.Request, kubeClient *kuberne
return
}

collectors := newCollectors()
registry := prometheus.NewRegistry()
collectors.register(registry)
collectors.reset()

for _, node := range nodes.Items {
summary, err := nodeSummary(ctx, kubeClient, node.Name)
Expand Down Expand Up @@ -414,6 +440,10 @@ func main() {
os.Exit(1)
}

registry = prometheus.NewRegistry()
collectors = newCollectors()
collectors.register(registry)

r := mux.NewRouter()
r.HandleFunc("/nodes", func(w http.ResponseWriter, r *http.Request) {
allNodesHandler(w, r, kubeClient)
Expand Down

0 comments on commit e0cb583

Please sign in to comment.