aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/admin/metricsproxy/TelegrafTest.java
blob: dbcbad7bf4908dd7f630fcac90287605d0874187 (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
package com.yahoo.vespa.model.admin.metricsproxy;

import ai.vespa.metricsproxy.telegraf.Telegraf;
import ai.vespa.metricsproxy.telegraf.TelegrafConfig;
import ai.vespa.metricsproxy.telegraf.TelegrafRegistry;
import com.yahoo.component.ComponentId;
import com.yahoo.vespa.model.VespaModel;
import org.junit.Test;

import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.CLUSTER_CONFIG_ID;
import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.TestMode.hosted;
import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.getModel;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

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

    @Test
    public void telegraf_components_are_set_up_when_cloudwatch_is_configured() {
        String services = servicesWithCloudwatch();
        VespaModel hostedModel = getModel(services, hosted);

        var clusterComponents = hostedModel.getAdmin().getMetricsProxyCluster().getComponentsMap();
        assertThat(clusterComponents.keySet(), hasItem(ComponentId.fromString(Telegraf.class.getName())));
        assertThat(clusterComponents.keySet(), hasItem(ComponentId.fromString(TelegrafRegistry.class.getName())));
    }

    @Test
    public void telegraf_components_are_not_set_up_when_no_external_systems_are_added_in_services() {
        String services = String.join("\n",
                                      "<services>",
                                      "    <admin version='2.0'>",
                                      "        <adminserver hostalias='node1'/>",
                                      "        <metrics>",
                                      "            <consumer id='foo' />",
                                      "        </metrics>",
                                      "    </admin>",
                                      "</services>");
        VespaModel hostedModel = getModel(services, hosted);

        var clusterComponents = hostedModel.getAdmin().getMetricsProxyCluster().getComponentsMap();
        assertThat(clusterComponents.keySet(), not(hasItem(ComponentId.fromString(Telegraf.class.getName()))));
        assertThat(clusterComponents.keySet(), not(hasItem(ComponentId.fromString(TelegrafRegistry.class.getName()))));
    }

    @Test
    public void telegraf_config_is_generated_for_cloudwatch_in_services() {
        String services = servicesWithCloudwatch();
        VespaModel hostedModel = getModel(services, hosted);
        TelegrafConfig config = hostedModel.getConfig(TelegrafConfig.class, CLUSTER_CONFIG_ID);

        var cloudWatch0 = config.cloudWatch(0);
        assertEquals("cloudwatch-consumer", cloudWatch0.consumer());
        assertEquals("us-east-1", cloudWatch0.region());
        assertEquals("my-namespace", cloudWatch0.namespace());
        assertEquals("my-access-key", cloudWatch0.accessKeyName());
        assertEquals("my-secret-key", cloudWatch0.secretKeyName());
        assertEquals("", cloudWatch0.profile());
    }

    private String servicesWithCloudwatch() {
        return String.join("\n",
                           "<services>",
                           "    <admin version='2.0'>",
                           "        <adminserver hostalias='node1'/>",
                           "        <metrics>",
                           "            <consumer id='cloudwatch-consumer'>",
                           "                <metric id='my-metric'/>",
                           "                <cloudwatch region='us-east-1' namespace='my-namespace' >",
                           "                    <access-key-name>my-access-key</access-key-name>",
                           "                    <secret-key-name>my-secret-key</secret-key-name>",
                           "                </cloudwatch>",
                           "            </consumer>",
                           "        </metrics>",
                           "    </admin>",
                           "</services>"
        );
    }

    @Test
    public void multiple_cloudwatches_are_allowed_for_the_same_consumer() {
        String services = String.join("\n",
                                      "<services>",
                                      "    <admin version='2.0'>",
                                      "        <adminserver hostalias='node1'/>",
                                      "        <metrics>",
                                      "            <consumer id='cloudwatch-consumer'>",
                                      "                <metric id='my-metric'/>",
                                      "                <cloudwatch region='us-east-1' namespace='namespace-1' >",
                                      "                    <access-key-name>access-key-1</access-key-name>",
                                      "                    <secret-key-name>secret-key-1</secret-key-name>",
                                      "                </cloudwatch>",
                                      "                <cloudwatch region='us-east-1' namespace='namespace-2' >",
                                      "                    <profile>profile-2</profile>",
                                      "                </cloudwatch>",
                                      "            </consumer>",
                                      "        </metrics>",
                                      "    </admin>",
                                      "</services>"
        );
        VespaModel hostedModel = getModel(services, hosted);
        TelegrafConfig config = hostedModel.getConfig(TelegrafConfig.class, CLUSTER_CONFIG_ID);

        var cloudWatch0 = config.cloudWatch(0);
        assertEquals("cloudwatch-consumer", cloudWatch0.consumer());
        assertEquals("us-east-1", cloudWatch0.region());
        assertEquals("namespace-1", cloudWatch0.namespace());
        assertEquals("access-key-1", cloudWatch0.accessKeyName());
        assertEquals("secret-key-1", cloudWatch0.secretKeyName());
        assertEquals("", cloudWatch0.profile());

        var cloudWatch1 = config.cloudWatch(1);
        assertEquals("cloudwatch-consumer", cloudWatch1.consumer());
        assertEquals("us-east-1", cloudWatch1.region());
        assertEquals("namespace-2", cloudWatch1.namespace());
        assertEquals("", cloudWatch1.accessKeyName());
        assertEquals("", cloudWatch1.secretKeyName());
        assertEquals("profile-2", cloudWatch1.profile());
    }

}