-
Notifications
You must be signed in to change notification settings - Fork 610
NMS-19910: Add support for OpenMetrics v1 text format scraping #8592
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
Open
jassuncao
wants to merge
3
commits into
OpenNMS:develop
Choose a base branch
from
jassuncao:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
203 changes: 203 additions & 0 deletions
203
...res/prometheus-collector/src/main/java/org/hawkular/agent/prometheus/text/LineParser.java
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 |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * Licensed to The OpenNMS Group, Inc (TOG) under one or more | ||
| * contributor license agreements. See the LICENSE.md file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * TOG licenses this file to You under the GNU Affero General | ||
| * Public License Version 3 (the "License") or (at your option) | ||
| * any later version. You may not use this file except in | ||
| * compliance with the License. You may obtain a copy of the | ||
| * License at: | ||
| * | ||
| * https://www.gnu.org/licenses/agpl-3.0.txt | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
| * either express or implied. See the License for the specific | ||
| * language governing permissions and limitations under the | ||
| * License. | ||
| */ | ||
| package org.hawkular.agent.prometheus.text; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.hawkular.agent.prometheus.Util; | ||
|
|
||
| class SampleLineParser { | ||
|
jassuncao marked this conversation as resolved.
Outdated
|
||
|
|
||
| private enum ParseState { | ||
| NAME, | ||
| ENDOFNAME, | ||
| STARTOFLABELNAME, | ||
| LABELNAME, | ||
| LABELVALUEEQUALS, | ||
| LABELVALUEQUOTE, | ||
| LABELVALUE, | ||
| LABELVALUESLASH, | ||
| NEXTLABEL, | ||
| ENDOFLABELS, | ||
| VALUE, | ||
| TIMESTAMP | ||
| } | ||
|
|
||
| private final StringBuilder name = new StringBuilder(); | ||
| private final StringBuilder labelname = new StringBuilder(); | ||
| private final StringBuilder labelvalue = new StringBuilder(); | ||
| private final StringBuilder value = new StringBuilder(); | ||
| private final StringBuilder timestamp = new StringBuilder(); | ||
| private final Map<String, String> labels = new LinkedHashMap<>(); | ||
| private ParseState state; | ||
|
|
||
| public void reset() { | ||
| name.setLength(0); | ||
| labelname.setLength(0); | ||
| labelvalue.setLength(0); | ||
| value.setLength(0); | ||
| timestamp.setLength(0); | ||
| labels.clear(); | ||
| state = ParseState.NAME; | ||
| } | ||
|
|
||
| public TextSample parse(String line) { | ||
| reset(); | ||
|
|
||
| for (int c = 0; c < line.length(); c++) { | ||
| char charAt = line.charAt(c); | ||
|
|
||
| switch (state) { | ||
| case NAME: | ||
| if (charAt == '{') { | ||
| state = ParseState.STARTOFLABELNAME; | ||
| } else if (charAt == ' ' || charAt == '\t') { | ||
| state = ParseState.ENDOFNAME; | ||
| } else { | ||
| name.append(charAt); | ||
| } | ||
| break; | ||
| case ENDOFNAME: | ||
| if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else if (charAt == '{') { | ||
| state = ParseState.STARTOFLABELNAME; | ||
| } else { | ||
| value.append(charAt); | ||
| state = ParseState.VALUE; | ||
| } | ||
| break; | ||
| case STARTOFLABELNAME: | ||
| if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else if (charAt == '}') { | ||
| state = ParseState.ENDOFLABELS; | ||
| } else { | ||
| labelname.append(charAt); | ||
| state = ParseState.LABELNAME; | ||
| } | ||
| break; | ||
| case LABELNAME: | ||
| if (charAt == '=') { | ||
| state = ParseState.LABELVALUEQUOTE; | ||
| } else if (charAt == '}') { | ||
| state = ParseState.ENDOFLABELS; | ||
| } else if (charAt == ' ' || charAt == '\t') { | ||
| state = ParseState.LABELVALUEEQUALS; | ||
| } else { | ||
| labelname.append(charAt); | ||
| } | ||
| break; | ||
| case LABELVALUEEQUALS: | ||
| if (charAt == '=') { | ||
| state = ParseState.LABELVALUEQUOTE; | ||
| } else if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else { | ||
| throw new IllegalStateException("Invalid line: " + line); | ||
| } | ||
| break; | ||
| case LABELVALUEQUOTE: | ||
| if (charAt == '"') { | ||
| state = ParseState.LABELVALUE; | ||
| } else if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else { | ||
| throw new IllegalStateException("Invalid line: " + line); | ||
| } | ||
| break; | ||
| case LABELVALUE: | ||
| if (charAt == '\\') { | ||
| state = ParseState.LABELVALUESLASH; | ||
| } else if (charAt == '"') { | ||
| labels.put(labelname.toString(), labelvalue.toString()); | ||
| labelname.setLength(0); | ||
| labelvalue.setLength(0); | ||
| state = ParseState.NEXTLABEL; | ||
| } else { | ||
| labelvalue.append(charAt); | ||
| } | ||
| break; | ||
| case LABELVALUESLASH: | ||
| state = ParseState.LABELVALUE; | ||
| if (charAt == '\\') { | ||
| labelvalue.append('\\'); | ||
| } else if (charAt == 'n') { | ||
| labelvalue.append('\n'); | ||
| } else if (charAt == '"') { | ||
| labelvalue.append('"'); | ||
| } else { | ||
| labelvalue.append('\\').append(charAt); | ||
| } | ||
| break; | ||
| case NEXTLABEL: | ||
| if (charAt == ',') { | ||
| state = ParseState.LABELNAME; | ||
| } else if (charAt == '}') { | ||
| state = ParseState.ENDOFLABELS; | ||
| } else if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else { | ||
| throw new IllegalStateException("Invalid line: " + line); | ||
| } | ||
| break; | ||
| case ENDOFLABELS: | ||
| if (charAt == ' ' || charAt == '\t') { | ||
| // do nothing | ||
| } else { | ||
| value.append(charAt); | ||
| state = ParseState.VALUE; | ||
| } | ||
| break; | ||
| case VALUE: | ||
| if (charAt == ' ' || charAt == '\t') { | ||
| state = ParseState.TIMESTAMP; | ||
| } else { | ||
| value.append(charAt); | ||
| } | ||
| break; | ||
| case TIMESTAMP: | ||
| if (charAt == ' ' || charAt == '\t') { | ||
| return buildSample(line); | ||
| } | ||
| else { | ||
| timestamp.append(charAt); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return buildSample(line); | ||
| } | ||
|
|
||
| private TextSample buildSample(String line) { | ||
| return new TextSample.Builder() | ||
| .setLine(line) | ||
| .setName(name.toString()) | ||
| .setValue(value.toString()) | ||
| .setTimestamp(Util.tryConvertTimestamp(timestamp)) | ||
| .addLabels(new LinkedHashMap<>(labels)) | ||
| .build(); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without that dependency the mock framework in use throws a ClassNotFoundException