-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1767 from torredil/metrics-0927364
Metric Instrumentation Framework
- Loading branch information
Showing
6 changed files
with
265 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/component-base/metrics" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
r *metricRecorder // singleton instance of metricRecorder | ||
once sync.Once | ||
) | ||
|
||
type metricRecorder struct { | ||
registry metrics.KubeRegistry | ||
metrics map[string]interface{} | ||
} | ||
|
||
// Recorder returns the singleton instance of metricRecorder. | ||
// nil is returned if the recorder is not initialized. | ||
func Recorder() *metricRecorder { | ||
return r | ||
} | ||
|
||
// InitializeRecorder initializes a new metricRecorder instance if it hasn't been initialized. | ||
func InitializeRecorder() *metricRecorder { | ||
once.Do(func() { | ||
r = &metricRecorder{ | ||
registry: metrics.NewKubeRegistry(), | ||
metrics: make(map[string]interface{}), | ||
} | ||
}) | ||
return r | ||
} | ||
|
||
// IncreaseCount increases the counter metric by 1. | ||
func (m *metricRecorder) IncreaseCount(name string, labels map[string]string) { | ||
if m == nil { | ||
return // recorder is not initialized | ||
} | ||
|
||
metric, ok := m.metrics[name] | ||
|
||
if !ok { | ||
klog.V(4).InfoS("Metric not found, registering", "name", name, "labels", labels) | ||
m.registerCounterVec(name, "ebs_csi_aws_com metric", getLabelNames(labels)) | ||
m.IncreaseCount(name, labels) | ||
return | ||
} | ||
|
||
metric.(*metrics.CounterVec).With(metrics.Labels(labels)).Inc() | ||
} | ||
|
||
// ObserveHistogram records the given value in the histogram metric. | ||
func (m *metricRecorder) ObserveHistogram(name string, value float64, labels map[string]string, buckets []float64) { | ||
if m == nil { | ||
return // recorder is not initialized | ||
} | ||
metric, ok := m.metrics[name] | ||
|
||
if !ok { | ||
klog.V(4).InfoS("Metric not found, registering", "name", name, "labels", labels, "buckets", buckets) | ||
m.registerHistogramVec(name, "ebs_csi_aws_com metric", getLabelNames(labels), buckets) | ||
m.ObserveHistogram(name, value, labels, buckets) | ||
return | ||
} | ||
|
||
metric.(*metrics.HistogramVec).With(metrics.Labels(labels)).Observe(value) | ||
} | ||
|
||
// InitializeMetricsHandler starts a new HTTP server to expose the metrics. | ||
func (m *metricRecorder) InitializeMetricsHandler(address, path string) { | ||
if m == nil { | ||
klog.InfoS("InitializeMetricsHandler: metric recorder is not initialized") | ||
return | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(path, metrics.HandlerFor( | ||
m.registry, | ||
metrics.HandlerOpts{ | ||
ErrorHandling: metrics.ContinueOnError, | ||
})) | ||
|
||
server := &http.Server{ | ||
Addr: address, | ||
Handler: mux, | ||
ReadTimeout: 3 * time.Second, | ||
} | ||
|
||
go func() { | ||
klog.InfoS("Metric server listening", "address", address, "path", path) | ||
|
||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
klog.ErrorS(err, "Failed to start metric server", "address", address, "path", path) | ||
klog.FlushAndExit(klog.ExitFlushTimeout, 1) | ||
} | ||
}() | ||
} | ||
|
||
func (m *metricRecorder) registerHistogramVec(name, help string, labels []string, buckets []float64) { | ||
if _, exists := m.metrics[name]; exists { | ||
return | ||
} | ||
histogram := createHistogramVec(name, help, labels, buckets) | ||
m.metrics[name] = histogram | ||
m.registry.MustRegister(histogram) | ||
} | ||
|
||
func (m *metricRecorder) registerCounterVec(name, help string, labels []string) { | ||
if _, exists := m.metrics[name]; exists { | ||
return | ||
} | ||
counter := createCounterVec(name, help, labels) | ||
m.metrics[name] = counter | ||
m.registry.MustRegister(counter) | ||
} | ||
|
||
func createHistogramVec(name, help string, labels []string, buckets []float64) *metrics.HistogramVec { | ||
opts := &metrics.HistogramOpts{ | ||
Name: name, | ||
Help: help, | ||
StabilityLevel: metrics.ALPHA, | ||
Buckets: buckets, | ||
} | ||
return metrics.NewHistogramVec(opts, labels) | ||
} | ||
|
||
func createCounterVec(name, help string, labels []string) *metrics.CounterVec { | ||
return metrics.NewCounterVec( | ||
&metrics.CounterOpts{ | ||
Name: name, | ||
Help: help, | ||
StabilityLevel: metrics.ALPHA, | ||
}, | ||
labels, | ||
) | ||
} | ||
|
||
func getLabelNames(labels map[string]string) []string { | ||
names := make([]string, 0, len(labels)) | ||
for n := range labels { | ||
names = append(names, n) | ||
} | ||
return names | ||
} |
Oops, something went wrong.