Add MeterRegistry monitoring

- update and cleanup in README.md
- extract webclient call from registerDevice and add metrics in DeviceRegistryService
- add WebClientCalls.kt
- update kotlin logging dependency
- add MeterRegistry dependency
This commit is contained in:
2024-12-20 17:09:22 +01:00
parent 10f6486e37
commit 93680e50d5
9 changed files with 138 additions and 52 deletions

View File

@@ -1,24 +1,46 @@
package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import ltd.hlaeja.library.deviceRegistry.Device
import ltd.hlaeja.util.logCall
import ltd.hlaeja.property.DeviceRegistryProperty
import org.springframework.http.HttpStatus.REQUEST_TIMEOUT
import ltd.hlaeja.util.deviceRegistryCreateDevice
import org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE
import org.springframework.stereotype.Service
import org.springframework.web.ErrorResponseException
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBodyOrNull
import org.springframework.web.reactive.function.client.WebClientRequestException
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@Service
class DeviceRegistryService(
meterRegistry: MeterRegistry,
private val webClient: WebClient,
private val deviceRegistryProperty: DeviceRegistryProperty,
) {
private val registerDeviceSuccess = Counter.builder("device.registry.success")
.description("Number of successful device registrations")
.register(meterRegistry)
private val registerDeviceFailure = Counter.builder("device.registry.failure")
.description("Number of failed device registrations")
.register(meterRegistry)
suspend fun registerDevice(
request: Device.Request,
): Device.Response = webClient.post()
.uri("${deviceRegistryProperty.url}/device".also(::logCall))
.bodyValue(request)
.retrieve()
.awaitBodyOrNull<Device.Response>() ?: throw ResponseStatusException(REQUEST_TIMEOUT)
): Device.Response = try {
webClient.deviceRegistryCreateDevice(request, deviceRegistryProperty)
.also { registerDeviceSuccess.increment() }
} catch (e: ErrorResponseException) {
registerDeviceFailure.increment()
throw e
} catch (e: WebClientRequestException) {
registerDeviceFailure.increment()
log.error(e) { "Error device registry" }
throw ResponseStatusException(SERVICE_UNAVAILABLE)
}
}

View File

@@ -1,9 +1,7 @@
package ltd.hlaeja.util
import mu.KotlinLogging
import io.github.oshai.kotlinlogging.KotlinLogging
private val log = KotlinLogging.logger {}
fun logCall(url: String) {
log.debug("calling: {}", url)
}
fun logCall(url: String) = log.debug { "calling: $url" }

View File

@@ -0,0 +1,17 @@
package ltd.hlaeja.util
import ltd.hlaeja.library.deviceRegistry.Device
import ltd.hlaeja.property.DeviceRegistryProperty
import org.springframework.http.HttpStatus.REQUEST_TIMEOUT
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBodyOrNull
import org.springframework.web.server.ResponseStatusException
suspend fun WebClient.deviceRegistryCreateDevice(
request: Device.Request,
property: DeviceRegistryProperty,
): Device.Response = post()
.uri("${property.url}/device".also(::logCall))
.bodyValue(request)
.retrieve()
.awaitBodyOrNull<Device.Response>() ?: throw ResponseStatusException(REQUEST_TIMEOUT)

View File

@@ -10,6 +10,25 @@ spring:
name: "%APP_BUILD_OS_NAME%"
version: "%APP_BUILD_OS_VERSION%"
management:
endpoints:
enabled-by-default: false
web:
exposure:
include: "health,info"
endpoint:
health:
enabled: true
show-details: always
info:
enabled: true
influx:
metrics:
export:
api-version: v2
bucket: hlaeja
org: hlaeja_ltd
---
###############################
### Development environment ###
@@ -30,6 +49,16 @@ server:
device-registry:
url: http://localhost:9010
management:
metrics:
tags:
application: register-api
influx:
metrics:
export:
enabled: false
token: %INFLUXDB_TOKEN%
---
##########################
### Docker environment ###
@@ -39,6 +68,15 @@ spring:
activate:
on-profile: docker
management:
metrics:
tags:
application: register-api
influx:
metrics:
export:
uri: http://InfluxDB:8086
server:
port: 8443
ssl: