Skip to content

Commit

Permalink
Move Tomcat test to use metric presence check (#4535)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Mar 26, 2024
1 parent 1062448 commit ca0e5bd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 39 deletions.
Expand Up @@ -4,7 +4,11 @@ receivers:
host: localhost
port: 5000
intervalSeconds: 1

processors:
batch:
groupbyattrs:
keys:
- host
exporters:
otlp:
endpoint: "${OTLP_ENDPOINT}"
Expand All @@ -16,4 +20,5 @@ service:
metrics:
receivers:
- smartagent/collectd_tomcat
processors: [batch, groupbyattrs]
exporters: [otlp]

This file was deleted.

89 changes: 86 additions & 3 deletions tests/receivers/smartagent/collectd-tomcat/tomcat_test.go
Expand Up @@ -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)
}

0 comments on commit ca0e5bd

Please sign in to comment.