Skip to content

Commit cd6d1dd

Browse files
committed
πŸ‘±πŸ»β€β™€οΈ Monika: Experimental OpenTelemetry support
This commit tells Monika to expose the `free_space` metric for each monitored path using the *OpenTelemetry SDK for Java*.
1 parent 9a1a836 commit cd6d1dd

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

β€Žbuild

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ cp src/main/docker/Dockerfile target
44
pushd ./target
55
docker build -t monika .
66
popd
7-
docker run -it --rm -P monika
7+
docker run -it --rm -P -e OTEL_JAVA_GLOBAL_AUTOCONFIGURE_ENABLED=True -e OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf -e OTEL_EXPORTER_OTLP_ENDPOINT=https://grafana.ijug.eu:8080/otlp -e OTEL_SERVICE_NAME=Monika -e OTEL_METRIC_EXPORT_INTERVAL=15000 -e OTEL_METRICS_EXPORTER=otlp -e OTEL_LOGS_EXPORTER=none -e OTEL_TRACES_EXPORTER=none monika
88

β€Žpom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<maven.compiler.release>17</maven.compiler.release>
1313
<jaxrs.version>3.1.0</jaxrs.version>
1414
<jersey.version>3.1.7</jersey.version>
15+
<otelsdk.version>1.39.0</otelsdk.version>
1516
</properties>
1617

1718
<dependencyManagement>
@@ -23,6 +24,14 @@
2324
<type>pom</type>
2425
<scope>import</scope>
2526
</dependency>
27+
28+
<dependency>
29+
<groupId>io.opentelemetry</groupId>
30+
<artifactId>opentelemetry-bom</artifactId>
31+
<version>${otelsdk.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
2635
</dependencies>
2736
</dependencyManagement>
2837

@@ -50,6 +59,26 @@
5059
<artifactId>jersey-netty-connector</artifactId>
5160
<scope>runtime</scope>
5261
</dependency>
62+
63+
<dependency>
64+
<groupId>io.opentelemetry</groupId>
65+
<artifactId>opentelemetry-api</artifactId>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>io.opentelemetry</groupId>
70+
<artifactId>opentelemetry-exporter-otlp</artifactId>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>io.opentelemetry</groupId>
75+
<artifactId>opentelemetry-exporter-logging</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>io.opentelemetry</groupId>
80+
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
81+
</dependency>
5382
</dependencies>
5483

5584
<build>
@@ -117,6 +146,12 @@
117146
<exclude>**/*.so</exclude>
118147
</excludes>
119148
</filter>
149+
<filter>
150+
<artifact>io.opentelemetry:*</artifact>
151+
<includes>
152+
<include>**/*</include>
153+
</includes>
154+
</filter>
120155
</filters>
121156
<transformers>
122157
<transformer

β€Žsrc/main/java/MonikaBootstrap.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import static jakarta.ws.rs.core.Response.Status.Family.REDIRECTION;
22
import static jakarta.ws.rs.core.Response.Status.Family.SUCCESSFUL;
33

4+
import io.opentelemetry.api.GlobalOpenTelemetry;
5+
import io.opentelemetry.api.common.AttributeKey;
6+
import io.opentelemetry.api.common.Attributes;
7+
48
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
511
import java.nio.file.Path;
612
import java.util.Arrays;
13+
import java.util.Collection;
714
import java.util.EnumSet;
815
import java.util.Map;
916
import java.util.Set;
@@ -16,6 +23,32 @@
1623
import jakarta.ws.rs.core.UriBuilder;
1724

1825
public class MonikaBootstrap {
26+
27+
private static final Collection<Path> mp;
28+
static {
29+
final var meter = GlobalOpenTelemetry.getMeter("eu.ijug.free_disk_space");
30+
final var monitoredPaths = System.getenv("MONITORED_PATHS");
31+
mp = monitoredPaths == null || monitoredPaths.isBlank() ? Set.of(Path.of("/"))
32+
: Arrays.asList(monitoredPaths.split(File.pathSeparator)).stream().map(Path::of).toList();
33+
assert mp != null && !mp.isEmpty() : "Monitored paths cannot be null nor empty";
34+
mp.forEach(monitoredPath -> System.out.printf("Monitoring: %s%n", monitoredPath));
35+
meter.gaugeBuilder("system.filesystem.free")
36+
.setDescription("Free disk space (measured in percent)")
37+
.setUnit("Percent")
38+
.buildWithCallback(measurement -> {
39+
mp.forEach(monitoredPath -> {
40+
try {
41+
final var fileStore = Files.getFileStore(monitoredPath);
42+
final var percentFree = Math.toIntExact(100 * fileStore.getUsableSpace() / fileStore.getTotalSpace());
43+
System.out.printf("%s is %d %% free%n", monitoredPath, percentFree);
44+
measurement.record(percentFree, Attributes.of(AttributeKey.stringKey("system.filesystem.mountpoint"), monitoredPath.toString()));
45+
} catch (final IOException e) {
46+
e.printStackTrace();
47+
}
48+
});
49+
});
50+
}
51+
1952
public static void main(final String[] args) throws InterruptedException, ExecutionException {
2053
final var port = Integer.parseInt(System.getenv().getOrDefault("IP_PORT", "8080"));
2154

@@ -57,15 +90,7 @@ public Set<Class<?>> getClasses() {
5790
return Set.of(MonikaResource.class);
5891
}
5992

60-
private static final Map<String, Object> PROPERTIES;
61-
static {
62-
final var monitoredPaths = System.getenv("MONITORED_PATHS");
63-
64-
final var mp = monitoredPaths == null || monitoredPaths.isBlank() ? Set.of(Path.of("/"))
65-
: Arrays.asList(monitoredPaths.split(File.pathSeparator)).stream().map(Path::of).toList();
66-
mp.forEach(monitoredPath -> System.out.printf("Monitoring: %s%n", monitoredPath));
67-
PROPERTIES = Map.of("MONITORED_PATHS", mp);
68-
}
93+
private static final Map<String, Object> PROPERTIES = Map.of("MONITORED_PATHS", mp);
6994

7095
@Override
7196
public Map<String, Object> getProperties() {
@@ -81,7 +106,7 @@ private static HEALTH checkHealth(final int port, final boolean ignoreErrors) {
81106
System.out.println("HEALTHCHECK " + uri);
82107
if (!EnumSet.of(SUCCESSFUL, REDIRECTION).contains(ClientBuilder.newClient().target(uri).request().get().getStatusInfo().getFamily()))
83108
throw new WebApplicationException();
84-
109+
85110
System.out.println("HEALTHY");
86111
return HEALTH.SUCCESS;
87112
} catch (final Throwable t) {

0 commit comments

Comments
Β (0)