Skip to content

Commit

Permalink
update test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
huyan0 committed Aug 28, 2020
1 parent f2c76d5 commit 73f05c5
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 11 deletions.
14 changes: 10 additions & 4 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (prwe *prwExporter) shutdown(context.Context) error {
// pushMetrics converts metrics to Prometheus remote write TimeSeries and send to remote endpoint. It maintain a map of
// TimeSeries, validates and handles each individual metric, adding the converted TimeSeries to the map, and finally
// exports the map.
func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int, error) {
func (prwe *prwExporter) PushMetrics(ctx context.Context, md pdata.Metrics) (int, error) {
prwe.wg.Add(1)
defer prwe.wg.Done()
select {
Expand Down Expand Up @@ -159,7 +159,9 @@ func (prwe *prwExporter) handleScalarMetric(tsMap map[string]*prompb.TimeSeries,
}

for _, pt := range metric.Int64DataPoints {

if pt == nil {
continue
}
// create parameters for addSample
name := getPromMetricName(metric.GetMetricDescriptor(), prwe.namespace)
labels := createLabelSet(pt.GetLabels(), nameStr, name)
Expand All @@ -179,7 +181,9 @@ func (prwe *prwExporter) handleScalarMetric(tsMap map[string]*prompb.TimeSeries,
return fmt.Errorf("nil data point field in metric %v", metric.GetMetricDescriptor().Name)
}
for _, pt := range metric.DoubleDataPoints {

if pt == nil {
continue
}
// create parameters for addSample
name := getPromMetricName(metric.GetMetricDescriptor(), prwe.namespace)
labels := createLabelSet(pt.GetLabels(), nameStr, name)
Expand All @@ -205,7 +209,9 @@ func (prwe *prwExporter) handleHistogramMetric(tsMap map[string]*prompb.TimeSeri
}

for _, pt := range metric.HistogramDataPoints {

if pt == nil {
continue
}
time := convertTimeStamp(pt.TimeUnixNano)
mType := metric.GetMetricDescriptor().GetType()

Expand Down
67 changes: 62 additions & 5 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ func Test_handleScalarMetric(t *testing.T) {
true,
map[string]*prompb.TimeSeries{},
},
{
"int_nil_point",
&otlp.Metric{
MetricDescriptor: getDescriptor("int_nil_point", monotonicInt64Comb, validCombinations),
Int64DataPoints: []*otlp.Int64DataPoint{nil},
DoubleDataPoints: nil,
HistogramDataPoints: nil,
SummaryDataPoints: nil,
},
false,
map[string]*prompb.TimeSeries{},
},
{
"double_nil_point",
&otlp.Metric{
MetricDescriptor: getDescriptor("double_nil_point", monotonicInt64Comb, validCombinations),
Int64DataPoints: nil,
DoubleDataPoints: []*otlp.DoubleDataPoint{nil},
HistogramDataPoints: nil,
SummaryDataPoints: nil,
},
false,
map[string]*prompb.TimeSeries{},
},
{
"same_ts_int_points",
&otlp.Metric{
Expand Down Expand Up @@ -199,6 +223,18 @@ func Test_handleHistogramMetric(t *testing.T) {
true,
map[string]*prompb.TimeSeries{},
},
{
"hist_nil_pt",
otlp.Metric{
MetricDescriptor: getDescriptor("hist_nil_pt", histogramComb, validCombinations),
Int64DataPoints: nil,
DoubleDataPoints: nil,
HistogramDataPoints: []*otlp.HistogramDataPoint{nil},
SummaryDataPoints: nil,
},
false,
map[string]*prompb.TimeSeries{},
},
{
"single_histogram_point",
otlp.Metric{
Expand Down Expand Up @@ -316,7 +352,7 @@ func Test_shutdown(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
_, ok := prwe.pushMetrics(context.Background(),
_, ok := prwe.PushMetrics(context.Background(),
pdatautil.MetricsFromOldInternalMetrics(testdataold.GenerateMetricDataEmpty()))
errChan <- ok
}()
Expand Down Expand Up @@ -448,6 +484,7 @@ func Test_pushMetrics(t *testing.T) {
nilBatch2 := testdataold.GenerateMetricDataManyMetricsSameResource(10)
nilBatch3 := testdataold.GenerateMetricDataManyMetricsSameResource(10)
nilBatch4 := testdataold.GenerateMetricDataManyMetricsSameResource(10)
nilBatch5 := testdataold.GenerateMetricDataOneEmptyResourceMetrics()

setCumulative(&nilBatch1)
setCumulative(&nilBatch2)
Expand Down Expand Up @@ -511,6 +548,8 @@ func Test_pushMetrics(t *testing.T) {
}
summaryBatch := pdatautil.MetricsFromOldInternalMetrics(dataold.MetricDataFromOtlp(summary))

nilResourceBatch := pdatautil.MetricsFromOldInternalMetrics(nilBatch5)

tests := []struct {
name string
md *pdata.Metrics
Expand Down Expand Up @@ -538,6 +577,15 @@ func Test_pushMetrics(t *testing.T) {
pdatautil.MetricCount(nilDescBatch),
true,
},
{
"nil_resourece_case",
&nilResourceBatch,
nil,
0,
http.StatusAccepted,
pdatautil.MetricCount(nilResourceBatch),
false,
},
{
"nil_int_point_case",
&nilIntDataPointsBatch,
Expand Down Expand Up @@ -565,6 +613,15 @@ func Test_pushMetrics(t *testing.T) {
pdatautil.MetricCount(nilHistogramDataPointsBatch),
true,
},
{
"nil_histogram_point_case",
&nilHistogramDataPointsBatch,
nil,
0,
http.StatusAccepted,
pdatautil.MetricCount(nilHistogramDataPointsBatch),
true,
},
{
"no_temp_case",
&noTempBatch,
Expand Down Expand Up @@ -603,10 +660,10 @@ func Test_pushMetrics(t *testing.T) {
{"summary_case",
&summaryBatch,
checkFunc,
3,
http.StatusAccepted,
0,
false,
http.StatusAccepted,
pdatautil.MetricCount(summaryBatch),
true,
},
}

Expand Down Expand Up @@ -644,7 +701,7 @@ func Test_pushMetrics(t *testing.T) {
c := http.DefaultClient
prwe, nErr := newPrwExporter(config.Namespace, serverURL.String(), c)
require.NoError(t, nErr)
numDroppedTimeSeries, err := prwe.pushMetrics(context.Background(), *tt.md)
numDroppedTimeSeries, err := prwe.PushMetrics(context.Background(), *tt.md)
assert.Equal(t, tt.numDroppedTimeSeries, numDroppedTimeSeries)
if tt.returnErr {
assert.Error(t, err)
Expand Down
3 changes: 1 addition & 2 deletions exporter/prometheusremotewriteexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ func createMetricsExporter(_ context.Context, _ component.ExporterCreateParams,

prwexp, err := exporterhelper.NewMetricsExporter(
cfg,
prwe.pushMetrics,
prwe.PushMetrics,
exporterhelper.WithTimeout(prwCfg.TimeoutSettings),
exporterhelper.WithQueue(prwCfg.QueueSettings),
exporterhelper.WithRetry(prwCfg.RetrySettings),
exporterhelper.WithShutdown(prwe.shutdown),
)

return prwexp, err
}

Expand Down
131 changes: 131 additions & 0 deletions exporter/prometheusremotewriteexporter/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
mode: set
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:52.99,54.19 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:58.2,59.16 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:63.2,69.8 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:54.19,56.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:59.16,61.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:74.58,78.2 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:83.90,86.9 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:87.24,88.75 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:89.10,95.50 5 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:134.3,134.49 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:139.3,139.19 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:143.3,143.16 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:95.50,96.29 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:100.4,100.88 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:96.29,97.13 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:100.88,101.38 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:105.5,105.59 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:101.38,102.14 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:105.59,106.23 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:110.6,110.61 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:116.6,116.52 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:106.23,107.15 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:110.61,113.15 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:118.76,119.68 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:123.43,124.71 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:127.14,128.65 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:119.68,122.8 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:124.71,126.8 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:134.49,137.4 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:139.19,141.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:150.109,154.15 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:199.2,199.75 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:156.74,157.36 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:161.3,161.45 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:176.3,176.13 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:179.76,180.37 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:183.3,183.46 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:197.3,197.13 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:157.36,159.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:161.45,162.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:166.4,174.76 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:162.17,163.13 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:180.37,182.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:183.46,184.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:188.4,195.76 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:184.17,185.13 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:205.112,207.39 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:211.2,211.48 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:260.2,260.12 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:207.39,209.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:211.48,212.16 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:215.3,241.39 11 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:253.3,258.48 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:212.16,213.12 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:241.39,251.4 5 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:264.97,267.16 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:272.2,273.16 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:276.2,281.16 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:287.2,297.16 8 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:301.2,301.34 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:310.2,310.12 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:267.16,269.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:273.16,275.3 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:281.16,283.3 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:297.16,299.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:301.34,304.21 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:307.3,308.28 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/exporter.go:304.21,306.4 1 0
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:32.45,37.2 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:40.64,43.9 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:47.2,49.16 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:53.2,55.16 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:59.2,67.20 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:43.9,45.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:49.16,51.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:55.16,57.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/factory.go:70.50,94.2 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:46.42,46.59 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:47.42,47.74 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:48.42,48.69 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:51.56,53.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:57.2,57.24 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:65.2,65.14 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:53.17,55.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:59.66,60.67 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:61.65,62.14 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:71.33,73.52 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:77.2,80.8 3 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:73.52,75.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:80.8,82.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:82.8,88.3 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:95.87,101.29 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:108.2,108.19 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:101.29,106.3 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:114.87,119.28 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:126.2,126.38 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:145.2,147.23 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:151.2,151.10 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:119.28,124.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:126.38,127.25 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:130.3,131.12 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:135.3,136.73 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:139.3,142.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:127.25,128.9 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:131.12,133.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:136.73,138.4 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:147.23,149.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:156.71,158.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:162.2,169.17 4 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:172.2,183.30 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:187.2,187.29 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:158.17,160.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:169.17,171.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:183.30,186.3 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:192.88,193.21 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:196.2,197.26 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:200.2,204.22 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:193.21,195.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:197.26,199.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:208.47,210.2 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:214.32,215.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:222.2,223.33 2 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:226.2,226.17 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:229.2,229.10 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:215.17,217.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:223.33,225.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:226.17,228.3 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:234.32,235.47 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:239.2,239.12 1 1
go.opentelemetry.io/collector/exporter/prometheusremotewriteexporter/helper.go:235.47,237.3 1 1

0 comments on commit 73f05c5

Please sign in to comment.