diff --git a/README.md b/README.md index 0ac5b19..8f6a96b 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ This example sets up a root logger with a Loki appender. - + ${sys:loki.host} ${sys:loki.port} @@ -112,7 +112,32 @@ a `X-Scope-OrgID` header when [running Loki in multi-tenant mode](https://grafan #### Label (optional) Specify additional labels attached to each log line sent via this appender instance. - + +#### bufferSizeMegabytes (optional, default = 1MB) + +Size of the `log buffer`. Must be power of two between 1MB and 1GB. See https://github.com/tkowalcz/tjahzi/wiki/Log-buffer-sizing for more explanations. + +#### maxRetries (optional, default = 3) + +Maximum number of retries to perform when delivering log message to Loki. Log buffer data is delivered in order, one batch after +the other, so too much retries will block delivery of subsequent log batches (on the other hand if we need to retry many times then +next batches will probably fail too). + +#### connectTimeoutMillis (optional, default = 5s) + +This configures socket connect timeout when connecting to Loki. After unsuccessful connection attempt it will continue to retry indefinitely +employing exponential backoff (initial backoff = 250ms, maximum backoff = 30s, multiplier = 3). + +#### readTimeoutMillis (optional, default = 60s) + +Sets socket read timeout on Loki connection. + +#### useOffHeapBuffer (optional, default = true) + +Whether Tjahzi should allocate native buffer for `Log buffer` component. We can go into a rabbit hole of divagations what are the +implications of this. Most important in our view is that having 10s or 100s of MB of space taken out of heap is not very +friendly to garbage collector which might have to occasionally copy it around. + # LICENSE This work is released under MIT license. Feel free to use, copy and modify this work as long as you credit original authors. diff --git a/loki-log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/LokiAppender.java b/loki-log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/LokiAppender.java index 0d86b6d..53cb9bc 100644 --- a/loki-log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/LokiAppender.java +++ b/loki-log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/LokiAppender.java @@ -54,13 +54,13 @@ public class LokiAppender extends AbstractAppender { private int port; @PluginBuilderAttribute - private int connectTimeoutMillis = 0; + private int connectTimeoutMillis = 5000; @PluginBuilderAttribute - private int readTimeoutMillis = 0; + private int readTimeoutMillis = 60_000; @PluginBuilderAttribute - private int maxRetries = 0; + private int maxRetries = 3; @PluginBuilderAttribute private int bufferSizeMegabytes = 32; diff --git a/loki-log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/BufferSizeConfigurationTest.java b/loki-log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/BufferSizeConfigurationTest.java index 2d66f07..c160303 100644 --- a/loki-log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/BufferSizeConfigurationTest.java +++ b/loki-log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/BufferSizeConfigurationTest.java @@ -3,10 +3,6 @@ package pl.tkowalcz.tjahzi.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.LoggerContext; import org.junit.jupiter.api.Test; -import org.testcontainers.containers.BindMode; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import java.net.URI; @@ -17,27 +13,29 @@ import static org.assertj.core.api.Assertions.assertThat; @Testcontainers class BufferSizeConfigurationTest { - @Container - public GenericContainer loki = new GenericContainer("grafana/loki:latest") - .withCommand("-config.file=/etc/loki-config.yaml") - .withClasspathResourceMapping("loki-config.yaml", - "/etc/loki-config.yaml", - BindMode.READ_ONLY) - .waitingFor( - Wait.forHttp("/ready") - .forPort(3100) - ) - .withExposedPorts(3100); - @Test - void shouldSendData() throws URISyntaxException { + void shouldUseDefaultWhenLogBufferSizeIsNotSetViaConfiguration() throws URISyntaxException { // Given - System.setProperty("loki.host", loki.getHost()); - System.setProperty("loki.port", loki.getFirstMappedPort().toString()); - URI uri = getClass() .getClassLoader() - .getResource("appender-test-with-custom-settings-log4j2-configuration.xml") + .getResource("appender-test-with-buffer-size-megabytes-unset.xml") + .toURI(); + + // When + LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.setConfigLocation(uri); + + // Then + LokiAppender loki = context.getConfiguration().getAppender("Loki"); + assertThat(loki.getLoggingSystem().getLogBufferSize()).isEqualTo(32 * 1024 * 1024); + } + + @Test + void shouldSetLogBufferSizeViaConfiguration() throws URISyntaxException { + // Given + URI uri = getClass() + .getClassLoader() + .getResource("appender-test-with-buffer-size-megabytes-set-to-64.xml") .toURI(); // When diff --git a/loki-log4j2-appender/src/test/resources/appender-test-with-custom-settings-log4j2-configuration.xml b/loki-log4j2-appender/src/test/resources/appender-test-with-buffer-size-megabytes-set-to-64.xml similarity index 100% rename from loki-log4j2-appender/src/test/resources/appender-test-with-custom-settings-log4j2-configuration.xml rename to loki-log4j2-appender/src/test/resources/appender-test-with-buffer-size-megabytes-set-to-64.xml diff --git a/loki-log4j2-appender/src/test/resources/appender-test-with-buffer-size-megabytes-unset.xml b/loki-log4j2-appender/src/test/resources/appender-test-with-buffer-size-megabytes-unset.xml new file mode 100644 index 0000000..663c75b --- /dev/null +++ b/loki-log4j2-appender/src/test/resources/appender-test-with-buffer-size-megabytes-unset.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + ${sys:loki.host} + ${sys:loki.port} + + + + %X{tid} [%t] %d{MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n%exception{full} + + +
+