Skip to content

Make InfoMetricFamily.add_metric labels and value optional - #1263

Open
harish-ram247 wants to merge 1 commit into
prometheus:masterfrom
harish-ram247:info-metric-defaults
Open

harish-ram247 wants to merge 1 commit into
prometheus:masterfrom
harish-ram247:info-metric-defaults

Conversation

@harish-ram247

Copy link
Copy Markdown

InfoMetricFamily.add_metric() required both labels and value, even when only one was needed. Both now have empty defaults. Argument order is unchanged, so existing calls behave as before. I added tests for leaving out each argument.

Addresses the default-values part of #1049. The naming and dictionary-label points are unchanged.

I had AI help writing the change and tests.

@csmarchbanks

Signed-off-by: Harish Ram S <harishram2407@gmail.com>

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good to merge as is.

non-blocking: the zip already drops labels on add_metric([], ...) at base, and labels=() makes that the default. I'd add a check that the merged labels cover every label name.

$ python -c "from prometheus_client.core import InfoMetricFamily as F; f=F('build','h',labels=['instance']); f.add_metric([],{'version':'1.2'}); print(f.samples[0].labels)"
{'version': '1.2'}   # c0f1bdf
$ python -c "from prometheus_client.core import InfoMetricFamily as F; f=F('build','h',labels=['instance']); f.add_metric(value={'version':'1.2'}); print(f.samples[0].labels)"
{'version': '1.2'}   # 1453c224bfc3
$ python -c "from prometheus_client.core import InfoMetricFamily as F; f=F('build','h',labels=['instance']); f.add_metric(value={'instance':'a','version':'1.2'}); print(f.samples[0].labels)"
{'instance': 'a', 'version': '1.2'}   # 1453c224bfc3

$ python -m pytest -q   # py3.12
508 passed, 1 skipped   # 1453c224bfc3 (base c0f1bdf: 506 passed, 1 skipped)
2 failed                # both new tests, metrics_core.py reverted to c0f1bdf
$ flake8 prometheus_client/ tests/ && isort --check prometheus_client/ tests/ && mypy prometheus_client/ tests/   # flake8 7.5.0, isort 5.10.1, mypy 0.991
Success: no issues found in 42 source files

@harish-ram247

Copy link
Copy Markdown
Author

Thanks for the review and testing. I agree that leaving out labels makes it easier to drop a declared label by accident. The same thing already happens with add_metric([], ...), and adding a check would make some calls that work today raise an error, so I'd prefer to keep this PR limited to the defaults. @csmarchbanks, if you'd like that check, I can add it here or in a separate PR.

This branch has not been deployed

No deployments
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