Skip to content

Commit 38bfe6e

Browse files
authored
feat: adding action to increment micrometer counter (#108)
1 parent 628693b commit 38bfe6e

5 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/main/kotlin/at/ac/uibk/dps/cirrina/Runtime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ constructor(
3232
@Main main: URI,
3333
persistentContext: Context?,
3434
stateMachineFactory: StateMachine.Factory,
35-
meterRegistry: MeterRegistry,
35+
val meterRegistry: MeterRegistry,
3636
) {
3737
val eventHandler = EventHandler()
3838
val extent = persistentContext?.let { Extent.of(it) } ?: Extent.of()

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/Action.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ sealed interface Action {
4141

4242
is LogDescription -> LogAction(Expression.create(description.message))
4343

44+
is IncrCtrDescription ->
45+
IncrCtrAction(
46+
description.counter,
47+
description.by ?: "1.0",
48+
description.tags?.mapValues { (_, v) -> Expression.create(v) } ?: emptyMap(),
49+
)
50+
4451
else -> error("unknown action type: ${description.javaClass.simpleName}")
4552
}
4653

@@ -107,3 +114,9 @@ class TimeoutResetAction internal constructor(val action: String) : Action {
107114
}
108115

109116
class LogAction internal constructor(val message: Expression) : Action
117+
118+
class IncrCtrAction
119+
internal constructor(val counter: String, val by: String, val tag: Map<String, Expression>) :
120+
Action {
121+
override fun toString() = "IncrCtrAction(metric='$counter', by='$by', tag='$tag')"
122+
}

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/ActionCommand.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package at.ac.uibk.dps.cirrina.execution.`object`
22

33
import at.ac.uibk.dps.cirrina.csm.Csml.EventChannel
44
import at.ac.uibk.dps.cirrina.execution.service.ServiceImplementationSelector
5+
import io.micrometer.core.instrument.MeterRegistry
6+
import io.micrometer.core.instrument.Tag
7+
import io.micrometer.core.instrument.Tags
58
import kotlinx.coroutines.CoroutineScope
69
import kotlinx.coroutines.launch
710
import mu.KotlinLogging
@@ -16,6 +19,7 @@ class ActionExecutor(
1619
private val selector: ServiceImplementationSelector,
1720
private val eventHandler: StateMachine.StateMachineEventHandler,
1821
private val coroutineScope: CoroutineScope,
22+
private val meterRegistry: MeterRegistry,
1923
) {
2024
fun execute(action: Action, scope: Scope): List<Action> =
2125
when (action) {
@@ -26,6 +30,7 @@ class ActionExecutor(
2630
is TimeoutAction -> listOf(action.triggers)
2731
is TimeoutResetAction -> emptyList()
2832
is LogAction -> executeLog(action, scope)
33+
is IncrCtrAction -> executeIncrCtr(action, scope)
2934
else -> error("unknown action type: ${action::class.simpleName}")
3035
}
3136

@@ -84,4 +89,13 @@ class ActionExecutor(
8489
action.message.execute(scope.extent).toString().also { logger.info(it) }
8590
return emptyList()
8691
}
92+
93+
private fun executeIncrCtr(action: IncrCtrAction, scope: Scope): List<Action> {
94+
val tags =
95+
Tags.of(
96+
action.tag.map { (key, value) -> Tag.of(key, value.execute(scope.extent).toString()) }
97+
)
98+
meterRegistry.counter(action.counter, tags).increment(action.by.toDouble())
99+
return emptyList()
100+
}
87101
}

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ internal constructor(
4646
private val timeoutActionManager = TimeoutActionManager(coroutineScope)
4747
private val stateMachineEventHandler = StateMachineEventHandler(runtime.eventHandler)
4848
private val actionExecutor =
49-
ActionExecutor(runtime.selector, stateMachineEventHandler, coroutineScope)
49+
ActionExecutor(
50+
runtime.selector,
51+
stateMachineEventHandler,
52+
coroutineScope,
53+
runtime.meterRegistry,
54+
)
5055

5156
private val stateInstances =
5257
specification.vertexSet().associate { it.name to stateFactory.create(it, this) }

src/main/resources/pkl/csm/csml.pkl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class EvalDescription extends ActionDescription {
103103
expression: Expression
104104
}
105105

106-
107106
class EmitDescription extends ActionDescription {
108107
event: EventDescription
109108
target: Expression?
@@ -114,12 +113,10 @@ class TimeoutDescription extends ActionDescription {
114113
triggers: EmitDescription
115114
}
116115

117-
118116
class ResetDescription extends ActionDescription {
119117
name: TimeoutName
120118
}
121119

122-
123120
class CaseDescription {
124121
of: Expression
125122
yields: Listing<ActionDescription>
@@ -192,4 +189,13 @@ typealias Log = LogDescription
192189

193190
// Event aliases
194191
typealias Event = EventDescription
195-
typealias Internal = InternalDescription
192+
typealias Internal = InternalDescription
193+
194+
// Private API - not meant for production use
195+
class IncrCtrDescription extends ActionDescription {
196+
counter: String
197+
by: String?
198+
tags: Mapping<String, Expression>?
199+
}
200+
201+
typealias IncrCtr = IncrCtrDescription

0 commit comments

Comments
 (0)