-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathmetrics_test.go
More file actions
68 lines (58 loc) · 2.31 KB
/
metrics_test.go
File metadata and controls
68 lines (58 loc) · 2.31 KB
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
package observability
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/supabase/auth/internal/conf"
)
func TestMeter(t *testing.T) {
require.NotNil(t, Meter("test-meter"))
}
func TestObtainMetricCounter(t *testing.T) {
require.NotNil(t, ObtainMetricCounter("test_counter", "a counter for tests"))
}
func TestConfigureMetricsNilContextPanics(t *testing.T) {
require.PanicsWithValue(t, "context must not be nil", func() {
_ = ConfigureMetrics(nil, &conf.MetricsConfig{})
})
}
func TestConfigureMetricsDisabled(t *testing.T) {
// metricsOnce is a package-level *sync.Once used to ensure ConfigureMetrics
// runs exactly once per process. Reset it for this isolated test so we can
// exercise the disabled-config branch without coupling to other tests.
metricsOnce = &sync.Once{}
require.NoError(t, ConfigureMetrics(context.Background(), &conf.MetricsConfig{Enabled: false}))
}
func TestEnableOpenTelemetryMetricsRejectsUnsupportedExporter(t *testing.T) {
err := enableOpenTelemetryMetrics(context.Background(), &conf.MetricsConfig{ExporterProtocol: "http/json"})
require.Error(t, err)
require.Contains(t, err.Error(), "unsupported OpenTelemetry exporter protocol")
}
func TestEnablePrometheusMetricsStartsAndShutsDown(t *testing.T) {
// Coverage of the synchronous setup path; the goroutine that calls
// ListenAndServe shuts down when the context is cancelled. The function
// returns nil whether or not the listener binds, so we only assert the
// happy synchronous outcome.
ctx, cancel := context.WithCancel(context.Background())
port := freeLocalPort(t)
require.NoError(t, enablePrometheusMetrics(ctx, &conf.MetricsConfig{
PrometheusListenHost: "127.0.0.1",
PrometheusListenPort: port,
}))
cancel()
time.Sleep(150 * time.Millisecond)
}
func TestEnableOpenTelemetryMetricsGRPC(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
require.NoError(t, enableOpenTelemetryMetrics(ctx, &conf.MetricsConfig{ExporterProtocol: "grpc"}))
cancel()
time.Sleep(150 * time.Millisecond)
}
func TestEnableOpenTelemetryMetricsHTTPProtobuf(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
require.NoError(t, enableOpenTelemetryMetrics(ctx, &conf.MetricsConfig{ExporterProtocol: "http/protobuf"}))
cancel()
time.Sleep(150 * time.Millisecond)
}