-
Notifications
You must be signed in to change notification settings - Fork 501
[MISC]: add vtc_bucket_size_active metric gauge for vtc-basic #1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,136 @@ | ||
| /* | ||
| Copyright 2025 The Aibrix Team. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| ) | ||
|
|
||
| var ( | ||
| customGauges = make(map[string]*prometheus.GaugeVec) | ||
| customGaugesMu sync.RWMutex | ||
| customCounters = make(map[string]*prometheus.CounterVec) | ||
| customCountersMu sync.RWMutex | ||
|
|
||
| // Function variables that can be overridden for testing | ||
| SetGaugeMetricFnForTest = defaultSetGaugeMetric | ||
| IncrementCounterMetricFnForTest = defaultIncrementCounterMetric | ||
| ) | ||
|
|
||
| func SetGaugeMetric(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| SetGaugeMetricFnForTest(name, help, value, labelNames, labelValues...) | ||
| } | ||
|
|
||
| func defaultSetGaugeMetric(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| customGaugesMu.RLock() | ||
| gauge, ok := customGauges[name] | ||
| customGaugesMu.RUnlock() | ||
|
|
||
| if !ok { | ||
| customGaugesMu.Lock() | ||
| gauge, ok = customGauges[name] | ||
| if !ok { | ||
| gauge = promauto.NewGaugeVec( | ||
| prometheus.GaugeOpts{Name: name, Help: help}, | ||
| labelNames, | ||
| ) | ||
| customGauges[name] = gauge | ||
| } | ||
| customGaugesMu.Unlock() | ||
| } | ||
|
|
||
| gauge.WithLabelValues(labelValues...).Set(value) | ||
| } | ||
|
|
||
| func IncrementCounterMetric(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| IncrementCounterMetricFnForTest(name, help, value, labelNames, labelValues...) | ||
| } | ||
|
|
||
| func defaultIncrementCounterMetric(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| customCountersMu.RLock() | ||
| counter, ok := customCounters[name] | ||
| customCountersMu.RUnlock() | ||
|
|
||
| if !ok { | ||
| customCountersMu.Lock() | ||
| counter, ok = customCounters[name] | ||
| if !ok { | ||
| counter = promauto.NewCounterVec( | ||
| prometheus.CounterOpts{Name: name, Help: help}, | ||
| labelNames, | ||
| ) | ||
| customCounters[name] = counter | ||
| } | ||
| customCountersMu.Unlock() | ||
| } | ||
|
|
||
| counter.WithLabelValues(labelValues...).Add(value) | ||
| } | ||
|
|
||
| func GetMetricHelp(metricName string) string { | ||
| metric, ok := Metrics[metricName] | ||
| if !ok { | ||
| return "" | ||
| } | ||
| return metric.Description | ||
| } | ||
|
|
||
| func GetGaugeValueForTest(name string, labelValues ...string) float64 { | ||
| customGaugesMu.RLock() | ||
| defer customGaugesMu.RUnlock() | ||
| // Tests should use their own registry | ||
| return 0 | ||
| } | ||
|
|
||
| func SetupMetricsForTest(metricName string, labelNames []string) (*prometheus.GaugeVec, func()) { | ||
| testRegistry := prometheus.NewRegistry() | ||
| testGauge := prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{Name: metricName}, | ||
| labelNames, | ||
| ) | ||
| testRegistry.MustRegister(testGauge) | ||
|
|
||
| originalFn := SetGaugeMetricFnForTest | ||
| SetGaugeMetricFnForTest = func(name string, help string, value float64, labels []string, labelValues ...string) { | ||
| if name == metricName { | ||
| testGauge.WithLabelValues(labelValues...).Set(value) | ||
| } | ||
| } | ||
|
|
||
| return testGauge, func() { SetGaugeMetricFnForTest = originalFn } | ||
| } | ||
|
|
||
| func SetupCounterMetricsForTest(metricName string, labelNames []string) (*prometheus.CounterVec, func()) { | ||
| testRegistry := prometheus.NewRegistry() | ||
| testCounter := prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{Name: metricName}, | ||
| labelNames, | ||
| ) | ||
| testRegistry.MustRegister(testCounter) | ||
|
|
||
| originalFn := IncrementCounterMetricFnForTest | ||
| IncrementCounterMetricFnForTest = func(name string, help string, value float64, labels []string, labelValues ...string) { | ||
| if name == metricName { | ||
| testCounter.WithLabelValues(labelValues...).Add(value) | ||
| } | ||
| } | ||
|
|
||
| return testCounter, func() { IncrementCounterMetricFnForTest = originalFn } | ||
| } | ||
This file contains hidden or 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,185 @@ | ||
| /* | ||
| Copyright 2025 The Aibrix Team. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/testutil" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const ( | ||
| testMetricName = "test_metric" | ||
| testMetricHelp = "Test metric for unit tests" | ||
| testLabelName = "test_label" | ||
| testLabelValue = "test_value" | ||
| testLabelName2 = "test_label2" | ||
| testLabelValue2 = "test_value2" | ||
| ) | ||
|
|
||
| func TestSetGaugeMetric(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
|
|
||
| registry := prometheus.NewRegistry() | ||
| gauge := prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{Name: testMetricName, Help: testMetricHelp}, | ||
| []string{testLabelName}, | ||
| ) | ||
| registry.MustRegister(gauge) | ||
|
|
||
| originalFn := SetGaugeMetricFnForTest | ||
| defer func() { SetGaugeMetricFnForTest = originalFn }() | ||
|
|
||
| SetGaugeMetricFnForTest = func(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| if name == testMetricName { | ||
| gauge.WithLabelValues(labelValues...).Set(value) | ||
| } | ||
| } | ||
|
|
||
| testValue := 42.0 | ||
| SetGaugeMetric(testMetricName, testMetricHelp, testValue, []string{testLabelName}, testLabelValue) | ||
|
|
||
| value := testutil.ToFloat64(gauge.WithLabelValues(testLabelValue)) | ||
| assert.Equal(t, testValue, value, "Metric value should match what was set") | ||
| } | ||
|
|
||
| func TestSetupMetricsForTest(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
|
|
||
| testGauge, cleanup := SetupMetricsForTest(testMetricName, []string{testLabelName}) | ||
| defer cleanup() | ||
|
|
||
| testValue := 42.0 | ||
| SetGaugeMetric(testMetricName, testMetricHelp, testValue, []string{testLabelName}, testLabelValue) | ||
|
|
||
| value := testutil.ToFloat64(testGauge.WithLabelValues(testLabelValue)) | ||
| assert.Equal(t, testValue, value, "Metric value should match what was set") | ||
|
|
||
| cleanup() | ||
| SetGaugeMetric(testMetricName, testMetricHelp, 99.0, []string{testLabelName}, testLabelValue) | ||
|
|
||
| value = testutil.ToFloat64(testGauge.WithLabelValues(testLabelValue)) | ||
| assert.Equal(t, testValue, value, "Metric value should not change after cleanup") | ||
| } | ||
|
|
||
| func TestMetricRegistrationOnlyOnce(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
| customGauges = make(map[string]*prometheus.GaugeVec) | ||
|
|
||
| for i := 0; i < 5; i++ { | ||
| SetGaugeMetric(testMetricName, testMetricHelp, float64(i), []string{testLabelName}, testLabelValue) | ||
| } | ||
|
|
||
| customGaugesMu.RLock() | ||
| count := len(customGauges) | ||
| customGaugesMu.RUnlock() | ||
|
|
||
| assert.Equal(t, 1, count, "Should only register the metric once") | ||
| } | ||
|
|
||
| func TestConcurrentMetricUpdates(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
| customGauges = make(map[string]*prometheus.GaugeVec) | ||
|
|
||
| numGoroutines := 10 | ||
| numUpdatesPerGoroutine := 100 | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(numGoroutines) | ||
|
|
||
| for i := 0; i < numGoroutines; i++ { | ||
| go func(id int) { | ||
| defer wg.Done() | ||
| for j := 0; j < numUpdatesPerGoroutine; j++ { | ||
| labelValue := testLabelValue + "_" + string(rune('A'+id)) | ||
| SetGaugeMetric(testMetricName, testMetricHelp, float64(j), []string{testLabelName}, labelValue) | ||
| } | ||
| }(i) | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| customGaugesMu.RLock() | ||
| count := len(customGauges) | ||
| gauge := customGauges[testMetricName] | ||
| customGaugesMu.RUnlock() | ||
|
|
||
| assert.Equal(t, 1, count, "Should only register the metric once even with concurrent updates") | ||
|
|
||
| ch := make(chan *prometheus.Desc, 1) | ||
| gauge.Describe(ch) | ||
| desc := <-ch | ||
| assert.Contains(t, desc.String(), testMetricName, "Metric description should contain the metric name") | ||
| } | ||
|
|
||
| func TestMultipleLabels(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
| customGauges = make(map[string]*prometheus.GaugeVec) | ||
|
|
||
| registry := prometheus.NewRegistry() | ||
| gauge := prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{Name: testMetricName, Help: testMetricHelp}, | ||
| []string{testLabelName, testLabelName2}, | ||
| ) | ||
| registry.MustRegister(gauge) | ||
|
|
||
| originalFn := SetGaugeMetricFnForTest | ||
| defer func() { SetGaugeMetricFnForTest = originalFn }() | ||
|
|
||
| SetGaugeMetricFnForTest = func(name string, help string, value float64, labelNames []string, labelValues ...string) { | ||
| if name == testMetricName { | ||
| gauge.WithLabelValues(labelValues...).Set(value) | ||
| } | ||
| } | ||
|
|
||
| testValue := 42.0 | ||
| SetGaugeMetric(testMetricName, testMetricHelp, testValue, | ||
| []string{testLabelName, testLabelName2}, | ||
| testLabelValue, testLabelValue2) | ||
|
|
||
| value := testutil.ToFloat64(gauge.WithLabelValues(testLabelValue, testLabelValue2)) | ||
| assert.Equal(t, testValue, value, "Metric value with multiple labels should match what was set") | ||
| } | ||
|
|
||
| func TestSetupCounterMetricsForTest(t *testing.T) { | ||
| prometheus.DefaultRegisterer = prometheus.NewRegistry() | ||
| customCounters = make(map[string]*prometheus.CounterVec) | ||
|
|
||
| testCounter, cleanup := SetupCounterMetricsForTest("test_counter", []string{"pod", "model"}) | ||
| defer cleanup() | ||
|
|
||
| IncrementCounterMetric("test_counter", "Test counter metric", 5.0, []string{"pod", "model"}, "pod-1", "model-1") | ||
|
|
||
| metricValue := testutil.ToFloat64(testCounter.WithLabelValues("pod-1", "model-1")) | ||
| assert.Equal(t, 5.0, metricValue, "Counter metric value should match the incremented value") | ||
|
|
||
| IncrementCounterMetric("test_counter", "Test counter metric", 3.0, []string{"pod", "model"}, "pod-1", "model-1") | ||
|
|
||
| metricValue = testutil.ToFloat64(testCounter.WithLabelValues("pod-1", "model-1")) | ||
| assert.Equal(t, 8.0, metricValue, "Counter metric value should accumulate") | ||
|
|
||
| IncrementCounterMetric("test_counter", "Test counter metric", 10.0, []string{"pod", "model"}, "pod-2", "model-1") | ||
|
|
||
| metricValue = testutil.ToFloat64(testCounter.WithLabelValues("pod-1", "model-1")) | ||
| assert.Equal(t, 8.0, metricValue, "Original counter metric should be unchanged") | ||
|
|
||
| metricValue = testutil.ToFloat64(testCounter.WithLabelValues("pod-2", "model-1")) | ||
| assert.Equal(t, 10.0, metricValue, "Counter metric with different labels should have correct value") | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.