|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | + |
| 3 | +package test |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + dto "github.com/prometheus/client_model/go" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "google.golang.org/protobuf/proto" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAssertGatherAndCompare(t *testing.T) { |
| 14 | + g := fakeGatherer{ |
| 15 | + metrics: []*dto.MetricFamily{ |
| 16 | + { |
| 17 | + Name: proto.String("cortex_distributor_deduped_samples_total"), |
| 18 | + Help: proto.String("The total number of deduplicated samples."), |
| 19 | + Metric: []*dto.Metric{ |
| 20 | + { |
| 21 | + Label: []*dto.LabelPair{ |
| 22 | + { |
| 23 | + Name: proto.String("environment"), |
| 24 | + Value: proto.String("test"), |
| 25 | + }, |
| 26 | + }, |
| 27 | + TimestampMs: proto.Int64(1000), |
| 28 | + Counter: &dto.Counter{ |
| 29 | + Value: proto.Float64(1), |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + t.Run("don't specify any metrics", func(t *testing.T) { |
| 38 | + // When not specifying any metrics, an error should be returned due to missing metric |
| 39 | + // cortex_distributor_latest_seen_sample_timestamp_seconds. |
| 40 | + err := gatherAndCompare(g, ` |
| 41 | + # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. |
| 42 | + # TYPE cortex_distributor_deduped_samples_total counter |
| 43 | + cortex_distributor_deduped_samples_total{environment="test"} 1 1000 |
| 44 | +
|
| 45 | + # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 46 | + # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 47 | + cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 48 | + `) |
| 49 | + require.EqualError(t, err, ` # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. |
| 50 | + # TYPE cortex_distributor_deduped_samples_total counter |
| 51 | + cortex_distributor_deduped_samples_total{environment="test"} 1 1000 |
| 52 | ++# HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 53 | ++# TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 54 | ++cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 55 | + `) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("specify required metric", func(t *testing.T) { |
| 59 | + // When specifying that cortex_distributor_deduped_samples_total as the one required metric, |
| 60 | + // cortex_distributor_latest_seen_sample_timestamp_seconds should be ignored even if it's missing. |
| 61 | + AssertGatherAndCompare(t, g, ` |
| 62 | + # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. |
| 63 | + # TYPE cortex_distributor_deduped_samples_total counter |
| 64 | + cortex_distributor_deduped_samples_total{environment="test"} 1 1000 |
| 65 | +
|
| 66 | + # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 67 | + # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 68 | + cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 69 | + `, "cortex_distributor_deduped_samples_total") |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("specify required metric which isn't there", func(t *testing.T) { |
| 73 | + // When specifying that cortex_distributor_deduped_samples_total as the one required metric, |
| 74 | + // cortex_distributor_latest_seen_sample_timestamp_seconds should be ignored even if it's missing. |
| 75 | + err := gatherAndCompare(g, ` |
| 76 | + # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. |
| 77 | + # TYPE cortex_distributor_deduped_samples_total counter |
| 78 | + cortex_distributor_deduped_samples_total{environment="test"} 1 1000 |
| 79 | +
|
| 80 | + # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 81 | + # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 82 | + cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 83 | + `, "cortex_distributor_latest_seen_sample_timestamp_seconds") |
| 84 | + require.EqualError(t, err, "expected metric name(s) not found: [cortex_distributor_latest_seen_sample_timestamp_seconds]") |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("specify required metric and absent metric", func(t *testing.T) { |
| 88 | + // Verify that cortex_distributor_deduped_samples_total is found among metrics returned by g, |
| 89 | + // and that conversely, cortex_distributor_non_ha_samples_received_total is not found among |
| 90 | + // metrics returned by g. |
| 91 | + // cortex_distributor_latest_seen_sample_timestamp_seconds is ignored, since it's not among |
| 92 | + // the specified metrics. |
| 93 | + AssertGatherAndCompare(t, g, ` |
| 94 | + # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. |
| 95 | + # TYPE cortex_distributor_deduped_samples_total counter |
| 96 | + cortex_distributor_deduped_samples_total{environment="test"} 1 1000 |
| 97 | +
|
| 98 | + # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 99 | + # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 100 | + cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 101 | + `, "cortex_distributor_deduped_samples_total", "cortex_distributor_non_ha_samples_received_total") |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("specify absent metric which is actually there", func(t *testing.T) { |
| 105 | + // Verify that cortex_distributor_deduped_samples_total is found among metrics returned by g, |
| 106 | + // and that conversely, cortex_distributor_non_ha_samples_received_total is not found among |
| 107 | + // metrics returned by g. |
| 108 | + // cortex_distributor_latest_seen_sample_timestamp_seconds is ignored, since it's not among |
| 109 | + // the specified metrics. |
| 110 | + err := gatherAndCompare(g, ` |
| 111 | + # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. |
| 112 | + # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge |
| 113 | + cortex_distributor_latest_seen_sample_timestamp_seconds{user="userA"} 1111 |
| 114 | + `, "cortex_distributor_deduped_samples_total", "cortex_distributor_non_ha_samples_received_total") |
| 115 | + require.EqualError(t, err, "should be absent: metrics=cortex_distributor_deduped_samples_total") |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +type fakeGatherer struct { |
| 120 | + metrics []*dto.MetricFamily |
| 121 | + err error |
| 122 | +} |
| 123 | + |
| 124 | +func (g fakeGatherer) Gather() ([]*dto.MetricFamily, error) { |
| 125 | + return g.metrics, g.err |
| 126 | +} |
0 commit comments