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

Custom metric support #384

Merged
merged 2 commits into from
Sep 25, 2023
Merged

Custom metric support #384

merged 2 commits into from
Sep 25, 2023

Conversation

cretz
Copy link
Member

@cretz cretz commented Sep 8, 2023

What was changed

  • Added MetricMeter, MetricCounter, MetricHistogram, and MetricGauge as (abstract) classes to temporalio.common
  • Added metric_meter property to temporalio.runtime.Runtime
  • Added temporalio.workflow.metric_meter() function to get replay-safe metric meter
  • Added temporalio.activity.metric_meter() function to get optional metric meter
    • NOTE: This does not return a metric meter for sync activities that are non-threaded (i.e. multiprocess) because a meter cannot be shared across processes and it's a large effort to support metric shipping across processes for this rarely used activity type
  • Added metric_meter attribute to temporalio.testing.ActivityEnvironment that defaults to noop impl
  • API docs and tests

Handles part of #369

@cretz cretz requested a review from a team as a code owner September 8, 2023 15:23
@dandavison
Copy link
Contributor

Could you add a link in the PR description to the GitHub / Jira issue that motivates the changes?

@cretz
Copy link
Member Author

cretz commented Sep 8, 2023

Could you add a link in the PR description to the GitHub / Jira issue that motivates the changes?

I linked this PR from that issue: #369. I will update the description here.

Copy link
Contributor

@dandavison dandavison left a comment

Choose a reason for hiding this comment

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

I don't have the necessary familiarity with the metrics code and Python-Rust bridge to review this properly, but it looks very clean, well-tested and plausible to me. So just in case you feel happy merging without expert review, here's my approval.

# sync function and we don't support cross-process metrics
if not self.runtime_metric_meter:
raise RuntimeError(
"Metrics meter not available in non-threaded sync activities like mulitprocess"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Metrics meter not available in non-threaded sync activities like mulitprocess"
"Metrics meter not available in non-threaded sync activities like multiprocess"

Comment on lines +21 to +23
if not ref:
return None
return MetricMeter(ref)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a personal Python style suggestion, obviously FFTI.

Suggested change
if not ref:
return None
return MetricMeter(ref)
return MetricMeter(ref) if ref else None

Copy link
Member Author

Choose a reason for hiding this comment

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

I try to avoid the RHS ternary in some of these cases, but you'll see I use it in others. I may use it here.

Comment on lines +3327 to +3330
for k, v in at_least_labels.items():
if not f'{k}="{v}"' in line:
return False
return line.endswith(f" {value}")
Copy link
Contributor

Choose a reason for hiding this comment

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

More small-minded personal style suggestions while trying to digest the PR, FFTI!

Suggested change
for k, v in at_least_labels.items():
if not f'{k}="{v}"' in line:
return False
return line.endswith(f" {value}")
return all(
f'{k}="{v}"' in line for k, v in at_least_labels.items()
) and line.endswith(f" {value}")

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's style preference. I do use any/all where I do think functional approach has more value.

Copy link
Contributor

@dandavison dandavison Sep 9, 2023

Choose a reason for hiding this comment

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

Yes, here I would personally say that all(...) is more Pythonic than explicitly implementing an early-exit algorithm.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 (just test code, and this style for assert is clearer with pytest because of how it expands the expression, but no assert in here so can change)

"""Initialize gauge."""
self._ref = meter._ref.new_gauge(name, description, unit)

def set(self, value: int, attrs: MetricAttributes) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Isn't value supposed to be a float here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Opened an issue in sdk-core temporalio/sdk-core#604

Copy link
Member Author

@cretz cretz Sep 25, 2023

Choose a reason for hiding this comment

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

I don't think that should be a blocker for this PR

Copy link
Member

@bergundy bergundy left a comment

Choose a reason for hiding this comment

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

LGTM, I would want float support though but that can be added later when it’s supported in core

@cretz cretz merged commit 3fade95 into temporalio:main Sep 25, 2023
12 checks passed
@cretz cretz deleted the user-metrics branch September 25, 2023 14:55
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.

None yet

3 participants