diff --git a/exporter/prometheusremotewriteexporter/exporter.go b/exporter/prometheusremotewriteexporter/exporter.go index 64084ebd24b..bc6ecdbb3ed 100644 --- a/exporter/prometheusremotewriteexporter/exporter.go +++ b/exporter/prometheusremotewriteexporter/exporter.go @@ -105,6 +105,7 @@ func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int case otlp.MetricDescriptor_MONOTONIC_INT64, otlp.MetricDescriptor_INT64, otlp.MetricDescriptor_MONOTONIC_DOUBLE, otlp.MetricDescriptor_DOUBLE: if err := prwe.handleScalarMetric(tsMap, metric); err != nil { + dropped++ errs = append(errs, err.Error()) } } @@ -220,7 +221,7 @@ func (prwe *prwExporter) export(ctx context.Context, tsMap map[string]*prompb.Ti if scanner.Scan() { line = scanner.Text() } - err = errors.Errorf("server returned HTTP status %s: %s", httpResp.Status, line) + return errors.Errorf("server returned HTTP status %s: %s", httpResp.Status, line) } - return err + return nil } diff --git a/exporter/prometheusremotewriteexporter/exporter_test.go b/exporter/prometheusremotewriteexporter/exporter_test.go index d9f5ef9e3ac..66d8390a4e3 100644 --- a/exporter/prometheusremotewriteexporter/exporter_test.go +++ b/exporter/prometheusremotewriteexporter/exporter_test.go @@ -35,6 +35,7 @@ import ( "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/consumer/pdatautil" "go.opentelemetry.io/collector/exporter/exporterhelper" + "go.opentelemetry.io/collector/internal/data" otlp "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1" "go.opentelemetry.io/collector/internal/data/testdata" ) @@ -322,27 +323,38 @@ func runExportPipeline(t *testing.T, ts *prompb.TimeSeries, endpoint *url.URL) e // Test_pushMetrics checks the number of TimeSeries received by server and the number of metrics dropped is the same as // expected func Test_pushMetrics(t *testing.T) { + // fail cases noTempBatch := pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataManyMetricsSameResource(10)) invalidTypeBatch := pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataMetricTypeInvalid()) - nilDescBatch := pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataNilMetricDescriptor()) - // 10 counter metrics, 2 points in each. Two TimeSeries in total - batch := testdata.GenerateMetricDataManyMetricsSameResource(10) - setCumulative(&batch) - scalarBatch := pdatautil.MetricsFromInternalMetrics(batch) + invalidTemp := testdata.GenerateMetricDataManyMetricsSameResource(10) + setTemporality(&invalidTemp,otlp.MetricDescriptor_INVALID_TEMPORALITY) + invalidTempBatch := pdatautil.MetricsFromInternalMetrics(invalidTemp) + nilDescBatch := pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataNilMetricDescriptor()) nilBatch1 := testdata.GenerateMetricDataManyMetricsSameResource(10) nilBatch2 := testdata.GenerateMetricDataManyMetricsSameResource(10) - setCumulative(&nilBatch1) - setCumulative(&nilBatch2) - + setTemporality(&nilBatch1,otlp.MetricDescriptor_CUMULATIVE) + setTemporality(&nilBatch2,otlp.MetricDescriptor_CUMULATIVE) setDataPointToNil(&nilBatch1, typeMonotonicInt64) setType(&nilBatch2, typeMonotonicDouble) nilIntDataPointsBatch := pdatautil.MetricsFromInternalMetrics(nilBatch1) nilDoubleDataPointsBatch := pdatautil.MetricsFromInternalMetrics(nilBatch2) + // Success cases: 10 counter metrics, 2 points in each. Two TimeSeries in total + batch1 := testdata.GenerateMetricDataManyMetricsSameResource(10) + setTemporality(&batch1,otlp.MetricDescriptor_CUMULATIVE) + scalarBatch := pdatautil.MetricsFromInternalMetrics(batch1) + + // Partial Success cases + batch2 := testdata.GenerateMetricDataManyMetricsSameResource(10) + setTemporality(&batch2,otlp.MetricDescriptor_CUMULATIVE) + failDesc := data.MetricDataToOtlp(batch2)[0].InstrumentationLibraryMetrics[0].Metrics[0].GetMetricDescriptor() + failDesc.Temporality = otlp.MetricDescriptor_INVALID_TEMPORALITY + partialBatch := pdatautil.MetricsFromInternalMetrics(batch2) + checkFunc := func(t *testing.T, r *http.Request, expected int) { body, err := ioutil.ReadAll(r.Body) if err != nil { @@ -365,7 +377,7 @@ func Test_pushMetrics(t *testing.T) { name string md *pdata.Metrics reqTestFunc func(t *testing.T, r *http.Request, expected int) - expected int + expectedTimeSeries int httpResponseCode int numDroppedTimeSeries int returnErr bool @@ -379,6 +391,15 @@ func Test_pushMetrics(t *testing.T) { pdatautil.MetricCount(invalidTypeBatch), true, }, + { + "invalid_temporality_case", + &invalidTempBatch, + nil, + 0, + http.StatusAccepted, + pdatautil.MetricCount(invalidTempBatch), + true, + }, { "nil_desc_case", &nilDescBatch, @@ -433,13 +454,22 @@ func Test_pushMetrics(t *testing.T) { 0, false, }, + { + "partial_success_case", + &partialBatch, + checkFunc, + 2, + http.StatusAccepted, + 1, + true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if tt.reqTestFunc != nil { - tt.reqTestFunc(t, r, tt.expected) + tt.reqTestFunc(t, r, tt.expectedTimeSeries) } w.WriteHeader(tt.httpResponseCode) })) diff --git a/exporter/prometheusremotewriteexporter/helper.go b/exporter/prometheusremotewriteexporter/helper.go index 89bad53f659..25e295f2cdb 100644 --- a/exporter/prometheusremotewriteexporter/helper.go +++ b/exporter/prometheusremotewriteexporter/helper.go @@ -186,7 +186,7 @@ func getPromMetricName(desc *otlp.MetricDescriptor, ns string) string { // and creates a WriteRequest from the struct -- can move to the helper.go file func wrapTimeSeries(tsMap map[string]*prompb.TimeSeries) (*prompb.WriteRequest, error) { if len(tsMap) == 0 { - return nil, errors.Errorf("invalid TsMap: cannot be empty map") + return nil, errors.Errorf("invalid tsMap: cannot be empty map") } TsArray := []prompb.TimeSeries{} for _, v := range tsMap { diff --git a/exporter/prometheusremotewriteexporter/out b/exporter/prometheusremotewriteexporter/out new file mode 100644 index 00000000000..c66340e6ab5 --- /dev/null +++ b/exporter/prometheusremotewriteexporter/out @@ -0,0 +1,109 @@ +mode: set +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:50.99,52.19 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:56.2,57.16 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:61.2,67.8 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:52.19,54.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:57.16,59.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:72.58,76.2 3 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:81.90,84.9 3 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:85.24,86.78 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:87.10,93.50 5 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:118.3,118.49 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:122.3,122.19 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:126.3,126.16 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:93.50,95.88 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:95.88,97.59 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:97.59,99.61 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:106.6,106.52 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:99.61,103.15 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:108.76,109.68 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:109.68,112.8 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:118.49,120.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:122.19,124.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:133.109,137.15 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:179.2,179.78 1 0 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:139.74,140.36 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:144.3,144.45 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:157.3,157.13 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:160.76,161.37 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:164.3,164.46 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:176.3,176.13 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:140.36,142.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:144.45,156.4 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:161.37,163.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:164.46,175.4 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:183.97,186.16 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:191.2,192.16 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:195.2,200.16 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:206.2,216.16 8 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:220.2,220.34 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:228.2,228.12 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:186.16,188.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:192.16,194.3 1 0 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:200.16,202.3 1 0 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:216.16,218.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:220.34,223.21 3 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:226.3,226.84 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:223.21,225.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:33.45,38.2 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:41.64,44.9 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:48.2,50.16 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:54.2,56.16 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:60.2,69.20 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:44.9,46.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:50.16,52.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:56.16,58.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:72.50,95.2 3 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:41.42,41.59 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:42.42,42.74 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:43.42,43.69 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:46.56,48.17 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:52.2,52.24 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:60.2,60.14 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:48.17,50.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:54.66,55.67 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:56.65,57.14 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:66.33,68.52 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:72.2,75.8 3 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:68.52,70.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:75.8,77.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:77.8,83.3 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:90.87,96.29 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:103.2,103.19 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:96.29,101.3 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:109.87,114.28 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:121.2,121.38 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:140.2,142.23 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:146.2,146.10 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:114.28,119.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:121.38,122.25 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:125.3,126.12 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:130.3,131.73 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:134.3,137.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:122.25,123.9 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:126.12,128.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:131.73,133.4 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:142.23,144.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:151.71,153.17 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:157.2,164.17 4 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:167.2,178.30 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:182.2,182.29 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:153.17,155.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:164.17,166.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:178.30,181.3 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:187.88,188.21 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:191.2,192.26 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:195.2,199.22 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:188.21,190.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:192.26,194.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:203.47,205.2 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:209.32,210.17 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:217.2,218.33 2 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:221.2,221.17 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:224.2,224.10 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:210.17,212.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:218.33,220.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:221.17,223.3 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:229.32,230.47 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:234.2,234.12 1 1 +go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:230.47,232.3 1 1 diff --git a/exporter/prometheusremotewriteexporter/testutil_test.go b/exporter/prometheusremotewriteexporter/testutil_test.go index 9db7da773d1..dd579bb80d7 100644 --- a/exporter/prometheusremotewriteexporter/testutil_test.go +++ b/exporter/prometheusremotewriteexporter/testutil_test.go @@ -189,11 +189,11 @@ func getTimeSeries(labels []prompb.Label, samples ...prompb.Sample) *prompb.Time } //setCumulative is for creating the data.MetricData to test with -func setCumulative(metricsData *data.MetricData) { +func setTemporality(metricsData *data.MetricData, temp otlp.MetricDescriptor_Temporality) { for _, r := range data.MetricDataToOtlp(*metricsData) { for _, instMetrics := range r.InstrumentationLibraryMetrics { for _, m := range instMetrics.Metrics { - m.MetricDescriptor.Temporality = otlp.MetricDescriptor_CUMULATIVE + m.MetricDescriptor.Temporality = temp } } }