Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Scraper test functionality to seperate package #5755

Merged
merged 23 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/scrapertest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
25 changes: 25 additions & 0 deletions internal/scrapertest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest

go 1.17

require (
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/collector/model v0.36.1-0.20211004155959-190f8fbb2b9a
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/scraperhelper => ../../receiver/scraperhelper
186 changes: 186 additions & 0 deletions internal/scrapertest/go.sum

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions internal/scrapertest/scrapertest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scrapertest

import (
"encoding/json"
"fmt"
"io/ioutil"

"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/model/pdata"
)

func FileToMetrics(filePath string) (pdata.Metrics, error) {
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
expectedFileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return pdata.Metrics{}, err
}
unmarshaller := otlp.NewJSONMetricsUnmarshaler()
return unmarshaller.UnmarshalMetrics(expectedFileBytes)
}

func MetricsToFile(filePath string, metrics pdata.Metrics) error {
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
bytes, err := otlp.NewJSONMetricsMarshaler().MarshalMetrics(metrics)
if err != nil {
return err
}
var jsonVal map[string]interface{}
json.Unmarshal(bytes, &jsonVal)
b, err := json.MarshalIndent(jsonVal, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filePath, b, 0600)
}
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

// CompareMetrics compares each part of two given metric slices and returns
// an error if they don't match. The error describes what didn't match.
func CompareMetricSlices(expectedAll, actualAll pdata.MetricSlice) error {
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
if actualAll.Len() != expectedAll.Len() {
return fmt.Errorf("metric slices not of same length")
}

lessFunc := func(a, b pdata.Metric) bool {
return a.Name() < b.Name()
}

actualMetrics := actualAll.Sort(lessFunc)
expectedMetrics := expectedAll.Sort(lessFunc)

for i := 0; i < actualMetrics.Len(); i++ {
actual := actualMetrics.At(i)
expected := expectedMetrics.At(i)
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

if actual.Name() != expected.Name() {
return fmt.Errorf("metric name does not match expected: %s, actual: %s", expected.Name(), actual.Name())
}
if actual.DataType() != expected.DataType() {
return fmt.Errorf("metric datatype does not match expected: %s, actual: %s", expected.DataType(), actual.DataType())
}
if actual.Description() != expected.Description() {
return fmt.Errorf("metric description does not match expected: %s, actual: %s", expected.Description(), actual.Description())
}
if actual.Unit() != expected.Unit() {
return fmt.Errorf("metric Unit does not match expected: %s, actual: %s", expected.Unit(), actual.Unit())
}

var actualDataPoints pdata.NumberDataPointSlice
var expectedDataPoints pdata.NumberDataPointSlice

switch actual.DataType() {
case pdata.MetricDataTypeGauge:
actualDataPoints = actual.Gauge().DataPoints()
expectedDataPoints = expected.Gauge().DataPoints()
case pdata.MetricDataTypeSum:
if actual.Sum().AggregationTemporality() != expected.Sum().AggregationTemporality() {
return fmt.Errorf("metric AggregationTemporality does not match expected: %s, actual: %s", expected.Sum().AggregationTemporality(), actual.Sum().AggregationTemporality())
}
if actual.Sum().IsMonotonic() != expected.Sum().IsMonotonic() {
return fmt.Errorf("metric IsMonotonic does not match expected: %t, actual: %t", expected.Sum().IsMonotonic(), actual.Sum().IsMonotonic())
}
actualDataPoints = actual.Sum().DataPoints()
expectedDataPoints = expected.Sum().DataPoints()
}

if actualDataPoints.Len() != expectedDataPoints.Len() {
return fmt.Errorf("length of datapoints don't match, metric name: %s", actual.Name())
}

dataPointMatches := 0
for j := 0; j < expectedDataPoints.Len(); j++ {
edp := expectedDataPoints.At(j)
for k := 0; k < actualDataPoints.Len(); k++ {
adp := actualDataPoints.At(k)
adpAttributes := adp.Attributes()
labelMatches := true
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

if edp.Attributes().Len() != adpAttributes.Len() {
break
}
edp.Attributes().Range(func(k string, v pdata.AttributeValue) bool {
if attributeVal, ok := adpAttributes.Get(k); ok && attributeVal.StringVal() == v.StringVal() {
return true
}
labelMatches = false
return false
})
if !labelMatches {
continue
}
if edp.Type() != adp.Type() {
return fmt.Errorf("metric datapoint types don't match: expected type: %v, actual type: %v", edp.Type(), adp.Type())
}
if edp.IntVal() != adp.IntVal() {
return fmt.Errorf("metric datapoint IntVal doesn't match expected: %d, actual: %d", edp.IntVal(), adp.IntVal())
}
if edp.DoubleVal() != adp.DoubleVal() {
return fmt.Errorf("metric datapoint DoubleVal doesn't match expected: %f, actual: %f", edp.DoubleVal(), adp.DoubleVal())
}
dataPointMatches++
break
}
}
if dataPointMatches != expectedDataPoints.Len() {
return fmt.Errorf("missing datapoints for: metric name: %s", actual.Name())
}
}
return nil
}
214 changes: 214 additions & 0 deletions internal/scrapertest/scrapertest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scrapertest

