Reduce duplicated code
CI / Go tests (push) Has been cancelled Details
CI / More Go tests (push) Has been cancelled Details
CI / Go tests with previous Go version (push) Has been cancelled Details
CI / UI tests (push) Has been cancelled Details
CI / Go tests on Windows (push) Has been cancelled Details
CI / Mixins tests (push) Has been cancelled Details
CI / Build Prometheus for common architectures (0) (push) Has been cancelled Details
CI / Build Prometheus for common architectures (1) (push) Has been cancelled Details
CI / Build Prometheus for common architectures (2) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (0) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (1) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (10) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (11) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (2) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (3) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (4) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (5) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (6) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (7) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (8) (push) Has been cancelled Details
CI / Build Prometheus for all architectures (9) (push) Has been cancelled Details
CI / Check generated parser (push) Has been cancelled Details
CI / golangci-lint (push) Has been cancelled Details
CI / fuzzing (push) Has been cancelled Details
CI / codeql (push) Has been cancelled Details
CI / Report status of build Prometheus for all architectures (push) Has been cancelled Details
CI / Publish main branch artifacts (push) Has been cancelled Details
CI / Publish release artefacts (push) Has been cancelled Details
CI / Publish UI on npm Registry (push) Has been cancelled Details

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
This commit is contained in:
Arthur Silva Sens 2025-07-21 19:46:39 -03:00
parent f7cd7ce923
commit 05925a50ca
No known key found for this signature in database
4 changed files with 22 additions and 29 deletions

View File

@ -119,7 +119,7 @@ var seps = []byte{'\xff'}
// if logOnOverwrite is true, the overwrite is logged. Resulting label names are sanitized.
// If settings.PromoteResourceAttributes is not empty, it's a set of resource attributes that should be promoted to labels.
func createAttributes(resource pcommon.Resource, attributes pcommon.Map, scope scope, settings Settings,
ignoreAttrs []string, logOnOverwrite bool, extras ...string,
ignoreAttrs []string, logOnOverwrite bool, metadata prompb.MetricMetadata, extras ...string,
) []prompb.Label {
resourceAttrs := resource.Attributes()
serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName)
@ -143,6 +143,9 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, scope s
if haveInstanceID {
maxLabelCount++
}
if settings.EnableTypeAndUnitLabels {
maxLabelCount += 2
}
// Ensure attributes are sorted by key for consistent merging of keys which
// collide when sanitized.
@ -187,6 +190,16 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, scope s
l["otel_scope_schema_url"] = scope.schemaURL
}
if settings.EnableTypeAndUnitLabels {
unitNamer := otlptranslator.UnitNamer{UTF8Allowed: settings.AllowUTF8}
if metadata.Type != prompb.MetricMetadata_UNKNOWN {
l["__type__"] = strings.ToLower(metadata.Type.String())
}
if metadata.Unit != "" {
l["__unit__"] = unitNamer.Build(metadata.Unit)
}
}
// Map service.name + service.namespace to job.
if haveServiceName {
val := serviceName.AsString()
@ -265,11 +278,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(ctx context.Context, dataPo
pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp())
baseLabels := createAttributes(resource, pt.Attributes(), scope, settings, nil, false)
if settings.EnableTypeAndUnitLabels {
baseLabels = addTypeAndUnitLabels(baseLabels, metadata, settings)
}
baseLabels := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata)
// If the sum is unset, it indicates the _sum metric point should be
// omitted
@ -479,11 +488,7 @@ func (c *PrometheusConverter) addSummaryDataPoints(ctx context.Context, dataPoin
pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp())
baseLabels := createAttributes(resource, pt.Attributes(), scope, settings, nil, false)
if settings.EnableTypeAndUnitLabels {
baseLabels = addTypeAndUnitLabels(baseLabels, metadata, settings)
}
baseLabels := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata)
// treat sum as a sample in an individual TimeSeries
sum := &prompb.Sample{
@ -650,7 +655,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, earlies
// Do not pass identifying attributes as ignoreAttrs below.
identifyingAttrs = nil
}
labels := createAttributes(resource, attributes, scope{}, settings, identifyingAttrs, false, model.MetricNameLabel, name)
labels := createAttributes(resource, attributes, scope{}, settings, identifyingAttrs, false, prompb.MetricMetadata{}, model.MetricNameLabel, name)
haveIdentifier := false
for _, l := range labels {
if l.Name == model.JobLabel || l.Name == model.InstanceLabel {

View File

@ -531,7 +531,7 @@ func TestCreateAttributes(t *testing.T) {
}),
PromoteScopeMetadata: tc.promoteScope,
}
lbls := createAttributes(resource, attrs, tc.scope, settings, tc.ignoreAttrs, false, model.MetricNameLabel, "test_metric")
lbls := createAttributes(resource, attrs, tc.scope, settings, tc.ignoreAttrs, false, prompb.MetricMetadata{}, model.MetricNameLabel, "test_metric")
require.ElementsMatch(t, lbls, tc.expectedLabels)
})

View File

@ -59,14 +59,11 @@ func (c *PrometheusConverter) addExponentialHistogramDataPoints(ctx context.Cont
settings,
nil,
true,
metadata,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
if settings.EnableTypeAndUnitLabels {
lbls = addTypeAndUnitLabels(lbls, metadata, settings)
}
ts, _ := c.getOrCreateTimeSeries(lbls)
ts.Histograms = append(ts.Histograms, histogram)
@ -281,14 +278,11 @@ func (c *PrometheusConverter) addCustomBucketsHistogramDataPoints(ctx context.Co
settings,
nil,
true,
metadata,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
if settings.EnableTypeAndUnitLabels {
lbls = addTypeAndUnitLabels(lbls, metadata, settings)
}
ts, _ := c.getOrCreateTimeSeries(lbls)
ts.Histograms = append(ts.Histograms, histogram)

View File

@ -44,6 +44,7 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data
settings,
nil,
true,
metadata,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
@ -61,10 +62,6 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data
sample.Value = math.Float64frombits(value.StaleNaN)
}
if settings.EnableTypeAndUnitLabels {
labels = addTypeAndUnitLabels(labels, metadata, settings)
}
c.addSample(sample, labels)
}
@ -87,6 +84,7 @@ func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPo
settings,
nil,
true,
metadata,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
@ -104,10 +102,6 @@ func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPo
sample.Value = math.Float64frombits(value.StaleNaN)
}
if settings.EnableTypeAndUnitLabels {
lbls = addTypeAndUnitLabels(lbls, metadata, settings)
}
ts := c.addSample(sample, lbls)
if ts != nil {
exemplars, err := getPromExemplars[pmetric.NumberDataPoint](ctx, &c.everyN, pt)