|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.server.master.metrics; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 | + |
| 24 | +import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import io.micrometer.core.instrument.Counter; |
| 29 | +import io.micrometer.core.instrument.Metrics; |
| 30 | +import io.micrometer.core.instrument.Timer; |
| 31 | + |
| 32 | +class WorkflowInstanceMetricsTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_submitState() { |
| 36 | + String defCode = "test_submit_1"; |
| 37 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 38 | + WorkflowExecutionStatus.SUBMITTED_SUCCESS, defCode); |
| 39 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 40 | + .tag("state", "submit") |
| 41 | + .tag("workflow.definition.code", defCode) |
| 42 | + .counter(); |
| 43 | + assertNotNull(counter, "Counter should be registered for submit state"); |
| 44 | + assertEquals(1, counter.count(), 0.001); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_failureState() { |
| 49 | + String defCode = "test_failure_1"; |
| 50 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 51 | + WorkflowExecutionStatus.FAILURE, defCode); |
| 52 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 53 | + .tag("state", "fail") |
| 54 | + .tag("workflow.definition.code", defCode) |
| 55 | + .counter(); |
| 56 | + assertNotNull(counter, "Counter should be registered for fail state"); |
| 57 | + assertEquals(1, counter.count(), 0.001); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_successState() { |
| 62 | + String defCode = "test_success_1"; |
| 63 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 64 | + WorkflowExecutionStatus.SUCCESS, defCode); |
| 65 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 66 | + .tag("state", "success") |
| 67 | + .tag("workflow.definition.code", defCode) |
| 68 | + .counter(); |
| 69 | + assertNotNull(counter, "Counter should be registered for success state"); |
| 70 | + assertEquals(1, counter.count(), 0.001); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_stopState() { |
| 75 | + String defCode = "test_stop_1"; |
| 76 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 77 | + WorkflowExecutionStatus.STOP, defCode); |
| 78 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 79 | + .tag("state", "stop") |
| 80 | + .tag("workflow.definition.code", defCode) |
| 81 | + .counter(); |
| 82 | + assertNotNull(counter, "Counter should be registered for stop state"); |
| 83 | + assertEquals(1, counter.count(), 0.001); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_pauseState() { |
| 88 | + String defCode = "test_pause_1"; |
| 89 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 90 | + WorkflowExecutionStatus.PAUSE, defCode); |
| 91 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 92 | + .tag("state", "pause") |
| 93 | + .tag("workflow.definition.code", defCode) |
| 94 | + .counter(); |
| 95 | + assertNotNull(counter, "Counter should be registered for pause state"); |
| 96 | + assertEquals(1, counter.count(), 0.001); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_failoverState() { |
| 101 | + String defCode = "test_failover_1"; |
| 102 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 103 | + WorkflowExecutionStatus.FAILOVER, defCode); |
| 104 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 105 | + .tag("state", "failover") |
| 106 | + .tag("workflow.definition.code", defCode) |
| 107 | + .counter(); |
| 108 | + assertNotNull(counter, "Counter should be registered for failover state"); |
| 109 | + assertEquals(1, counter.count(), 0.001); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testIncWorkflowInstanceByStateAndWorkflowDefinitionCode_defaultMapping() { |
| 114 | + String defCode = "test_running_1"; |
| 115 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 116 | + WorkflowExecutionStatus.RUNNING_EXECUTION, defCode); |
| 117 | + Counter counter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 118 | + .tag("state", "running_execution") |
| 119 | + .tag("workflow.definition.code", defCode) |
| 120 | + .counter(); |
| 121 | + assertNotNull(counter, "Counter should be registered for default-mapped state"); |
| 122 | + assertEquals(1, counter.count(), 0.001); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testRecordCommandQueryTime() { |
| 127 | + WorkflowInstanceMetrics.recordCommandQueryTime(100L); |
| 128 | + Timer timer = Metrics.globalRegistry.find("ds.workflow.command.query.duration").timer(); |
| 129 | + assertNotNull(timer, "Command query timer should be registered"); |
| 130 | + assertEquals(1, timer.count(), "Timer should have recorded one event"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void testRecordWorkflowInstanceGenerateTime() { |
| 135 | + WorkflowInstanceMetrics.recordWorkflowInstanceGenerateTime(200L); |
| 136 | + Timer timer = Metrics.globalRegistry.find("ds.workflow.instance.generate.duration").timer(); |
| 137 | + assertNotNull(timer, "Workflow instance generate timer should be registered"); |
| 138 | + assertEquals(1, timer.count(), "Timer should have recorded one event"); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testRegisterWorkflowInstanceRunningGauge() { |
| 143 | + WorkflowInstanceMetrics.registerWorkflowInstanceRunningGauge(() -> 10); |
| 144 | + assertNotNull(Metrics.globalRegistry.find("ds.workflow.instance.running").gauge(), |
| 145 | + "Running gauge should be registered"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testRegisterWorkflowInstanceResubmitGauge() { |
| 150 | + WorkflowInstanceMetrics.registerWorkflowInstanceResubmitGauge(() -> 3); |
| 151 | + assertNotNull(Metrics.globalRegistry.find("ds.workflow.instance.resubmit").gauge(), |
| 152 | + "Resubmit gauge should be registered"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void testCleanUpWorkflowInstanceCountMetricsByDefinitionCode() { |
| 157 | + String defCode = "99999"; |
| 158 | + WorkflowInstanceMetrics.incWorkflowInstanceByStateAndWorkflowDefinitionCode( |
| 159 | + WorkflowExecutionStatus.SUCCESS, defCode); |
| 160 | + Counter counterBefore = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 161 | + .tag("state", "success") |
| 162 | + .tag("workflow.definition.code", defCode) |
| 163 | + .counter(); |
| 164 | + assertNotNull(counterBefore, "Counter should exist before cleanup"); |
| 165 | + |
| 166 | + WorkflowInstanceMetrics.cleanUpWorkflowInstanceCountMetricsByDefinitionCode(99999L); |
| 167 | + |
| 168 | + Counter counterAfter = Metrics.globalRegistry.find("ds.workflow.instance.count") |
| 169 | + .tag("state", "success") |
| 170 | + .tag("workflow.definition.code", defCode) |
| 171 | + .counter(); |
| 172 | + assertNull(counterAfter, "Counter should be removed after cleanup"); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments