|
| 1 | +package up |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go/shared" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/dio/transit/up/testutil" |
| 10 | +) |
| 11 | + |
| 12 | +func TestWriter_MetricLabelsDirectWrite(t *testing.T) { |
| 13 | + h := testutil.NewFilterHandle() |
| 14 | + w := NewWriter(h) |
| 15 | + |
| 16 | + w.IncrementCounterLabels(11, 2, "chat", "openai") |
| 17 | + w.RecordHistogramLabels(12, 42, "chat", "openai", "input") |
| 18 | + |
| 19 | + require.Equal(t, []testutil.MetricRecord{ |
| 20 | + {ID: shared.MetricID(11), Value: 2, Labels: []string{"chat", "openai"}}, |
| 21 | + }, h.Counters) |
| 22 | + require.Equal(t, []testutil.MetricRecord{ |
| 23 | + {ID: shared.MetricID(12), Value: 42, Labels: []string{"chat", "openai", "input"}}, |
| 24 | + }, h.Histograms) |
| 25 | +} |
| 26 | + |
| 27 | +func TestWriter_MetricLabelsQueuedFlush(t *testing.T) { |
| 28 | + h := testutil.NewFilterHandle() |
| 29 | + f := &filter{handle: h} |
| 30 | + w := &Writer{f: f} |
| 31 | + |
| 32 | + counterLabels := []string{"chat", "openai"} |
| 33 | + histogramLabels := []string{"chat", "openai", "input"} |
| 34 | + w.IncrementCounterLabels(21, 3, counterLabels...) |
| 35 | + w.RecordHistogramLabels(22, 84, histogramLabels...) |
| 36 | + |
| 37 | + counterLabels[0] = "mutated" |
| 38 | + histogramLabels[0] = "mutated" |
| 39 | + f.flush(false) |
| 40 | + |
| 41 | + require.Equal(t, []testutil.MetricRecord{ |
| 42 | + {ID: shared.MetricID(21), Value: 3, Labels: []string{"chat", "openai"}}, |
| 43 | + }, h.Counters) |
| 44 | + require.Equal(t, []testutil.MetricRecord{ |
| 45 | + {ID: shared.MetricID(22), Value: 84, Labels: []string{"chat", "openai", "input"}}, |
| 46 | + }, h.Histograms) |
| 47 | +} |
| 48 | + |
| 49 | +func TestWriter_MetricNoLabelMethodsRemainUnlabeled(t *testing.T) { |
| 50 | + h := testutil.NewFilterHandle() |
| 51 | + w := NewWriter(h) |
| 52 | + |
| 53 | + w.IncrementCounter(31, 4) |
| 54 | + w.RecordHistogram(32, 168) |
| 55 | + |
| 56 | + require.Equal(t, []testutil.MetricRecord{ |
| 57 | + {ID: shared.MetricID(31), Value: 4}, |
| 58 | + }, h.Counters) |
| 59 | + require.Equal(t, []testutil.MetricRecord{ |
| 60 | + {ID: shared.MetricID(32), Value: 168}, |
| 61 | + }, h.Histograms) |
| 62 | +} |
0 commit comments