Overwrite type/unit labels when already set
CI / Go tests (push) Waiting to run Details
CI / More Go tests (push) Waiting to run Details
CI / Go tests with previous Go version (push) Waiting to run Details
CI / UI tests (push) Waiting to run Details
CI / Go tests on Windows (push) Waiting to run Details
CI / Mixins tests (push) Waiting to run Details
CI / Build Prometheus for common architectures (0) (push) Waiting to run Details
CI / Build Prometheus for common architectures (1) (push) Waiting to run Details
CI / Build Prometheus for common architectures (2) (push) Waiting to run Details
CI / Build Prometheus for all architectures (0) (push) Waiting to run Details
CI / Build Prometheus for all architectures (1) (push) Waiting to run Details
CI / Build Prometheus for all architectures (10) (push) Waiting to run Details
CI / Build Prometheus for all architectures (11) (push) Waiting to run Details
CI / Build Prometheus for all architectures (2) (push) Waiting to run Details
CI / Build Prometheus for all architectures (3) (push) Waiting to run Details
CI / Build Prometheus for all architectures (4) (push) Waiting to run Details
CI / Build Prometheus for all architectures (5) (push) Waiting to run Details
CI / Build Prometheus for all architectures (6) (push) Waiting to run Details
CI / Build Prometheus for all architectures (7) (push) Waiting to run Details
CI / Build Prometheus for all architectures (8) (push) Waiting to run Details
CI / Build Prometheus for all architectures (9) (push) Waiting to run Details
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions Details
CI / Check generated parser (push) Waiting to run Details
CI / golangci-lint (push) Waiting to run Details
CI / fuzzing (push) Waiting to run Details
CI / codeql (push) Waiting to run Details
CI / Publish main branch artifacts (push) Blocked by required conditions Details
CI / Publish release artefacts (push) Blocked by required conditions Details
CI / Publish UI on npm Registry (push) Blocked by required conditions Details

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
This commit is contained in:
Arthur Silva Sens 2025-07-17 15:35:18 -03:00
parent bdcf4663dc
commit 11e806ed86
No known key found for this signature in database
2 changed files with 61 additions and 15 deletions

View File

@ -554,21 +554,12 @@ func createLabels(name string, baseLabels []prompb.Label, extras ...string) []pr
func addTypeAndUnitLabels(labels []prompb.Label, metadata prompb.MetricMetadata, settings Settings) []prompb.Label { func addTypeAndUnitLabels(labels []prompb.Label, metadata prompb.MetricMetadata, settings Settings) []prompb.Label {
unitNamer := otlptranslator.UnitNamer{UTF8Allowed: settings.AllowUTF8} unitNamer := otlptranslator.UnitNamer{UTF8Allowed: settings.AllowUTF8}
var hasTypeLabel, hasUnitLabel bool labels = slices.DeleteFunc(labels, func(l prompb.Label) bool {
for _, l := range labels { return l.Name == "__type__" || l.Name == "__unit__"
if l.Name == "__type__" { })
hasTypeLabel = true
} labels = append(labels, prompb.Label{Name: "__type__", Value: strings.ToLower(metadata.Type.String())})
if l.Name == "__unit__" { labels = append(labels, prompb.Label{Name: "__unit__", Value: unitNamer.Build(metadata.Unit)})
hasUnitLabel = true
}
}
if !hasTypeLabel {
labels = append(labels, prompb.Label{Name: "__type__", Value: strings.ToLower(metadata.Type.String())})
}
if !hasUnitLabel {
labels = append(labels, prompb.Label{Name: "__unit__", Value: unitNamer.Build(metadata.Unit)})
}
return labels return labels
} }

View File

@ -966,3 +966,58 @@ func TestGetPromExemplars(t *testing.T) {
require.Error(t, err) require.Error(t, err)
}) })
} }
func TestAddTypeAndUnitLabels(t *testing.T) {
testCases := []struct {
name string
inputLabels []prompb.Label
metadata prompb.MetricMetadata
expectedLabels []prompb.Label
}{
{
name: "overwrites existing type and unit labels and preserves other labels",
inputLabels: []prompb.Label{
{Name: "job", Value: "test-job"},
{Name: "__type__", Value: "old_type"},
{Name: "instance", Value: "test-instance"},
{Name: "__unit__", Value: "old_unit"},
{Name: "custom_label", Value: "custom_value"},
},
metadata: prompb.MetricMetadata{
Type: prompb.MetricMetadata_COUNTER,
Unit: "seconds",
},
expectedLabels: []prompb.Label{
{Name: "job", Value: "test-job"},
{Name: "instance", Value: "test-instance"},
{Name: "custom_label", Value: "custom_value"},
{Name: "__type__", Value: "counter"},
{Name: "__unit__", Value: "seconds"},
},
},
{
name: "adds type and unit labels when missing",
inputLabels: []prompb.Label{
{Name: "job", Value: "test-job"},
{Name: "instance", Value: "test-instance"},
},
metadata: prompb.MetricMetadata{
Type: prompb.MetricMetadata_GAUGE,
Unit: "bytes",
},
expectedLabels: []prompb.Label{
{Name: "job", Value: "test-job"},
{Name: "instance", Value: "test-instance"},
{Name: "__type__", Value: "gauge"},
{Name: "__unit__", Value: "bytes"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := addTypeAndUnitLabels(tc.inputLabels, tc.metadata, Settings{AllowUTF8: false})
require.ElementsMatch(t, tc.expectedLabels, result)
})
}
}