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

feat(store): forbidden negative metrics #567

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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 @@ -23,8 +23,13 @@
import io.opentelemetry.api.metrics.Meter;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StreamMetricsCounter extends BaseStreamMetrics implements Counter {
protected static final Logger LOGGER = LoggerFactory.getLogger(StreamMetricsCounter.class);

private final LongCounter counter;

public StreamMetricsCounter(String name, Map<String, String> tags,
Expand All @@ -41,6 +46,11 @@ public void inc() {

@Override
public void inc(long n) {
if (n < 0) {
String tag = tags.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining(", "));
LOGGER.warn("Counter value is negative, name: {}, tag: {}, value: {}", metricsName, tag, n);
return;
}
counter.add(n, newAttributesBuilder().build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
import io.opentelemetry.api.metrics.Meter;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StreamMetricsHistogram extends BaseStreamMetrics implements Histogram {
protected static final Logger LOGGER = LoggerFactory.getLogger(StreamMetricsHistogram.class);

private final LongHistogram histogram;

public StreamMetricsHistogram(String name, Map<String, String> tags,
Expand All @@ -38,6 +43,11 @@ public StreamMetricsHistogram(String name, Map<String, String> tags,

@Override
public void update(long value) {
if (value < 0) {
String tag = tags.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining(", "));
LOGGER.warn("Histogram value is negative, name: {}, tag: {}, value: {}", metricsName, tag, value);
return;
}
histogram.record(value, newAttributesBuilder().build());
}

Expand Down