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

Improved .NET metrics support #129

Merged
merged 2 commits into from
Aug 25, 2023
Merged

Conversation

cretz
Copy link
Member

@cretz cretz commented Aug 23, 2023

What was changed

  • Exposed metric meter from core
  • Added interfaces/impls for metric meter, counters, histograms, and gauges all with tag support
  • Added TemporalRuntime.MetricMeter for users to add/record metrics
  • Added ActivityExecutionContext.MetricMeter for users to add/record metrics from activities
  • Added Workflow.MetricMeter for users to add/record metrics from workflows (replay safe)
  • Added options to MetricsOptions, PrometheusOptions, and OpenTelemetryOptions to support all current metric options from core
  • 💥 BREAKING CHANGE - Removed Temporalio.Runtime.TelemetryOptions.Tracing property and associated class
    • This is only for advanced internal support for core tracing which wasn't really used anyways

This is only part of the things needed for #126. This notably doesn't include a user-defined implementation for metrics handling which will come later.

Also, this PR will wait on temporalio/sdk-core#598 which will then change a unit test here.

@cretz cretz requested a review from a team August 23, 2023 21:10
Copy link
Member

@Sushisource Sushisource left a comment

Choose a reason for hiding this comment

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

Making sense to me, just a few things.

Bool,
}

internal enum MetricIntegerKind
Copy link
Member

Choose a reason for hiding this comment

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

I'd just call this MetricKind. If/when we accept non-integer values, the kinds will become generic over the value types.

Copy link
Member Author

@cretz cretz Aug 24, 2023

Choose a reason for hiding this comment

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

This is only used for MetricIntegerOptions on a call to metric_integer_new which returns a MetricInteger. It'd make no sense to have to make metric_integer_new/MetricIntegerOptions support a non-metric-integer kind. If there's a metric_double_new/MetricDoubleOptions, it'd have MetricDoubleKind because it'd make no sense for such a method to support metric-integer kinds. The separation of metrics by their accepted type is due to the inability to expose generics in C.

Copy link
Member

Choose a reason for hiding this comment

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

Huh? Why wouldn't metric_integer_new and metric_double_new be able to just accept MetricKind? All the kinds would be supported.

Copy link
Member Author

Choose a reason for hiding this comment

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

If that is the case I can refactor then. My concern was that every kind may not be supported by every data type. For right now there is only one, so we can just wait until another comes and then I can share if all are supported, sure.

/// </summary>
/// <param name="attributes">Attributes to append.</param>
/// <returns>New attributes with the given ones appended.</returns>
public MetricAttributes Append(IEnumerable<KeyValuePair<string, object>> attributes)
Copy link
Member

Choose a reason for hiding this comment

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

You'll probably want to add a method which uses the merge function exposed on attributes. This is the most efficient way (when using lang-side exporting) as it re-uses the IDs and doesn't need to allocate any new attributes - and it's fairly common to build up some attributes as a series of combined ones.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is internal code not user code. There is no exposed user code for an attribute class, so there is no value in a "merge" in internal code. It would never be called.

Copy link
Member

Choose a reason for hiding this comment

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

WithTags could work in terms of merging is I suppose what I really mean, whereas just recording and passing the tags there wouldn't. WithTags implying you're going to keep around that new meter for a while.

Copy link
Member Author

Choose a reason for hiding this comment

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

Given the .NET API currently, what would merging look like from a user POV? We don't expose MetricAttributes to users, we only expose WithTags that only ever needs to append.

Copy link
Member

Choose a reason for hiding this comment

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

It wouldn't be any different - I mean WithTags can work by calling merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

But there is no real difference between "clone + create MetricsAttributes + merge" and just "clone + add_new_attrs" . It's all just clone + vec extend with how I use this. I don't allow metrics attributes to be mutable at this time because I don't give users the concept of a standalone set of pre-created attributes because it's usually not worth it (and the .NET API at https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics works this way as well though they go one further and make you provide the full set every record call).

internal MetricMeter(Bridge.MetricMeter meter, Bridge.MetricAttributes attributes)
{
Meter = meter;
this.attributes = attributes;
Copy link
Member

Choose a reason for hiding this comment

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

This is sort of more like defaultAttributes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. You can call WithTags which makes a new meter with new tags. This is more like "base attributes", but it's pretty clear that "attributes" means the attributes on all metrics created from this meter just like "attributes" on the metric is for all values recorded on the metric.

Comment on lines +249 to +250
/// <inheritdoc />
public IMetricMeter MetricMeter => metricMeter.Value;
Copy link
Member

Choose a reason for hiding this comment

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

Do we really want to expose a way to record metrics directly from the workflow? I can certainly see how it's useful but I definitely don't love adding things that do side effects in the middle of workflow code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it is replay safe just like loggers and tracing. Our other SDKs have this and is a primary parity point we want to achieve with this project IMO.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense

Copy link
Contributor

@mjameswh mjameswh left a comment

Choose a reason for hiding this comment

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

That all makes sense to me.

@cretz cretz merged commit 9d56fe8 into temporalio:main Aug 25, 2023
@cretz cretz deleted the advanced-metrics branch August 25, 2023 21:53
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.

4 participants