Skip to content

Commit

Permalink
remove resource_metrics, fully move to upstream testing
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jan 15, 2025
1 parent c3e6a70 commit f056cb9
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 285 deletions.
51 changes: 1 addition & 50 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ provides a set of integration tests and associated utilities. The general testi
is:

1. Building the Collector (`make otelcol` or `make all`)
1. Defining your expected [resource metric content](./testutils/README.md#resource-metrics) as a yaml file
([see example](testutils/telemetry/testdata/metrics/resource-metrics.yaml))
1. Defining [your expected golden file content as a yaml file](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/golden)
1. Spin up your target resources as [docker containers](./testutils/README.md#test-containers).
1. Stand up an in-memory [OTLP metrics receiver and sink](./testutils/README.md#otlp-metrics-receiver-sink) capable of detecting if/when desired data are received.
1. Spin up your Collector [as a subprocess](./testutils/README.md#collector-process) or [as a container](./testutils/README.md#collector-container) configured to report to this OTLP receiver.
Expand All @@ -17,51 +16,3 @@ is more useful overall.
**NOTE** At this time, integration tests generally target collector containers (`SPLUNK_OTEL_COLLECTOR_IMAGE` env var),
and test coverage for the subprocess is best effort only, unless the test cases explicitly maintain one.
The collector process targets are generally for test development without requiring frequent rebuilds of a local docker image.

```go
package example_test

import (
"context"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/signalfx/splunk-otel-collector/tests/testutils"
"github.com/signalfx/splunk-otel-collector/tests/testutils/telemetry"
)

func TestMyExampleComponent(t *testing.T) {
expectedResourceMetrics, err := telemetry.LoadResourceMetrics(
filepath.Join(".", "testdata", "metrics", "my_resource_metrics.yaml"),
)
require.NoError(t, err)
require.NotNil(t, expectedResourceMetrics)

// combination OTLP Receiver, consumertests.MetricsSink, and consumertests.LogsSink
otlp, err := testutils.NewOTLPReceiverSink().WithEndpoint("localhost:23456").Build()
require.NoError(t, err)
require.NoError(t, otlp.Start())

defer func() {
require.NoError(t, otlp.Shutdown())
}()

myContainer := testutils.NewContainer().WithImage("someTarget").Build()
err = myContainer.Start(context.Background())
require.NoError(t, err)

// running collector subprocess that uses the provided config set to export OTLP to our test receiver
myCollector, err := testutils.NewCollectorProcess().WithConfigPath(filepath.Join(".", "testdata", "config.yaml")).Build()
require.NoError(t, err)
err = myCollector.Start()
require.NoError(t, err)
defer func() {
require.NoError(t, myCollector.Shutdown() )
}()

require.NoError(t, otlp.AssertAllMetricsReceived(t, *expectedResourceMetrics, 30*time.Second))
}
```
2 changes: 0 additions & 2 deletions tests/general/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestSpecifiedContainerConfigDefaultsToCmdLineArgIfEnvVarConflict(t *testing
return false
}
receivedOTLPMetrics := tc.OTLPReceiverSink.AllMetrics()
tc.OTLPReceiverSink.Reset()

for _, rom := range receivedOTLPMetrics {
for i := 0; i < rom.ResourceMetrics().Len(); i++ {
Expand Down Expand Up @@ -189,7 +188,6 @@ service:
return false
}
receivedOTLPMetrics := tc.OTLPReceiverSink.AllMetrics()
tc.OTLPReceiverSink.Reset()

for _, rom := range receivedOTLPMetrics {
for i := 0; i < rom.ResourceMetrics().Len(); i++ {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions tests/receivers/smartagent/jmx/testdata/resource_metrics/all.yaml

This file was deleted.

72 changes: 13 additions & 59 deletions tests/testutils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,6 @@
The `testutils` package provides an internal test format for Collector data, and helpers to help assert its integrity
from arbitrary components.

### Resource Metrics

`ResourceMetrics` are at the core of the internal metric data format for these tests and are intended to be defined
in yaml files or by converting from obtained `pdata.Metrics` items. They provide a strict `Equals()` helper method as
well as `RelaxedEquals()` to help in verifying desired field and values, ignoring those not specified.

```yaml
resource_metrics:
- attributes:
a_resource_attribute: a_value
another_resource_attribute: another_value
scope_metrics:
- instrumentation_scope:
name: a library
version: some version
- metrics:
- name: my.int.gauge
type: IntGauge
description: my.int.gauge description
unit: ms
attributes:
my_attribute: my_attribute_value
my_other_attribute: my_other_attribute_value
value: 123
- name: my.double.sum
type: DoubleNonmonotonicDeltaSum
attributes: {} # enforced empty attribute map in RelaxedEquals() (only point with no attributes matches)
value: -123.456
- scope_metrics:
- instrumentation_scope:
name: an instrumentation library from a different resource without attributes
metrics:
- name: my.double.gauge
type: DoubleGauge
# missing attributes field, so values are not compared in RelaxedEquals() (any attribute values are accepted)
value: 456.789
- name: my.double.gauge
type: DoubleGauge
attributes:
another: attribute
value: 567.890
- instrumentation_scope:
name: another instrumentation library
version: this_library_version
metrics:
- name: another_int_gauge
type: IntGauge
value: 456
```
Using `telemetry.LoadResourceMetrics("my_yaml.path")` you can create an equivalent `ResourceMetrics` instance to what your yaml file specifies.
Using `telemetry.PDataToResourceMetrics(myReceivedPDataMetrics)` you can use the assertion helpers to determine if your expected
`ResourceMetrics` are the same as those received in your test case. `telemetry.FlattenResourceMetrics()` is a good way to "normalize"
metrics received over time to ensure that only unique datapoints are represented, and that all unique Resources and
Instrumentation Libraries have a single item.

### Test Containers

The Testcontainers project is a popular testing resource for easy container creation and usage for a number of languages
Expand Down Expand Up @@ -178,9 +122,19 @@ func MyTest(t *testing.T) {
}

// will implicitly create a Testcase with OTLPReceiverSink listening at $OTLP_ENDPOINT,
// ./testdata/resource_metrics/my_resource_metrics.yaml ResourceMetrics instance, CollectorProcess with
// ./testdata/expected.yaml golden file, CollectorProcess with
// ./testdata/my_collector_config.yaml config, and build and start all specified containers before calling
// OTLPReceiverSink.AssertAllMetricsReceived() with a 30s wait duration.
testutils.AssertAllMetricsReceived(t, "my_resource_metrics.yaml", "my_collector_config.yaml", containers, nil)
testutils.RunMetricsCollectionTest(t, "my_collector_config.yaml", "expected.yaml",
testutils.WithCompareMetricsOptions(
pmetrictest.IgnoreScopeVersion(),
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreMetricValues(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
),
)
}
```
12 changes: 0 additions & 12 deletions tests/testutils/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"runtime"
"strings"
"testing"

"github.com/signalfx/splunk-otel-collector/tests/testutils/telemetry"
)

type CollectorBuilder func(Collector) Collector
Expand Down Expand Up @@ -75,16 +73,6 @@ func (t *Testcase) setOTLPEndpoint() {
t.OTLPEndpointForCollector = t.OTLPEndpoint
}

// Loads and validates a ResourceMetrics instance, assuming it's located in ./testdata/resource_metrics
func (t *Testcase) ResourceMetrics(filename string) *telemetry.ResourceMetrics {
expectedResourceMetrics, err := telemetry.LoadResourceMetrics(
path.Join(".", "testdata", "resource_metrics", filename),
)
require.NoError(t, err)
require.NotNil(t, expectedResourceMetrics)
return expectedResourceMetrics
}

// Builds and starts all provided Container builder instances, returning them and a validating stop function.
func (t *Testcase) Containers(builders ...Container) (containers []*Container, stop func()) {
for _, builder := range builders {
Expand Down

0 comments on commit f056cb9

Please sign in to comment.