@@ -9,58 +9,26 @@ import com.cosmotech.api.events.RunStart
9
9
import com.cosmotech.api.metrics.DownSamplingAggregationType
10
10
import com.cosmotech.api.metrics.PersistentMetric
11
11
import com.cosmotech.api.metrics.PersitentMetricType
12
- import com.cosmotech.run.WORKFLOW_TYPE_LABEL
13
- import com.cosmotech.run.service.WORKFLOW_TYPE_RUN
14
- import com.cosmotech.run.workflow.WorkflowContextData
15
- import com.cosmotech.run.workflow.WorkflowService
16
12
import com.cosmotech.runner.domain.Runner
17
13
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
18
14
import org.springframework.context.event.EventListener
19
- import org.springframework.scheduling.annotation.Scheduled
20
15
import org.springframework.stereotype.Service
21
16
22
- private const val RUNNING_STATUS = " Running"
23
-
24
17
private const val SERVICE_NAME = " run"
25
-
26
18
private const val ORGANIZATION_ID_LABEL = " organizationId"
27
19
private const val WORKSPACE_ID_LABEL = " workspaceId"
28
20
private const val RUNNER_ID_LABEL = " runnerId"
29
-
30
- private const val DEFAULT_EMPTY_WORKFLOW_LABEL = " none"
31
21
private const val TOTAL_QUALIFIER = " total"
32
- private const val TS_RUNNING_WORKFLOW_NAME = " running"
33
22
private const val TS_RUN_WORKFLOW_NAME = " run"
34
23
35
24
@Service
36
25
@ConditionalOnProperty(
37
26
name = [" csm.platform.metrics.enabled" ], havingValue = " true" , matchIfMissing = false )
38
27
internal class RunMetrics (
39
- private val workflowService : WorkflowService ,
40
28
private val eventPublisher : CsmEventPublisher ,
41
29
private val csmPlatformProperties : CsmPlatformProperties ,
42
30
) {
43
31
44
- @Scheduled(fixedDelay = 10000 )
45
- fun publishCurrentRunningRunner () {
46
- if (! csmPlatformProperties.metrics.enabled) return
47
-
48
- val countRunningWorkflows = getRunningWorkflowsCount()
49
- val labels =
50
- mutableMapOf (
51
- " usage" to " licensing" ,
52
- )
53
- // Global
54
- publishRunningLabeledMetric(
55
- countRunningWorkflows.values.sum(), TS_RUNNING_WORKFLOW_NAME , labels)
56
-
57
- // By Organization, Workspace, Runner
58
- countRunningWorkflows.forEach { (workflowContext, workflowCount) ->
59
- publishOrganizationWorkspaceAndRunnerRunningLabeledMetricIfAny(
60
- workflowContext!! , workflowCount, TS_RUNNING_WORKFLOW_NAME , labels)
61
- }
62
- }
63
-
64
32
@EventListener(RunStart ::class )
65
33
fun onRunStart (event : RunStart ) {
66
34
if (! csmPlatformProperties.metrics.enabled) return
@@ -93,15 +61,6 @@ internal class RunMetrics(
93
61
publishRunStartLabeledMetric(name, labels)
94
62
}
95
63
96
- private fun getRunningWorkflowsCount (): Map <WorkflowContextData ?, Int > {
97
- val runWorkflows =
98
- this .workflowService.findWorkflowStatusByLabel(" $WORKFLOW_TYPE_LABEL =$WORKFLOW_TYPE_RUN " )
99
- return runWorkflows
100
- .filter { it.status == RUNNING_STATUS }
101
- .groupingBy { it.contextData }
102
- .eachCount()
103
- }
104
-
105
64
internal fun publishRunStartLabeledMetric (name : String , labels : Map <String , String >) {
106
65
val metric =
107
66
PersistentMetric (
@@ -117,46 +76,4 @@ internal class RunMetrics(
117
76
)
118
77
eventPublisher.publishEvent(PersistentMetricEvent (this , metric))
119
78
}
120
-
121
- internal fun publishOrganizationWorkspaceAndRunnerRunningLabeledMetricIfAny (
122
- workflowContext : WorkflowContextData ,
123
- workflowCount : Int ,
124
- name : String ,
125
- labels : MutableMap <String , String >
126
- ) {
127
- var existingName = name
128
- val organizationId = workflowContext.organizationId!!
129
- if (organizationId != DEFAULT_EMPTY_WORKFLOW_LABEL ) {
130
- labels[ORGANIZATION_ID_LABEL ] = organizationId
131
- existingName + = " :$organizationId "
132
- publishRunningLabeledMetric(workflowCount, existingName, labels)
133
- val workspaceId = workflowContext.workspaceId!!
134
- if (workspaceId != DEFAULT_EMPTY_WORKFLOW_LABEL ) {
135
- labels[WORKSPACE_ID_LABEL ] = workspaceId
136
- existingName + = " :$workspaceId "
137
- publishRunningLabeledMetric(workflowCount, existingName, labels)
138
- val runnerId = workflowContext.runnerId!!
139
- if (runnerId != DEFAULT_EMPTY_WORKFLOW_LABEL ) {
140
- labels[RUNNER_ID_LABEL ] = runnerId
141
- existingName + = " :$runnerId "
142
- publishRunningLabeledMetric(workflowCount, existingName, labels)
143
- }
144
- }
145
- }
146
- }
147
-
148
- internal fun publishRunningLabeledMetric (value : Int , name : String , labels : Map <String , String >) {
149
- val metric =
150
- PersistentMetric (
151
- service = SERVICE_NAME ,
152
- name = name,
153
- value = value.toDouble(),
154
- qualifier = TOTAL_QUALIFIER ,
155
- labels = labels,
156
- type = PersitentMetricType .GAUGE ,
157
- downSampling = true ,
158
- downSamplingAggregation = DownSamplingAggregationType .MAX ,
159
- )
160
- eventPublisher.publishEvent(PersistentMetricEvent (this , metric))
161
- }
162
79
}
0 commit comments