Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

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

Copy link
Author

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

Copy link
Member

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

String metricName,
double value,
Labels labels,
Exemplar exemplar,
long createdTimestampMillis,
long scrapeTimestampMillis) {
super(metricName, labels, createdTimestampMillis, scrapeTimestampMillis);
this.value = value;
this.exemplar = exemplar;
validate();
Expand All @@ -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");
}
}

Expand All @@ -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;
Expand All @@ -105,12 +116,17 @@ public Builder createdTimestampMillis(long createdTimestampMillis) {
return this;
}

public Builder metricName(String metricName) {
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The 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 @Nullable

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.");
Expand Down Expand Up @@ -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;
Expand Down