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 procstat test to golden files #4536

Merged
merged 2 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,13 +17,71 @@
package tests

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

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"

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

func TestTelegrafProcstatReceiverProvidesAllMetrics(t *testing.T) {
// collector is monitoring itself so use container for env consistency
testutils.SkipIfNotContainerTest(t)
testutils.AssertAllMetricsReceived(t, "all.yaml", "all_metrics_config.yaml", nil, nil)
checkGoldenFile(t, "all_metrics_config.yaml", "expected_all.yaml",
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreMetricValues())
}

func checkGoldenFile(t *testing.T, configFile string, expectedFilePath string, options ...pmetrictest.CompareMetricsOption) {
f := otlpreceiver.NewFactory()
port := testutils.GetAvailablePort(t)
c := f.CreateDefaultConfig().(*otlpreceiver.Config)
c.GRPC.NetAddr.Endpoint = fmt.Sprintf("localhost:%d", port)
sink := &consumertest.MetricsSink{}
receiver, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, sink)
require.NoError(t, err)
require.NoError(t, receiver.Start(context.Background(), componenttest.NewNopHost()))
t.Cleanup(func() {
require.NoError(t, receiver.Shutdown(context.Background()))
})
logger, _ := zap.NewDevelopment()

dockerHost := "0.0.0.0"
if runtime.GOOS == "darwin" {
dockerHost = "host.docker.internal"
}
p, err := testutils.NewCollectorContainer().
WithConfigPath(filepath.Join("testdata", configFile)).
WithLogger(logger).
WithEnv(map[string]string{"OTLP_ENDPOINT": fmt.Sprintf("%s:%d", dockerHost, port)}).
Build()
require.NoError(t, err)
require.NoError(t, p.Start())
t.Cleanup(func() {
require.NoError(t, p.Shutdown())
})

expected, err := golden.ReadMetrics(filepath.Join("testdata", expectedFilePath))
require.NoError(t, err)

assert.EventuallyWithT(t, func(tt *assert.CollectT) {
if len(sink.AllMetrics()) == 0 {
assert.Fail(tt, "No metrics collected")
return
}
err := pmetrictest.CompareMetrics(expected, sink.AllMetrics()[len(sink.AllMetrics())-1], options...)
assert.NoError(tt, err)
}, 30*time.Second, 1*time.Second)
}
Expand Up @@ -2,7 +2,11 @@ receivers:
smartagent/telegraf_procstat:
type: telegraf/procstat
exe: "otelcol"

processors:
batch:
groupbyattrs:
keys:
- exe
exporters:
otlp:
endpoint: "${OTLP_ENDPOINT}"
Expand All @@ -14,4 +18,5 @@ service:
metrics:
receivers:
- smartagent/telegraf_procstat
processors: [batch, groupbyattrs]
exporters: [otlp]