Skip to content

Conversation

@robcao
Copy link
Contributor

@robcao robcao commented Mar 18, 2025

What was changed

Added new protected constructors for MetricCounter, MetricGauge, and MetricHistogram in the Temporalio.Common namespace. This will allow users to supply their own implementations when passing in a custom MetricMeter on the activity environment for tests.

Previously the only constructors for these classes were internal, so they were not instantiable from outside of the Temporalio library.

Why?

Users want to be able to test their metric instrumentation within an activity using the ActivityEnvironment abstraction, without having to run their activities inside a full TemporalWorker.

It's fairly common to use an in-memory exporter to validate metric data points: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Exporter.InMemory/README.md, so users want to be able to have the ability to supply a custom metric meter hooked up to an in memory exporter to write tests validating metrics.

Checklist

  1. Closes [Feature Request] Allow metric meters to be constructed by users for tests #430

  2. How was this tested:

No logic was added, just an additional constructor. It would be difficult to write a test for this without creating a new test assembly where internals are not visible.

In C#, protected constructors are guaranteed to be accessible by any derived class outside of the assembly: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels

  1. Any docs updates needed?

I updated the section on activity testing to inform users of the presence of the MetricMeter on the ActivityEnvironment, to be consistent with the rest of the public properties.

I can re-order the properties in this list to be alphabetical, but they currently roughly appear to be in the order as the properties appear on the ActivityExecutionContext, so placing the property at the bottom to be roughly consistent

Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks! Will merge when CI passes.

@cretz cretz merged commit 8631656 into temporalio:main Mar 18, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Allow metric meters to be constructed by users for tests

2 participants