Skip to content

Commit

Permalink
Polish gh-4583 (#4863)
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Mar 22, 2024
1 parent 52075c6 commit faeeeec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ class WarnThenDebugLoggers {

static class StackTraceLogger extends WarnThenDebugLogger {

public StackTraceLogger() {
StackTraceLogger() {
super(StackTraceLogger.class);
}

}

static class NanGaugeLogger extends WarnThenDebugLogger {

public NanGaugeLogger() {
NanGaugeLogger() {
super(NanGaugeLogger.class);
}

}

static class MetadataDiscrepancyLogger extends WarnThenDebugLogger {

public MetadataDiscrepancyLogger() {
MetadataDiscrepancyLogger() {
super(MetadataDiscrepancyLogger.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit faeeeec

Please sign in to comment.