8000 Reduce unnecessary metric creation in loop by umax · Pull Request #256 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Reduce unnecessary metric creation in loop #256

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

Conversation

umax
Copy link
Contributor
@umax umax commented Mar 21, 2018

Hi!

I will try to improve multiprocess metrics collections by a series of pull requests.
This one fixes unnecessary metric creation in loop.

The more correct way to use dict.setdefault is result = dict.setdefault(key, value). In our case:

metric = metrics.setdefault(metric_name, core.Metric(...))

instead of

metrics.setdefault(metric_name, core.Metric(...))
metric = metrics[metric_name]

But the problem is that core.Metric(...) will be executed each time when setdefault method calls whether needed or not.


metric = metrics.get(metric_name)
if metric is None:
metrics[metric_name] = metric = core.Metric(metric_name, 'Multiprocess metric', typ)
Copy link
Contributor

Choose a reason for hiding this comment

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

Chaining = can be confusing, can you make this two statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem

@umax umax force-pushed the reduce-unnecessary-metric-creation branch from 17c8202 to 350d24b Compare March 21, 2018 10:44
@brian-brazil brian-brazil merged commit e2a5061 into prometheus:master Mar 21, 2018
@brian-brazil
Copy link
Contributor

Thanks!

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.

2 participants
0