Skip to content

Commit e856547

Browse files
authored
Add tests for Spring Monitor (Azure#36498)
1 parent b3d0cdb commit e856547

File tree

11 files changed

+208
-74
lines changed

11 files changed

+208
-74
lines changed

eng/versioning/external_dependencies.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ com.github.spotbugs:spotbugs-annotations;4.2.2
2424
com.github.spotbugs:spotbugs-maven-plugin;4.2.2
2525
com.google.code.gson:gson;2.10
2626
com.google.guava:guava;32.0.1-jre
27+
com.h2database:h2;2.1.214
2728
com.h3xstream.findsecbugs:findsecbugs-plugin;1.9.0
2829
com.microsoft.azure:applicationinsights-core;3.4.1
2930
com.microsoft.azure:azure-annotations;1.7.0
@@ -127,6 +128,7 @@ org.springframework.boot:spring-boot-starter-actuator;2.7.14
127128
org.springframework.boot:spring-boot-starter-aop;2.7.14
128129
org.springframework.boot:spring-boot-starter-cache;2.7.14
129130
org.springframework.boot:spring-boot-starter-data-redis;2.7.14
131+
org.springframework.boot:spring-boot-starter-data-jdbc;2.7.14
130132
org.springframework.boot:spring-boot-starter-integration;2.7.14
131133
org.springframework.boot:spring-boot-starter-logging;2.7.14
132134
org.springframework.boot:spring-boot-starter-test;2.7.14
@@ -204,8 +206,10 @@ io.opentelemetry:opentelemetry-api-logs;1.26.0-alpha
204206
io.opentelemetry:opentelemetry-sdk-testing;1.28.0
205207
io.opentelemetry:opentelemetry-sdk-extension-autoconfigure;1.28.0
206208
io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi;1.28.0
207-
io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java8;1.27.0-alpha
208-
io.opentelemetry.instrumentation:opentelemetry-spring-boot-starter;1.27.0-alpha
209+
io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java8;1.28.0-alpha
210+
io.opentelemetry.instrumentation:opentelemetry-spring-boot-starter;1.28.0-alpha
211+
io.opentelemetry.instrumentation:opentelemetry-jdbc;1.28.0-alpha
212+
io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0;1.28.0-alpha
209213
io.projectreactor:reactor-test;3.4.31
210214
io.github.hakky54:logcaptor;2.9.0
211215
com.squareup.okio:okio;3.3.0
@@ -268,6 +272,7 @@ net.jonathangiles.tools:dependencyChecker-maven-plugin;1.0.6
268272
net.jonathangiles.tools:whitelistgenerator-maven-plugin;1.0.2
269273
org.apache.commons:commons-collections4;4.4
270274
org.apache.commons:commons-text;1.10.0
275+
org.apache.commons:commons-dbcp2;2.9.0
271276
org.apache.maven:maven-plugin-api;3.8.6
272277
org.apache.maven:maven-project;2.2.1
273278
org.apache.maven.plugin-tools:maven-plugin-annotations;3.6.4

sdk/spring/spring-cloud-azure-starter-monitor-test/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,36 @@
3131
<artifactId>spring-cloud-azure-starter-monitor</artifactId>
3232
<version>1.0.0-beta.1</version> <!-- {x-version-update;com.azure.spring:azure-monitor-spring-native;dependency} -->
3333
</dependency>
34+
35+
<!-- OTel dependencies -->
36+
<dependency>
37+
<groupId>io.opentelemetry.instrumentation</groupId>
38+
<artifactId>opentelemetry-jdbc</artifactId>
39+
<version>1.28.0-alpha</version><!-- {x-version-update;io.opentelemetry.instrumentation:opentelemetry-jdbc;external_dependency} -->
40+
</dependency>
41+
<dependency>
42+
<groupId>io.opentelemetry.instrumentation</groupId>
43+
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
44+
<version>1.28.0-alpha</version><!-- {x-version-update;io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0;external_dependency} -->
45+
<scope>runtime</scope>
46+
</dependency>
47+
<!-- End of OTel dependencies -->
48+
3449
<dependency>
3550
<groupId>org.springframework.boot</groupId>
3651
<artifactId>spring-boot-starter-web</artifactId>
3752
<version>2.7.14</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-web;external_dependency} -->
3853
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
57+
<version>2.7.14</version><!-- {x-version-update;org.springframework.boot:spring-boot-starter-data-jdbc;external_dependency} -->
58+
</dependency>
59+
<dependency>
60+
<groupId>org.apache.commons</groupId>
61+
<artifactId>commons-dbcp2</artifactId>
62+
<version>2.9.0</version> <!-- {x-version-update;org.apache.commons:commons-dbcp2;external_dependency} -->
63+
</dependency>
3964
<dependency>
4065
<groupId>org.springframework.boot</groupId>
4166
<artifactId>spring-boot-starter-test</artifactId>
@@ -48,6 +73,11 @@
4873
<version>1.19.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
4974
<scope>test</scope>
5075
</dependency>
76+
<dependency>
77+
<groupId>com.h2database</groupId>
78+
<artifactId>h2</artifactId>
79+
<version>2.1.214</version> <!-- {x-version-update;com.h2database:h2;external_dependency} -->
80+
</dependency>
5181
</dependencies>
5282

