diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ElasticCommonSchemaProperties.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ElasticCommonSchemaProperties.java index ea033b2cff0a..6191481c6b02 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ElasticCommonSchemaProperties.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ElasticCommonSchemaProperties.java @@ -71,14 +71,19 @@ public record Service(@Nullable String name, @Nullable String version, @Nullable static final Service NONE = new Service(null, null, null, null); void jsonMembers(Members members) { - members.add("service").usingMembers((service) -> { + members.add("service").whenNot((instance) -> isEmpty()).usingMembers((service) -> { service.add("name", this::name).whenHasLength(); service.add("version", this::version).whenHasLength(); service.add("environment", this::environment).whenHasLength(); - service.add("node").usingMembers((node) -> node.add("name", this::nodeName).whenHasLength()); + service.add("node", this::nodeName).whenHasLength().usingMembers((node) -> node.add("name")); }); } + private boolean isEmpty() { + return !StringUtils.hasLength(this.name) && !StringUtils.hasLength(this.version) + && !StringUtils.hasLength(this.environment) && !StringUtils.hasLength(this.nodeName); + } + Service withDefaults(Environment environment) { String name = withFallbackProperty(environment, this.name, "spring.application.name"); String version = withFallbackProperty(environment, this.version, "spring.application.version"); diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/structured/ElasticCommonSchemaPropertiesTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/structured/ElasticCommonSchemaPropertiesTests.java index d72318648c41..6d7a81e54041 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/structured/ElasticCommonSchemaPropertiesTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/structured/ElasticCommonSchemaPropertiesTests.java @@ -78,6 +78,23 @@ void addToJsonMembersCreatesValidJson() { }""".replaceAll("\\s+", "")); } + @Test + void addToJsonMembersWhenNoNodeNameDoesNotAddNode() { + ElasticCommonSchemaProperties properties = new ElasticCommonSchemaProperties( + new Service("spring", "1.2.3", "prod", null)); + JsonWriter writer = JsonWriter.of(properties::jsonMembers); + assertThat(writer.writeToString(properties)).isEqualTo(""" + {"service":{"name":"spring","version":"1.2.3","environment":"prod"}}"""); + } + + @Test + void addToJsonMembersWhenNoServiceValuesDoesNotAddService() { + ElasticCommonSchemaProperties properties = new ElasticCommonSchemaProperties( + new Service(null, null, null, null)); + JsonWriter writer = JsonWriter.of(properties::jsonMembers); + assertThat(writer.writeToString(properties)).isEqualTo("{}"); + } + @Test void shouldRegisterRuntimeHints() throws Exception { RuntimeHints hints = new RuntimeHints();