Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 869
Support UTF-8 in metric creation, parsing, and exposition#1070
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,6 +2,9 @@ | ||
| from ..utils import floatToGoString | ||
| from ..validation import ( | ||
| _is_valid_legacy_labelname, _is_valid_legacy_metric_name, | ||
| ) | ||
| CONTENT_TYPE_LATEST = 'application/openmetrics-text; version=1.0.0; charset=utf-8' | ||
| """Content type of the latest OpenMetrics text format""" | ||
| @@ -24,18 +27,27 @@ def generate_latest(registry): | ||
| try: | ||
| mname = metric.name | ||
| output.append('# HELP {} {}\n'.format( | ||
| mname, metric.documentation.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))) | ||
| output.append(f'# TYPE {mname} {metric.type}\n') | ||
| escape_metric_name(mname), _escape(metric.documentation))) | ||
| output.append(f'# TYPE {escape_metric_name(mname)} {metric.type}\n') | ||
| if metric.unit: | ||
| output.append(f'# UNIT {mname} {metric.unit}\n') | ||
| output.append(f'# UNIT {escape_metric_name(mname)} {metric.unit}\n') | ||
| for s in metric.samples: | ||
| if s.labels: | ||
| labelstr = '{{{0}}}'.format(','.join( | ||
| ['{}="{}"'.format( | ||
| k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"')) | ||
| for k, v in sorted(s.labels.items())])) | ||
| if not _is_valid_legacy_metric_name(s.name): | ||
| labelstr = escape_metric_name(s.name) | ||
| if s.labels: | ||
| labelstr += ', ' | ||
| else: | ||
| labelstr = '' | ||
| if s.labels: | ||
| items = sorted(s.labels.items()) | ||
| labelstr += ','.join( | ||
| ['{}="{}"'.format( | ||
| escape_label_name(k), _escape(v)) | ||
| for k, v in items]) | ||
| if labelstr: | ||
| labelstr = "{" + labelstr + "}" | ||
| if s.exemplar: | ||
| if not _is_valid_exemplar_metric(metric, s): | ||
| raise ValueError(f"Metric {metric.name} has exemplars, but is not a histogram bucket or counter") | ||
| @@ -59,16 +71,47 @@ def generate_latest(registry): | ||
| timestamp = '' | ||
| if s.timestamp is not None: | ||
| timestamp = f' {s.timestamp}' | ||
| output.append('{}{} {}{}{}\n'.format( | ||
| s.name, | ||
| labelstr, | ||
| floatToGoString(s.value), | ||
| timestamp, | ||
| exemplarstr, | ||
| )) | ||
| if _is_valid_legacy_metric_name(s.name): | ||
| output.append('{}{} {}{}{}\n'.format( | ||
| s.name, | ||
| labelstr, | ||
| floatToGoString(s.value), | ||
| timestamp, | ||
| exemplarstr, | ||
| )) | ||
| else: | ||
| output.append('{} {}{}{}\n'.format( | ||
| labelstr, | ||
| floatToGoString(s.value), | ||
| timestamp, | ||
| exemplarstr, | ||
| )) | ||
| except Exception as exception: | ||
| exception.args = (exception.args or ('',)) + (metric,) | ||
| raise | ||
| output.append('# EOF\n') | ||
| return ''.join(output).encode('utf-8') | ||
| def escape_metric_name(s: str) -> str: | ||
| """Escapes the metric name and puts it in quotes iff the name does not | ||
| conform to the legacy Prometheus character set. | ||
| """ | ||
| if _is_valid_legacy_metric_name(s): | ||
| return s | ||
| return '"{}"'.format(_escape(s)) | ||
| def escape_label_name(s: str) -> str: | ||
| """Escapes the label name and puts it in quotes iff the name does not | ||
csmarchbanks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| conform to the legacy Prometheus character set. | ||
| """ | ||
| if _is_valid_legacy_labelname(s): | ||
| return s | ||
| return '"{}"'.format(_escape(s)) | ||
| def _escape(s: str) -> str: | ||
| """Performs backslash escaping on backslash, newline, and double-quote characters.""" | ||
| return s.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"') | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.