Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Apr 4, 2023
1 parent df43c8f commit d347675
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions core/pkg/otel/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package otel

import (
"context"
"fmt"
"testing"

"github.com/open-feature/flagd/core/pkg/eval"
Expand Down Expand Up @@ -91,65 +92,91 @@ func TestNewOTelRecorder(t *testing.T) {
}

func TestMetrics(t *testing.T) {
exp := metric.NewManualReader()
rec := NewOTelRecorder(exp, svcName)
ctx := context.TODO()
attrs := []attribute.KeyValue{
semconv.ServiceNameKey.String(svcName),
}
const n = 5
type MetricF func()
type MetricF func(exp metric.Reader)
tests := []struct {
name string
metricFunc MetricF
metricsLen int
}{
{
name: "HTTPRequestDuration",
metricFunc: func() {
metricFunc: func(exp metric.Reader) {
rec := NewOTelRecorder(exp, svcName)
for i := 0; i < n; i++ {
rec.HTTPRequestDuration(ctx, 10, attrs)
rec.HTTPRequestDuration(context.TODO(), 10, attrs)
}
},
metricsLen: 1,
},
{
name: "HTTPResponseSize",
metricFunc: func() {
metricFunc: func(exp metric.Reader) {
rec := NewOTelRecorder(exp, svcName)
for i := 0; i < n; i++ {
rec.HTTPResponseSize(ctx, 100, attrs)
rec.HTTPResponseSize(context.TODO(), 100, attrs)
}
},
metricsLen: 1,
},
{
name: "InFlightRequestStart",
metricFunc: func() {
metricFunc: func(exp metric.Reader) {
rec := NewOTelRecorder(exp, svcName)
ctx := context.TODO()
for i := 0; i < n; i++ {
rec.InFlightRequestStart(ctx, attrs)
rec.InFlightRequestEnd(ctx, attrs)
}
},
metricsLen: 1,
},
{
name: "Impressions",
metricFunc: func() {
metricFunc: func(exp metric.Reader) {
rec := NewOTelRecorder(exp, svcName)
for i := 0; i < n; i++ {
rec.Impressions(context.TODO(), eval.NewAnyValue("value", "variant", "reason", "key", nil))
}
},
metricsLen: 1,
},
{
name: "Impressions error",
metricFunc: func(exp metric.Reader) {
rec := NewOTelRecorder(exp, svcName)
ctx := context.TODO()
for i := 0; i < n; i++ {
rec.Impressions(ctx, eval.NewAnyValue("value", "variant", "reason", "key", nil))
rec.Impressions(ctx, eval.NewAnyValue("", "variant", "reason-err", "key", fmt.Errorf("some err1")))
}
for i := 0; i < n; i++ {
rec.Impressions(ctx, eval.NewAnyValue("", "variant", "reason-err", "key", fmt.Errorf("some err2")))
}
for i := 0; i < n; i++ {
rec.Impressions(ctx, eval.NewAnyValue("", "variant", "reason-err", "key", fmt.Errorf("some err3")))
}
},
metricsLen: 1,
},
}
i := 0

for _, tt := range tests {
i++
tt.metricFunc()
data, err := exp.Collect(context.TODO())
if err != nil {
t.Errorf("Got %v", err)
}
if len(data.ScopeMetrics) != 1 {
t.Errorf("A single scope is expected, got %d", len(data.ScopeMetrics))
}
scopeMetrics := data.ScopeMetrics[0]
require.Equal(t, svcName, scopeMetrics.Scope.Name)
require.Equal(t, i, len(scopeMetrics.Metrics))
t.Run(tt.name, func(t *testing.T) {
exp := metric.NewManualReader()
tt.metricFunc(exp)
data, err := exp.Collect(context.TODO())
if err != nil {
t.Errorf("Got %v", err)
}
if len(data.ScopeMetrics) != 1 {
t.Errorf("A single scope is expected, got %d", len(data.ScopeMetrics))
}
scopeMetrics := data.ScopeMetrics[0]
require.Equal(t, svcName, scopeMetrics.Scope.Name)
require.Equal(t, tt.metricsLen, len(scopeMetrics.Metrics))
})
}
}

0 comments on commit d347675

Please sign in to comment.