Skip to content

Commit 452e283

Browse files
authored
Added test that verified custom metrics published by users (temporalio#335)
* added test testCountActivityRetriesMetric * replaced invocation to deprecated methods * removed internal classes usage
1 parent c3de6bf commit 452e283

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

src/main/java/io/temporal/samples/metrics/MetricsStarter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void main(String[] args) {
5858
WorkflowServiceStubsOptions stubOptions =
5959
WorkflowServiceStubsOptions.newBuilder().setMetricsScope(scope).build();
6060

61-
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance(stubOptions);
61+
WorkflowServiceStubs service = WorkflowServiceStubs.newServiceStubs(stubOptions);
6262
WorkflowClient client = WorkflowClient.newInstance(service);
6363

6464
WorkflowOptions workflowOptions =

src/main/java/io/temporal/samples/metrics/MetricsWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static void main(String[] args) {
6464
WorkflowServiceStubsOptions stubOptions =
6565
WorkflowServiceStubsOptions.newBuilder().setMetricsScope(scope).build();
6666

67-
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance(stubOptions);
67+
WorkflowServiceStubs service = WorkflowServiceStubs.newServiceStubs(stubOptions);
6868
WorkflowClient client = WorkflowClient.newInstance(service);
6969
WorkerFactory factory = WorkerFactory.newInstance(client);
7070

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.metrics;
21+
22+
import static junit.framework.TestCase.assertEquals;
23+
24+
import com.google.common.collect.ImmutableMap;
25+
import com.uber.m3.tally.RootScopeBuilder;
26+
import com.uber.m3.tally.Scope;
27+
import com.uber.m3.tally.StatsReporter;
28+
import com.uber.m3.util.Duration;
29+
import io.micrometer.core.instrument.Counter;
30+
import io.micrometer.core.instrument.ImmutableTag;
31+
import io.micrometer.core.instrument.Tag;
32+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
33+
import io.temporal.client.WorkflowClient;
34+
import io.temporal.client.WorkflowOptions;
35+
import io.temporal.common.reporter.MicrometerClientStatsReporter;
36+
import io.temporal.samples.metrics.activities.MetricsActivitiesImpl;
37+
import io.temporal.samples.metrics.workflow.MetricsWorkflow;
38+
import io.temporal.samples.metrics.workflow.MetricsWorkflowImpl;
39+
import io.temporal.serviceclient.MetricsTag;
40+
import io.temporal.serviceclient.WorkflowServiceStubs;
41+
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
42+
import io.temporal.testing.TestWorkflowRule;
43+
import io.temporal.worker.WorkerOptions;
44+
import java.util.List;
45+
import java.util.Map;
46+
import java.util.stream.Collectors;
47+
import org.jetbrains.annotations.NotNull;
48+
import org.junit.After;
49+
import org.junit.Before;
50+
import org.junit.Rule;
51+
import org.junit.Test;
52+
53+
public class MetricsTest {
54+
55+
private static final long REPORTING_FLUSH_TIME = 50;
56+
private static List<Tag> TAGS_NAMESPACE_QUEUE;
57+
private final String SDK_CUSTOM_KEY = "sdkCustomTag1Key";
58+
private final String SDK_CUSTOM_VALUE = "sdkCustomTag1Value";
59+
private final SimpleMeterRegistry registry = new SimpleMeterRegistry();
60+
private final StatsReporter reporter = new MicrometerClientStatsReporter(registry);
61+
private final Scope metricsScope =
62+
new RootScopeBuilder()
63+
.tags(ImmutableMap.of(SDK_CUSTOM_KEY, SDK_CUSTOM_VALUE))
64+
.reporter(reporter)
65+
.reportEvery(Duration.ofMillis(REPORTING_FLUSH_TIME >> 1));
66+
private final String TEST_NAMESPACE = "UnitTest";
67+
68+
@Rule
69+
public TestWorkflowRule testWorkflowRule =
70+
TestWorkflowRule.newBuilder()
71+
.setWorkflowTypes(MetricsWorkflowImpl.class)
72+
.setMetricsScope(metricsScope)
73+
.setWorkerOptions(WorkerOptions.newBuilder().build())
74+
.setActivityImplementations(new MetricsActivitiesImpl())
75+
.build();
76+
77+
private WorkflowServiceStubs clientStubs;
78+
private WorkflowClient workflowClient;
79+
80+
private static List<Tag> replaceTags(List<Tag> tags, String... nameValuePairs) {
81+
for (int i = 0; i < nameValuePairs.length; i += 2) {
82+
tags = replaceTag(tags, nameValuePairs[i], nameValuePairs[i + 1]);
83+
}
84+
return tags;
85+
}
86+
87+
private static List<Tag> replaceTag(List<Tag> tags, String name, String value) {
88+
List<Tag> result =
89+
tags.stream().filter(tag -> !name.equals(tag.getKey())).collect(Collectors.toList());
90+
result.add(new ImmutableTag(name, value));
91+
return result;
92+
}
93+
94+
@Before
95+
public void setUp() {
96+
97+
final WorkflowServiceStubsOptions options =
98+
testWorkflowRule.getWorkflowClient().getWorkflowServiceStubs().getOptions();
99+
100+
this.clientStubs = WorkflowServiceStubs.newServiceStubs(options);
101+
102+
this.workflowClient =
103+
WorkflowClient.newInstance(clientStubs, testWorkflowRule.getWorkflowClient().getOptions());
104+
105+
final Map<String, String> stringStringMap = MetricsTag.defaultTags(TEST_NAMESPACE);
106+
final List<Tag> TAGS_NAMESPACE =
107+
stringStringMap.entrySet().stream()
108+
.map(
109+
nameValueEntry ->
110+
new ImmutableTag(nameValueEntry.getKey(), nameValueEntry.getValue()))
111+
.collect(Collectors.toList());
112+
113+
TAGS_NAMESPACE_QUEUE =
114+
replaceTags(TAGS_NAMESPACE, MetricsTag.TASK_QUEUE, testWorkflowRule.getTaskQueue());
115+
}
116+
117+
@After
118+
public void tearDown() {
119+
this.clientStubs.shutdownNow();
120+
this.registry.close();
121+
}
122+
123+
@Test
124+
public void testCountActivityRetriesMetric() throws InterruptedException {
125+
final MetricsWorkflow metricsWorkflow =
126+
workflowClient.newWorkflowStub(
127+
MetricsWorkflow.class,
128+
WorkflowOptions.newBuilder()
129+
.setTaskQueue(testWorkflowRule.getTaskQueue())
130+
.validateBuildWithDefaults());
131+
132+
metricsWorkflow.exec("hello metrics");
133+
134+
Thread.sleep(REPORTING_FLUSH_TIME);
135+
136+
assertIntCounter(4, countMetricActivityRetriesForActivity("PerformB"));
137+
138+
assertIntCounter(2, countMetricActivityRetriesForActivity("PerformA"));
139+
}
140+
141+
@NotNull
142+
private Counter countMetricActivityRetriesForActivity(String performB) {
143+
final List<Tag> tags =
144+
replaceTags(
145+
TAGS_NAMESPACE_QUEUE,
146+
MetricsTag.ACTIVITY_TYPE,
147+
performB,
148+
MetricsTag.WORKFLOW_TYPE,
149+
"MetricsWorkflow",
150+
MetricsTag.WORKER_TYPE,
151+
"ActivityWorker",
152+
SDK_CUSTOM_KEY,
153+
SDK_CUSTOM_VALUE);
154+
return registry.counter("activity_retries", tags);
155+
}
156+
157+
private void assertIntCounter(int expectedValue, Counter counter) {
158+
assertEquals(expectedValue, Math.round(counter.count()));
159+
}
160+
}

0 commit comments

Comments
 (0)