From bb812965f6703f8a34a818cdbae90217d8ee69b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jerna=C5=9B?= Date: Mon, 11 Aug 2025 15:06:23 +0200 Subject: [PATCH] Add missing Kotlin Observability examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See gh-46775 Signed-off-by: Łukasz Jernaś --- .../OpenTelemetryAppenderInitializer.kt | 31 ++++++++++++++++ .../observability/MyCustomObservation.kt | 36 +++++++++++++++++++ .../MyObservationPredicate.kt | 30 ++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/loggers/opentelemetry/OpenTelemetryAppenderInitializer.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/MyCustomObservation.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/preventingobservations/MyObservationPredicate.kt diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/loggers/opentelemetry/OpenTelemetryAppenderInitializer.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/loggers/opentelemetry/OpenTelemetryAppenderInitializer.kt new file mode 100644 index 00000000000..d393896a115 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/loggers/opentelemetry/OpenTelemetryAppenderInitializer.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.actuator.loggers.opentelemetry + +import io.opentelemetry.api.OpenTelemetry +import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender +import org.springframework.beans.factory.InitializingBean +import org.springframework.stereotype.Component + +@Component +class OpenTelemetryAppenderInitializer( + private val openTelemetry: OpenTelemetry +) : InitializingBean { + + override fun afterPropertiesSet() = OpenTelemetryAppender.install(openTelemetry) + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/MyCustomObservation.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/MyCustomObservation.kt new file mode 100644 index 00000000000..052e9aaf368 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/MyCustomObservation.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.actuator.observability + +import io.micrometer.observation.Observation +import io.micrometer.observation.ObservationRegistry; + +import org.springframework.stereotype.Component + +@Component +class MyCustomObservation(private val observationRegistry: ObservationRegistry) { + + fun doSomething() { + Observation.createNotStarted("doSomething", observationRegistry) + .lowCardinalityKeyValue("locale", "en-US") + .highCardinalityKeyValue("userId", "42") + .observe { + // Execute business logic here + } + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/preventingobservations/MyObservationPredicate.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/preventingobservations/MyObservationPredicate.kt new file mode 100644 index 00000000000..2445557061e --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/actuator/observability/preventingobservations/MyObservationPredicate.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.actuator.observability.preventingobservations + +import io.micrometer.observation.Observation.Context +import io.micrometer.observation.ObservationPredicate +import org.springframework.stereotype.Component + +@Component +class MyObservationPredicate : ObservationPredicate { + + override fun test(name: String, context: Context): Boolean { + return !name.contains("denied") + } + +}