Simplify logic, improve error handling and update docs

Signed-off-by: Carrie Edwards <edwrdscarrie@gmail.com>
This commit is contained in:
Carrie Edwards 2025-08-14 09:21:52 -07:00
parent 54ba59948c
commit d3b14065ba
7 changed files with 19 additions and 21 deletions

View File

@ -241,7 +241,7 @@ Examples of equivalent durations:
When enabled, allows for the native ingestion of delta OTLP metrics, storing the raw sample values without conversion. This cannot be enabled in conjunction with `otlp-deltatocumulative`. It is recommended to enable `type-and-unit-labels`.
Currently, the StartTimeUnixNano field is ignored. Delta metrics are given a `__temporality__` label with a value of "delta" and a `__type__` label with a value of "gauge"/"gaugehistogram".
Currently, the StartTimeUnixNano field is ignored. Delta metrics are given a `__temporality__` label with a value of "delta", and a `__type__` label with a value of "gauge"/"gaugehistogram" if `type-and-unit-labels` is enabled.
Delta support is in a very early stage of development and the ingestion and querying process my change over time. For the open proposal see [prometheus/proposals#48](https://github.com/prometheus/proposals/pull/48).

View File

@ -118,7 +118,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, metadata prompb.MetricMetadata, temporality pmetric.AggregationTemporality, hasTemporality bool, extras ...string,
ignoreAttrs []string, logOnOverwrite bool, metadata prompb.MetricMetadata, temporality pmetric.AggregationTemporality, extras ...string,
) ([]prompb.Label, error) {
resourceAttrs := resource.Attributes()
serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName)
@ -144,9 +144,9 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, scope s
}
if settings.EnableTypeAndUnitLabels {
maxLabelCount += 2
if settings.AllowDeltaTemporality && hasTemporality {
maxLabelCount++
}
if settings.AllowDeltaTemporality && (temporality == pmetric.AggregationTemporalityCumulative || temporality == pmetric.AggregationTemporalityDelta) {
maxLabelCount++
}
// Ensure attributes are sorted by key for consistent merging of keys which
@ -216,12 +216,16 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, scope s
l["__unit__"] = unitNamer.Build(metadata.Unit)
}
}
if settings.AllowDeltaTemporality && hasTemporality {
if settings.AllowDeltaTemporality {
switch temporality {
case pmetric.AggregationTemporalityCumulative:
l["__temporality__"] = "cumulative"
case pmetric.AggregationTemporalityDelta:
l["__temporality__"] = "delta"
case pmetric.AggregationTemporalityUnspecified:
// For metric types without temporality (Gauge, Summary), we don't add a temporality label.
default:
return nil, fmt.Errorf("unknown aggregation temporality: %d", temporality)
}
}
@ -298,7 +302,7 @@ func aggregationTemporality(metric pmetric.Metric) (pmetric.AggregationTemporali
// However, work is under way to resolve this shortcoming through a feature called native histograms custom buckets:
// https://github.com/prometheus/prometheus/issues/13485.
func (c *PrometheusConverter) addHistogramDataPoints(ctx context.Context, dataPoints pmetric.HistogramDataPointSlice,
resource pcommon.Resource, settings Settings, metadata prompb.MetricMetadata, scope scope, temporality pmetric.AggregationTemporality, hasTemporality bool,
resource pcommon.Resource, settings Settings, metadata prompb.MetricMetadata, scope scope, temporality pmetric.AggregationTemporality,
) error {
for x := 0; x < dataPoints.Len(); x++ {
if err := c.everyN.checkContext(ctx); err != nil {
@ -307,7 +311,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(ctx context.Context, dataPo
pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp())
baseLabels, err := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata, temporality, hasTemporality)
baseLabels, err := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata, temporality)
if err != nil {
return err
}
@ -514,7 +518,7 @@ func (c *PrometheusConverter) addSummaryDataPoints(ctx context.Context, dataPoin
pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp())
baseLabels, err := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata, 0, false)
baseLabels, err := createAttributes(resource, pt.Attributes(), scope, settings, nil, false, metadata, 0)
if err != nil {
return err
}
@ -662,7 +666,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, earlies
// Do not pass identifying attributes as ignoreAttrs below.
identifyingAttrs = nil
}
labels, err := createAttributes(resource, attributes, scope{}, settings, identifyingAttrs, false, prompb.MetricMetadata{}, 0, false, model.MetricNameLabel, name)
labels, err := createAttributes(resource, attributes, scope{}, settings, identifyingAttrs, false, prompb.MetricMetadata{}, 0, model.MetricNameLabel, name)
if err != nil {
return err
}

View File

@ -531,7 +531,7 @@ func TestCreateAttributes(t *testing.T) {
}),
PromoteScopeMetadata: tc.promoteScope,
}
lbls, err := createAttributes(resource, attrs, tc.scope, settings, tc.ignoreAttrs, false, prompb.MetricMetadata{}, 0, false, model.MetricNameLabel, "test_metric")
lbls, err := createAttributes(resource, attrs, tc.scope, settings, tc.ignoreAttrs, false, prompb.MetricMetadata{}, 0, model.MetricNameLabel, "test_metric")
require.NoError(t, err)
require.ElementsMatch(t, lbls, tc.expectedLabels)
@ -907,7 +907,6 @@ func TestPrometheusConverter_AddHistogramDataPoints(t *testing.T) {
prompb.MetricMetadata{MetricFamilyName: metric.Name()},
tt.scope,
pmetric.AggregationTemporalityCumulative,
true,
)
require.Equal(t, tt.want(), converter.unique)

