11
11
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
12
12
import io .opentelemetry .sdk .autoconfigure .spi .ResourceProvider ;
13
13
import io .opentelemetry .sdk .resources .Resource ;
14
+ import io .opentelemetry .sdk .resources .ResourceBuilder ;
14
15
import io .opentelemetry .semconv .ResourceAttributes ;
15
16
import java .io .IOException ;
16
17
import java .io .InputStream ;
17
18
import java .util .Optional ;
18
19
import java .util .Properties ;
19
20
import java .util .logging .Logger ;
20
21
22
+ /**
23
+ * Detects <code>service.name</code> and <code>service.version</code> from Spring Boot's <code>
24
+ * build-info.properties</code> file.
25
+ *
26
+ * <p><Use the following snippet in your gradle file to generate the build-info.properties file:
27
+ *
28
+ * <pre>{@code
29
+ * springBoot {
30
+ * buildInfo {
31
+ * }
32
+ * }
33
+ * }</pre>
34
+ */
21
35
@ AutoService (ResourceProvider .class )
22
36
public class SpringBootServiceVersionDetector implements ResourceProvider {
23
37
@@ -37,29 +51,41 @@ public SpringBootServiceVersionDetector() {
37
51
38
52
@ Override
39
53
public Resource createResource (ConfigProperties config ) {
40
- return getServiceVersionFromBuildInfo ()
54
+ return getPropertiesFromBuildInfo ()
41
55
.map (
42
- version -> {
43
- logger .log (FINE , "Auto-detected Spring Boot service version: {0}" , version );
44
- return Resource .builder ().put (ResourceAttributes .SERVICE_VERSION , version ).build ();
56
+ properties -> {
57
+ logger .log (FINE , "Auto-detected Spring Boot service version: {0}" , properties );
58
+ ResourceBuilder builder = Resource .builder ();
59
+
60
+ String version = properties .getProperty ("build.version" );
61
+ if (version != null ) {
62
+ builder .put (ResourceAttributes .SERVICE_VERSION , version );
63
+ }
64
+
65
+ String name = properties .getProperty ("build.name" );
66
+ if (name != null ) {
67
+ builder .put (ResourceAttributes .SERVICE_NAME , name );
68
+ }
69
+
70
+ return builder .build ();
45
71
})
46
72
.orElseGet (Resource ::empty );
47
73
}
48
74
49
- private Optional <String > getServiceVersionFromBuildInfo () {
75
+ private Optional <Properties > getPropertiesFromBuildInfo () {
50
76
try (InputStream in = system .openClasspathResource ("META-INF" , "build-info.properties" )) {
51
- return in != null ? getServiceVersionPropertyFromStream (in ) : Optional .empty ();
77
+ return in != null ? getPropertiesFromStream (in ) : Optional .empty ();
52
78
} catch (Exception e ) {
53
79
return Optional .empty ();
54
80
}
55
81
}
56
82
57
- private static Optional <String > getServiceVersionPropertyFromStream (InputStream in ) {
83
+ private static Optional <Properties > getPropertiesFromStream (InputStream in ) {
58
84
Properties properties = new Properties ();
59
85
try {
60
86
// Note: load() uses ISO 8859-1 encoding, same as spring uses by default for property files
61
87
properties .load (in );
62
- return Optional .ofNullable (properties . getProperty ( "build.version" ) );
88
+ return Optional .of (properties );
63
89
} catch (IOException e ) {
64
90
return Optional .empty ();
65
91
}
0 commit comments