5383
<!-- Apply checkstyle, spotbugs, and revapi for all java versions -->

sdk/spring/spring-cloud-azure-starter-monitor-test/src/main/java/com/azure/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13
package com.azure;
24

35
import org.springframework.boot.SpringApplication;

sdk/spring/spring-cloud-azure-starter-monitor-test/src/main/java/com/azure/Controller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13
package com.azure;
24

35
import org.springframework.web.bind.annotation.GetMapping;
46
import org.springframework.web.bind.annotation.RestController;
57

68
@RestController
79
public class Controller {
8-
910
public static final String URL = "/controller-url";
1011

1112
@GetMapping(URL)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure;
4+
5+
import io.opentelemetry.api.OpenTelemetry;
6+
import io.opentelemetry.instrumentation.jdbc.datasource.OpenTelemetryDataSource;
7+
import javax.sql.DataSource;
8+
import org.apache.commons.dbcp2.BasicDataSource;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
@Configuration
13+
public class DatasourceConfig {
14+
15+
@Bean
16+
public DataSource dataSource(OpenTelemetry openTelemetry) {
17+
BasicDataSource dataSource = new BasicDataSource();
18+
dataSource.setDriverClassName("org.h2.Driver");
19+
dataSource.setUrl("jdbc:h2:mem:db");
20+
dataSource.setUsername("username");
21+
dataSource.setPassword("pwd");
22+
return new OpenTelemetryDataSource(dataSource, openTelemetry);
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure;
4+
5+
import org.springframework.boot.context.event.ApplicationReadyEvent;
6+
import org.springframework.context.event.EventListener;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class SqlExecutor {
12+
13+
private final JdbcTemplate jdbcTemplate;
14+
15+
public SqlExecutor(JdbcTemplate jdbcTemplate) {
16+
this.jdbcTemplate = jdbcTemplate;
17+
}
18+
19+
@EventListener(ApplicationReadyEvent.class)
20+
public void createDatabaseTable() {
21+
jdbcTemplate.execute("create table test_table (id bigint not null, primary key (id))");
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="OpenTelemetry"
5+
class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="OpenTelemetry"/>
10+
</root>
11+
12+
</configuration>

sdk/spring/spring-cloud-azure-starter-monitor-test/src/test/java/com/azure/CustomValidationPolicy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13
package com.azure;
24

35
import com.azure.core.http.HttpPipelineCallContext;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure;
4+
5+
import static java.util.concurrent.TimeUnit.SECONDS;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
import com.azure.core.http.*;
9+
import com.azure.core.http.policy.HttpPipelinePolicy;
10+
import com.azure.monitor.opentelemetry.exporter.implementation.models.*;
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
import java.util.List;
14+
import java.util.concurrent.CountDownLatch;
15+
import org.junit.jupiter.api.Test;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.boot.test.web.client.TestRestTemplate;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import reactor.util.annotation.Nullable;
22+
23+
@SpringBootTest(
24+
classes = {Application.class, SpringMonitorTest.TestConfiguration.class},
25+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
26+
properties = {
27+
"applicationinsights.connection.string=InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=https://test.in.applicationinsights.azure.com/;LiveEndpoint=https://test.livediagnostics.monitor.azure.com/"
28+
})
29+
public class SpringMonitorTest {
30+
31+
private static CountDownLatch countDownLatch;
32+
33+
private static CustomValidationPolicy customValidationPolicy;
34+
35+
@Autowired private TestRestTemplate restTemplate;
36+
37+
@Configuration(proxyBeanMethods = false)
38+
static class TestConfiguration {
39+
40+
@Bean
41+
HttpPipeline httpPipeline() {
42+
countDownLatch = new CountDownLatch(2);
43+
customValidationPolicy = new CustomValidationPolicy(countDownLatch);
44+
return getHttpPipeline(customValidationPolicy);
45+
}
46+
47+
HttpPipeline getHttpPipeline(@Nullable HttpPipelinePolicy policy) {
48+
return new HttpPipelineBuilder()
49+
.httpClient(HttpClient.createDefault())
50+
.policies(policy)
51+
.build();
52+
}
53+
}
54+
55+
@Test
56+
public void shouldMonitor() throws InterruptedException, MalformedURLException {
57+
String response = restTemplate.getForObject(Controller.URL, String.class);
58+
assertThat(response).isEqualTo("OK!");
59+
60+
countDownLatch.await(10, SECONDS);
61+
62+
assertThat(customValidationPolicy.url)
63+
.isEqualTo(new URL("https://test.in.applicationinsights.azure.com/v2.1/track"));
64+
65+
List<TelemetryItem> telemetryItems = customValidationPolicy.actualTelemetryItems;
66+
assertThat(telemetryItems.size()).isEqualTo(5);
67+
68+
// Log telemetry
69+
TelemetryItem telemetry1 = telemetryItems.get(0);
70+
assertThat(telemetry1.getName()).isEqualTo("Message");
71+
MonitorDomain logBaseData = telemetry1.getData().getBaseData();
72+
MessageData logData = (MessageData) logBaseData;
73+
assertThat(logData.getMessage())
74+
.isEqualTo("Initializing Spring DispatcherServlet 'dispatcherServlet'");
75+
assertThat(logData.getSeverityLevel()).isEqualTo(SeverityLevel.INFORMATION);
76+
77+
TelemetryItem telemetry2 = telemetryItems.get(1);
78+
assertThat(telemetry2.getName()).isEqualTo("Message");
79+
80+
TelemetryItem telemetry3 = telemetryItems.get(2);
81+
assertThat(telemetry3.getName()).isEqualTo("Message");
82+
83+
// SQL telemetry
84+
TelemetryItem telemetry4 = telemetryItems.get(3);
85+
assertThat(telemetry4.getName()).isEqualTo("RemoteDependency");
86+
MonitorDomain remoteBaseData = telemetry4.getData().getBaseData();
87+
RemoteDependencyData remoteDependencyData = (RemoteDependencyData) remoteBaseData;
88+
assertThat(remoteDependencyData.getType()).isEqualTo("SQL");
89+
assertThat(remoteDependencyData.getData())
90+
.isEqualTo("create table test_table (id bigint not null, primary key (id))");
91+
92+
// HTTP telemetry
93+
TelemetryItem telemetry5 = telemetryItems.get(4);
94+
assertThat(telemetry5.getName()).isEqualTo("Request");
95+
MonitorDomain requestBaseData = telemetry5.getData().getBaseData();
96+
RequestData requestData = (RequestData) requestBaseData;
97+
assertThat(requestData.getUrl()).contains(Controller.URL);
98+
assertThat(requestData.isSuccess()).isTrue();
99+
assertThat(requestData.getResponseCode()).isEqualTo("200");
100+
assertThat(requestData.getName()).isEqualTo("GET /controller-url");
101+
}
102+
}

sdk/spring/spring-cloud-azure-starter-monitor-test/src/test/java/com/azure/TelemetrySpringNativeTest.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)