-
Notifications
You must be signed in to change notification settings - Fork 805
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
Allow passing counter metric name to datapoint to show in case of validation error #1121
Open
AntuanRokanten
wants to merge
1
commit into
prometheus:main
Choose a base branch
from
AntuanRokanten:pass-metric-name-to-datapoint
base: main
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 all commits
Commits
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 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 |
---|---|---|
|
@@ -56,7 +56,17 @@ public CounterDataPointSnapshot( | |
Exemplar exemplar, | ||
long createdTimestampMillis, | ||
long scrapeTimestampMillis) { | ||
super(labels, createdTimestampMillis, scrapeTimestampMillis); | ||
this(null, value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); | ||
} | ||
|
||
public CounterDataPointSnapshot( | ||
String metricName, | ||
double value, | ||
Labels labels, | ||
Exemplar exemplar, | ||
long createdTimestampMillis, | ||
long scrapeTimestampMillis) { | ||
super(metricName, labels, createdTimestampMillis, scrapeTimestampMillis); | ||
this.value = value; | ||
this.exemplar = exemplar; | ||
validate(); | ||
|
@@ -73,7 +83,7 @@ public Exemplar getExemplar() { | |
|
||
protected void validate() { | ||
if (value < 0.0) { | ||
throw new IllegalArgumentException(value + ": counters cannot have a negative value"); | ||
throw new IllegalArgumentException((getMetricName() == null ? "" : getMetricName() + "=") + value + ": counters cannot have a negative value"); | ||
} | ||
} | ||
|
||
|
@@ -83,6 +93,7 @@ public static Builder builder() { | |
|
||
public static class Builder extends DataPointSnapshot.Builder<Builder> { | ||
|
||
private String metricName = null; | ||
private Exemplar exemplar = null; | ||
private Double value = null; | ||
private long createdTimestampMillis = 0L; | ||
|
@@ -105,12 +116,17 @@ public Builder createdTimestampMillis(long createdTimestampMillis) { | |
return this; | ||
} | ||
|
||
public Builder metricName(String metricName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. javadoc please |
||
this.metricName = metricName; | ||
return this; | ||
} | ||
|
||
public CounterDataPointSnapshot build() { | ||
if (value == null) { | ||
throw new IllegalArgumentException("Missing required field: value is null."); | ||
} | ||
return new CounterDataPointSnapshot( | ||
value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); | ||
metricName, value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); | ||
} | ||
|
||
@Override | ||
|
This file contains 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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package io.prometheus.metrics.model.snapshots; | ||
|
||
public abstract class DataPointSnapshot { | ||
private final String metricName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305/3.0.2 (complile only) and annotate with |
||
private final Labels labels; | ||
private final long createdTimestampMillis; | ||
private final long scrapeTimestampMillis; | ||
|
||
protected DataPointSnapshot( | ||
Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { | ||
Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { | ||
this(null, labels, createdTimestampMillis, scrapeTimestampMillis); | ||
} | ||
|
||
protected DataPointSnapshot( | ||
String metricName, Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { | ||
this.metricName = metricName; | ||
this.labels = labels; | ||
this.createdTimestampMillis = createdTimestampMillis; | ||
this.scrapeTimestampMillis = scrapeTimestampMillis; | ||
validate(); | ||
} | ||
|
||
private void validate() { | ||
if (metricName != null && metricName.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Metric name cannot be an empty string"); | ||
} | ||
|
||
if (labels == null) { | ||
throw new IllegalArgumentException( | ||
"Labels cannot be null. Use Labels.EMPTY if there are no labels."); | ||
|
@@ -60,6 +71,10 @@ public long getCreatedTimestampMillis() { | |
return createdTimestampMillis; | ||
} | ||
|
||
public String getMetricName() { | ||
return metricName; | ||
} | ||
|
||
public abstract static class Builder<T extends Builder<T>> { | ||
|
||
protected Labels labels = Labels.EMPTY; | ||
|
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.
I think builder is enough
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.
not sure what you mean here, please elaborate
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.
I think we don't need an overloaded ctor - you can just use the builder to pass the metric name