-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
219 additions
and
18 deletions.
There are no files selected for viewing
17 changes: 14 additions & 3 deletions
17
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/logic/SpringHomeworkService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
package ru.vityaman.lms.botalka.app.spring.logic | ||
|
||
import org.springframework.stereotype.Service | ||
import ru.vityaman.lms.botalka.app.spring.monitoring.MetricsFactory | ||
import ru.vityaman.lms.botalka.core.logging.Slf4jLog | ||
import ru.vityaman.lms.botalka.core.logic.HomeworkService | ||
import ru.vityaman.lms.botalka.core.logic.basic.BasicHomeworkService | ||
import ru.vityaman.lms.botalka.core.logic.logging.LoggingHomeworkService | ||
import ru.vityaman.lms.botalka.core.logic.metered.MeteredHomeworkService | ||
import ru.vityaman.lms.botalka.core.storage.HomeworkStorage | ||
|
||
@Service | ||
class SpringHomeworkService( | ||
metrics: MetricsFactory, | ||
storage: HomeworkStorage, | ||
) : HomeworkService by | ||
LoggingHomeworkService( | ||
Slf4jLog("HomeworkService"), | ||
BasicHomeworkService(storage), | ||
MeteredHomeworkService( | ||
run { | ||
val service = metrics.service("homework") | ||
MeteredHomeworkService.Metrics( | ||
create = service.method("create").status(), | ||
) | ||
}, | ||
LoggingHomeworkService( | ||
Slf4jLog("HomeworkService"), | ||
BasicHomeworkService(storage), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/monitoring/MetricsFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ru.vityaman.lms.botalka.app.spring.monitoring | ||
|
||
import io.micrometer.core.instrument.Counter | ||
import io.micrometer.core.instrument.MeterRegistry | ||
import org.springframework.stereotype.Component | ||
import ru.vityaman.lms.botalka.core.logic.metered.StatusCount | ||
|
||
@Component | ||
class MetricsFactory(private val registry: MeterRegistry) { | ||
fun service(name: String) = Service(name) | ||
|
||
inner class Service(private val name: String) { | ||
fun method(name: String) = Method(name) | ||
|
||
inner class Method(private val name: String) { | ||
@Suppress("LabeledExpression") | ||
fun status(): StatusCount { | ||
val count = Counter | ||
.builder("lms_service_method_call") | ||
.tag("service", this@Service.name) | ||
.tag("method", this@Method.name) | ||
.withRegistry(registry) | ||
|
||
return StatusCount( | ||
successes = MicrometerCount( | ||
count.withTag("status", "success"), | ||
), | ||
failures = MicrometerCount( | ||
count.withTag("status", "failure"), | ||
), | ||
) | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/monitoring/MicrometerCount.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.vityaman.lms.botalka.app.spring.monitoring | ||
|
||
import io.micrometer.core.instrument.Counter | ||
import ru.vityaman.lms.botalka.core.monitoring.Count | ||
|
||
class MicrometerCount( | ||
private val origin: Counter, | ||
) : Count { | ||
override fun add(amount: Int) = | ||
origin.increment(amount.toDouble()) | ||
} |
23 changes: 23 additions & 0 deletions
23
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/logic/metered/MeteredAuthService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ru.vityaman.lms.botalka.core.logic.metered | ||
|
||
import ru.vityaman.lms.botalka.core.logic.AuthService | ||
import ru.vityaman.lms.botalka.core.model.AuthUser | ||
import ru.vityaman.lms.botalka.core.model.User | ||
import ru.vityaman.lms.botalka.core.security.auth.AccessToken | ||
|
||
class MeteredAuthService<T>( | ||
private val metrics: Metrics, | ||
private val origin: AuthService<T>, | ||
) : AuthService<T> by origin { | ||
|
||
override suspend fun signUp(draft: User.Draft, credentials: T): AuthUser = | ||
metrics.signUp.observing { origin.signUp(draft, credentials) } | ||
|
||
override suspend fun signIn(credentials: T): AccessToken = | ||
metrics.signUp.observing { origin.signIn(credentials) } | ||
|
||
data class Metrics( | ||
val signUp: StatusCount, | ||
val signIn: StatusCount, | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/logic/metered/MeteredHomeworkService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ru.vityaman.lms.botalka.core.logic.metered | ||
|
||
import ru.vityaman.lms.botalka.core.logic.HomeworkService | ||
import ru.vityaman.lms.botalka.core.model.Homework | ||
|
||
class MeteredHomeworkService( | ||
private val metrics: Metrics, | ||
private val origin: HomeworkService, | ||
) : HomeworkService by origin { | ||
override suspend fun create(homework: Homework.Draft): Homework = | ||
metrics.create.observing { origin.create(homework) } | ||
|
||
data class Metrics( | ||
val create: StatusCount, | ||
) | ||
} |
26 changes: 26 additions & 0 deletions
26
...lka/src/main/kotlin/ru/vityaman/lms/botalka/core/logic/metered/MeteredPromotionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ru.vityaman.lms.botalka.core.logic.metered | ||
|
||
import ru.vityaman.lms.botalka.core.logic.PromotionService | ||
import ru.vityaman.lms.botalka.core.model.PromotionRequest | ||
|
||
class MeteredPromotionService( | ||
private val metrics: Metrics, | ||
private val origin: PromotionService, | ||
) : PromotionService by origin { | ||
override suspend fun request( | ||
promotion: PromotionRequest.Draft, | ||
): PromotionRequest = | ||
metrics.request.observing { origin.request(promotion) } | ||
|
||
override suspend fun approve(id: PromotionRequest.Id) = | ||
metrics.approve.observing { origin.approve(id) } | ||
|
||
override suspend fun reject(id: PromotionRequest.Id) = | ||
metrics.reject.observing { origin.reject(id) } | ||
|
||
data class Metrics( | ||
val request: StatusCount, | ||
val approve: StatusCount, | ||
val reject: StatusCount, | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
...lka/src/main/kotlin/ru/vityaman/lms/botalka/core/logic/metered/MeteredWorkspaceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ru.vityaman.lms.botalka.core.logic.metered | ||
|
||
import ru.vityaman.lms.botalka.core.logic.WorkspaceService | ||
import ru.vityaman.lms.botalka.core.model.Workspace | ||
|
||
class MeteredWorkspaceService( | ||
private val metrics: Metrics, | ||
private val origin: WorkspaceService, | ||
) : WorkspaceService by origin { | ||
|
||
override suspend fun produce( | ||
id: Workspace.Id, | ||
event: Workspace.Event.Draft, | ||
): Workspace.Event = | ||
metrics.produce.observing { origin.produce(id, event) } | ||
|
||
data class Metrics( | ||
val produce: StatusCount, | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/logic/metered/StatusCount.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.vityaman.lms.botalka.core.logic.metered | ||
|
||
import ru.vityaman.lms.botalka.core.monitoring.Count | ||
|
||
class StatusCount( | ||
val successes: Count, | ||
val failures: Count, | ||
) { | ||
suspend fun <T> observing(supply: suspend () -> T): T = | ||
runCatching { supply() } | ||
.onSuccess { successes.increment() } | ||
.onFailure { failures.increment() } | ||
.getOrThrow() | ||
} |
7 changes: 7 additions & 0 deletions
7
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/monitoring/Count.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.vityaman.lms.botalka.core.monitoring | ||
|
||
interface Count { | ||
fun add(amount: Int) | ||
|
||
fun increment() = add(1) | ||
} |