From bd377105a5d728def070a36cf70b5ff30b02968f Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Mon, 25 Mar 2024 23:10:47 -0700 Subject: [PATCH] Move Tomcat test to use metric presence check --- .../testdata/default_metrics_config.yaml | 7 +- .../testdata/resource_metrics/default.yaml | 35 -------- .../smartagent/collectd-tomcat/tomcat_test.go | 89 ++++++++++++++++++- 3 files changed, 92 insertions(+), 39 deletions(-) delete mode 100644 tests/receivers/smartagent/collectd-tomcat/testdata/resource_metrics/default.yaml diff --git a/tests/receivers/smartagent/collectd-tomcat/testdata/default_metrics_config.yaml b/tests/receivers/smartagent/collectd-tomcat/testdata/default_metrics_config.yaml index e037c230c6..96221fda15 100644 --- a/tests/receivers/smartagent/collectd-tomcat/testdata/default_metrics_config.yaml +++ b/tests/receivers/smartagent/collectd-tomcat/testdata/default_metrics_config.yaml @@ -4,7 +4,11 @@ receivers: host: localhost port: 5000 intervalSeconds: 1 - +processors: + batch: + groupbyattrs: + keys: + - host exporters: otlp: endpoint: "${OTLP_ENDPOINT}" @@ -16,4 +20,5 @@ service: metrics: receivers: - smartagent/collectd_tomcat + processors: [batch, groupbyattrs] exporters: [otlp] diff --git a/tests/receivers/smartagent/collectd-tomcat/testdata/resource_metrics/default.yaml b/tests/receivers/smartagent/collectd-tomcat/testdata/resource_metrics/default.yaml deleted file mode 100644 index b22703b0db..0000000000 --- a/tests/receivers/smartagent/collectd-tomcat/testdata/resource_metrics/default.yaml +++ /dev/null @@ -1,35 +0,0 @@ -resource_metrics: - - scope_metrics: - - metrics: - - name: counter.tomcat.GlobalRequestProcessor.bytesReceived - type: IntMonotonicCumulativeSum - - name: counter.tomcat.GlobalRequestProcessor.bytesSent - type: IntMonotonicCumulativeSum - - name: counter.tomcat.GlobalRequestProcessor.errorCount - type: IntMonotonicCumulativeSum - - name: counter.tomcat.GlobalRequestProcessor.processingTime - type: IntMonotonicCumulativeSum - - name: counter.tomcat.GlobalRequestProcessor.requestCount - type: IntMonotonicCumulativeSum - - name: gauge.tomcat.GlobalRequestProcessor.maxTime - type: IntGauge - - name: gauge.tomcat.ThreadPool.currentThreadsBusy - type: IntGauge - - name: gauge.tomcat.ThreadPool.maxThreads - type: IntGauge - - name: gauge.loaded_classes - type: IntGauge - - name: jmx_memory.init - type: IntGauge - - name: jmx_memory.max - type: IntGauge - - name: jmx_memory.used - type: IntGauge - - name: gauge.jvm.threads.count - type: IntGauge - - name: invocations - type: IntMonotonicCumulativeSum - - name: jmx_memory.committed - type: IntGauge - - name: total_time_in_ms.collection_time - type: IntMonotonicCumulativeSum diff --git a/tests/receivers/smartagent/collectd-tomcat/tomcat_test.go b/tests/receivers/smartagent/collectd-tomcat/tomcat_test.go index 297967af7e..7f5d5ac741 100644 --- a/tests/receivers/smartagent/collectd-tomcat/tomcat_test.go +++ b/tests/receivers/smartagent/collectd-tomcat/tomcat_test.go @@ -16,13 +16,96 @@ package tests import ( + "context" + "fmt" + "path/filepath" + "runtime" "testing" + "time" + + "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 TestCollectdTomcatReceiverProvidesDefaultMetrics(t *testing.T) { - testutils.AssertAllMetricsReceived( - t, "default.yaml", "default_metrics_config.yaml", nil, nil, - ) + checkMetricsPresence(t, []string{ + "counter.tomcat.GlobalRequestProcessor.bytesReceived", + "counter.tomcat.GlobalRequestProcessor.bytesSent", + "counter.tomcat.GlobalRequestProcessor.errorCount", + "counter.tomcat.GlobalRequestProcessor.processingTime", + "counter.tomcat.GlobalRequestProcessor.requestCount", + "gauge.tomcat.GlobalRequestProcessor.maxTime", + "gauge.tomcat.ThreadPool.currentThreadsBusy", + "gauge.tomcat.ThreadPool.maxThreads", + "gauge.loaded_classes", + "jmx_memory.init", + "jmx_memory.max", + "jmx_memory.used", + "gauge.jvm.threads.count", + "invocations", + "jmx_memory.committed", + "total_time_in_ms.collection_time", + }, "default_metrics_config.yaml") +} + +func checkMetricsPresence(t *testing.T, metricNames []string, configFile string) { + 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()) + }) + + missingMetrics := make(map[string]any, len(metricNames)) + for _, m := range metricNames { + missingMetrics[m] = struct{}{} + } + + assert.EventuallyWithT(t, func(tt *assert.CollectT) { + for i := 0; i < len(sink.AllMetrics()); i++ { + m := sink.AllMetrics()[i] + for j := 0; j < m.ResourceMetrics().Len(); j++ { + rm := m.ResourceMetrics().At(j) + for k := 0; k < rm.ScopeMetrics().Len(); k++ { + sm := rm.ScopeMetrics().At(k) + for l := 0; l < sm.Metrics().Len(); l++ { + delete(missingMetrics, sm.Metrics().At(l).Name()) + } + } + } + } + msg := "Missing metrics:\n" + for k := range missingMetrics { + msg += fmt.Sprintf("- %q\n", k) + } + assert.Len(tt, missingMetrics, 0, msg) + }, 1*time.Minute, 1*time.Second) }