-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractOtelSpringStarterSmokeTest.java
More file actions
369 lines (330 loc) · 15.5 KB
/
Copy pathAbstractOtelSpringStarterSmokeTest.java
File metadata and controls
369 lines (330 loc) · 15.5 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.spring.smoketest;
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldCodeSemconv;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static io.opentelemetry.semconv.ClientAttributes.CLIENT_ADDRESS;
import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_INSTANCE_ID;
import static io.opentelemetry.semconv.UrlAttributes.URL_FULL;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import ch.qos.logback.classic.LoggerContext;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.OtelResourceProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.OtelSpringProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.OtlpExporterProperties;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.SpringConfigProperties;
import io.opentelemetry.instrumentation.test.utils.GcUtils;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.resources.Resource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeoutException;
import org.assertj.core.api.AbstractIterableAssert;
import org.assertj.core.api.MapAssert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.condition.OS;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* This test class enforces the order of the tests to make sure that {@link #shouldSendTelemetry()},
* which asserts the telemetry data from the application startup, is executed first.
*/
@SuppressWarnings("deprecation") // using deprecated semconv
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
abstract class AbstractOtelSpringStarterSmokeTest extends AbstractSpringStarterSmokeTest {
@Autowired private Environment environment;
@Autowired private OtelSpringProperties otelSpringProperties;
@Autowired private OtelResourceProperties otelResourceProperties;
@Autowired private OtlpExporterProperties otlpExporterProperties;
@Autowired private JdbcTemplate jdbcTemplate;
abstract void makeClientCall();
abstract void restTemplateCall(String path);
protected boolean preferJfr() {
return false;
}
// can't use @LocalServerPort annotation since it moved packages between Spring Boot 2 and 3
@Value("${local.server.port}")
protected int port;
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Autowired private ObjectProvider<JdbcTemplate> jdbcTemplate;
@EventListener(ApplicationReadyEvent.class)
public void loadData() {
jdbcTemplate
.getObject()
.execute(
"create table customer (id bigint not null, name varchar not null, primary key (id))");
}
@Bean
@Order(1)
AutoConfigurationCustomizerProvider hiddenPropagatorCustomizer() {
return customizer ->
customizer.addResourceCustomizer(
(resource, config) ->
resource.merge(
Resource.create(
Attributes.of(booleanKey("keyFromResourceCustomizer"), false))));
}
@Bean
@Order(2)
AutoConfigurationCustomizerProvider propagatorCustomizer() {
return customizer ->
customizer.addResourceCustomizer(
(resource, config) ->
resource.merge(
Resource.create(
Attributes.of(booleanKey("keyFromResourceCustomizer"), true))));
}
@Bean
AutoConfigurationCustomizerProvider customizerUsingPropertyDefinedInaSpringFile() {
return customizer ->
customizer.addResourceCustomizer(
(resource, config) -> {
String valueForKeyDeclaredZsEnvVariable = config.getString("APPLICATION_PROP");
assertThat(valueForKeyDeclaredZsEnvVariable).isNotEmpty();
String valueForKeyWithDash = config.getString("application.prop-with-dash");
assertThat(valueForKeyWithDash).isNotEmpty();
return resource;
});
}
}
@AfterAll
static void resetLogger() {
ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory();
if (!(loggerFactorySpi instanceof LoggerContext)) {
return;
}
LoggerContext loggerContext = (LoggerContext) loggerFactorySpi;
loggerContext.reset();
}
@Test
void propertyConversion() {
ConfigProperties configProperties =
SpringConfigProperties.create(
environment,
otlpExporterProperties,
otelResourceProperties,
otelSpringProperties,
DefaultConfigProperties.createFromMap(
singletonMap("otel.exporter.otlp.headers", "a=1,b=2")));
assertThat(configProperties.getMap("otel.exporter.otlp.headers"))
.containsEntry("a", "1")
.containsEntry("b", "2")
.containsEntry("c", "3");
assertThat(configProperties.getList("otel.propagators")).containsExactly("b3");
}
@Test
@org.junit.jupiter.api.Order(1) // This test validates startup telemetry, so must run first
@SuppressWarnings("deprecation") // testing deprecated code semconv
void shouldSendTelemetry() throws InterruptedException, TimeoutException {
makeClientCall();
testing.waitAndAssertTraces(
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
spanDataAssert ->
spanDataAssert
.hasKind(SpanKind.CLIENT)
.hasAttribute(
DB_STATEMENT,
"create table customer (id bigint not null, name varchar not null, primary key (id))")),
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
clientSpan ->
clientSpan
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfying(
satisfies(URL_FULL, val -> val.endsWith("/ping")),
equalTo(SERVER_ADDRESS, "localhost"),
satisfies(SERVER_PORT, val -> val.isNotZero())),
serverSpan ->
HttpSpanDataAssert.create(serverSpan)
.assertServerGetRequest("/ping")
.hasResourceSatisfying(
r ->
r.hasAttribute(booleanKey("keyFromResourceCustomizer"), true)
.hasAttribute(stringKey("attributeFromYaml"), "true")
.hasAttribute(
satisfies(SERVICE_INSTANCE_ID, val -> val.isNotBlank())))
.hasAttributesSatisfying(
equalTo(HTTP_REQUEST_METHOD, "GET"),
equalTo(HTTP_RESPONSE_STATUS_CODE, 200L),
equalTo(HTTP_ROUTE, "/ping"),
equalTo(SERVER_ADDRESS, "localhost"),
satisfies(CLIENT_ADDRESS, s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1")),
equalTo(
stringArrayKey("http.request.header.key"), singletonList("value")),
satisfies(SERVER_PORT, val -> val.isNotZero()),
satisfies(THREAD_ID, val -> val.isNotZero()),
satisfies(THREAD_NAME, val -> val.isNotBlank())),
val -> withSpanAssert(val)));
// Metric
testing.waitAndAssertMetrics(
OtelSpringStarterSmokeTestController.METER_SCOPE_NAME,
OtelSpringStarterSmokeTestController.TEST_HISTOGRAM,
AbstractIterableAssert::isNotEmpty);
// Runtime metrics - test one per JMX/JFR source
List<String> runtimeMetrics =
new ArrayList<>(asList("jvm.thread.count", "jvm.memory.used", "jvm.memory.init"));
double javaVersion = Double.parseDouble(System.getProperty("java.specification.version"));
// See https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/13503
// Also not available on Windows (getSystemLoadAverage returns -1)
// Not available when prefer-jfr is enabled (JMX-only metric with no JFR equivalent)
if (javaVersion < 23 && !OS.WINDOWS.isCurrentOs() && !preferJfr()) {
runtimeMetrics.add("jvm.system.cpu.load_1m");
}
boolean nativeImage = System.getProperty("org.graalvm.nativeimage.imagecode") != null;
if (!nativeImage) {
// GraalVM native image does not support buffer pools - have to investigate why
runtimeMetrics.add("jvm.buffer.memory.used");
}
runtimeMetrics.forEach(
metricName ->
testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry",
metricName,
AbstractIterableAssert::isNotEmpty));
assertAdditionalMetrics();
// Log
List<LogRecordData> exportedLogRecords = testing.getExportedLogRecords();
assertThat(exportedLogRecords).isNotEmpty();
if (!nativeImage) {
// log records differ in native image mode due to different startup timing
Optional<LogRecordData> firstInfo =
exportedLogRecords.stream().filter(log -> log.getSeverity() == Severity.INFO).findFirst();
assertThat(firstInfo).isPresent();
LogRecordData firstLog = firstInfo.get();
assertThat(firstLog.getBodyValue().asString())
.startsWith("Starting ")
.contains(this.getClass().getSimpleName());
MapAssert<AttributeKey<?>, Object> attributesAssert =
assertThat(firstLog.getAttributes().asMap());
if (emitStableDatabaseSemconv()) {
attributesAssert.containsEntry(
CODE_FUNCTION_NAME, "org.springframework.boot.StartupInfoLogger.logStarting");
}
if (emitOldCodeSemconv()) {
attributesAssert
.containsEntry(CODE_NAMESPACE, "org.springframework.boot.StartupInfoLogger")
.containsEntry(CODE_FUNCTION, "logStarting");
}
}
}
protected void assertAdditionalMetrics() throws InterruptedException, TimeoutException {
if (!isFlightRecorderAvailable()) {
return;
}
// force gc so we'd get the jvm.gc.duration metric
GcUtils.awaitGc(Duration.ofSeconds(10));
double javaVersion = Double.parseDouble(System.getProperty("java.specification.version"));
// JFR based metrics
// Note: jvm.network.io is intentionally omitted because it is flaky across all JDK versions.
for (String metric :
asList(
"jvm.cpu.count",
"jvm.buffer.count",
"jvm.class.count",
"jvm.cpu.context_switch",
"jvm.cpu.longlock",
"jvm.system.cpu.utilization",
"jvm.gc.duration",
"jvm.memory.init",
"jvm.memory.used",
"jvm.memory.allocation",
"jvm.thread.count")) {
// cpu longlock is missing on jdk 25
if (javaVersion >= 25 && "jvm.cpu.longlock".equals(metric)) {
continue;
}
testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry", metric, AbstractIterableAssert::isNotEmpty);
}
}
private static boolean isFlightRecorderAvailable() {
try {
return (boolean)
Class.forName("jdk.jfr.FlightRecorder").getMethod("isAvailable").invoke(null);
} catch (ReflectiveOperationException ignored) {
return false;
}
}
@Test
void databaseQuery() {
testing.runWithSpan(
"server",
() -> {
jdbcTemplate.query(
"select name from customer where id = 1", (rs, rowNum) -> rs.getString("name"));
});
// 1 is not replaced by ?, otel.instrumentation.common.db.query-sanitization.enabled=false
testing.waitAndAssertTraces(
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
span -> span.hasName("server"),
span ->
span.hasKind(SpanKind.CLIENT)
.hasAttribute(DB_STATEMENT, "select name from customer where id = 1")));
}
@Test
void restTemplate() {
restTemplateCall(OtelSpringStarterSmokeTestController.PING);
testing.waitAndAssertTraces(
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
span -> HttpSpanDataAssert.create(span).assertClientGetRequest("/ping"),
span -> span.hasKind(SpanKind.SERVER).hasAttribute(HTTP_ROUTE, "/ping"),
span -> withSpanAssert(span)));
}
@Test
void shouldRedactSomeUrlParameters() {
restTemplateCall("/test?X-Goog-Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0");
testing.waitAndAssertTraces(
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
span ->
HttpSpanDataAssert.create(span)
.assertClientGetRequest("/test?X-Goog-Signature=REDACTED"),
span -> span.hasKind(SpanKind.SERVER).hasAttribute(HTTP_ROUTE, "/test")));
}
}