228 lines
7.4 KiB
Go
228 lines
7.4 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
)
|
|
|
|
func TestRuntimeRecordsMetrics(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
reader := sdkmetric.NewManualReader()
|
|
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
|
|
tracerProvider := sdktrace.NewTracerProvider()
|
|
|
|
runtime, err := NewWithProviders(meterProvider, tracerProvider)
|
|
require.NoError(t, err)
|
|
|
|
runtime.RecordInternalHTTPRequest(context.Background(), []attribute.KeyValue{
|
|
attribute.String("route", "/api/v1/internal/login-code-deliveries"),
|
|
attribute.String("method", "POST"),
|
|
attribute.String("edge_outcome", "success"),
|
|
}, 5*time.Millisecond)
|
|
runtime.RecordAuthDeliveryOutcome(context.Background(), "sent")
|
|
runtime.RecordGenericDeliveryOutcome(context.Background(), "accepted")
|
|
runtime.RecordMalformedCommand(context.Background(), "invalid_payload")
|
|
runtime.RecordAcceptedAuthDelivery(context.Background())
|
|
runtime.RecordAcceptedGenericDelivery(context.Background())
|
|
runtime.RecordDeliveryStatusTransition(context.Background(), "queued", "notification")
|
|
runtime.RecordDeliveryStatusTransition(context.Background(), "suppressed", "authsession")
|
|
runtime.RecordDeliveryStatusTransition(context.Background(), "dead_letter", "notification")
|
|
runtime.RecordAttemptOutcome(context.Background(), "provider_accepted", "notification")
|
|
runtime.RecordLocaleFallback(context.Background(), "auth.login_code", "fr-FR", "en")
|
|
runtime.RecordProviderSendDuration(context.Background(), "smtp", "accepted", 15*time.Millisecond)
|
|
scheduledAt := time.Now().Add(-time.Second).UTC()
|
|
runtime.SetAttemptScheduleSnapshotReader(stubAttemptScheduleSnapshotReader{
|
|
snapshot: AttemptScheduleSnapshot{
|
|
Depth: 3,
|
|
OldestScheduledFor: &scheduledAt,
|
|
},
|
|
})
|
|
|
|
assertMetricCount(t, reader, "mail.internal_http.requests", map[string]string{
|
|
"route": "/api/v1/internal/login-code-deliveries",
|
|
"method": "POST",
|
|
"edge_outcome": "success",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.auth_delivery.outcomes", map[string]string{
|
|
"outcome": "sent",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.generic_delivery.outcomes", map[string]string{
|
|
"outcome": "accepted",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.stream_commands.malformed", map[string]string{
|
|
"failure_code": "invalid_payload",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.accepted_auth", nil, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.accepted_generic", nil, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.suppressed", nil, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.status_transitions", map[string]string{
|
|
"status": "queued",
|
|
"source": "notification",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.status_transitions", map[string]string{
|
|
"status": "suppressed",
|
|
"source": "authsession",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.delivery.dead_letters", map[string]string{
|
|
"source": "notification",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.attempt.outcomes", map[string]string{
|
|
"status": "provider_accepted",
|
|
"source": "notification",
|
|
}, 1)
|
|
assertMetricCount(t, reader, "mail.template.locale_fallback", map[string]string{
|
|
"template_id": "auth.login_code",
|
|
"requested_locale": "fr-FR",
|
|
"resolved_locale": "en",
|
|
}, 1)
|
|
assertHistogramCount(t, reader, "mail.provider.send.duration_ms", map[string]string{
|
|
"provider": "smtp",
|
|
"outcome": "accepted",
|
|
}, 1)
|
|
assertGaugeValue(t, reader, "mail.attempt_schedule.depth", nil, 3)
|
|
assertGaugePositive(t, reader, "mail.attempt_schedule.oldest_age_ms", nil)
|
|
}
|
|
|
|
func assertMetricCount(t *testing.T, reader *sdkmetric.ManualReader, metricName string, wantAttrs map[string]string, wantValue int64) {
|
|
t.Helper()
|
|
|
|
var resourceMetrics metricdata.ResourceMetrics
|
|
require.NoError(t, reader.Collect(context.Background(), &resourceMetrics))
|
|
|
|
for _, scopeMetrics := range resourceMetrics.ScopeMetrics {
|
|
for _, metric := range scopeMetrics.Metrics {
|
|
if metric.Name != metricName {
|
|
continue
|
|
}
|
|
|
|
sum, ok := metric.Data.(metricdata.Sum[int64])
|
|
require.True(t, ok)
|
|
|
|
for _, point := range sum.DataPoints {
|
|
if hasMetricAttributes(point.Attributes.ToSlice(), wantAttrs) {
|
|
assert.Equal(t, wantValue, point.Value)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require.Failf(t, "test failed", "metric %q with attrs %v not found", metricName, wantAttrs)
|
|
}
|
|
|
|
func assertHistogramCount(t *testing.T, reader *sdkmetric.ManualReader, metricName string, wantAttrs map[string]string, wantCount uint64) {
|
|
t.Helper()
|
|
|
|
var resourceMetrics metricdata.ResourceMetrics
|
|
require.NoError(t, reader.Collect(context.Background(), &resourceMetrics))
|
|
|
|
for _, scopeMetrics := range resourceMetrics.ScopeMetrics {
|
|
for _, metric := range scopeMetrics.Metrics {
|
|
if metric.Name != metricName {
|
|
continue
|
|
}
|
|
|
|
histogram, ok := metric.Data.(metricdata.Histogram[float64])
|
|
require.True(t, ok)
|
|
|
|
for _, point := range histogram.DataPoints {
|
|
if hasMetricAttributes(point.Attributes.ToSlice(), wantAttrs) {
|
|
assert.Equal(t, wantCount, point.Count)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require.Failf(t, "test failed", "histogram %q with attrs %v not found", metricName, wantAttrs)
|
|
}
|
|
|
|
func assertGaugeValue(t *testing.T, reader *sdkmetric.ManualReader, metricName string, wantAttrs map[string]string, wantValue int64) {
|
|
t.Helper()
|
|
|
|
var resourceMetrics metricdata.ResourceMetrics
|
|
require.NoError(t, reader.Collect(context.Background(), &resourceMetrics))
|
|
|
|
for _, scopeMetrics := range resourceMetrics.ScopeMetrics {
|
|
for _, metric := range scopeMetrics.Metrics {
|
|
if metric.Name != metricName {
|
|
continue
|
|
}
|
|
|
|
gauge, ok := metric.Data.(metricdata.Gauge[int64])
|
|
require.True(t, ok)
|
|
|
|
for _, point := range gauge.DataPoints {
|
|
if hasMetricAttributes(point.Attributes.ToSlice(), wantAttrs) {
|
|
assert.Equal(t, wantValue, point.Value)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require.Failf(t, "test failed", "gauge %q with attrs %v not found", metricName, wantAttrs)
|
|
}
|
|
|
|
func assertGaugePositive(t *testing.T, reader *sdkmetric.ManualReader, metricName string, wantAttrs map[string]string) {
|
|
t.Helper()
|
|
|
|
var resourceMetrics metricdata.ResourceMetrics
|
|
require.NoError(t, reader.Collect(context.Background(), &resourceMetrics))
|
|
|
|
for _, scopeMetrics := range resourceMetrics.ScopeMetrics {
|
|
for _, metric := range scopeMetrics.Metrics {
|
|
if metric.Name != metricName {
|
|
continue
|
|
}
|
|
|
|
gauge, ok := metric.Data.(metricdata.Gauge[int64])
|
|
require.True(t, ok)
|
|
|
|
for _, point := range gauge.DataPoints {
|
|
if hasMetricAttributes(point.Attributes.ToSlice(), wantAttrs) {
|
|
assert.Greater(t, point.Value, int64(0))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require.Failf(t, "test failed", "gauge %q with attrs %v not found", metricName, wantAttrs)
|
|
}
|
|
|
|
func hasMetricAttributes(values []attribute.KeyValue, want map[string]string) bool {
|
|
if len(want) == 0 {
|
|
return len(values) == 0
|
|
}
|
|
if len(values) != len(want) {
|
|
return false
|
|
}
|
|
|
|
for _, value := range values {
|
|
if want[string(value.Key)] != value.Value.AsString() {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
type stubAttemptScheduleSnapshotReader struct {
|
|
snapshot AttemptScheduleSnapshot
|
|
err error
|
|
}
|
|
|
|
func (reader stubAttemptScheduleSnapshotReader) ReadAttemptScheduleSnapshot(context.Context) (AttemptScheduleSnapshot, error) {
|
|
return reader.snapshot, reader.err
|
|
}
|