aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/admin/metricsproxy/TelegrafTest.java
blob: 5bd364f4e215901aeb05a4891404320bd9acf7ba (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
143
144
145
146
147
148
149
150
// Copyright Yahoo. 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.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.TestMode.self_hosted;
import static com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyModelTester.getModel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @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();
        assertTrue(clusterComponents.containsKey(ComponentId.fromString(Telegraf.class.getName())));
        assertTrue(clusterComponents.containsKey(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();
        assertFalse(clusterComponents.containsKey(ComponentId.fromString(Telegraf.class.getName())));
        assertFalse(clusterComponents.containsKey(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);
        assertTrue(config.isHostedVespa());

        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("default", 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' >",
                           "                    <credentials access-key-name='my-access-key' ",
                           "                                 secret-key-name='my-secret-key' />",
                           "                </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' >",
                                      "                    <credentials access-key-name='access-key-1' ",
                                      "                                 secret-key-name='secret-key-1' />",
                                      "                </cloudwatch>",
                                      "                <cloudwatch region='us-east-1' namespace='namespace-2' >",
                                      "                    <shared-credentials profile='profile-2' />",
                                      "                </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("default", 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());
    }

    @Test
    public void profile_named_default_is_used_when_no_profile_is_given_in_shared_credentials() {
        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='foo' >",
                                      "                    <shared-credentials file='/path/to/file' />",
                                      "                </cloudwatch>",
                                      "            </consumer>",
                                      "        </metrics>",
                                      "    </admin>",
                                      "</services>"
        );
        VespaModel model = getModel(services, self_hosted);
        TelegrafConfig config = model.getConfig(TelegrafConfig.class, CLUSTER_CONFIG_ID);
        assertEquals("default", config.cloudWatch(0).profile());
    }

}