From a4e9ff5e2740987b44bbf4ec25f58245abc41eac Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Thu, 29 Jun 2023 18:26:19 -0700 Subject: [PATCH] add more test coverage --- .../process_telemetry_linux_test.go | 53 +++++++++++++++++++ .../proctelemetry/process_telemetry_test.go | 38 +------------ 2 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 service/internal/proctelemetry/process_telemetry_linux_test.go diff --git a/service/internal/proctelemetry/process_telemetry_linux_test.go b/service/internal/proctelemetry/process_telemetry_linux_test.go new file mode 100644 index 00000000000..c45a4a273b8 --- /dev/null +++ b/service/internal/proctelemetry/process_telemetry_linux_test.go @@ -0,0 +1,53 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +//go:build linux +// +build linux + +package proctelemetry + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opencensus.io/metric" + "go.opentelemetry.io/otel/metric/noop" +) + +func TestOCProcessTelemetryWithHostProc(t *testing.T) { + ocRegistry := metric.NewRegistry() + // Make the sure the environment variable value is not used. + t.Setenv("HOST_PROC", "foo/bar") + + require.NoError(t, RegisterProcessMetrics(ocRegistry, noop.NewMeterProvider(), false, 0, "/proc")) + + // Check that the metrics are actually filled. + time.Sleep(200 * time.Millisecond) + + metrics := ocRegistry.Read() + + for _, metricName := range expectedMetrics { + m := findMetric(metrics, metricName) + require.NotNil(t, m) + require.Len(t, m.TimeSeries, 1) + ts := m.TimeSeries[0] + assert.Len(t, ts.LabelValues, 0) + require.Len(t, ts.Points, 1) + + var value float64 + if metricName == "process/uptime" || metricName == "process/cpu_seconds" { + value = ts.Points[0].Value.(float64) + } else { + value = float64(ts.Points[0].Value.(int64)) + } + + if metricName == "process/uptime" || metricName == "process/cpu_seconds" { + // This likely will still be zero when running the test. + assert.GreaterOrEqual(t, value, float64(0), metricName) + continue + } + assert.Greater(t, value, float64(0), metricName) + } +} diff --git a/service/internal/proctelemetry/process_telemetry_test.go b/service/internal/proctelemetry/process_telemetry_test.go index ca53b3dd2b3..ae0142963e2 100644 --- a/service/internal/proctelemetry/process_telemetry_test.go +++ b/service/internal/proctelemetry/process_telemetry_test.go @@ -140,43 +140,7 @@ func TestOCProcessTelemetry(t *testing.T) { require.NoError(t, RegisterProcessMetrics(ocRegistry, noop.NewMeterProvider(), false, 0, "")) // Check that the metrics are actually filled. - <-time.After(200 * time.Millisecond) - - metrics := ocRegistry.Read() - - for _, metricName := range expectedMetrics { - m := findMetric(metrics, metricName) - require.NotNil(t, m) - require.Len(t, m.TimeSeries, 1) - ts := m.TimeSeries[0] - assert.Len(t, ts.LabelValues, 0) - require.Len(t, ts.Points, 1) - - var value float64 - if metricName == "process/uptime" || metricName == "process/cpu_seconds" { - value = ts.Points[0].Value.(float64) - } else { - value = float64(ts.Points[0].Value.(int64)) - } - - if metricName == "process/uptime" || metricName == "process/cpu_seconds" { - // This likely will still be zero when running the test. - assert.GreaterOrEqual(t, value, float64(0), metricName) - continue - } - assert.Greater(t, value, float64(0), metricName) - } -} - -func TestOCProcessTelemetryWithHostProc(t *testing.T) { - ocRegistry := metric.NewRegistry() - // Make the sure the environment variable value is not used. - t.Setenv("HOST_PROC", "foo/bar") - - require.NoError(t, RegisterProcessMetrics(ocRegistry, noop.NewMeterProvider(), false, 0, "/proc")) - - // Check that the metrics are actually filled. - <-time.After(200 * time.Millisecond) + time.Sleep(200 * time.Millisecond) metrics := ocRegistry.Read()