aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/admin/metricsproxy/MetricsProxyModelTester.java
blob: 332426ff9a8f113cc9986b06c8eb53c705a3d3c7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.model.admin.metricsproxy;

import ai.vespa.metrics.set.Metric;
import ai.vespa.metricsproxy.core.ConsumersConfig;
import ai.vespa.metricsproxy.http.application.MetricsNodesConfig;
import ai.vespa.metricsproxy.metric.dimensions.ApplicationDimensionsConfig;
import ai.vespa.metricsproxy.metric.dimensions.NodeDimensionsConfig;
import ai.vespa.metricsproxy.rpc.RpcConnectorConfig;
import ai.vespa.metricsproxy.service.VespaServicesConfig;
import com.yahoo.config.model.api.ApplicationClusterEndpoint;
import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.search.config.QrStartConfig;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.admin.monitoring.MetricsConsumer;
import com.yahoo.vespa.model.test.VespaModelTester;

import java.util.List;
import java.util.Set;

import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.TestMode.hosted;
import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.TestMode.self_hosted;

/**
 * @author gjoranv
 */
class MetricsProxyModelTester {

    static final String MY_TENANT = "mytenant";
    static final String MY_APPLICATION = "myapp";
    static final String MY_INSTANCE = "myinstance";

    static final String CLUSTER_CONFIG_ID = "admin/metrics";

    // Used for all configs that are produced by the container, not the cluster.
    static final String CONTAINER_CONFIG_ID = CLUSTER_CONFIG_ID + "/localhost";

    enum TestMode {
        self_hosted,
        hosted
    }

    static VespaModel getModel(String servicesXml, TestMode testMode) {
        return getModel(servicesXml, testMode, new DeployState.Builder());
    }

    static VespaModel getModel(String servicesXml, TestMode testMode, DeployState.Builder builder) {
        return getModel(servicesXml, testMode, new DeployState.Builder(), 4);
    }

    static VespaModel getModel(String servicesXml, TestMode testMode, DeployState.Builder builder, int hostCount) {
        var numberOfHosts = testMode == hosted ? hostCount : 1;
        var tester = new VespaModelTester();
        tester.addHosts(numberOfHosts);
        tester.setHosted(testMode == hosted);
        if (testMode == hosted) {
            tester.setApplicationId(MY_TENANT, MY_APPLICATION, MY_INSTANCE);
            builder.endpoints(Set.of(new ContainerEndpoint("foo", ApplicationClusterEndpoint.Scope.zone, List.of("foo.example.com"))));
        }
        return tester.createModel(servicesXml, true, builder);
    }

    static String containerConfigId(VespaModel model, MetricsProxyModelTester.TestMode mode) {
        return (mode == hosted)
                ? CLUSTER_CONFIG_ID + "/" + model.getHosts().iterator().next().getHostname()
                : CONTAINER_CONFIG_ID;
    }

    static String servicesWithAdminOnly() {
        return String.join("\n",
                           "<services>",
                           "    <admin version='4.0'>",
                           "        <adminserver hostalias='node1'/>",
                           "    </admin>",
                           "</services>"
        );
    }

    static boolean checkMetric(ConsumersConfig.Consumer consumer, Metric metric) {
        for (ConsumersConfig.Consumer.Metric m : consumer.metric()) {
            if (metric.name.equals(m.name()) && metric.outputName.equals(m.outputname()))
                return true;
        }
        return false;
    }

    static ConsumersConfig.Consumer getCustomConsumer(String servicesXml) {
        ConsumersConfig config = consumersConfigFromXml(servicesXml, self_hosted);
        for (ConsumersConfig.Consumer consumer : config.consumer()) {
            if (! consumer.name().equals(MetricsConsumer.vespa.id()) &&
                ! consumer.name().equals(MetricsConsumer.defaultConsumer.id()))
                return consumer;
        }
        throw new RuntimeException("Custom consumer not found!");
    }

    static ConsumersConfig consumersConfigFromXml(String servicesXml, TestMode testMode) {
        return consumersConfigFromModel(getModel(servicesXml, testMode));
    }

    static ConsumersConfig consumersConfigFromModel(VespaModel model) {
        return model.getConfig(ConsumersConfig.class, CLUSTER_CONFIG_ID);
    }

    static MetricsNodesConfig getMetricsNodesConfig(VespaModel model) {
        return model.getConfig(MetricsNodesConfig.class, CLUSTER_CONFIG_ID);
    }

    static ApplicationDimensionsConfig getApplicationDimensionsConfig(VespaModel model) {
        return model.getConfig(ApplicationDimensionsConfig.class, CLUSTER_CONFIG_ID);
    }

    static QrStartConfig getQrStartConfig(VespaModel model, String hostname) {
        return model.getConfig(QrStartConfig.class, CLUSTER_CONFIG_ID + "/" + hostname);
    }

    static NodeDimensionsConfig getNodeDimensionsConfig(VespaModel model, String configId) {
        return model.getConfig(NodeDimensionsConfig.class, configId);
    }

    static VespaServicesConfig getVespaServicesConfig(String servicesXml) {
        VespaModel model = getModel(servicesXml, self_hosted);
        return model.getConfig(VespaServicesConfig.class, CONTAINER_CONFIG_ID);
    }

    static RpcConnectorConfig getRpcConnectorConfig(VespaModel model) {
        return model.getConfig(RpcConnectorConfig.class, CONTAINER_CONFIG_ID);
    }

}