@@ -49,6 +49,9 @@ public static class CounterDataPointSnapshot extends DataPointSnapshot {
4949 private final double value ;
5050 @ Nullable private final Exemplar exemplar ;
5151
52+ /** Optional metric name used only in validation error messages. */
53+ @ Nullable private final String metricName ;
54+
5255 /**
5356 * To create a new {@link CounterDataPointSnapshot}, you can either call the constructor
5457 * directly or use the Builder with {@link CounterDataPointSnapshot#builder()}.
@@ -62,7 +65,7 @@ public static class CounterDataPointSnapshot extends DataPointSnapshot {
6265 */
6366 public CounterDataPointSnapshot (
6467 double value , Labels labels , @ Nullable Exemplar exemplar , long createdTimestampMillis ) {
65- this (value , labels , exemplar , createdTimestampMillis , 0 );
68+ this (value , labels , exemplar , createdTimestampMillis , 0 , false , null );
6669 }
6770
6871 /**
@@ -77,7 +80,7 @@ public CounterDataPointSnapshot(
7780 @ Nullable Exemplar exemplar ,
7881 long createdTimestampMillis ,
7982 long scrapeTimestampMillis ) {
80- this (value , labels , exemplar , createdTimestampMillis , scrapeTimestampMillis , false );
83+ this (value , labels , exemplar , createdTimestampMillis , scrapeTimestampMillis , false , null );
8184 }
8285
8386 @ SuppressWarnings ("this-escape" )
@@ -88,9 +91,22 @@ public CounterDataPointSnapshot(
8891 long createdTimestampMillis ,
8992 long scrapeTimestampMillis ,
9093 boolean internal ) {
94+ this (value , labels , exemplar , createdTimestampMillis , scrapeTimestampMillis , internal , null );
95+ }
96+
97+ @ SuppressWarnings ("this-escape" )
98+ private CounterDataPointSnapshot (
99+ double value ,
100+ Labels labels ,
101+ @ Nullable Exemplar exemplar ,
102+ long createdTimestampMillis ,
103+ long scrapeTimestampMillis ,
104+ boolean internal ,
105+ @ Nullable String metricName ) {
91106 super (labels , createdTimestampMillis , scrapeTimestampMillis , internal );
92107 this .value = value ;
93108 this .exemplar = exemplar ;
109+ this .metricName = metricName ;
94110 if (!internal ) {
95111 validate ();
96112 }
@@ -107,7 +123,16 @@ public Exemplar getExemplar() {
107123
108124 protected void validate () {
109125 if (value < 0.0 ) {
110- throw new IllegalArgumentException (value + ": counters cannot have a negative value" );
126+ StringBuilder message = new StringBuilder ();
127+ if (metricName != null && !metricName .isEmpty ()) {
128+ message .append (metricName ).append ('=' );
129+ }
130+ message .append (value ).append (": counters cannot have a negative value" );
131+ Labels labels = getLabels ();
132+ if (labels != null && !labels .isEmpty ()) {
133+ message .append (" (labels=" ).append (labels ).append (')' );
134+ }
135+ throw new IllegalArgumentException (message .toString ());
111136 }
112137 }
113138
@@ -119,7 +144,8 @@ DataPointSnapshot escape(EscapingScheme escapingScheme) {
119144 SnapshotEscaper .escapeExemplar (exemplar , escapingScheme ),
120145 getCreatedTimestampMillis (),
121146 getScrapeTimestampMillis (),
122- true );
147+ true ,
148+ metricName );
123149 }
124150
125151 public static Builder builder () {
@@ -131,6 +157,7 @@ public static class Builder extends DataPointSnapshot.Builder<Builder> {
131157 @ Nullable private Exemplar exemplar = null ;
132158 @ Nullable private Double value = null ;
133159 private long createdTimestampMillis = 0L ;
160+ @ Nullable private String metricName = null ;
134161
135162 private Builder () {}
136163
@@ -150,12 +177,27 @@ public Builder createdTimestampMillis(long createdTimestampMillis) {
150177 return this ;
151178 }
152179
180+ /**
181+ * Optional metric name included in the exception message when {@link #value(double)} is
182+ * negative. Does not change the snapshot identity.
183+ */
184+ public Builder metricName (@ Nullable String metricName ) {
185+ this .metricName = metricName ;
186+ return this ;
187+ }
188+
153189 public CounterDataPointSnapshot build () {
154190 if (value == null ) {
155191 throw new IllegalArgumentException ("Missing required field: value is null." );
156192 }
157193 return new CounterDataPointSnapshot (
158- value , labels , exemplar , createdTimestampMillis , scrapeTimestampMillis );
194+ value ,
195+ labels ,
196+ exemplar ,
197+ createdTimestampMillis ,
198+ scrapeTimestampMillis ,
199+ false ,
200+ metricName );
159201 }
160202
161203 @ Override
0 commit comments