diff --git a/.golangci.yml b/.golangci.yml index 8548774b76..d379b4db70 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -40,10 +40,6 @@ issues: exclude-files: # Skip autogenerated files. - ^.*\.(pb|y)\.go$ - exclude-dirs: - # Copied it from a different source. - - storage/remote/otlptranslator/prometheusremotewrite - - storage/remote/otlptranslator/prometheus exclude-rules: - linters: - errcheck diff --git a/storage/remote/otlptranslator/prometheusremotewrite/helper.go b/storage/remote/otlptranslator/prometheusremotewrite/helper.go index 98ac266423..0660f8ee5f 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/helper.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/helper.go @@ -116,7 +116,8 @@ 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, settings Settings, - ignoreAttrs []string, logOnOverwrite bool, extras ...string) []prompb.Label { + ignoreAttrs []string, logOnOverwrite bool, extras ...string, +) []prompb.Label { resourceAttrs := resource.Attributes() serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName) instance, haveInstanceID := resourceAttrs.Get(conventions.AttributeServiceInstanceID) @@ -248,7 +249,8 @@ func isValidAggregationTemporality(metric pmetric.Metric) bool { // 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, baseName string) error { + resource pcommon.Resource, settings Settings, baseName string, +) error { for x := 0; x < dataPoints.Len(); x++ { if err := c.everyN.checkContext(ctx); err != nil { return err @@ -272,7 +274,6 @@ func (c *PrometheusConverter) addHistogramDataPoints(ctx context.Context, dataPo sumlabels := createLabels(baseName+sumStr, baseLabels) c.addSample(sum, sumlabels) - } // treat count as a sample in an individual TimeSeries @@ -412,7 +413,7 @@ func getPromExemplars[T exemplarType](ctx context.Context, everyN *everyNTimes, return promExemplars, nil } -// mostRecentTimestampInMetric returns the latest timestamp in a batch of metrics +// mostRecentTimestampInMetric returns the latest timestamp in a batch of metrics. func mostRecentTimestampInMetric(metric pmetric.Metric) pcommon.Timestamp { var ts pcommon.Timestamp // handle individual metric based on type @@ -448,7 +449,8 @@ func mostRecentTimestampInMetric(metric pmetric.Metric) pcommon.Timestamp { } func (c *PrometheusConverter) addSummaryDataPoints(ctx context.Context, dataPoints pmetric.SummaryDataPointSlice, resource pcommon.Resource, - settings Settings, baseName string) error { + settings Settings, baseName string, +) error { for x := 0; x < dataPoints.Len(); x++ { if err := c.everyN.checkContext(ctx); err != nil { return err @@ -562,7 +564,7 @@ func (c *PrometheusConverter) getOrCreateTimeSeries(lbls []prompb.Label) (*promp // addTimeSeriesIfNeeded adds a corresponding time series if it doesn't already exist. // If the time series doesn't already exist, it gets added with startTimestamp for its value and timestamp for its timestamp, // both converted to milliseconds. -func (c *PrometheusConverter) addTimeSeriesIfNeeded(lbls []prompb.Label, startTimestamp pcommon.Timestamp, timestamp pcommon.Timestamp) { +func (c *PrometheusConverter) addTimeSeriesIfNeeded(lbls []prompb.Label, startTimestamp, timestamp pcommon.Timestamp) { ts, created := c.getOrCreateTimeSeries(lbls) if created { ts.Samples = []prompb.Sample{ @@ -631,7 +633,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta converter.addSample(sample, labels) } -// convertTimeStamp converts OTLP timestamp in ns to timestamp in ms +// convertTimeStamp converts OTLP timestamp in ns to timestamp in ms. func convertTimeStamp(timestamp pcommon.Timestamp) int64 { return int64(timestamp) / 1_000_000 } diff --git a/storage/remote/otlptranslator/prometheusremotewrite/helper_test.go b/storage/remote/otlptranslator/prometheusremotewrite/helper_test.go index b4bc704d4e..ea5a3fce57 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/helper_test.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/helper_test.go @@ -21,11 +21,11 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" "github.com/prometheus/common/model" + "github.com/stretchr/testify/require" "github.com/prometheus/prometheus/prompb" ) @@ -204,7 +204,7 @@ func TestCreateAttributes(t *testing.T) { } lbls := createAttributes(resource, attrs, settings, tc.ignoreAttrs, false, model.MetricNameLabel, "test_metric") - assert.ElementsMatch(t, lbls, tc.expectedLabels) + require.ElementsMatch(t, lbls, tc.expectedLabels) }) } } @@ -222,7 +222,7 @@ func Test_convertTimeStamp(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := convertTimeStamp(tt.arg) - assert.Equal(t, tt.want, got) + require.Equal(t, tt.want, got) }) } } @@ -330,8 +330,8 @@ func TestPrometheusConverter_AddSummaryDataPoints(t *testing.T) { metric.Name(), ) - assert.Equal(t, tt.want(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.want(), converter.unique) + require.Empty(t, converter.conflicts) }) } } @@ -441,8 +441,8 @@ func TestPrometheusConverter_AddHistogramDataPoints(t *testing.T) { metric.Name(), ) - assert.Equal(t, tt.want(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.want(), converter.unique) + require.Empty(t, converter.conflicts) }) } } @@ -457,9 +457,9 @@ func TestGetPromExemplars(t *testing.T) { exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano())) exemplar.SetIntValue(42) exemplars, err := getPromExemplars(ctx, everyN, pt) - assert.NoError(t, err) - assert.Len(t, exemplars, 1) - assert.Equal(t, float64(42), exemplars[0].Value) + require.NoError(t, err) + require.Len(t, exemplars, 1) + require.Equal(t, float64(42), exemplars[0].Value) }) t.Run("Exemplars with double value", func(t *testing.T) { @@ -468,9 +468,9 @@ func TestGetPromExemplars(t *testing.T) { exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano())) exemplar.SetDoubleValue(69.420) exemplars, err := getPromExemplars(ctx, everyN, pt) - assert.NoError(t, err) - assert.Len(t, exemplars, 1) - assert.Equal(t, 69.420, exemplars[0].Value) + require.NoError(t, err) + require.Len(t, exemplars, 1) + require.Equal(t, 69.420, exemplars[0].Value) }) t.Run("Exemplars with unsupported value type", func(t *testing.T) { @@ -478,6 +478,6 @@ func TestGetPromExemplars(t *testing.T) { exemplar := pt.Exemplars().AppendEmpty() exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano())) _, err := getPromExemplars(ctx, everyN, pt) - assert.Error(t, err) + require.Error(t, err) }) } diff --git a/storage/remote/otlptranslator/prometheusremotewrite/histograms.go b/storage/remote/otlptranslator/prometheusremotewrite/histograms.go index ba63f16ff3..db26b62925 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/histograms.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/histograms.go @@ -19,13 +19,13 @@ package prometheusremotewrite import ( "context" "fmt" - "github.com/prometheus/prometheus/model/histogram" "math" "github.com/prometheus/common/model" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" + "github.com/prometheus/prometheus/model/histogram" "github.com/prometheus/prometheus/model/value" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/util/annotations" @@ -36,7 +36,8 @@ const defaultZeroThreshold = 1e-128 // addExponentialHistogramDataPoints adds OTel exponential histogram data points to the corresponding time series // as native histogram samples. func (c *PrometheusConverter) addExponentialHistogramDataPoints(ctx context.Context, dataPoints pmetric.ExponentialHistogramDataPointSlice, - resource pcommon.Resource, settings Settings, promName string) (annotations.Annotations, error) { + resource pcommon.Resource, settings Settings, promName string, +) (annotations.Annotations, error) { var annots annotations.Annotations for x := 0; x < dataPoints.Len(); x++ { if err := c.everyN.checkContext(ctx); err != nil { @@ -151,7 +152,7 @@ func exponentialToNativeHistogram(p pmetric.ExponentialHistogramDataPoint) (prom // // When converting from OTel Explicit Histograms to Native Histograms with Custom Buckets, // the bucket indexes are not scaled, and the indices are not adjusted by 1. -func convertBucketsLayout(bucketCounts []uint64, offset int32, scaleDown int32, adjustOffset bool) ([]prompb.BucketSpan, []int64) { +func convertBucketsLayout(bucketCounts []uint64, offset, scaleDown int32, adjustOffset bool) ([]prompb.BucketSpan, []int64) { if len(bucketCounts) == 0 { return nil, nil } @@ -240,7 +241,8 @@ func convertBucketsLayout(bucketCounts []uint64, offset int32, scaleDown int32, } func (c *PrometheusConverter) addCustomBucketsHistogramDataPoints(ctx context.Context, dataPoints pmetric.HistogramDataPointSlice, - resource pcommon.Resource, settings Settings, promName string) (annotations.Annotations, error) { + resource pcommon.Resource, settings Settings, promName string, +) (annotations.Annotations, error) { var annots annotations.Annotations for x := 0; x < dataPoints.Len(); x++ { diff --git a/storage/remote/otlptranslator/prometheusremotewrite/histograms_test.go b/storage/remote/otlptranslator/prometheusremotewrite/histograms_test.go index 8484266f99..63e453a535 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/histograms_test.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/histograms_test.go @@ -24,7 +24,6 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/otlptranslator" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" @@ -380,8 +379,8 @@ func TestConvertBucketsLayout(t *testing.T) { for scaleDown, wantLayout := range tt.wantLayout { t.Run(fmt.Sprintf("%s-scaleby-%d", tt.name, scaleDown), func(t *testing.T) { gotSpans, gotDeltas := convertBucketsLayout(tt.buckets().BucketCounts().AsRaw(), tt.buckets().Offset(), scaleDown, true) - assert.Equal(t, wantLayout.wantSpans, gotSpans) - assert.Equal(t, wantLayout.wantDeltas, gotDeltas) + require.Equal(t, wantLayout.wantSpans, gotSpans) + require.Equal(t, wantLayout.wantDeltas, gotDeltas) }) } } @@ -569,13 +568,13 @@ func TestExponentialToNativeHistogram(t *testing.T) { validateExponentialHistogramCount(t, tt.exponentialHist()) // Sanity check. got, annots, err := exponentialToNativeHistogram(tt.exponentialHist()) if tt.wantErrMessage != "" { - assert.ErrorContains(t, err, tt.wantErrMessage) + require.ErrorContains(t, err, tt.wantErrMessage) return } require.NoError(t, err) require.Empty(t, annots) - assert.Equal(t, tt.wantNativeHist(), got) + require.Equal(t, tt.wantNativeHist(), got) validateNativeHistogramCount(t, got) }) } @@ -617,7 +616,7 @@ func validateNativeHistogramCount(t *testing.T, h prompb.Histogram) { prevBucket += delta actualCount += uint64(prevBucket) } - assert.Equal(t, want, actualCount, "native histogram count mismatch") + require.Equal(t, want, actualCount, "native histogram count mismatch") } func TestPrometheusConverter_addExponentialHistogramDataPoints(t *testing.T) { @@ -774,8 +773,8 @@ func TestPrometheusConverter_addExponentialHistogramDataPoints(t *testing.T) { require.NoError(t, err) require.Empty(t, annots) - assert.Equal(t, tt.wantSeries(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.wantSeries(), converter.unique) + require.Empty(t, converter.conflicts) }) } } @@ -814,7 +813,7 @@ func TestConvertExplicitHistogramBucketsToNHCBLayout(t *testing.T) { }, { name: "trailing empty buckets", - buckets: []uint64{0, 0, 1, 1, 2, 3, 0, 0}, //TODO: add tests for 3 trailing buckets + buckets: []uint64{0, 0, 1, 1, 2, 3, 0, 0}, // TODO: add tests for 3 trailing buckets wantLayout: expectedBucketLayout{ wantSpans: []prompb.BucketSpan{ { @@ -880,8 +879,8 @@ func TestConvertExplicitHistogramBucketsToNHCBLayout(t *testing.T) { bucketCounts := buckets[offset:] gotSpans, gotDeltas := convertBucketsLayout(bucketCounts, int32(offset), 0, false) - assert.Equal(t, tt.wantLayout.wantSpans, gotSpans) - assert.Equal(t, tt.wantLayout.wantDeltas, gotDeltas) + require.Equal(t, tt.wantLayout.wantSpans, gotSpans) + require.Equal(t, tt.wantLayout.wantDeltas, gotDeltas) }) } } @@ -975,13 +974,13 @@ func TestHistogramToCustomBucketsHistogram(t *testing.T) { validateHistogramCount(t, tt.hist()) got, annots, err := explicitHistogramToCustomBucketsHistogram(tt.hist()) if tt.wantErrMessage != "" { - assert.ErrorContains(t, err, tt.wantErrMessage) + require.ErrorContains(t, err, tt.wantErrMessage) return } require.NoError(t, err) require.Empty(t, annots) - assert.Equal(t, tt.wantNativeHist(), got) + require.Equal(t, tt.wantNativeHist(), got) validateNativeHistogramCount(t, got) }) } @@ -1143,8 +1142,8 @@ func TestPrometheusConverter_addCustomBucketsHistogramDataPoints(t *testing.T) { require.NoError(t, err) require.Empty(t, annots) - assert.Equal(t, tt.wantSeries(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.wantSeries(), converter.unique) + require.Empty(t, converter.conflicts) }) } } diff --git a/storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go b/storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go index 9c5b18682c..d9f433d713 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go @@ -72,12 +72,12 @@ func TestFromMetrics(t *testing.T) { ts := converter.TimeSeries() require.Len(t, ts, 1408+1) // +1 for the target_info. - target_info_count := 0 + tgtInfoCount := 0 for _, s := range ts { b := labels.NewScratchBuilder(2) lbls := s.ToLabels(&b, nil) if lbls.Get(labels.MetricName) == "target_info" { - target_info_count++ + tgtInfoCount++ require.Equal(t, "test-namespace/test-service", lbls.Get("job")) require.Equal(t, "id1234", lbls.Get("instance")) if keepIdentifyingResourceAttributes { @@ -91,7 +91,7 @@ func TestFromMetrics(t *testing.T) { } } } - require.Equal(t, 1, target_info_count) + require.Equal(t, 1, tgtInfoCount) }) } @@ -129,7 +129,7 @@ func TestFromMetrics(t *testing.T) { if convertHistogramsToNHCB { require.Len(t, series[0].Histograms, 1) - require.Len(t, series[0].Samples, 0) + require.Empty(t, series[0].Samples) } else { require.Len(t, series, 3) for i := range series { diff --git a/storage/remote/otlptranslator/prometheusremotewrite/number_data_points.go b/storage/remote/otlptranslator/prometheusremotewrite/number_data_points.go index 6cdab450e1..e89dfd9815 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/number_data_points.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/number_data_points.go @@ -29,7 +29,8 @@ import ( ) func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, dataPoints pmetric.NumberDataPointSlice, - resource pcommon.Resource, settings Settings, name string) error { + resource pcommon.Resource, settings Settings, name string, +) error { for x := 0; x < dataPoints.Len(); x++ { if err := c.everyN.checkContext(ctx); err != nil { return err @@ -65,7 +66,8 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data } func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPoints pmetric.NumberDataPointSlice, - resource pcommon.Resource, metric pmetric.Metric, settings Settings, name string) error { + resource pcommon.Resource, metric pmetric.Metric, settings Settings, name string, +) error { for x := 0; x < dataPoints.Len(); x++ { if err := c.everyN.checkContext(ctx); err != nil { return err diff --git a/storage/remote/otlptranslator/prometheusremotewrite/number_data_points_test.go b/storage/remote/otlptranslator/prometheusremotewrite/number_data_points_test.go index b01d2cb1fe..ca01a162ec 100644 --- a/storage/remote/otlptranslator/prometheusremotewrite/number_data_points_test.go +++ b/storage/remote/otlptranslator/prometheusremotewrite/number_data_points_test.go @@ -22,7 +22,7 @@ import ( "time" "github.com/prometheus/common/model" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" @@ -56,7 +56,8 @@ func TestPrometheusConverter_addGaugeNumberDataPoints(t *testing.T) { { Value: 1, Timestamp: convertTimeStamp(pcommon.Timestamp(ts)), - }}, + }, + }, }, } }, @@ -77,8 +78,8 @@ func TestPrometheusConverter_addGaugeNumberDataPoints(t *testing.T) { metric.Name(), ) - assert.Equal(t, tt.want(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.want(), converter.unique) + require.Empty(t, converter.conflicts) }) } } @@ -111,7 +112,8 @@ func TestPrometheusConverter_addSumNumberDataPoints(t *testing.T) { { Value: 1, Timestamp: convertTimeStamp(ts), - }}, + }, + }, }, } }, @@ -255,8 +257,8 @@ func TestPrometheusConverter_addSumNumberDataPoints(t *testing.T) { metric.Name(), ) - assert.Equal(t, tt.want(), converter.unique) - assert.Empty(t, converter.conflicts) + require.Equal(t, tt.want(), converter.unique) + require.Empty(t, converter.conflicts) }) } }