Skip to content
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

Sample for a test verifying published metrics in unit tests #289

Open
Spikhalskiy opened this issue Apr 30, 2022 · 2 comments
Open

Sample for a test verifying published metrics in unit tests #289

Spikhalskiy opened this issue Apr 30, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Spikhalskiy
Copy link
Contributor

Spikhalskiy commented Apr 30, 2022

Temporal should provide a sample for users on how they may check the metrics in unit tests.
I propose to wire MetricsScope with our standard MicrometerClientStatsReporter initialized with SimpleMeterRegistry micrometer.
A little bit like micrometer does in one of their tests:
https://www.programcreek.com/java-api-examples/?code=micrometer-metrics%2Fmicrometer%2Fmicrometer-master%2Fmicrometer-test%2Fsrc%2Fmain%2Fjava%2Fio%2Fmicrometer%2Fcore%2Ftck%2FTimerTest.java

        MeterRegistry registry = new SimpleMeterRegistry();

        try (Timer.ResourceSample sample = Timer.resource(registry, "requests")
                .description("This is an operation")
                .publishPercentileHistogram()) {
            try {
                if (outcome.equals("error")) {
                    throw new IllegalArgumentException("boom");
                }
                sample.tag("outcome", "success");
            } catch (Throwable t) {
                sample.tag("outcome", "error");
            }
        }

        assertThat(registry.get("requests").tag("outcome", outcome).timer().count())
                .isEqualTo(1);
@Spikhalskiy Spikhalskiy added the enhancement New feature or request label Apr 30, 2022
@tsurdilo tsurdilo self-assigned this May 9, 2022
@Spikhalskiy
Copy link
Contributor Author

Since then I used this approach in SDK tests for metrics and it worked out quite fine. This is an example of using this approach in Temporal code: https://github.com/temporalio/sdk-java/blob/37a0e463fae2312882b5c946f423a0dbf85f9666/temporal-sdk/src/test/java/io/temporal/client/functional/MetricsTest.java#L76

@antmendoza
Copy link
Member

Thank you @Spikhalskiy , I have started to work on this

@Spikhalskiy Spikhalskiy changed the title Sample for a test verifying published metrics Sample for a test verifying published metrics in unit tests Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants