summaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MetricsParserTest.java
blob: 5fc260e99125f4c0cdbe9195b6386aa2115e11e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ai.vespa.metricsproxy.service;

import ai.vespa.metricsproxy.metric.Metric;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static ai.vespa.metricsproxy.service.MetricsParser.dimensionsHashCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
 * @author gjoranv
 */
public class MetricsParserTest {

    private static class MetricsCollector implements MetricsParser.Collector {
        List<Metric> metrics = new ArrayList<>();

        @Override
        public void accept(Metric metric) {
            metrics.add(metric);
        }
    }

    @Test
    public void dimensions_hashcode_is_different_for_distinct_but_duplicate_dimension_values() {
        var dimensions1 = List.of(
                new MetricsParser.Dimension("cluster", "CLUSTER-1"),
                new MetricsParser.Dimension("clusterid", "CLUSTER-1"));

        var dimensions2 = List.of(
                new MetricsParser.Dimension("cluster", "CLUSTER-2"),
                new MetricsParser.Dimension("clusterid", "CLUSTER-2"));

        assertNotEquals(dimensionsHashCode(dimensions1), dimensionsHashCode(dimensions2));
    }

    @Test
    public void dimensions_hashcode_is_different_for_distinct_with_swapped_dimension_values() {
        var dimensions1 = List.of(
                new MetricsParser.Dimension("cluster", "CLUSTER-1"),
                new MetricsParser.Dimension("clusterid", "CLUSTER-2"));

        var dimensions2 = List.of(
                new MetricsParser.Dimension("cluster", "CLUSTER-2"),
                new MetricsParser.Dimension("clusterid", "CLUSTER-1"));

        assertNotEquals(dimensionsHashCode(dimensions1), dimensionsHashCode(dimensions2));
    }


    @Test
    public void different_dimension_values_are_not_treated_as_equal() throws Exception {
        var collector = new MetricsCollector();
        MetricsParser.parse(metricsJson(), collector);
        assertEquals(2, collector.metrics.size());
        assertNotEquals("Dimensions should not be equal",
                        collector.metrics.get(0).getDimensions(),
                        collector.metrics.get(1).getDimensions());
    }

    // The duplicate dimension values for 'cluster' and 'clusterid' exposed a bug in a previously used hashing algo for dimensions.
    private String metricsJson() {
        return """
                {
                  "time": 1671035366573,
                  "status": {
                    "code": "up"
                  },
                  "metrics": {
                    "snapshot": {
                      "from": 1671035306.562,
                      "to": 1671035366.562
                    },
                    "values": [
                      {
                        "name": "cluster-controller.resource_usage.nodes_above_limit",
                        "values": {
                          "last": 1.0
                        },
                        "dimensions": {
                          "controller-index": "0",
                          "cluster": "CLUSTER-1",
                          "clusterid": "CLUSTER-1"
                        }
                      },
                      {
                        "name": "cluster-controller.resource_usage.nodes_above_limit",
                        "values": {
                          "last": 2.0
                        },
                        "dimensions": {
                          "controller-index": "0",
                          "cluster": "CLUSTER-2",
                          "clusterid": "CLUSTER-2"
                        }
                      }
                    ]
                  }
                }
                """;
    }

}