Skip to content

Commit

Permalink
Optimized forEach loop into counted loop to avoid allocation. Added d…
Browse files Browse the repository at this point in the history
…ocumentation of MDC lookups.
  • Loading branch information
tkowalcz committed Jul 20, 2021
1 parent b6a3a5a commit 40149bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
44 changes: 43 additions & 1 deletion logback-appender/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ On top of the `Core` component that sends logs to Loki this project gives you Lo

### Note on Loki HTTP endpoint and host/port configuration

Tjahzi sends `POST` requests to `/loki/api/v1/push` HTTP endpoint. Specifying e.g. `<host>loki.mydomain.com</host><port>3100</port>`
Tjahzi sends `POST` requests to `/loki/api/v1/push` HTTP endpoint. Specifying
e.g. `<host>loki.mydomain.com</host><port>3100</port>`
will configure the appender to call to URL: `http://loki.mydomain.com:3100/loki/api/v1/push`.

## Custom pattern layout and decreasing allocations
Expand Down Expand Up @@ -116,6 +117,47 @@ Contents of the properties are automatically interpolated by Logback (
see [here](http://logback.qos.ch/manual/configuration.html#variableSubstitution)). All environment, system etc. variable
references will be replaced by their values during initialization of the appender.

### MDC support

MDC is supported via `mdcLogLabel` tag. It will dynamically extract MDC value associated with its content and will turn
it into a label.

<details>
<summary>Click to expand example</summary>

```xml
<configuration debug="true">
<appender name="Loki" class="pl.tkowalcz.tjahzi.logback.LokiAppender">
<host>${loki.host}</host>
<port>${loki.port}</port>

<efficientLayout>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</efficientLayout>

<label>
<name>server</name>
<value>127.0.0.1</value>
</label>

<!-- MDC -->
<mdcLogLabel>
trace_id
</mdcLogLabel>

<!-- MDC -->
<mdcLogLabel>
span_id
</mdcLogLabel>
</appender>

<root level="debug">
<appender-ref ref="Loki"/>
</root>
</configuration>
```
</details>

## Details

Let's go through the example config above and analyze configuration options (**Note: Tags are case-insensitive**).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class LokiAppender extends LokiAppenderConfigurator {
private TjahziLogger logger;
private String logLevelLabel;
private List<String> mdcLogLabels;

private MutableMonitoringModuleWrapper monitoringModuleWrapper;

public EfficientPatternLayout getEfficientLayout() {
Expand Down Expand Up @@ -80,13 +81,16 @@ private void appendLogLabel(LabelSerializer labelSerializer, String logLevel) {
}
}

@SuppressWarnings("ForLoopReplaceableByForEach") // Allocator goes brrrr
private void appendMdcLogLabels(LabelSerializer serializer,
Map<String, String> mdcPropertyMap) {
mdcLogLabels.forEach(mdcLogLabel -> {
for (int i = 0; i < mdcLogLabels.size(); i++) {
String mdcLogLabel = mdcLogLabels.get(i);

if (mdcPropertyMap.containsKey(mdcLogLabel)) {
serializer.appendLabel(mdcLogLabel, mdcPropertyMap.get(mdcLogLabel));
}
});
}
}

@Override
Expand Down

0 comments on commit 40149bc

Please sign in to comment.