1
1
import static jakarta .ws .rs .core .Response .Status .Family .REDIRECTION ;
2
2
import static jakarta .ws .rs .core .Response .Status .Family .SUCCESSFUL ;
3
3
4
+ import io .opentelemetry .api .GlobalOpenTelemetry ;
5
+ import io .opentelemetry .api .common .AttributeKey ;
6
+ import io .opentelemetry .api .common .Attributes ;
7
+
4
8
import java .io .File ;
9
+ import java .io .IOException ;
10
+ import java .nio .file .Files ;
5
11
import java .nio .file .Path ;
6
12
import java .util .Arrays ;
13
+ import java .util .Collection ;
7
14
import java .util .EnumSet ;
8
15
import java .util .Map ;
9
16
import java .util .Set ;
16
23
import jakarta .ws .rs .core .UriBuilder ;
17
24
18
25
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
+
19
52
public static void main (final String [] args ) throws InterruptedException , ExecutionException {
20
53
final var port = Integer .parseInt (System .getenv ().getOrDefault ("IP_PORT" , "8080" ));
21
54
@@ -57,15 +90,7 @@ public Set<Class<?>> getClasses() {
57
90
return Set .of (MonikaResource .class );
58
91
}
59
92
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 );
69
94
70
95
@ Override
71
96
public Map <String , Object > getProperties () {
@@ -81,7 +106,7 @@ private static HEALTH checkHealth(final int port, final boolean ignoreErrors) {
81
106
System .out .println ("HEALTHCHECK " + uri );
82
107
if (!EnumSet .of (SUCCESSFUL , REDIRECTION ).contains (ClientBuilder .newClient ().target (uri ).request ().get ().getStatusInfo ().getFamily ()))
83
108
throw new WebApplicationException ();
84
-
109
+
85
110
System .out .println ("HEALTHY" );
86
111
return HEALTH .SUCCESS ;
87
112
} catch (final Throwable t ) {
0 commit comments