import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/model/pdata"
)

func baseTestMetrics() pdata.MetricSlice {
slice := pdata.NewMetricSlice()
metric := slice.AppendEmpty()
metric.SetDataType(pdata.MetricDataTypeGauge)
metric.SetDescription("test description")
metric.SetName("test name")
metric.SetUnit("test unit")
dps := metric.Gauge().DataPoints()
dp := dps.AppendEmpty()
dp.SetDoubleVal(1)
dp.SetTimestamp(pdata.NewTimestampFromTime(time.Time{}))
attributes := pdata.NewAttributeMap()
attributes.Insert("testKey1", pdata.NewAttributeValueString("teststringvalue1"))
attributes.CopyTo(dp.Attributes())
return slice
}
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

// TestCompareMetrics tests the ability of comparing one metric slice to another
func TestCompareMetrics(t *testing.T) {
tcs := []struct {
name string
expected pdata.MetricSlice
actual pdata.MetricSlice
expectedError error
}{
{
name: "Equal MetricSlice",
actual: baseTestMetrics(),
expected: baseTestMetrics(),
expectedError: nil,
},
{
name: "Wrong DataType",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
m.SetDataType(pdata.MetricDataTypeSum)
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric datatype does not match expected: Gauge, actual: Sum"),
},
{
name: "Wrong Name",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
m.SetName("wrong name")
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric name does not match expected: test name, actual: wrong name"),
},
{
name: "Wrong Description",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
m.SetDescription("wrong description")
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric description does not match expected: test description, actual: wrong description"),
},
{
name: "Wrong Unit",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
m.SetUnit("Wrong Unit")
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric Unit does not match expected: test unit, actual: Wrong Unit"),
},
{
name: "Wrong doubleVal",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
dp := m.Gauge().DataPoints().At(0)
dp.SetDoubleVal(2)
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric datapoint DoubleVal doesn't match expected: 1.000000, actual: 2.000000"),
},
{
name: "Wrong datatype",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
dp := m.Gauge().DataPoints().At(0)
dp.SetDoubleVal(0)
dp.SetIntVal(2)
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("metric datapoint types don't match: expected type: 2, actual type: 1"),
},
{
name: "Wrong attribute key",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
dp := m.Gauge().DataPoints().At(0)
attributes := pdata.NewAttributeMap()
attributes.Insert("wrong key", pdata.NewAttributeValueString("teststringvalue1"))
dp.Attributes().Clear()
attributes.CopyTo(dp.Attributes())
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("missing datapoints for: metric name: test name"),
},
{
name: "Wrong attribute value",
actual: func() pdata.MetricSlice {
metrics := baseTestMetrics()
m := metrics.At(0)
dp := m.Gauge().DataPoints().At(0)
attributes := pdata.NewAttributeMap()
attributes.Insert("testKey1", pdata.NewAttributeValueString("wrong value"))
dp.Attributes().Clear()
attributes.CopyTo(dp.Attributes())
return metrics
}(),
expected: baseTestMetrics(),
expectedError: fmt.Errorf("missing datapoints for: metric name: test name"),
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := CompareMetricSlices(tc.expected, tc.actual)
if tc.expectedError != nil {
require.Equal(t, tc.expectedError, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestRoundTrip(t *testing.T) {
metricslice := baseTestMetrics()
expectedMetrics := pdata.NewMetrics()
metricslice.CopyTo(expectedMetrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

tempDir := filepath.Join(t.TempDir(), "metrics.json")
err := MetricsToFile(tempDir, expectedMetrics)
require.NoError(t, err)

actualMetrics, err := FileToMetrics(tempDir)
require.NoError(t, err)
require.Equal(t, expectedMetrics, actualMetrics)
}

func TestMetricsToFile(t *testing.T) {
metricslice := baseTestMetrics()
metrics := pdata.NewMetrics()
metricslice.CopyTo(metrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

tempDir := filepath.Join(t.TempDir(), "metrics.json")
MetricsToFile(tempDir, metrics)

actualBytes, err := ioutil.ReadFile(tempDir)
require.NoError(t, err)

expectedFile := filepath.Join("testdata", "roundtrip", "expected.json")
expectedBytes, err := ioutil.ReadFile(expectedFile)
require.NoError(t, err)

require.Equal(t, expectedBytes, actualBytes)
}

func TestFileToMetrics(t *testing.T) {
metricslice := baseTestMetrics()
expectedMetrics := pdata.NewMetrics()
metricslice.CopyTo(expectedMetrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

expectedFile := filepath.Join("testdata", "roundtrip", "expected.json")
actualMetrics, err := FileToMetrics(expectedFile)
require.NoError(t, err)
require.Equal(t, expectedMetrics, actualMetrics)
}
Loading