|
25 | 25 | import java.util.HashMap; |
26 | 26 | import java.util.List; |
27 | 27 | import java.util.Map; |
28 | | -import java.util.function.Function; |
29 | 28 |
|
30 | 29 | import static org.elasticsearch.test.MapMatcher.matchesMap; |
31 | 30 | import static org.hamcrest.Matchers.allOf; |
32 | 31 | import static org.hamcrest.Matchers.containsInAnyOrder; |
33 | | -import static org.hamcrest.Matchers.containsString; |
34 | 32 | import static org.hamcrest.Matchers.endsWith; |
35 | 33 | import static org.hamcrest.Matchers.equalTo; |
36 | 34 | import static org.hamcrest.Matchers.hasEntry; |
37 | 35 | import static org.hamcrest.Matchers.hasKey; |
38 | 36 | import static org.hamcrest.Matchers.hasSize; |
39 | | -import static org.hamcrest.Matchers.is; |
40 | 37 | import static org.hamcrest.Matchers.not; |
41 | 38 | import static org.mockito.Mockito.doReturn; |
42 | 39 | import static org.mockito.Mockito.mock; |
@@ -82,109 +79,63 @@ public void testFileDeleteWorks() throws IOException { |
82 | 79 | } |
83 | 80 |
|
84 | 81 | public void testExtractSecureSettings() { |
85 | | - MockSecureSettings duplicateSecureSettings = new MockSecureSettings(); |
| 82 | + MockSecureSettings secureSettings = new MockSecureSettings(); |
| 83 | + secureSettings.setString("telemetry.secret_token", "token"); |
| 84 | + secureSettings.setString("telemetry.api_key", "key"); |
86 | 85 |
|
87 | | - for (String prefix : List.of("telemetry.", "tracing.apm.")) { |
88 | | - MockSecureSettings secureSettings = new MockSecureSettings(); |
89 | | - secureSettings.setString(prefix + "secret_token", "token"); |
90 | | - secureSettings.setString(prefix + "api_key", "key"); |
91 | | - |
92 | | - duplicateSecureSettings.setString(prefix + "api_key", "secret"); |
93 | | - |
94 | | - Map<String, String> propertiesMap = new HashMap<>(); |
95 | | - APMJvmOptions.extractSecureSettings(secureSettings, propertiesMap); |
96 | | - |
97 | | - assertThat(propertiesMap, matchesMap(Map.of("secret_token", "token", "api_key", "key"))); |
98 | | - } |
99 | | - |
100 | | - Exception exception = expectThrows( |
101 | | - IllegalStateException.class, |
102 | | - () -> APMJvmOptions.extractSecureSettings(duplicateSecureSettings, new HashMap<>()) |
103 | | - ); |
104 | | - assertThat(exception.getMessage(), containsString("Duplicate telemetry setting")); |
105 | | - assertThat(exception.getMessage(), containsString("telemetry.api_key")); |
106 | | - assertThat(exception.getMessage(), containsString("tracing.apm.api_key")); |
| 86 | + Map<String, String> propertiesMap = new HashMap<>(); |
| 87 | + APMJvmOptions.extractSecureSettings(secureSettings, propertiesMap); |
107 | 88 |
|
| 89 | + assertThat(propertiesMap, matchesMap(Map.of("secret_token", "token", "api_key", "key"))); |
108 | 90 | } |
109 | 91 |
|
110 | 92 | public void testExtractSettings() throws UserException { |
111 | | - Function<String, Settings.Builder> buildSettings = (prefix) -> Settings.builder() |
112 | | - .put(prefix + "server_url", "https://myurl:443") |
113 | | - .put(prefix + "service_node_name", "instance-0000000001"); |
114 | | - |
115 | | - for (String prefix : List.of("tracing.apm.agent.", "telemetry.agent.")) { |
116 | | - var name = "APM Tracing"; |
117 | | - var deploy = "123"; |
118 | | - var org = "456"; |
119 | | - var extracted = APMJvmOptions.extractApmSettings( |
120 | | - buildSettings.apply(prefix) |
121 | | - .put(prefix + "global_labels.deployment_name", name) |
122 | | - .put(prefix + "global_labels.deployment_id", deploy) |
123 | | - .put(prefix + "global_labels.organization_id", org) |
124 | | - .build() |
125 | | - ); |
126 | | - |
127 | | - assertThat( |
128 | | - extracted, |
129 | | - allOf( |
130 | | - hasEntry("server_url", "https://myurl:443"), |
131 | | - hasEntry("service_node_name", "instance-0000000001"), |
132 | | - hasEntry(equalTo("global_labels"), not(endsWith(","))), // test that we have collapsed all global labels into one |
133 | | - not(hasKey("global_labels.organization_id")) // tests that we strip out the top level label keys |
134 | | - ) |
135 | | - ); |
136 | | - |
137 | | - List<String> labels = Arrays.stream(extracted.get("global_labels").split(",")).toList(); |
138 | | - assertThat(labels, hasSize(3)); |
139 | | - assertThat(labels, containsInAnyOrder("deployment_name=APM Tracing", "organization_id=" + org, "deployment_id=" + deploy)); |
140 | | - |
141 | | - // test replacing with underscores and skipping empty |
142 | | - name = "APM=Tracing"; |
143 | | - deploy = ""; |
144 | | - org = ",456"; |
145 | | - extracted = APMJvmOptions.extractApmSettings( |
146 | | - buildSettings.apply(prefix) |
147 | | - .put(prefix + "global_labels.deployment_name", name) |
148 | | - .put(prefix + "global_labels.deployment_id", deploy) |
149 | | - .put(prefix + "global_labels.organization_id", org) |
150 | | - .build() |
151 | | - ); |
152 | | - labels = Arrays.stream(extracted.get("global_labels").split(",")).toList(); |
153 | | - assertThat(labels, hasSize(2)); |
154 | | - assertThat(labels, containsInAnyOrder("deployment_name=APM_Tracing", "organization_id=_456")); |
155 | | - } |
156 | | - |
157 | | - IllegalStateException err = expectThrows( |
158 | | - IllegalStateException.class, |
159 | | - () -> APMJvmOptions.extractApmSettings( |
160 | | - Settings.builder() |
161 | | - .put("tracing.apm.agent.server_url", "https://myurl:443") |
162 | | - .put("telemetry.agent.server_url", "https://myurl-2:443") |
163 | | - .build() |
164 | | - ) |
165 | | - ); |
166 | | - assertThat(err.getMessage(), is("Duplicate telemetry setting: [telemetry.agent.server_url] and [tracing.apm.agent.server_url]")); |
167 | | - } |
168 | | - |
169 | | - public void testNoMixedLabels() { |
170 | | - String telemetryAgent = "telemetry.agent."; |
171 | | - String tracingAgent = "tracing.apm.agent."; |
172 | | - Settings settings = Settings.builder() |
173 | | - .put("tracing.apm.enabled", true) |
174 | | - .put(telemetryAgent + "server_url", "https://myurl:443") |
175 | | - .put(telemetryAgent + "service_node_name", "instance-0000000001") |
176 | | - .put(tracingAgent + "global_labels.deployment_id", "123") |
177 | | - .put(telemetryAgent + "global_labels.organization_id", "456") |
| 93 | + Settings defaults = Settings.builder() |
| 94 | + .put("telemetry.agent.server_url", "https://myurl:443") |
| 95 | + .put("telemetry.agent.service_node_name", "instance-0000000001") |
178 | 96 | .build(); |
179 | 97 |
|
180 | | - IllegalArgumentException err = assertThrows(IllegalArgumentException.class, () -> APMJvmOptions.extractApmSettings(settings)); |
| 98 | + var name = "APM Tracing"; |
| 99 | + var deploy = "123"; |
| 100 | + var org = "456"; |
| 101 | + var extracted = APMJvmOptions.extractApmSettings( |
| 102 | + Settings.builder() |
| 103 | + .put(defaults) |
| 104 | + .put("telemetry.agent.global_labels.deployment_name", name) |
| 105 | + .put("telemetry.agent.global_labels.deployment_id", deploy) |
| 106 | + .put("telemetry.agent.global_labels.organization_id", org) |
| 107 | + .build() |
| 108 | + ); |
| 109 | + |
181 | 110 | assertThat( |
182 | | - err.getMessage(), |
183 | | - is( |
184 | | - "Cannot have global labels with tracing.agent prefix [organization_id=456] and" |
185 | | - + " telemetry.apm.agent prefix [deployment_id=123]" |
| 111 | + extracted, |
| 112 | + allOf( |
| 113 | + hasEntry("server_url", "https://myurl:443"), |
| 114 | + hasEntry("service_node_name", "instance-0000000001"), |
| 115 | + hasEntry(equalTo("global_labels"), not(endsWith(","))), // test that we have collapsed all global labels into one |
| 116 | + not(hasKey("global_labels.organization_id")) // tests that we strip out the top level label keys |
186 | 117 | ) |
187 | 118 | ); |
| 119 | + |
| 120 | + List<String> labels = Arrays.stream(extracted.get("global_labels").split(",")).toList(); |
| 121 | + assertThat(labels, hasSize(3)); |
| 122 | + assertThat(labels, containsInAnyOrder("deployment_name=APM Tracing", "organization_id=" + org, "deployment_id=" + deploy)); |
| 123 | + |
| 124 | + // test replacing with underscores and skipping empty |
| 125 | + name = "APM=Tracing"; |
| 126 | + deploy = ""; |
| 127 | + org = ",456"; |
| 128 | + extracted = APMJvmOptions.extractApmSettings( |
| 129 | + Settings.builder() |
| 130 | + .put(defaults) |
| 131 | + .put("telemetry.agent.global_labels.deployment_name", name) |
| 132 | + .put("telemetry.agent.global_labels.deployment_id", deploy) |
| 133 | + .put("telemetry.agent.global_labels.organization_id", org) |
| 134 | + .build() |
| 135 | + ); |
| 136 | + labels = Arrays.stream(extracted.get("global_labels").split(",")).toList(); |
| 137 | + assertThat(labels, hasSize(2)); |
| 138 | + assertThat(labels, containsInAnyOrder("deployment_name=APM_Tracing", "organization_id=_456")); |
188 | 139 | } |
189 | 140 |
|
190 | 141 | private Path makeFakeAgentJar() throws IOException { |
|
0 commit comments