Skip to content

Log metrics with fine log level. #131

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

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add pool index to all logs.
  • Loading branch information
nimf committed May 23, 2022
commit d960e84495432a3fa3c00d8a6eb82d8590a4c587
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.logging.Logger;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -196,6 +197,14 @@ public GcpManagedChannel(
}
}

private Supplier<String> log(Supplier<String> messageSupplier) {
return () -> String.format("%s: %s", metricPoolIndex, messageSupplier.get());
}

private String log(String message) {
return String.format("%s: %s", metricPoolIndex, message);
}

private void initOptions() {
GcpManagedChannelOptions.GcpChannelPoolOptions poolOptions = options.getChannelPoolOptions();
if (poolOptions != null) {
Expand All @@ -216,16 +225,16 @@ private synchronized void initLogMetrics() {
private void initMetrics() {
final GcpMetricsOptions metricsOptions = options.getMetricsOptions();
if (metricsOptions == null) {
logger.info("Metrics options are empty. Metrics disabled.");
logger.info(log("Metrics options are empty. Metrics disabled."));
initLogMetrics();
return;
}
if (metricsOptions.getMetricRegistry() == null) {
logger.info("Metric registry is null. Metrics disabled.");
logger.info(log("Metric registry is null. Metrics disabled."));
initLogMetrics();
return;
}
logger.info("Metrics enabled.");
logger.info(log("Metrics enabled."));

metricRegistry = metricsOptions.getMetricRegistry();
labelKeys.addAll(metricsOptions.getLabelKeys());
Expand Down Expand Up @@ -426,15 +435,15 @@ private void initMetrics() {
}

private void logGauge(String key, long value) {
logger.fine(String.format("%s stat: %s = %d", metricPoolIndex, key, value));
logger.fine(log(String.format("stat: %s = %d", key, value)));
}

private void logCumulative(String key, long value) {
logger.fine(() -> {
logger.fine(log(() -> {
Long prevValue = cumulativeMetricValues.put(key, value);
long logValue = prevValue == null ? value : value - prevValue;
return String.format("%s stat: %s = %d", metricPoolIndex, key, logValue);
});
return String.format("stat: %s = %d", key, logValue);
}));
}

private void logMetrics() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public void testMetrics() {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_CHANNELS + " = 5");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_CHANNELS + " = 5");

List<PointWithFunction<?>> maxAllowedChannels =
record.getMetrics().get(prefix + GcpMetricsConstants.METRIC_MAX_ALLOWED_CHANNELS);
Expand All @@ -636,7 +636,7 @@ public void testMetrics() {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_ALLOWED_CHANNELS + " = 10");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_ALLOWED_CHANNELS + " = 10");

List<PointWithFunction<?>> minActiveStreams =
record.getMetrics().get(prefix + GcpMetricsConstants.METRIC_MIN_ACTIVE_STREAMS);
Expand All @@ -647,7 +647,7 @@ public void testMetrics() {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MIN_ACTIVE_STREAMS + " = 0");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MIN_ACTIVE_STREAMS + " = 0");

List<PointWithFunction<?>> maxActiveStreams =
record.getMetrics().get(prefix + GcpMetricsConstants.METRIC_MAX_ACTIVE_STREAMS);
Expand All @@ -658,7 +658,7 @@ public void testMetrics() {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_ACTIVE_STREAMS + " = 7");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_ACTIVE_STREAMS + " = 7");

List<PointWithFunction<?>> totalActiveStreams =
record.getMetrics().get(prefix + GcpMetricsConstants.METRIC_MAX_TOTAL_ACTIVE_STREAMS);
Expand All @@ -670,7 +670,7 @@ public void testMetrics() {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_TOTAL_ACTIVE_STREAMS + " = " +
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_TOTAL_ACTIVE_STREAMS + " = " +
totalStreamsExpected);
} finally {
pool.shutdownNow();
Expand Down Expand Up @@ -727,31 +727,31 @@ public void testUnresponsiveDetection() throws InterruptedException {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 1");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 1");

metric = record.getMetrics().get(GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DROPPED_CALLS);
assertThat(metric.size()).isEqualTo(1);
assertThat(metric.get(0).value()).isEqualTo(3L);
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DROPPED_CALLS + " = 3");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DROPPED_CALLS + " = 3");

metric = record.getMetrics().get(GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DROPPED_CALLS);
assertThat(metric.size()).isEqualTo(1);
assertThat(metric.get(0).value()).isEqualTo(3L);
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DROPPED_CALLS + " = 3");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DROPPED_CALLS + " = 3");

metric = record.getMetrics().get(GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DETECTION_TIME);
assertThat(metric.size()).isEqualTo(1);
assertThat(metric.get(0).value()).isAtLeast(100L);
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).matches(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DETECTION_TIME +
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MIN_UNRESPONSIVE_DETECTION_TIME +
" = 1\\d\\d");

metric = record.getMetrics().get(GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DETECTION_TIME);
Expand All @@ -760,7 +760,7 @@ public void testUnresponsiveDetection() throws InterruptedException {
assertThat(logRecords.size()).isEqualTo(++logCount);
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
assertThat(lastLogMessage()).matches(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DETECTION_TIME +
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_MAX_UNRESPONSIVE_DETECTION_TIME +
" = 1\\d\\d");

// Any message from the server must reset the dropped requests count and timestamp.
Expand Down Expand Up @@ -814,7 +814,7 @@ public void testUnresponsiveDetection() throws InterruptedException {
// But the log metric count the detections since previous report for num_unresponsive_detections
// in the logs. It is always delta in the logs, not cumulative.
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 1");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 1");
// If we log it again the cumulative metric value must remain unchanged.
metric = record.getMetrics().get(GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS);
assertThat(metric.size()).isEqualTo(1);
Expand All @@ -823,7 +823,7 @@ public void testUnresponsiveDetection() throws InterruptedException {
assertThat(lastLogLevel()).isEqualTo(Level.FINE);
// But in the log it must post 0.
assertThat(lastLogMessage()).isEqualTo(
poolIndex + " stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 0");
poolIndex + ": stat: " + GcpMetricsConstants.METRIC_NUM_UNRESPONSIVE_DETECTIONS + " = 0");
}

static class FakeIdleCountingManagedChannel extends ManagedChannel {
Expand Down