diff --git a/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/WarnThenDebugLoggers.java b/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/WarnThenDebugLoggers.java index bef7df8311..3d54496b17 100644 --- a/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/WarnThenDebugLoggers.java +++ b/implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v2/WarnThenDebugLoggers.java @@ -29,7 +29,7 @@ class WarnThenDebugLoggers { static class StackTraceLogger extends WarnThenDebugLogger { - public StackTraceLogger() { + StackTraceLogger() { super(StackTraceLogger.class); } @@ -37,7 +37,7 @@ public StackTraceLogger() { static class NanGaugeLogger extends WarnThenDebugLogger { - public NanGaugeLogger() { + NanGaugeLogger() { super(NanGaugeLogger.class); } @@ -45,7 +45,7 @@ public NanGaugeLogger() { static class MetadataDiscrepancyLogger extends WarnThenDebugLogger { - public MetadataDiscrepancyLogger() { + MetadataDiscrepancyLogger() { super(MetadataDiscrepancyLogger.class); } diff --git a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java index 47dc8691cf..d51cd14c71 100644 --- a/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java +++ b/implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java @@ -63,6 +63,8 @@ */ class DynatraceExporterV2Test { + private static final String SUBSEQUENT_LOGS_AS_DEBUG = "Note that subsequent logs will be logged at debug level."; + private MockLoggerFactory loggerFactory; private MockLogger logger; @@ -79,8 +81,6 @@ class DynatraceExporterV2Test { private DynatraceExporterV2 exporter; - private static final String subsequentLogsAsDebug = "Note that subsequent logs will be logged at debug level."; - @BeforeEach void setUp() { this.config = createDefaultDynatraceConfig(); @@ -126,7 +126,7 @@ void toGaugeLineShouldDropNanValue_testLogWarnThenDebug() { String expectedMessage = "Meter 'my.gauge' returned a value of NaN, which will not be exported. This can be a deliberate value or because the weak reference to the backing object expired."; - LogEvent warnEvent = new LogEvent(WARN, String.join(" ", expectedMessage, subsequentLogsAsDebug), null); + LogEvent warnEvent = new LogEvent(WARN, String.join(" ", expectedMessage, SUBSEQUENT_LOGS_AS_DEBUG), null); LogEvent debugEvent = new LogEvent(DEBUG, expectedMessage, null); meterRegistry.gauge("my.gauge", NaN); @@ -692,15 +692,14 @@ void failOnSendWithExceptionShouldHaveProperLogging_warnThenDebug() { // these two will be logged by the WarnThenDebugLogger: // the warning message is suffixed with "Note that subsequent logs will be logged // at debug level.". - LogEvent warnThenDebugWarningLog = new LogEvent(WARN, - String.join(" ", expectedWarnThenDebugMessage, expectedException.getMessage(), subsequentLogsAsDebug), - expectedException); + LogEvent warnThenDebugWarningLog = new LogEvent(WARN, String.join(" ", expectedWarnThenDebugMessage, + expectedException.getMessage(), SUBSEQUENT_LOGS_AS_DEBUG), expectedException); LogEvent warnThenDebugDebugLog = new LogEvent(DEBUG, String.join(" ", expectedWarnThenDebugMessage, expectedException.getMessage()), expectedException); // this will be logged by the "general" logger in a single line (once per export) - LogEvent expectedExceptionLogMessage = new LogEvent(WARN, - "Failed metric ingestion: java.lang.RuntimeException: " + expectedException.getMessage(), null); + LogEvent expectedExceptionLogMessage = new LogEvent(WARN, "Failed metric ingestion: " + expectedException, + null); meterRegistry.gauge("my.gauge", 1d); Gauge gauge = meterRegistry.find("my.gauge").gauge(); @@ -712,28 +711,18 @@ void failOnSendWithExceptionShouldHaveProperLogging_warnThenDebug() { // debug event. assertThat(logger.getLogEvents()).containsOnlyOnce(expectedExceptionLogMessage); - long countExceptionLogsFirstExport = logger.getLogEvents() - .stream() - .filter(event -> event.equals(expectedExceptionLogMessage)) - .count(); - assertThat(countExceptionLogsFirstExport).isEqualTo(1); - // the WarnThenDebugLogger only has one event so far. - assertThat(stackTraceLogger.getLogEvents()).hasSize(1).containsExactly(warnThenDebugWarningLog); + assertThat(stackTraceLogger.getLogEvents()).containsExactly(warnThenDebugWarningLog); // second export exporter.export(Collections.singletonList(gauge)); // after the second export, the general logger contains the warning log twice - long countExceptionLogsSecondExport = logger.getLogEvents() - .stream() - .filter(event -> event.equals(expectedExceptionLogMessage)) - .count(); - assertThat(countExceptionLogsSecondExport).isEqualTo(2); + assertThat(logger.getLogEvents().stream().filter(event -> event.equals(expectedExceptionLogMessage))) + .hasSize(2); // the WarnThenDebugLogger now has two logs. - assertThat(stackTraceLogger.getLogEvents()).hasSize(2) - .containsExactly(warnThenDebugWarningLog, warnThenDebugDebugLog); + assertThat(stackTraceLogger.getLogEvents()).containsExactly(warnThenDebugWarningLog, warnThenDebugDebugLog); } @Test @@ -1031,7 +1020,7 @@ void conflictingMetadataIsIgnored_testLogWarnThenDebug() { + "original metadata:\t#my.count count dt.meta.description=count\\ 1\\ description,dt.meta.unit=Bytes\n" + "tried to set new:\t#my.count count dt.meta.description=count\\ description\n" + "Metadata for metric key my.count will not be sent."; - LogEvent warnEvent = new LogEvent(WARN, String.join(" ", expectedLogMessage, subsequentLogsAsDebug), null); + LogEvent warnEvent = new LogEvent(WARN, String.join(" ", expectedLogMessage, SUBSEQUENT_LOGS_AS_DEBUG), null); LogEvent debugEvent = new LogEvent(DEBUG, expectedLogMessage, null); HttpSender.Request.Builder builder = mock(HttpSender.Request.Builder.class); @@ -1056,12 +1045,11 @@ void conflictingMetadataIsIgnored_testLogWarnThenDebug() { // first export exporter.export(meterRegistry.getMeters()); - assertThat(metadataDiscrepancyLogger.getLogEvents()).hasSize(1).containsExactly(warnEvent); + assertThat(metadataDiscrepancyLogger.getLogEvents()).containsExactly(warnEvent); // second export exporter.export(meterRegistry.getMeters()); - assertThat(metadataDiscrepancyLogger.getLogEvents()).hasSize(2).containsExactly(warnEvent, debugEvent); - + assertThat(metadataDiscrepancyLogger.getLogEvents()).containsExactly(warnEvent, debugEvent); } @Test diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/logging/Log4j2Metrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/logging/Log4j2Metrics.java index 2d19fd8ff1..166b424db9 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/logging/Log4j2Metrics.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/logging/Log4j2Metrics.java @@ -55,6 +55,8 @@ public class Log4j2Metrics implements MeterBinder, AutoCloseable { private static final String METER_NAME = "log4j2.events"; + private static final String METER_DESCRIPTION = "Number of log events"; + private final Iterable tags; private final LoggerContext loggerContext; @@ -156,42 +158,42 @@ static class MetricsFilter extends AbstractFilter { fatalCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "fatal") - .description("Number of fatal level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); errorCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "error") - .description("Number of error level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); warnCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "warn") - .description("Number of warn level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); infoCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "info") - .description("Number of info level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); debugCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "debug") - .description("Number of debug level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); traceCounter = Counter.builder(METER_NAME) .tags(tags) .tags("level", "trace") - .description("Number of trace level log events") + .description(METER_DESCRIPTION) .baseUnit(BaseUnits.EVENTS) .register(registry); }