/
jmx_test.go
108 lines (97 loc) · 3.25 KB
/
jmx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright Splunk, Inc.
//
// 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.
//go:build integration
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 TestJmxReceiverProvidesAllMetrics(t *testing.T) {
metricNames := []string{
"cassandra.status",
"cassandra.state",
"cassandra.load",
"cassandra.ownership",
}
checkMetricsPresence(t, metricNames, "all_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"
}
mountDir, err := filepath.Abs(filepath.Join("testdata", "script.groovy"))
require.NoError(t, err)
p, err := testutils.NewCollectorContainer().
WithConfigPath(filepath.Join("testdata", configFile)).
WithLogger(logger).
WithEnv(map[string]string{
"OTLP_ENDPOINT": fmt.Sprintf("%s:%d", dockerHost, port),
"HOST": dockerHost,
}).
WithMount(mountDir, "/opt/script.groovy").
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)
}