|
| 1 | +// Copyright Envoy AI Gateway Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// The full text of the Apache license is available in the LICENSE file at |
| 4 | +// the root of the repo. |
| 5 | + |
| 6 | +package metrics |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + "testing/synctest" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "go.opentelemetry.io/otel/attribute" |
| 16 | + "go.opentelemetry.io/otel/sdk/metric" |
| 17 | + |
| 18 | + "github.com/envoyproxy/ai-gateway/internal/filterapi" |
| 19 | +) |
| 20 | + |
| 21 | +func TestImageGeneration_RecordTokenUsage(t *testing.T) { |
| 22 | + // Mirrors chat/embeddings token usage tests, but for image_generation. |
| 23 | + var ( |
| 24 | + mr = metric.NewManualReader() |
| 25 | + meter = metric.NewMeterProvider(metric.WithReader(mr)).Meter("test") |
| 26 | + im = NewImageGeneration(meter, nil).(*imageGeneration) |
| 27 | + |
| 28 | + attrsBase = []attribute.KeyValue{ |
| 29 | + attribute.Key(genaiAttributeOperationName).String(genaiOperationImageGeneration), |
| 30 | + attribute.Key(genaiAttributeProviderName).String(genaiProviderOpenAI), |
| 31 | + attribute.Key(genaiAttributeRequestModel).String("test-model"), |
| 32 | + attribute.Key(genaiAttributeResponseModel).String("test-model"), |
| 33 | + } |
| 34 | + inputAttrs = attribute.NewSet(append(attrsBase, attribute.Key(genaiAttributeTokenType).String(genaiTokenTypeInput))...) |
| 35 | + outputAttrs = attribute.NewSet(append(attrsBase, attribute.Key(genaiAttributeTokenType).String(genaiTokenTypeOutput))...) |
| 36 | + ) |
| 37 | + |
| 38 | + // Set labels and record usage. |
| 39 | + im.SetModel("test-model", "test-model") |
| 40 | + im.SetBackend(&filterapi.Backend{Schema: filterapi.VersionedAPISchema{Name: filterapi.APISchemaOpenAI}}) |
| 41 | + im.RecordTokenUsage(t.Context(), 3, 7, nil) |
| 42 | + |
| 43 | + count, sum := getHistogramValues(t, mr, genaiMetricClientTokenUsage, inputAttrs) |
| 44 | + assert.Equal(t, uint64(1), count) |
| 45 | + assert.Equal(t, 3.0, sum) |
| 46 | + |
| 47 | + count, sum = getHistogramValues(t, mr, genaiMetricClientTokenUsage, outputAttrs) |
| 48 | + assert.Equal(t, uint64(1), count) |
| 49 | + assert.Equal(t, 7.0, sum) |
| 50 | +} |
| 51 | + |
| 52 | +func TestImageGeneration_RecordImageGeneration(t *testing.T) { |
| 53 | + // Use synctest to keep time-based assertions deterministic. |
| 54 | + synctest.Test(t, func(t *testing.T) { |
| 55 | + mr := metric.NewManualReader() |
| 56 | + meter := metric.NewMeterProvider(metric.WithReader(mr)).Meter("test") |
| 57 | + im := NewImageGeneration(meter, nil).(*imageGeneration) |
| 58 | + |
| 59 | + // Base attributes plus image-specific ones |
| 60 | + attrs := attribute.NewSet( |
| 61 | + attribute.Key(genaiAttributeOperationName).String(genaiOperationImageGeneration), |
| 62 | + attribute.Key(genaiAttributeProviderName).String(genaiProviderOpenAI), |
| 63 | + attribute.Key(genaiAttributeRequestModel).String("img-model"), |
| 64 | + attribute.Key(genaiAttributeResponseModel).String("img-model"), |
| 65 | + attribute.Key("gen_ai.image.count").Int(2), |
| 66 | + attribute.Key("gen_ai.image.model").String("img-model"), |
| 67 | + attribute.Key("gen_ai.image.size").String("1024x1024"), |
| 68 | + ) |
| 69 | + |
| 70 | + im.StartRequest(nil) |
| 71 | + im.SetModel("img-model", "img-model") |
| 72 | + im.SetBackend(&filterapi.Backend{Schema: filterapi.VersionedAPISchema{Name: filterapi.APISchemaOpenAI}}) |
| 73 | + |
| 74 | + time.Sleep(10 * time.Millisecond) |
| 75 | + im.RecordImageGeneration(t.Context(), 2, "img-model", "1024x1024", nil) |
| 76 | + |
| 77 | + count, sum := getHistogramValues(t, mr, genaiMetricServerRequestDuration, attrs) |
| 78 | + assert.Equal(t, uint64(1), count) |
| 79 | + assert.Equal(t, 10*time.Millisecond.Seconds(), sum) |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func TestImageGeneration_HeaderLabelMapping(t *testing.T) { |
| 84 | + // Verify header mapping is honored for token usage metrics. |
| 85 | + var ( |
| 86 | + mr = metric.NewManualReader() |
| 87 | + meter = metric.NewMeterProvider(metric.WithReader(mr)).Meter("test") |
| 88 | + headerMapping = map[string]string{"x-user-id": "user_id", "x-org-id": "org_id"} |
| 89 | + im = NewImageGeneration(meter, headerMapping).(*imageGeneration) |
| 90 | + ) |
| 91 | + |
| 92 | + requestHeaders := map[string]string{ |
| 93 | + "x-user-id": "user123", |
| 94 | + "x-org-id": "org456", |
| 95 | + } |
| 96 | + |
| 97 | + im.SetModel("test-model", "test-model") |
| 98 | + im.SetBackend(&filterapi.Backend{Schema: filterapi.VersionedAPISchema{Name: filterapi.APISchemaOpenAI}}) |
| 99 | + im.RecordTokenUsage(t.Context(), 5, 0, requestHeaders) |
| 100 | + |
| 101 | + attrs := attribute.NewSet( |
| 102 | + attribute.Key(genaiAttributeOperationName).String(genaiOperationImageGeneration), |
| 103 | + attribute.Key(genaiAttributeProviderName).String(genaiProviderOpenAI), |
| 104 | + attribute.Key(genaiAttributeRequestModel).String("test-model"), |
| 105 | + attribute.Key(genaiAttributeResponseModel).String("test-model"), |
| 106 | + attribute.Key(genaiAttributeTokenType).String(genaiTokenTypeInput), |
| 107 | + attribute.Key("user_id").String("user123"), |
| 108 | + attribute.Key("org_id").String("org456"), |
| 109 | + ) |
| 110 | + |
| 111 | + count, _ := getHistogramValues(t, mr, genaiMetricClientTokenUsage, attrs) |
| 112 | + require.Equal(t, uint64(1), count) |
| 113 | +} |
0 commit comments