Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/upgrade spring boot 3.3 #180

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -22,11 +22,14 @@
<jdk.version>17</jdk.version>

<logstash-encoder.version>7.4</logstash-encoder.version>
<kotlin.logging.version>3.0.5</kotlin.logging.version>
<projectreactor.kotlin.version>1.1.7</projectreactor.kotlin.version>

<kotest.version>5.9.1</kotest.version>
<kotest-spring.version>1.3.0</kotest-spring.version>
<mockk.version>1.13.10</mockk.version>
<kotest-spring.version>1.1.3</kotest-spring.version>
<mockk.version>1.13.11</mockk.version>
<wiremock.version>2.35.2</wiremock.version>

<maven.compiler.target>${jdk.version}</maven.compiler.target>
Expand Down Expand Up @@ -80,6 +83,12 @@
<version>${logstash-encoder.version}</version>
</dependency>

<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>${kotlin.logging.version}</version>
</dependency>

<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions tado-exporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
</dependency>

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import click.dobel.tado.api.Zone
import click.dobel.tado.api.ZoneState
import click.dobel.tado.exporter.apiclient.TadoConfigurationProperties
import click.dobel.tado.exporter.metrics.TadoMeterFactory
import click.dobel.tado.exporter.metrics.ValueFilteringCollectorRegistry
import click.dobel.tado.exporter.metrics.ValueFilteringPrometheusRegistry
import click.dobel.tado.util.aop.CallLoggingInterceptor
import com.github.benmanes.caffeine.cache.Caffeine
import io.micrometer.core.instrument.Meter
import io.micrometer.core.instrument.config.MeterFilter
import io.micrometer.core.instrument.config.MeterFilterReply
import io.prometheus.client.CollectorRegistry
import io.prometheus.metrics.model.registry.PrometheusRegistry
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.EnableCaching
Expand Down Expand Up @@ -76,7 +76,7 @@ class TadoExporterConfiguration {
fun aopLogger() = CallLoggingInterceptor()

@Bean
fun collectorRegistry(configProperties: TadoConfigurationProperties): CollectorRegistry {
return ValueFilteringCollectorRegistry(Double.NaN, true)
fun prometheusRegistry(): PrometheusRegistry {
return ValueFilteringPrometheusRegistry(Double.NaN)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import click.dobel.tado.exporter.apiclient.TadoConfigurationProperties
import click.dobel.tado.exporter.apiclient.auth.model.request.TadoAuthLoginRequest
import click.dobel.tado.exporter.apiclient.auth.model.request.TadoAuthRefreshRequest
import click.dobel.tado.exporter.apiclient.auth.model.response.TadoAuthResponse
import click.dobel.tado.util.logger
import mu.KLogging
import org.springframework.http.HttpRequest
import org.springframework.http.MediaType
import org.springframework.http.client.ClientHttpRequestExecution
Expand All @@ -20,9 +20,7 @@ class TadoAuthFilter(
private val authClient: AuthClient,
) : ClientHttpRequestInterceptor {

private companion object {
private val LOGGER = logger()
}
private companion object : KLogging()

private val lastAuthResponse = AtomicReference<TadoAuthResponse?>()

Expand All @@ -31,8 +29,8 @@ class TadoAuthFilter(
body: ByteArray,
execution: ClientHttpRequestExecution
): ClientHttpResponse {
request.headers.accept = listOf(MediaType.APPLICATION_JSON)
request.headers.setBearerAuth(getAccessToken())
request.headers.accept = listOf(MediaType.APPLICATION_JSON)
return execution.execute(request, body)
}

Expand All @@ -57,16 +55,16 @@ class TadoAuthFilter(
}

private fun newAuth(): TadoAuthResponse {
LOGGER.info("Obtaining new bearer token for {}.", configuration.username)
logger.info { "Obtaining new bearer token for ${configuration.username}." }
return authClient.token(TadoAuthLoginRequest(configuration))
}

private fun refreshAuth(refreshToken: String): TadoAuthResponse {
LOGGER.info("Refreshing bearer token.")
logger.info("Refreshing bearer token.")
return try {
authClient.token(TadoAuthRefreshRequest(configuration, refreshToken))
} catch (ex: RestClientException) {
LOGGER.warn("Refreshing bearer token failed.", ex)
logger.warn(ex) { "Refreshing bearer token failed." }
newAuth()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package click.dobel.tado.exporter.metrics

class DoubleValidator(private val invalidValue: Double) {
private val blockInfinite = invalidValue.isInfinite()
private val blockNaNs = invalidValue.isNaN()

fun isValid(value: Double): Boolean {
// special cases for NaN and Infinity: can't be compared using equality.
return !(blockInfinite && value.isInfinite()) &&
!(blockNaNs && value.isNaN()) &&
(invalidValue != value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package click.dobel.tado.exporter.metrics
import click.dobel.tado.api.UserHomes
import click.dobel.tado.api.Zone
import click.dobel.tado.exporter.apiclient.model.toEntrySet
import click.dobel.tado.util.logger
import mu.KLogging
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap

Expand All @@ -14,9 +14,7 @@ data class HomeModel(
val homes: Map<Int, UserHomes>
) {

companion object {
private val LOGGER = logger()
}
companion object : KLogging()

private val homeZones: ConcurrentMap<Int, MutableSet<ZoneEntry>> = ConcurrentHashMap()

Expand All @@ -40,23 +38,17 @@ data class HomeModel(
knownZoneEntries.removeAll(zoneEntriesToDelete)
knownZoneEntries.addAll(zoneEntriesToAdd)

LOGGER.info(
"Zones for home '{}' ({}) updated, {} zones total, {} zones added, {} zones deleted.",
userHomes.name,
userHomes.id,
knownZoneEntries.size,
zoneEntriesToAdd.size,
zoneEntriesToDelete.size
)
logger.info {
"Zones for home '${userHomes.name}' (${userHomes.id}) updated, ${knownZoneEntries.size} zones total, " +
"${zoneEntriesToAdd.size} zones added, ${zoneEntriesToDelete.size} zones deleted."
}

return zoneEntriesToAdd
}

private fun logZones(prefix: String, zones: Collection<ZoneEntry>) {
LOGGER.debug(
"${prefix}: {} ({})",
zones.size,
zones.joinToString(separator = ", ") { "${it.id}: ${it.name}" }
)
logger.debug {
"${prefix}: ${zones.size} (${zones.joinToString(separator = ", ") { "${it.id}: ${it.name}" }})"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package click.dobel.tado.exporter.metrics

import click.dobel.tado.exporter.apiclient.TadoApiClient
import click.dobel.tado.util.logger
import mu.KLogging
import org.springframework.stereotype.Component

@Component
class HomeModelRefresher(
private val tadoMeterFactory: TadoMeterFactory,
private val tadoApiClient: TadoApiClient
) {
companion object {
private val LOGGER = logger()
companion object : KLogging() {

private fun initializeHomeModel(
tadoMeterFactory: TadoMeterFactory,
tadoApiClient: TadoApiClient
): HomeModel {
LOGGER.info("Initializing homes from API.")
logger.info("Initializing homes from API.")
val result = HomeModel(
tadoApiClient.me().homes
.map { userHomes -> tadoMeterFactory.createHomeMeters(userHomes) }
.map { userHomes -> userHomes.id to userHomes }
.toMap()
)

LOGGER.info("{} homes initialized.", result.homes.size)
logger.info { "${result.homes.size} homes initialized." }
return result
}
}
Expand All @@ -34,14 +33,12 @@ class HomeModelRefresher(
}

fun refreshHomeModel() {
LOGGER.info("Refreshing zones for all known homes.")
logger.info("Refreshing zones for all known homes.")

homeModel.homes.values.forEach { userHomes ->
LOGGER.info(
"Refreshing zones for home '{}' ({}).",
userHomes.name,
userHomes.id
)
logger.info {
"Refreshing zones for home '${userHomes.name}' (${userHomes.id})."
}

val allZones = tadoApiClient.zones(userHomes.id)
val newZoneEntries = homeModel.updateHomeZones(userHomes, allZones)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ import click.dobel.tado.exporter.metrics.TadoMeterFactory.Companion.TAG_HOME_ID
import click.dobel.tado.exporter.metrics.TadoMeterFactory.Companion.TAG_ZONE_ID
import click.dobel.tado.exporter.metrics.TadoMeterFactory.Companion.TAG_ZONE_NAME
import click.dobel.tado.exporter.metrics.TadoMeterFactory.Companion.TAG_ZONE_TYPE
import click.dobel.tado.util.logger
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.Tags
import mu.KLogging
import org.springframework.stereotype.Component

@Component
class TadoMeterFactory(
private val meterRegistry: MeterRegistry,
private val tadoApiClient: TadoApiClient
) {
companion object {
private val LOGGER = logger()
companion object : KLogging() {
internal const val PREFIX = "tado_"

internal const val SOLAR_INTENSITY_PERCENTAGE = "solar_intensity_percentage"
Expand All @@ -51,7 +50,7 @@ class TadoMeterFactory(
}

fun createHomeMeters(home: UserHomes): UserHomes {
LOGGER.info("Adding gauges for weather information for home '{}' ({})", home.name, home.id)
logger.info { "Adding gauges for weather information for home '${home.name}' (${home.id})" }

val homeTags = homeTags(home)

Expand Down Expand Up @@ -110,7 +109,7 @@ class TadoMeterFactory(
}

else -> {
LOGGER.warn("Unknown zone type {} for zone '{}' ({}).", zone.type, zone.name, zone.id)
logger.warn { "Unknown zone type ${zone.type} for zone '${zone.name}' (${zone.id})." }
}
}
}
Expand Down Expand Up @@ -154,7 +153,7 @@ class TadoMeterFactory(
}

private fun createHeatingZoneMeters(home: UserHomes, zone: ZoneEntry, zoneTags: Tags) {
LOGGER.info("Adding gauges for heating zone '{}' ({}).", zone.name, zone.id)
logger.info { "Adding gauges for heating zone '${zone.name}' (${zone.id})." }
createGenericZoneMeters(home, zone, zoneTags)
registerGauge(
HEATING_POWER_PERCENTAGE,
Expand Down Expand Up @@ -192,7 +191,7 @@ class TadoMeterFactory(

private fun createCoolingZoneMeters(home: UserHomes, zone: ZoneEntry, zoneTags: Tags) {
// TODO: check if these values are available in AC zones.
LOGGER.info("Adding gauges for AC zone '{}' ({}).", zone.name, zone.id)
logger.info { "Adding gauges for AC zone '${zone.name}' (${zone.id})." }
createGenericZoneMeters(home, zone, zoneTags)
registerGauge(
TEMPERATURE_SET_CELSIUS,
Expand Down Expand Up @@ -221,7 +220,7 @@ class TadoMeterFactory(
}

private fun createHotWaterZoneMeters(home: UserHomes, zone: ZoneEntry, zoneTags: Tags) {
LOGGER.info("Adding gauges for hot water zone '{}' ({}).", zone.name, zone.id)
logger.info("Adding gauges for hot water zone '${zone.name}' (${zone.id}).")
registerBooleanGauge(
IS_ZONE_POWERED,
"Zone power state. 1 if powered, 0 otherwise",
Expand Down

This file was deleted.

Loading