View File

@ -61,7 +61,6 @@ func (c *PrometheusConverter) addExponentialHistogramDataPoints(ctx context.Cont
true,
metadata,
temporality,
true,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
@ -284,7 +283,6 @@ func (c *PrometheusConverter) addCustomBucketsHistogramDataPoints(ctx context.Co
true,
metadata,
temporality,
true,
model.MetricNameLabel,
metadata.MetricFamilyName,
)

View File

@ -204,7 +204,7 @@ func (c *PrometheusConverter) FromMetrics(ctx context.Context, md pmetric.Metric
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name()))
break
}
if err := c.addSumNumberDataPoints(ctx, dataPoints, resource, settings, metadata, scope, temporality, hasTemporality); err != nil {
if err := c.addSumNumberDataPoints(ctx, dataPoints, resource, settings, metadata, scope, temporality); err != nil {
errs = multierr.Append(errs, err)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return
@ -228,7 +228,7 @@ func (c *PrometheusConverter) FromMetrics(ctx context.Context, md pmetric.Metric
}
}
} else {
if err := c.addHistogramDataPoints(ctx, dataPoints, resource, settings, metadata, scope, temporality, hasTemporality); err != nil {
if err := c.addHistogramDataPoints(ctx, dataPoints, resource, settings, metadata, scope, temporality); err != nil {
errs = multierr.Append(errs, err)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return

View File

@ -46,7 +46,6 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data
true,
metadata,
0,
false,
model.MetricNameLabel,
metadata.MetricFamilyName,
)
@ -74,7 +73,7 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data
}
func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPoints pmetric.NumberDataPointSlice,
resource pcommon.Resource, settings Settings, metadata prompb.MetricMetadata, scope scope, temporality pmetric.AggregationTemporality, hasTemporality bool,
resource pcommon.Resource, settings Settings, metadata prompb.MetricMetadata, scope scope, temporality pmetric.AggregationTemporality,
) error {
for x := 0; x < dataPoints.Len(); x++ {
if err := c.everyN.checkContext(ctx); err != nil {
@ -91,7 +90,6 @@ func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPo
true,
metadata,
temporality,
hasTemporality,
model.MetricNameLabel,
metadata.MetricFamilyName,
)

View File

@ -353,7 +353,6 @@ func TestPrometheusConverter_addSumNumberDataPoints(t *testing.T) {
prompb.MetricMetadata{MetricFamilyName: metric.Name()},
tt.scope,
pmetric.AggregationTemporalityCumulative,
true,
)
require.Equal(t, tt.want(), converter.unique)