aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/IntegrationTester.java
blob: 85ac4742d8ea2ab450cb2e60487ddb8a85dd2c63 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.rpc;

import ai.vespa.metricsproxy.core.ConsumersConfig;
import ai.vespa.metricsproxy.core.ConsumersConfig.Consumer;
import ai.vespa.metricsproxy.core.MetricsConsumers;
import ai.vespa.metricsproxy.core.MetricsManager;
import ai.vespa.metricsproxy.core.MonitoringConfig;
import ai.vespa.metricsproxy.core.VespaMetrics;
import ai.vespa.metricsproxy.metric.ExternalMetrics;
import ai.vespa.metricsproxy.metric.dimensions.ApplicationDimensions;
import ai.vespa.metricsproxy.metric.dimensions.ApplicationDimensionsConfig;
import ai.vespa.metricsproxy.metric.dimensions.NodeDimensions;
import ai.vespa.metricsproxy.metric.dimensions.NodeDimensionsConfig;
import ai.vespa.metricsproxy.metric.model.ConsumerId;
import ai.vespa.metricsproxy.metric.model.ServiceId;
import ai.vespa.metricsproxy.service.HttpMetricFetcher;
import ai.vespa.metricsproxy.service.MockHttpServer;
import ai.vespa.metricsproxy.service.VespaServices;
import ai.vespa.metricsproxy.service.VespaServicesConfig;
import ai.vespa.metricsproxy.service.VespaServicesConfig.Service;

import java.io.IOException;

import static ai.vespa.metricsproxy.core.VespaMetrics.vespaMetricsConsumerId;
import static ai.vespa.metricsproxy.metric.model.ConsumerId.toConsumerId;
import static ai.vespa.metricsproxy.metric.model.ServiceId.toServiceId;
import static ai.vespa.metricsproxy.service.HttpMetricFetcher.STATE_PATH;


/**
 * Setup and shutdown of config and servers for integration-style unit tests.
 *
 * @author hmusum
 * @author gjoranv
 */
public class IntegrationTester implements  AutoCloseable {

    static final String MONITORING_SYSTEM = "test-system";
    static final ConsumerId CUSTOM_CONSUMER_ID = toConsumerId("custom-consumer");
    static final String SERVICE_1_CONFIG_ID = "container/default.0";
    static final String SERVICE_2_CONFIG_ID = "storage/cluster.storage/storage/0";

    private final RpcConnector connector;
    private final MockHttpServer mockHttpServer;
    private final VespaServices vespaServices;

    static {
        HttpMetricFetcher.CONNECTION_TIMEOUT = 60000; // 60 secs in unit tests
    }

    IntegrationTester() {
        try {
            mockHttpServer = new MockHttpServer(null, STATE_PATH);
        } catch (IOException e) {
            throw new RuntimeException("Unable to start web server");
        }

        vespaServices = new VespaServices(servicesConfig(), monitoringConfig(), null);
        MetricsConsumers consumers = new MetricsConsumers(consumersConfig());
        VespaMetrics vespaMetrics = new VespaMetrics(consumers);
        ExternalMetrics externalMetrics = new ExternalMetrics(consumers);
        ApplicationDimensions appDimensions = new ApplicationDimensions(applicationDimensionsConfig());
        NodeDimensions nodeDimensions = new NodeDimensions(nodeDimensionsConfig());

        connector = new RpcConnector(rpcConnectorConfig());
        RpcServer server = new RpcServer(connector, vespaServices,
                new MetricsManager(vespaServices, vespaMetrics, externalMetrics, appDimensions, nodeDimensions));
    }

    MockHttpServer httpServer() {
        return mockHttpServer;
    }

    VespaServices vespaServices() { return vespaServices; }

    @Override
    public void close() {
        mockHttpServer.close();
        this.connector.stop();
    }

    private RpcConnectorConfig rpcConnectorConfig() {
        return new RpcConnectorConfig.Builder()
                .port(0)
                .build();
    }

    private ConsumersConfig consumersConfig() {
        return new ConsumersConfig.Builder()
                .consumer(createConsumer(vespaMetricsConsumerId, "foo.count", "foo_count"))
                .consumer(createConsumer(CUSTOM_CONSUMER_ID, "foo.count", "foo.count"))
                .build();
    }

    private static Consumer.Builder createConsumer(ConsumerId consumerId, String metricName, String outputName) {
        return new Consumer.Builder()
                .name(consumerId.id)
                .metric(new Consumer.Metric.Builder()
                                .dimension(new Consumer.Metric.Dimension.Builder().key("somekey").value("somevalue"))
                                .name(metricName)
                                .outputname(outputName));
    }

    private VespaServicesConfig servicesConfig() {
        return new VespaServicesConfig.Builder()
                .service(createService(toServiceId("container"), SERVICE_1_CONFIG_ID, httpPort()))
                .service(createService(toServiceId("storagenode"), SERVICE_2_CONFIG_ID, httpPort()))
                .build();
    }

    private static Service.Builder createService(ServiceId serviceId, String configId, int port) {
        return new Service.Builder()
                .name(serviceId.id)
                .configId(configId)
                .port(port)
                .dimension(new Service.Dimension.Builder().key("serviceDim").value("serviceDimValue"));
    }

    private MonitoringConfig monitoringConfig() {
        return new MonitoringConfig.Builder()
                .systemName(MONITORING_SYSTEM)
                .build();
    }

    private ApplicationDimensionsConfig applicationDimensionsConfig() {
        return new ApplicationDimensionsConfig.Builder().build();
    }

    private NodeDimensionsConfig nodeDimensionsConfig() {
        return new NodeDimensionsConfig.Builder().build();
    }

    public int rpcPort() {
        return connector.port();
    }

    public int httpPort() {
        return mockHttpServer.port();
    }

}