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

adds the logger_name and thread_name labels for logback #108

Merged
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 @@ -17,15 +17,21 @@ public class LabelFactory {
private final ContextAware internalLogger;

private final String logLevelLabel;
private final String loggerNameLabel;
private final String threadNameLabel;
private final Label[] labels;

public LabelFactory(
ContextAware internalLogger,
String logLevelLabel,
String loggerNameLabel,
String threadNameLabel,
Label... labels
) {
this.internalLogger = internalLogger;
this.logLevelLabel = logLevelLabel;
this.loggerNameLabel = loggerNameLabel;
this.threadNameLabel = threadNameLabel;
this.labels = labels;
}

Expand All @@ -36,9 +42,34 @@ public HashMap<String, String> convertLabelsDroppingInvalid() {

public String validateLogLevelLabel(HashMap<String, String> existingLabels) {
if (logLevelLabel != null) {
return validateLogLevelLabelAgainst(
return validatePredefinedLabelAgainst(
existingLabels,
logLevelLabel
logLevelLabel,
"log level label"
);
}

return null;
}

public String validateLoggerNameLabel(HashMap<String, String> existingLabels) {
if (loggerNameLabel != null) {
return validatePredefinedLabelAgainst(
existingLabels,
loggerNameLabel,
"logger name label"
);
}

return null;
}

public String validateThreadNameLabel(HashMap<String, String> existingLabels) {
if (threadNameLabel != null) {
return validatePredefinedLabelAgainst(
existingLabels,
threadNameLabel,
"thread name label"
);
}

Expand Down Expand Up @@ -89,31 +120,34 @@ private HashMap<String, String> convertAndLogViolations() {
return lokiLabels;
}

private String validateLogLevelLabelAgainst(
private String validatePredefinedLabelAgainst(
Map<String, String> existingLabels,
String logLevelLabel
String predefinedLabel,
String predefinedLabelDescription
) {
if (!Label.hasValidName(logLevelLabel)) {
if (!Label.hasValidName(predefinedLabel)) {
internalLogger.addWarn(
String.format(
"Ignoring log level label '%s' - contains invalid characters. %s\n",
logLevelLabel,
"Ignoring %s '%s' - contains invalid characters. %s\n",
predefinedLabelDescription,
predefinedLabel,
GitHubDocs.LABEL_NAMING.getLogMessage()
)
);

return null;
}

if (existingLabels.remove(logLevelLabel) != null) {
if (existingLabels.remove(predefinedLabel) != null) {
internalLogger.addWarn(
String.format(
"Ignoring log level label '%s' - conflicts with label defined in configuration.\n",
logLevelLabel
"Ignoring %s '%s' - conflicts with label defined in configuration.\n",
predefinedLabelDescription,
predefinedLabel
)
);
}

return logLevelLabel;
return predefinedLabel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class LokiAppender extends LokiAppenderConfigurator {

private TjahziLogger logger;
private String logLevelLabel;
private String loggerNameLabel;
private String threadNameLabel;
private List<String> mdcLogLabels;

private MutableMonitoringModuleWrapper monitoringModuleWrapper;
Expand Down Expand Up @@ -62,11 +64,15 @@ public LoggingSystem getLoggingSystem() {
@Override
protected void append(ILoggingEvent event) {
String logLevel = event.getLevel().toString();
String loggerName = event.getLoggerName();
String threadName = event.getThreadName();
ByteBuffer logLine = actualEncoder.apply(event);
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();

LabelSerializer labelSerializer = LabelSerializers.threadLocal();
appendLogLabel(labelSerializer, logLevel);
appendLoggerLabel(labelSerializer, loggerName);
appendThreadLabel(labelSerializer, threadName);
appendMdcLogLabels(labelSerializer, mdcPropertyMap);

logger.log(
Expand All @@ -83,6 +89,17 @@ private void appendLogLabel(LabelSerializer labelSerializer, String logLevel) {
}
}

private void appendLoggerLabel(LabelSerializer labelSerializer, String loggerName) {
if (loggerNameLabel != null) {
labelSerializer.appendLabel(loggerNameLabel, loggerName);
}
}

private void appendThreadLabel(LabelSerializer labelSerializer, String threadName) {
if (threadNameLabel != null) {
labelSerializer.appendLabel(threadNameLabel, threadName);
}
}
@SuppressWarnings("ForLoopReplaceableByForEach") // Allocator goes brrrr
private void appendMdcLogLabels(LabelSerializer serializer,
Map<String, String> mdcPropertyMap) {
Expand Down Expand Up @@ -112,6 +129,8 @@ public void start() {
LokiAppenderFactory lokiAppenderFactory = new LokiAppenderFactory(this);
loggingSystem = lokiAppenderFactory.createAppender();
logLevelLabel = lokiAppenderFactory.getLogLevelLabel();
loggerNameLabel = lokiAppenderFactory.getLoggerNameLabel();
threadNameLabel = lokiAppenderFactory.getThreadNameLabel();
mdcLogLabels = lokiAppenderFactory.getMdcLogLabels();
monitoringModuleWrapper = lokiAppenderFactory.getMonitoringModuleWrapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public abstract class LokiAppenderConfigurator extends UnsynchronizedAppenderBas
private boolean useOffHeapBuffer = true;

private String logLevelLabel;
private String loggerNameLabel;
private String threadNameLabel;
private final List<String> mdcLogLabels = new ArrayList<>();

private long batchSize = 10_2400;
Expand Down Expand Up @@ -156,6 +158,22 @@ public void setLogLevelLabel(String logLevelLabel) {
this.logLevelLabel = logLevelLabel;
}

public String getLoggerNameLabel() {
return loggerNameLabel;
}

public void setLoggerNameLabel(String loggerNameLabel) {
this.loggerNameLabel = loggerNameLabel;
}

public String getThreadNameLabel() {
return threadNameLabel;
}

public void setThreadNameLabel(String threadNameLabel) {
this.threadNameLabel = threadNameLabel;
}

public void setBatchSize(long batchSize) {
this.batchSize = batchSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class LokiAppenderFactory {

private final HashMap<String, String> lokiLabels;
private final String logLevelLabel;
private final String loggerNameLabel;
private final String threadNameLabel;
private final List<String> mdcLogLabels;
private final MutableMonitoringModuleWrapper monitoringModuleWrapper;

Expand All @@ -29,11 +31,15 @@ public LokiAppenderFactory(LokiAppenderConfigurator configurator) {
LabelFactory labelFactory = new LabelFactory(
configurator,
configurator.getLogLevelLabel(),
configurator.getLoggerNameLabel(),
configurator.getThreadNameLabel(),
configurator.getLabels().toArray(new Label[0])
);

lokiLabels = labelFactory.convertLabelsDroppingInvalid();
logLevelLabel = labelFactory.validateLogLevelLabel(lokiLabels);
loggerNameLabel = labelFactory.validateLoggerNameLabel(lokiLabels);
threadNameLabel = labelFactory.validateThreadNameLabel(lokiLabels);
mdcLogLabels = configurator.getMdcLogLabels();
monitoringModuleWrapper = new MutableMonitoringModuleWrapper();
}
Expand Down Expand Up @@ -96,6 +102,14 @@ public String getLogLevelLabel() {
return logLevelLabel;
}

public String getLoggerNameLabel() {
return loggerNameLabel;
}

public String getThreadNameLabel() {
return threadNameLabel;
}

public MutableMonitoringModuleWrapper getMonitoringModuleWrapper() {
return monitoringModuleWrapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ void shouldRemoveDuplicatedLabels() {
LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
"log_level",
"logger_name",
"thread_name",
thisIsADuplicateAndShouldBeDropped,
thisShouldStay,
thisTooShouldStay
Expand All @@ -45,6 +47,8 @@ void shouldSkipLabelsWithInvalidName() {
LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
"log_level",
"logger_name",
"thread_name",
thisShouldStayToo,
thisShouldStay,
invalidNameShouldBeDropped
Expand All @@ -61,14 +65,16 @@ void shouldSkipLabelsWithInvalidName() {
}

@Test
void shouldAcceptNullLogLevel() {
void shouldAcceptNullPredefinedLabels() {
// Given
Label label1 = Label.createLabel("ip", "10.0.2.34");
Label label2 = Label.createLabel("region", "coruscant-west-2");

LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
null,
null,
null,
label2,
label1
);
Expand All @@ -94,6 +100,8 @@ void logLevelLabelShouldOverrideConflictingLabel() {
LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
logLevelLabel,
null,
null,
thisShouldBeRemovedDueToConflict,
thisShouldStay
);
Expand Down Expand Up @@ -121,7 +129,7 @@ void shouldDisableLogLevelLabelIfItHasInvalidName() {
// Given
String logLevelLabel = "log-level";

LabelFactory labelFactory = new LabelFactory(new ConsoleAppender<>(), logLevelLabel);
LabelFactory labelFactory = new LabelFactory(new ConsoleAppender<>(), logLevelLabel, null, null);

// When
String actual = labelFactory.validateLogLevelLabel(new HashMap<>());
Expand All @@ -131,10 +139,115 @@ void shouldDisableLogLevelLabelIfItHasInvalidName() {
}

@Test
void shouldHandleDisabledLogLevelLabel() {
void loggerNameLabelShouldOverrideConflictingLabel() {
// Given
String loggerNameLabel = "logger_name";

Label thisShouldStay = Label.createLabel("ip", "10.0.2.34");
Label thisShouldBeRemovedDueToConflict = Label.createLabel(loggerNameLabel, "coruscant-west-2");

LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
null,
loggerNameLabel,
null,
thisShouldBeRemovedDueToConflict,
thisShouldStay
);

HashMap<String, String> labels = new HashMap<>(
Map.ofEntries(
asEntry(thisShouldStay),
asEntry(thisShouldBeRemovedDueToConflict)
)
);

// When
String actual = labelFactory.validateLoggerNameLabel(labels);

// Then
assertThat(labels).containsOnly(
asEntry(thisShouldStay)
);

assertThat(actual).isEqualTo(loggerNameLabel);
}

@Test
void shouldDisableLoggerNameLabelIfItHasInvalidName() {
// Given
String loggerNameLabel = "logger-name";

LabelFactory labelFactory = new LabelFactory(new ConsoleAppender<>(), null, loggerNameLabel, null);

// When
String actual = labelFactory.validateLogLevelLabel(new HashMap<>());

// Then
assertThat(actual).isNull();
}

@Test
void threadNameLabelShouldOverrideConflictingLabel() {
// Given
String threadNameLabel = "thread_name";

Label thisShouldStay = Label.createLabel("ip", "10.0.2.34");
Label thisShouldBeRemovedDueToConflict = Label.createLabel(threadNameLabel, "coruscant-west-2");

LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
threadNameLabel,
null,
null,
thisShouldBeRemovedDueToConflict,
thisShouldStay
);

HashMap<String, String> labels = new HashMap<>(
Map.ofEntries(
asEntry(thisShouldStay),
asEntry(thisShouldBeRemovedDueToConflict)
)
);

// When
String actual = labelFactory.validateLogLevelLabel(labels);

// Then
assertThat(labels).containsOnly(
asEntry(thisShouldStay)
);

assertThat(actual).isEqualTo(threadNameLabel);
}

@Test
void shouldDisableThreadNameLabelIfItHasInvalidName() {
// Given
String threadNameLabel = "thread-name";

LabelFactory labelFactory = new LabelFactory(new ConsoleAppender<>(), threadNameLabel, null, null);

// When
String actual = labelFactory.validateLogLevelLabel(new HashMap<>());

// Then
assertThat(actual).isNull();
}

@Test
void shouldHandleDisabledPredefinedLabels() {
// Given
String logLevelLabel = null;
LabelFactory labelFactory = new LabelFactory(new ConsoleAppender<>(), logLevelLabel);
String loggerNameLabel = null;
String threadNameLabel = null;
LabelFactory labelFactory = new LabelFactory(
new ConsoleAppender<>(),
logLevelLabel,
loggerNameLabel,
threadNameLabel
);

// When
String actual = labelFactory.validateLogLevelLabel(new HashMap<>());
Expand Down
Loading