-
Notifications
You must be signed in to change notification settings - Fork 189
Added test that verified custom metrics published by users #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
160 changes: 160 additions & 0 deletions
160
src/test/java/io/temporal/samples/metrics/MetricsTest.java
This file contains hidden or 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,160 @@ | ||
| /* | ||
| * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
| * | ||
| * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
| * use this file except in compliance with the License. A copy of the License is | ||
| * located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.samples.metrics; | ||
|
|
||
| import static junit.framework.TestCase.assertEquals; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.uber.m3.tally.RootScopeBuilder; | ||
| import com.uber.m3.tally.Scope; | ||
| import com.uber.m3.tally.StatsReporter; | ||
| import com.uber.m3.util.Duration; | ||
| import io.micrometer.core.instrument.Counter; | ||
| import io.micrometer.core.instrument.ImmutableTag; | ||
| import io.micrometer.core.instrument.Tag; | ||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowOptions; | ||
| import io.temporal.common.reporter.MicrometerClientStatsReporter; | ||
| import io.temporal.samples.metrics.activities.MetricsActivitiesImpl; | ||
| import io.temporal.samples.metrics.workflow.MetricsWorkflow; | ||
| import io.temporal.samples.metrics.workflow.MetricsWorkflowImpl; | ||
| import io.temporal.serviceclient.MetricsTag; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import io.temporal.serviceclient.WorkflowServiceStubsOptions; | ||
| import io.temporal.testing.TestWorkflowRule; | ||
| import io.temporal.worker.WorkerOptions; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| public class MetricsTest { | ||
|
|
||
| private static final long REPORTING_FLUSH_TIME = 50; | ||
| private static List<Tag> TAGS_NAMESPACE_QUEUE; | ||
| private final String SDK_CUSTOM_KEY = "sdkCustomTag1Key"; | ||
| private final String SDK_CUSTOM_VALUE = "sdkCustomTag1Value"; | ||
| private final SimpleMeterRegistry registry = new SimpleMeterRegistry(); | ||
| private final StatsReporter reporter = new MicrometerClientStatsReporter(registry); | ||
| private final Scope metricsScope = | ||
| new RootScopeBuilder() | ||
| .tags(ImmutableMap.of(SDK_CUSTOM_KEY, SDK_CUSTOM_VALUE)) | ||
| .reporter(reporter) | ||
| .reportEvery(Duration.ofMillis(REPORTING_FLUSH_TIME >> 1)); | ||
| private final String TEST_NAMESPACE = "UnitTest"; | ||
|
|
||
| @Rule | ||
| public TestWorkflowRule testWorkflowRule = | ||
| TestWorkflowRule.newBuilder() | ||
| .setWorkflowTypes(MetricsWorkflowImpl.class) | ||
| .setMetricsScope(metricsScope) | ||
| .setWorkerOptions(WorkerOptions.newBuilder().build()) | ||
| .setActivityImplementations(new MetricsActivitiesImpl()) | ||
| .build(); | ||
|
|
||
| private WorkflowServiceStubs clientStubs; | ||
| private WorkflowClient workflowClient; | ||
|
|
||
| private static List<Tag> replaceTags(List<Tag> tags, String... nameValuePairs) { | ||
| for (int i = 0; i < nameValuePairs.length; i += 2) { | ||
| tags = replaceTag(tags, nameValuePairs[i], nameValuePairs[i + 1]); | ||
| } | ||
| return tags; | ||
| } | ||
|
|
||
| private static List<Tag> replaceTag(List<Tag> tags, String name, String value) { | ||
| List<Tag> result = | ||
| tags.stream().filter(tag -> !name.equals(tag.getKey())).collect(Collectors.toList()); | ||
| result.add(new ImmutableTag(name, value)); | ||
| return result; | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
|
|
||
| final WorkflowServiceStubsOptions options = | ||
| testWorkflowRule.getWorkflowClient().getWorkflowServiceStubs().getOptions(); | ||
|
|
||
| this.clientStubs = WorkflowServiceStubs.newServiceStubs(options); | ||
|
|
||
| this.workflowClient = | ||
| WorkflowClient.newInstance(clientStubs, testWorkflowRule.getWorkflowClient().getOptions()); | ||
|
|
||
| final Map<String, String> stringStringMap = MetricsTag.defaultTags(TEST_NAMESPACE); | ||
| final List<Tag> TAGS_NAMESPACE = | ||
| stringStringMap.entrySet().stream() | ||
| .map( | ||
| nameValueEntry -> | ||
| new ImmutableTag(nameValueEntry.getKey(), nameValueEntry.getValue())) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| TAGS_NAMESPACE_QUEUE = | ||
| replaceTags(TAGS_NAMESPACE, MetricsTag.TASK_QUEUE, testWorkflowRule.getTaskQueue()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| this.clientStubs.shutdownNow(); | ||
| this.registry.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountActivityRetriesMetric() throws InterruptedException { | ||
| final MetricsWorkflow metricsWorkflow = | ||
| workflowClient.newWorkflowStub( | ||
| MetricsWorkflow.class, | ||
| WorkflowOptions.newBuilder() | ||
| .setTaskQueue(testWorkflowRule.getTaskQueue()) | ||
| .validateBuildWithDefaults()); | ||
|
|
||
| metricsWorkflow.exec("hello metrics"); | ||
|
|
||
| Thread.sleep(REPORTING_FLUSH_TIME); | ||
|
|
||
| assertIntCounter(4, countMetricActivityRetriesForActivity("PerformB")); | ||
|
|
||
| assertIntCounter(2, countMetricActivityRetriesForActivity("PerformA")); | ||
| } | ||
|
|
||
| @NotNull | ||
| private Counter countMetricActivityRetriesForActivity(String performB) { | ||
| final List<Tag> tags = | ||
| replaceTags( | ||
| TAGS_NAMESPACE_QUEUE, | ||
| MetricsTag.ACTIVITY_TYPE, | ||
| performB, | ||
| MetricsTag.WORKFLOW_TYPE, | ||
| "MetricsWorkflow", | ||
| MetricsTag.WORKER_TYPE, | ||
| "ActivityWorker", | ||
| SDK_CUSTOM_KEY, | ||
| SDK_CUSTOM_VALUE); | ||
| return registry.counter("activity_retries", tags); | ||
| } | ||
|
|
||
| private void assertIntCounter(int expectedValue, Counter counter) { | ||
| assertEquals(expectedValue, Math.round(counter.count())); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newInstanceis deprecated