summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/metrics/MetricsRetrieverTest.java
blob: 86874a0dae90eaebd22b43268bd2729d79608bbb (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
package com.yahoo.vespa.config.server.metrics;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.config.server.http.v2.MetricsResponse;
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.*;


/**
 * @author olaa
 */
public class MetricsRetrieverTest {

    @Rule
    public final WireMockRule wireMock = new WireMockRule(options().port(8080), true);

    @Test
    public void testMetricAggregation() throws IOException {
        MetricsRetriever metricsRetriever = new MetricsRetriever();

        ApplicationId applicationId = ApplicationId.from("tenant", "app", "default");
        Map<String, List<URI>> clusterHosts = Map.of(
                "cluster1", List.of(URI.create("http://localhost:8080/1"), URI.create("http://localhost:8080/2")),
                "cluster2", List.of(URI.create("http://localhost:8080/3"))
        );
        Map<ApplicationId, Map<String, List<URI>>> applications = Map.of(applicationId, clusterHosts);

        stubFor(get(urlEqualTo("/1"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(metricsString())));

        stubFor(get(urlEqualTo("/2"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(metricsString())));

        stubFor(get(urlEqualTo("/3"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(metricsString())));

        MetricsResponse metricsResponse = metricsRetriever.retrieveAllMetrics(applications);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        metricsResponse.render(bos);
        String expectedResponse = "[\n" +
                " {\n" +
                "  \"applicationId\": \"tenant:app:default\",\n" +
                "  \"clusters\": [\n" +
                "   {\n" +
                "    \"clusterName\": \"cluster1\",\n" +
                "    \"metrics\": {\n" +
                "     \"queriesPerSecond\": 2.8666666666666667,\n" +
                "     \"writesPerSecond\": 1.4333333333333333,\n" +
                "     \"documentCount\": 6000.0,\n" +
                "     \"queryLatencyMillis\": 93.02325581395348,\n" +
                "     \"feedLatency\": 69.76744186046511\n" +
                "    },\n" +
                "    \"timestamp\": 1557306075\n" +
                "   },\n" +
                "   {\n" +
                "    \"clusterName\": \"cluster2\",\n" +
                "    \"metrics\": {\n" +
                "     \"queriesPerSecond\": 1.4333333333333333,\n" +
                "     \"writesPerSecond\": 0.7166666666666667,\n" +
                "     \"documentCount\": 3000.0,\n" +
                "     \"queryLatencyMillis\": 93.02325581395348,\n" +
                "     \"feedLatency\": 69.76744186046511\n" +
                "    },\n" +
                "    \"timestamp\": 1557306075\n" +
                "   }\n" +
                "  ]\n" +
                " }\n" +
                "]\n";
        assertEquals(expectedResponse, bos.toString());
        wireMock.stop();
    }

    private String metricsString() throws IOException {
        return Files.readString(Path.of("src/test/resources/metrics_response"));
    }
}