-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from efimmatytsin/master
Logback mdcLogLabel functionality
- Loading branch information
Showing
5 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
logback-appender/src/test/java/pl/tkowalcz/tjahzi/logback/MdcLogLabelConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package pl.tkowalcz.tjahzi.logback; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.JoranConfigurator; | ||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.parsing.Parser; | ||
import org.awaitility.Awaitility; | ||
import org.awaitility.Durations; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
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; | ||
import java.util.UUID; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
|
||
@Testcontainers | ||
class MdcLogLabelConfigurationTest { | ||
|
||
@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 shouldSendMdcLogLabelAsConfigured() throws Exception { | ||
// Given | ||
System.setProperty("loki.host", loki.getHost()); | ||
System.setProperty("loki.port", loki.getFirstMappedPort().toString()); | ||
|
||
URI uri = getClass() | ||
.getClassLoader() | ||
.getResource("appender-test-with-md-log-label-set.xml") | ||
.toURI(); | ||
|
||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
|
||
JoranConfigurator configurator = new JoranConfigurator(); | ||
configurator.setContext(context); | ||
|
||
context.reset(); | ||
configurator.doConfigure(uri.toURL()); | ||
|
||
// When | ||
String traceIdValue = UUID.randomUUID().toString(); | ||
String spanIdValue = UUID.randomUUID().toString(); | ||
|
||
MDC.put("trace_id", traceIdValue); | ||
MDC.put("span_id", spanIdValue); | ||
|
||
Logger logger = context.getLogger(LokiAppenderLargeBatchesTest.class); | ||
logger.info("Test"); | ||
|
||
// Then | ||
RestAssured.port = loki.getFirstMappedPort(); | ||
RestAssured.baseURI = "http://" + loki.getHost(); | ||
RestAssured.registerParser("text/plain", Parser.JSON); | ||
|
||
Awaitility | ||
.await() | ||
.atMost(Durations.TEN_SECONDS) | ||
.pollInterval(Durations.ONE_SECOND) | ||
.ignoreExceptions() | ||
.untilAsserted(() -> { | ||
given() | ||
.contentType(ContentType.URLENC) | ||
.urlEncodingEnabled(false) | ||
.formParam("query=%7Bserver%3D%22127.0.0.1%22%7D") | ||
.when() | ||
.get("/loki/api/v1/query_range") | ||
.then() | ||
.log() | ||
.all() | ||
.statusCode(200) | ||
.body("status", equalTo("success")) | ||
.body("data.result.size()", equalTo(1)) | ||
.body("data.result[0].stream.server", equalTo("127.0.0.1")) | ||
.body("data.result[0].stream.trace_id", equalTo(traceIdValue)) | ||
.body("data.result[0].stream.span_id", equalTo(spanIdValue)); | ||
}); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
logback-appender/src/test/resources/appender-test-with-md-log-label-set.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<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> | ||
|
||
<header> | ||
<name>X-Org-Id</name> | ||
<value>Circus</value> | ||
</header> | ||
|
||
<label> | ||
<name>server</name> | ||
<value>127.0.0.1</value> | ||
</label> | ||
|
||
<mdcLogLabel> | ||
trace_id | ||
</mdcLogLabel> | ||
|
||
<mdcLogLabel> | ||
span_id | ||
</mdcLogLabel> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="Loki"/> | ||
</root> | ||
</configuration> |