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

Lisätään parempi telemetria PasswordServiceen #6349

Merged
merged 1 commit into from
Feb 6, 2025
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
3 changes: 3 additions & 0 deletions service/src/main/kotlin/fi/espoo/evaka/shared/Tracing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import fi.espoo.evaka.shared.security.Action
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.metrics.Meter
import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.trace.SpanBuilder
import io.opentelemetry.api.trace.Tracer
import java.util.UUID

fun noopTracer(): Tracer = OpenTelemetry.noop().getTracer("evaka-service")

fun noopMeter(): Meter = OpenTelemetry.noop().getMeter("evaka-service")

object Tracing {
val action = ToStringAttributeKey<Action>("action")
val actionClass = ToStringAttributeKey<Class<out Any>>("actionclass")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ package fi.espoo.evaka.shared.auth

import fi.espoo.evaka.Sensitive
import fi.espoo.evaka.shared.domain.ServiceUnavailable
import fi.espoo.evaka.shared.noopMeter
import fi.espoo.evaka.shared.noopTracer
import fi.espoo.evaka.shared.withSpan
import io.opentelemetry.api.metrics.Meter
import io.opentelemetry.api.trace.Tracer
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread
import org.springframework.stereotype.Service

@Service
class PasswordService : AutoCloseable {
class PasswordService(private val tracer: Tracer = noopTracer(), meter: Meter = noopMeter()) :
AutoCloseable {
private val algorithm = PasswordHashAlgorithm.DEFAULT
private val passwordPlaceholder = algorithm.placeholder()
private val pool: ExecutorService = run {
private val pool: ThreadPoolExecutor = run {
val corePoolSize = 1
val maximumPoolSize = 16
val workQueueCapacity = 128
Expand Down Expand Up @@ -43,6 +49,19 @@ class PasswordService : AutoCloseable {
handler,
)
}
private val activeWorkersGauge =
meter
.gaugeBuilder("evaka.auth.password_service.active_workers")
.ofLongs()
.buildWithCallback { it.record(pool.activeCount.toLong()) }
private val queueCapacityGauge =
meter
.gaugeBuilder("evaka.auth.password_service.queue_capacity")
.ofLongs()
.buildWithCallback { it.record(pool.queue.remainingCapacity().toLong()) }
private val matchCount = meter.counterBuilder("evaka.auth.password_service.match_count").build()
private val encodeCount =
meter.counterBuilder("evaka.auth.password_service.encode_count").build()

/**
* Checks if the given password matches the given optional encoded password.
Expand All @@ -53,7 +72,13 @@ class PasswordService : AutoCloseable {
*/
@Throws(ServiceUnavailable::class)
fun isMatch(password: Sensitive<String>, encoded: EncodedPassword?): Boolean =
pool.submit<Boolean> { (encoded ?: passwordPlaceholder).isMatch(password) }.get()
tracer.withSpan("isMatch") {
pool
.submit<Boolean> {
(encoded ?: passwordPlaceholder).isMatch(password).also { matchCount.add(1) }
}
.get()
}

/**
* Encodes the given raw password.
Expand All @@ -63,13 +88,21 @@ class PasswordService : AutoCloseable {
*/
@Throws(ServiceUnavailable::class)
fun encode(password: Sensitive<String>): EncodedPassword =
pool.submit<EncodedPassword> { algorithm.encode(password) }.get()
tracer.withSpan("encode") {
pool
.submit<EncodedPassword> { algorithm.encode(password).also { encodeCount.add(1) } }
.get()
}

/**
* Returns true if the encoded password should be rehashed for security and/or maintenance
* reasons.
*/
fun needsRehashing(encoded: EncodedPassword): Boolean = encoded.algorithm != algorithm

override fun close() = pool.close()
override fun close() {
pool.close()
activeWorkersGauge.close()
queueCapacityGauge.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fi.espoo.evaka.shared.config

import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.metrics.Meter
import io.opentelemetry.api.trace.Tracer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -16,4 +17,6 @@ class TracingConfig {

@Bean
fun tracer(openTelemetry: OpenTelemetry): Tracer = openTelemetry.getTracer("evaka-service")

@Bean fun meter(openTelemetry: OpenTelemetry): Meter = openTelemetry.getMeter("evaka-service")
}