diff --git a/pkg/translator/prometheusremotewrite/helper.go b/pkg/translator/prometheusremotewrite/helper.go index 2a6658949bef..d85151e2dc8c 100644 --- a/pkg/translator/prometheusremotewrite/helper.go +++ b/pkg/translator/prometheusremotewrite/helper.go @@ -26,7 +26,6 @@ import ( ) const ( - nameStr = "__name__" sumStr = "_sum" countStr = "_count" bucketStr = "_bucket" @@ -70,15 +69,14 @@ func (a ByLabelName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // tsMap will be unmodified if either labels or sample is nil, but can still be modified if the exemplar is nil. func addSample(tsMap map[string]*prompb.TimeSeries, sample *prompb.Sample, labels []prompb.Label, datatype string) string { - if sample == nil || labels == nil || tsMap == nil { + // This shouldn't happen return "" } - sig := timeSeriesSignature(datatype, &labels) - ts, ok := tsMap[sig] - - if ok { + sig := timeSeriesSignature(datatype, labels) + ts := tsMap[sig] + if ts != nil { ts.Samples = append(ts.Samples, *sample) } else { newTs := &prompb.TimeSeries{ @@ -95,7 +93,7 @@ func addSample(tsMap map[string]*prompb.TimeSeries, sample *prompb.Sample, label // we only add exemplars if samples are presents // tsMap is unmodified if either of its parameters is nil and samples are nil. func addExemplars(tsMap map[string]*prompb.TimeSeries, exemplars []prompb.Exemplar, bucketBoundsData []bucketBoundsData) { - if tsMap == nil || bucketBoundsData == nil || exemplars == nil { + if len(tsMap) == 0 || len(bucketBoundsData) == 0 || len(exemplars) == 0 { return } @@ -111,14 +109,10 @@ func addExemplar(tsMap map[string]*prompb.TimeSeries, bucketBounds []bucketBound sig := bucketBound.sig bound := bucketBound.bound - _, ok := tsMap[sig] - if ok { - if tsMap[sig].Samples != nil { - if exemplar.Value <= bound { - tsMap[sig].Exemplars = append(tsMap[sig].Exemplars, exemplar) - return - } - } + ts := tsMap[sig] + if ts != nil && len(ts.Samples) > 0 && exemplar.Value <= bound { + ts.Exemplars = append(ts.Exemplars, exemplar) + return } } } @@ -129,10 +123,10 @@ func addExemplar(tsMap map[string]*prompb.TimeSeries, bucketBounds []bucketBound // // the label slice should not contain duplicate label names; this method sorts the slice by label name before creating // the signature. -func timeSeriesSignature(datatype string, labels *[]prompb.Label) string { +func timeSeriesSignature(datatype string, labels []prompb.Label) string { length := len(datatype) - for _, lb := range *labels { + for _, lb := range labels { length += 2 + len(lb.GetName()) + len(lb.GetValue()) } @@ -140,9 +134,9 @@ func timeSeriesSignature(datatype string, labels *[]prompb.Label) string { b.Grow(length) b.WriteString(datatype) - sort.Sort(ByLabelName(*labels)) + sort.Sort(ByLabelName(labels)) - for _, lb := range *labels { + for _, lb := range labels { b.WriteString("-") b.WriteString(lb.GetName()) b.WriteString("-") @@ -152,9 +146,9 @@ func timeSeriesSignature(datatype string, labels *[]prompb.Label) string { return b.String() } -// createAttributes creates a slice of Cortex Label with OTLP attributes and pairs of string values. -// Unpaired string value is ignored. String pairs overwrites OTLP labels if collision happens, and the overwrite is -// logged. Resultant label names are sanitized. +// createAttributes creates a slice of Prometheus Labels with OTLP attributes and pairs of string values. +// Unpaired string values are ignored. String pairs overwrite OTLP labels if collisions happen, and overwrites are +// logged. Resulting label names are sanitized. func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externalLabels map[string]string, extras ...string) []prompb.Label { serviceName, haveServiceName := resource.Attributes().Get(conventions.AttributeServiceName) instance, haveInstanceID := resource.Attributes().Get(conventions.AttributeServiceInstanceID) @@ -184,8 +178,8 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externa for _, label := range labels { var finalKey = prometheustranslator.NormalizeLabel(label.Name) - if existingLabel, alreadyExists := l[finalKey]; alreadyExists { - l[finalKey] = existingLabel + ";" + label.Value + if existingValue, alreadyExists := l[finalKey]; alreadyExists { + l[finalKey] = existingValue + ";" + label.Value } else { l[finalKey] = label.Value } @@ -269,7 +263,7 @@ func addSingleHistogramDataPoint(pt pmetric.HistogramDataPoint, resource pcommon } // sum, count, and buckets of the histogram should append suffix to baseName - labels = append(labels, prompb.Label{Name: nameStr, Value: baseName + nameSuffix}) + labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: baseName + nameSuffix}) return labels } @@ -361,7 +355,7 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar { exemplar := pt.Exemplars().At(i) exemplarRunes := 0 - promExemplar := &prompb.Exemplar{ + promExemplar := prompb.Exemplar{ Value: exemplar.DoubleValue(), Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), } @@ -404,7 +398,7 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar { promExemplar.Labels = append(promExemplar.Labels, labelsFromAttributes...) } - promExemplars = append(promExemplars, *promExemplar) + promExemplars = append(promExemplars, promExemplar) } return promExemplars @@ -467,7 +461,7 @@ func addSingleSummaryDataPoint(pt pmetric.SummaryDataPoint, resource pcommon.Res labels = append(labels, prompb.Label{Name: extras[extrasIdx], Value: extras[extrasIdx+1]}) } - labels = append(labels, prompb.Label{Name: nameStr, Value: name}) + labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: name}) return labels } @@ -527,7 +521,7 @@ func addCreatedTimeSeriesIfNeeded( timestamp pcommon.Timestamp, metricType string, ) { - sig := timeSeriesSignature(metricType, &labels) + sig := timeSeriesSignature(metricType, labels) if _, ok := series[sig]; !ok { series[sig] = &prompb.TimeSeries{ Labels: labels, @@ -568,7 +562,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta if len(settings.Namespace) > 0 { name = settings.Namespace + "_" + name } - labels := createAttributes(resource, attributes, settings.ExternalLabels, nameStr, name) + labels := createAttributes(resource, attributes, settings.ExternalLabels, model.MetricNameLabel, name) sample := &prompb.Sample{ Value: float64(1), // convert ns to ms diff --git a/pkg/translator/prometheusremotewrite/helper_test.go b/pkg/translator/prometheusremotewrite/helper_test.go index 887c864082be..f87cb7a226a8 100644 --- a/pkg/translator/prometheusremotewrite/helper_test.go +++ b/pkg/translator/prometheusremotewrite/helper_test.go @@ -205,8 +205,7 @@ func Test_timeSeriesSignature(t *testing.T) { // run tests for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - lbs := tt.lbs - assert.EqualValues(t, tt.want, timeSeriesSignature(tt.metric.Type().String(), &lbs)) + assert.EqualValues(t, tt.want, timeSeriesSignature(tt.metric.Type().String(), tt.lbs)) }) } } @@ -691,19 +690,19 @@ func TestAddSingleSummaryDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test_summary" + sumStr}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSummary.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSummary.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeSummary.String(), &sumLabels): { + timeSeriesSignature(pmetric.MetricTypeSummary.String(), sumLabels): { Labels: sumLabels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeSummary.String(), &createdLabels): { + timeSeriesSignature(pmetric.MetricTypeSummary.String(), createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, @@ -732,13 +731,13 @@ func TestAddSingleSummaryDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test_summary" + sumStr}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSummary.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSummary.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeSummary.String(), &sumLabels): { + timeSeriesSignature(pmetric.MetricTypeSummary.String(), sumLabels): { Labels: sumLabels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, @@ -802,19 +801,19 @@ func TestAddSingleHistogramDataPoint(t *testing.T) { {Name: model.BucketLabel, Value: "+Inf"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &infLabels): { + timeSeriesSignature(pmetric.MetricTypeHistogram.String(), infLabels): { Labels: infLabels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeHistogram.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &createdLabels): { + timeSeriesSignature(pmetric.MetricTypeHistogram.String(), createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, @@ -844,13 +843,13 @@ func TestAddSingleHistogramDataPoint(t *testing.T) { {Name: model.BucketLabel, Value: "+Inf"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &infLabels): { + timeSeriesSignature(pmetric.MetricTypeHistogram.String(), infLabels): { Labels: infLabels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeHistogram.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, diff --git a/pkg/translator/prometheusremotewrite/histograms.go b/pkg/translator/prometheusremotewrite/histograms.go index bae65448ae00..bae675ef446b 100644 --- a/pkg/translator/prometheusremotewrite/histograms.go +++ b/pkg/translator/prometheusremotewrite/histograms.go @@ -27,12 +27,13 @@ func addSingleExponentialHistogramDataPoint( resource, pt.Attributes(), settings.ExternalLabels, - model.MetricNameLabel, metric, + model.MetricNameLabel, + metric, ) sig := timeSeriesSignature( pmetric.MetricTypeExponentialHistogram.String(), - &labels, + labels, ) ts, ok := series[sig] if !ok { diff --git a/pkg/translator/prometheusremotewrite/histograms_test.go b/pkg/translator/prometheusremotewrite/histograms_test.go index fcef1d4dc924..84261b4e7da4 100644 --- a/pkg/translator/prometheusremotewrite/histograms_test.go +++ b/pkg/translator/prometheusremotewrite/histograms_test.go @@ -634,7 +634,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) { {Name: "attr", Value: "test_attr"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labels): { Labels: labels, Histograms: []prompb.Histogram{ { @@ -698,7 +698,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) { } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labels): { Labels: labels, Histograms: []prompb.Histogram{ { @@ -714,7 +714,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) { {Value: 1}, }, }, - timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labelsAnother): { + timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labelsAnother): { Labels: labelsAnother, Histograms: []prompb.Histogram{ { diff --git a/pkg/translator/prometheusremotewrite/metrics_to_prw.go b/pkg/translator/prometheusremotewrite/metrics_to_prw.go index adfe58e80c78..f048f7534fba 100644 --- a/pkg/translator/prometheusremotewrite/metrics_to_prw.go +++ b/pkg/translator/prometheusremotewrite/metrics_to_prw.go @@ -24,7 +24,7 @@ type Settings struct { SendMetadata bool } -// FromMetrics converts pmetric.Metrics to prometheus remote write format. +// FromMetrics converts pmetric.Metrics to Prometheus remote write format. func FromMetrics(md pmetric.Metrics, settings Settings) (tsMap map[string]*prompb.TimeSeries, errs error) { tsMap = make(map[string]*prompb.TimeSeries) diff --git a/pkg/translator/prometheusremotewrite/number_data_points.go b/pkg/translator/prometheusremotewrite/number_data_points.go index 650738715a2a..a2d0e33a8ba8 100644 --- a/pkg/translator/prometheusremotewrite/number_data_points.go +++ b/pkg/translator/prometheusremotewrite/number_data_points.go @@ -13,7 +13,7 @@ import ( "go.opentelemetry.io/collector/pdata/pmetric" ) -// addSingleSumNumberDataPoint converts the Gauge metric data point to a +// addSingleGaugeNumberDataPoint converts the Gauge metric data point to a // Prometheus time series with samples and labels. The result is stored in the // series map. func addSingleGaugeNumberDataPoint( @@ -28,7 +28,8 @@ func addSingleGaugeNumberDataPoint( resource, pt.Attributes(), settings.ExternalLabels, - model.MetricNameLabel, name, + model.MetricNameLabel, + name, ) sample := &prompb.Sample{ // convert ns to ms @@ -78,7 +79,7 @@ func addSingleSumNumberDataPoint( } sig := addSample(series, sample, labels, metric.Type().String()) - if ts, ok := series[sig]; sig != "" && ok { + if ts := series[sig]; sig != "" && ts != nil { exemplars := getPromExemplars[pmetric.NumberDataPoint](pt) ts.Exemplars = append(ts.Exemplars, exemplars...) } @@ -86,16 +87,18 @@ func addSingleSumNumberDataPoint( // add _created time series if needed if settings.ExportCreatedMetric && metric.Sum().IsMonotonic() { startTimestamp := pt.StartTimestamp() - if startTimestamp != 0 { - createdLabels := make([]prompb.Label, len(labels)) - copy(createdLabels, labels) - for i, l := range createdLabels { - if l.Name == model.MetricNameLabel { - createdLabels[i].Value = name + createdSuffix - break - } + if startTimestamp == 0 { + return + } + + createdLabels := make([]prompb.Label, len(labels)) + copy(createdLabels, labels) + for i, l := range createdLabels { + if l.Name == model.MetricNameLabel { + createdLabels[i].Value = name + createdSuffix + break } - addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String()) } + addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String()) } } diff --git a/pkg/translator/prometheusremotewrite/number_data_points_test.go b/pkg/translator/prometheusremotewrite/number_data_points_test.go index f8813b0c0bde..c9be5bad343b 100644 --- a/pkg/translator/prometheusremotewrite/number_data_points_test.go +++ b/pkg/translator/prometheusremotewrite/number_data_points_test.go @@ -35,7 +35,7 @@ func TestAddSingleGaugeNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeGauge.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeGauge.String(), labels): { Labels: labels, Samples: []prompb.Sample{ { @@ -90,7 +90,7 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSum.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), labels): { Labels: labels, Samples: []prompb.Sample{ { @@ -118,7 +118,7 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSum.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), labels): { Labels: labels, Samples: []prompb.Sample{{ Value: 1, @@ -154,13 +154,13 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test_sum" + createdSuffix}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSum.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 1, Timestamp: convertTimeStamp(ts)}, }, }, - timeSeriesSignature(pmetric.MetricTypeSum.String(), &createdLabels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, @@ -187,7 +187,7 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test_sum"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSum.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)}, @@ -214,7 +214,7 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { {Name: model.MetricNameLabel, Value: "test_sum"}, } return map[string]*prompb.TimeSeries{ - timeSeriesSignature(pmetric.MetricTypeSum.String(), &labels): { + timeSeriesSignature(pmetric.MetricTypeSum.String(), labels): { Labels: labels, Samples: []prompb.Sample{ {Value: 0, Timestamp: convertTimeStamp(ts)},