Skip to content

Commit 3147149

Browse files
committed
OpenTelemetry Java agent extension to exclude telemetry log
1 parent 1fa1cf1 commit 3147149

9 files changed

Lines changed: 230 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Use OpenTelemetry Java Agent Extensions to filter logs based on body content
2+
3+
This is a sample app demonstrating how to filter logs based on their body content using a custom `LogRecordExporter`.
4+
5+
## Build the extension artifact
6+
7+
To build the extension artifact, from this directory:
8+
9+
`cd ../extensions/FilterLogBasedOnBody`
10+
`../../../../mvnw package`
11+
12+
To run the application with the extension and Java agent, from this directory:
13+
14+
`cd ../../TelemetryFilteredBaseOnLogBody`
15+
16+
```
17+
../../../mvnw package
18+
export APPLICATIONINSIGHTS_CONNECTION_STRING=<Copy connection string from Application Insights Resource Overview>
19+
export APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL=debug
20+
21+
java -javaagent:target/agent/applicationinsights-agent.jar -Dotel.javaagent.extensions=../extensions/FilterLogBasedOnBody/target/FilterLogBasedOnBody-1.0-SNAPSHOT.jar -jar target/app.jar
22+
```
23+
24+
At the application startup, Spring Boot logs an "Initializing Spring embedded WebApplicationContext" message.
25+
26+
The OpenTelemetry extension excludes this log from the telemetry data.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.2.3</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.example</groupId>
14+
<artifactId>TelemetryFilteredBaseOnLogBody</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
17+
<properties>
18+
<java.version>17</java.version>
19+
<applicationinsights.agent.version>3.7.6</applicationinsights.agent.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.opentelemetry</groupId>
29+
<artifactId>opentelemetry-api</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-autoconfigure</artifactId>
34+
<version>3.2.3</version>
35+
<scope>compile</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
<configuration>
45+
<finalName>app</finalName>
46+
</configuration>
47+
</plugin>
48+
<plugin>
49+
<artifactId>maven-dependency-plugin</artifactId>
50+
<version>3.9.0</version>
51+
<executions>
52+
<execution>
53+
<id>copy-agent</id>
54+
<phase>package</phase>
55+
<goals>
56+
<goal>copy</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
<configuration>
61+
<artifactItems>
62+
<artifactItem>
63+
<groupId>com.microsoft.azure</groupId>
64+
<artifactId>applicationinsights-agent</artifactId>
65+
<version>${applicationinsights.agent.version}</version>
66+
<outputDirectory>${project.build.directory}/agent</outputDirectory>
67+
<destFileName>applicationinsights-agent.jar</destFileName>
68+
</artifactItem>
69+
</artifactItems>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<artifactId>extensions</artifactId>
8+
<groupId>com.example</groupId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>FilterLogBasedOnBody</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<opentelemetry.version>1.57.0</opentelemetry.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.opentelemetry</groupId>
22+
<artifactId>opentelemetry-sdk-common</artifactId>
23+
<version>${opentelemetry.version}</version>
24+
<scope>compile</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.opentelemetry</groupId>
28+
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
29+
<version>${opentelemetry.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.opentelemetry</groupId>
33+
<artifactId>opentelemetry-sdk-logs</artifactId>
34+
<version>${opentelemetry.version}</version>
35+
<scope>compile</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-api</artifactId>
40+
<version>2.0.17</version>
41+
<scope>compile</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.google.auto.service</groupId>
45+
<artifactId>auto-service-annotations</artifactId>
46+
<version>1.1.1</version>
47+
</dependency>
48+
</dependencies>
49+
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.example;
5+
6+
import com.google.auto.service.AutoService;
7+
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
8+
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
9+
10+
@AutoService(AutoConfigurationCustomizerProvider.class)
11+
public class LogBodyAutoConfigurationCustomizerProvider implements AutoConfigurationCustomizerProvider {
12+
13+
@Override
14+
public void customize(AutoConfigurationCustomizer autoConfiguration) {
15+
autoConfiguration.addLogRecordExporterCustomizer((logRecordExporter, configProperties) -> new LogFilter(logRecordExporter));
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example;
2+
3+
import io.opentelemetry.api.common.Value;
4+
import io.opentelemetry.sdk.common.CompletableResultCode;
5+
import io.opentelemetry.sdk.logs.data.LogRecordData;
6+
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
7+
8+
import java.util.ArrayList;
9+
import java.util.Collection;
10+
import java.util.List;
11+
12+
public class LogFilter implements LogRecordExporter {
13+
14+
private final LogRecordExporter delegate;
15+
16+
public LogFilter(LogRecordExporter delegate) {
17+
this.delegate = delegate;
18+
}
19+
20+
@Override
21+
public CompletableResultCode export(Collection<LogRecordData> collection) {
22+
List<LogRecordData> filteredLogs = new ArrayList<>();
23+
for (LogRecordData logRecordData : collection) {
24+
if (shouldInclude(logRecordData)) {
25+
filteredLogs.add(logRecordData);
26+
}
27+
}
28+
return delegate.export(filteredLogs);
29+
}
30+
31+
private boolean shouldInclude(LogRecordData logRecordData) {
32+
Value<?> bodyValue = logRecordData.getBodyValue();
33+
String logMessageToExclude = "Initializing Spring embedded WebApplicationContext";
34+
return bodyValue != null && !bodyValue.asString().equals(logMessageToExclude);
35+
}
36+
37+
@Override
38+
public CompletableResultCode flush() {
39+
return delegate.flush();
40+
}
41+
42+
@Override
43+
public CompletableResultCode shutdown() {
44+
return delegate.shutdown();
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.example.LogBodyAutoConfigurationCustomizerProvider

opentelemetry-api/java-agent/extensions/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<packaging>pom</packaging>
1414
<modules>
1515
<module>CustomSpanExporter</module>
16+
<module>FilterSpanBasedOnDuration</module>
17+
<module>FilterLogBasedOnBody</module>
1618
</modules>
1719

1820
<properties>

opentelemetry-api/java-agent/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
<module>TelemetryFilteredBaseOnSpanEvents</module>
1717
<module>extensions</module>
1818
<module>TelemetryFilteredBaseOnRequestDuration</module>
19+
<module>TelemetryFilteredBaseOnLogBody</module>
1920
<module>extensions/FilterSpanBasedOnDuration</module>
21+
<module>extensions/FilterLogBasedOnBody</module>
2022
<module>AzureHttpClient</module>
2123
</modules>
2224

0 commit comments

Comments
 (0)