-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add warning log when duplicate leables are created or detected #29
Changes from 1 commit
c25469d
7ede3d8
8f3c7f7
3660769
b24f143
5704539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,20 +12,30 @@ | |
import io.prometheus.metrics.model.snapshots.Quantiles; | ||
import io.prometheus.metrics.model.snapshots.SummarySnapshot; | ||
|
||
import java.util.logging.Logger; | ||
/** | ||
* Builder methods to convert Kafka metrics into Prometheus metrics | ||
*/ | ||
public class DataPointSnapshotBuilder { | ||
|
||
private static final Logger LOG = Logger.getLogger(DataPointSnapshotBuilder.class.getName()); | ||
|
||
/** | ||
* Create a datapoint for a {@link InfoSnapshot} metric | ||
* @param labels The labels associated with the datapoint | ||
* @param value The value to insert as a label | ||
* @param metricName The name of the new label | ||
* @return The {@link InfoSnapshot.InfoDataPointSnapshot} datapoint | ||
*/ | ||
|
||
scholzj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static InfoSnapshot.InfoDataPointSnapshot infoDataPoint(Labels labels, Object value, String metricName) { | ||
Labels newLabels = labels.add(PrometheusNaming.sanitizeLabelName(metricName), String.valueOf(value)); | ||
String sanitizedMetricName = PrometheusNaming.sanitizeLabelName(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. I think naming this 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. Changed to 'newLabelName` |
||
Labels newLabels = labels; | ||
if (labels.contains(sanitizedMetricName)) { | ||
LOG.warning("Ignoring metric value duplicate key: " + sanitizedMetricName); | ||
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. It's probably worth print a bit more information such as the metric name so operators can identify the culprit. 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. Also we should not use concatenation in log lines but instead use interpolation with the 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. I appended |
||
} else { | ||
newLabels = labels.add(sanitizedMetricName, String.valueOf(value)); | ||
} | ||
return InfoSnapshot.InfoDataPointSnapshot.builder() | ||
.labels(newLabels) | ||
.build(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import io.prometheus.metrics.model.snapshots.Labels; | ||
import io.prometheus.metrics.model.snapshots.MetricSnapshot; | ||
import io.prometheus.metrics.model.snapshots.MetricSnapshots; | ||
import io.prometheus.metrics.model.snapshots.PrometheusNaming; | ||
import org.apache.kafka.common.MetricName; | ||
import org.apache.kafka.common.metrics.Gauge; | ||
import org.apache.kafka.common.metrics.KafkaMetric; | ||
|
@@ -108,6 +109,19 @@ public void testCollectNonNumericMetric() { | |
assertEquals(expectedLabels, snapshot.getDataPoints().get(0).getLabels()); | ||
} | ||
|
||
@Test | ||
public void testLabelNameSanitizing() { | ||
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. We want to avoid tests that only exercise code from dependencies. This test only validates 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. Removed |
||
String labelName = PrometheusNaming.sanitizeLabelName("k-1"); | ||
assertEquals("k_1", labelName); | ||
} | ||
|
||
@Test | ||
public void testLabelDuplicateWarning() { | ||
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. I think we should put this test in a new class 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. New |
||
Labels labels = Labels.builder().label("k1", "v1").build(); | ||
InfoSnapshot.InfoDataPointSnapshot snapshot = DataPointSnapshotBuilder.infoDataPoint(labels, "v1", "k1"); | ||
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. We should use a different value than 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. Yes, I missed that. Changed to just |
||
assertEquals("v1", snapshot.getLabels().get("k1")); | ||
} | ||
|
||
private void assertGaugeSnapshot(MetricSnapshot snapshot, double expectedValue, Labels expectedLabels) { | ||
assertInstanceOf(GaugeSnapshot.class, snapshot); | ||
GaugeSnapshot gaugeSnapshot = (GaugeSnapshot) snapshot; | ||
|
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.
We should use
org.slf4j.Logger
instead ofjava.util.logging.Logger
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.
That's why I couldn't use interpolation with
{}
. I was using the java.util logger. Changed tosl4j