adding live test
This commit is contained in:
parent
16dc441ac5
commit
3563a3e35a
1
spring-boot-modules/spring-boot-logging-loki/README.md
Normal file
1
spring-boot-modules/spring-boot-logging-loki/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
### Relevant Articles:
|
41
spring-boot-modules/spring-boot-logging-loki/pom.xml
Normal file
41
spring-boot-modules/spring-boot-logging-loki/pom.xml
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spring-boot-logging-loki</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<name>loki</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||||
|
<artifactId>spring-boot-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.loki4j</groupId>
|
||||||
|
<artifactId>loki-logback-appender</artifactId>
|
||||||
|
<version>${loki-logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<loki-logback.version>1.4.2</loki-logback.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.loki;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DemoService {
|
||||||
|
|
||||||
|
private final Logger LOG = LoggerFactory.getLogger(DemoService.class);
|
||||||
|
|
||||||
|
public void log() {
|
||||||
|
LOG.info("DemoService.log invoked!!!");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.extensions;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootLogbackExtensionsApplication {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SpringBootLogbackExtensionsApplication.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootLogbackExtensionsApplication.class, args);
|
||||||
|
|
||||||
|
logger.debug("Debug log message");
|
||||||
|
logger.info("Info log message");
|
||||||
|
logger.error("Error log message");
|
||||||
|
logger.warn("Warn log message");
|
||||||
|
logger.trace("Trace log message");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<springProperty name="name" source="spring.application.name"/>
|
||||||
|
|
||||||
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>
|
||||||
|
{
|
||||||
|
"level":"%level",
|
||||||
|
"class":"%logger{36}",
|
||||||
|
"thread":"%thread",
|
||||||
|
"message": "%message",
|
||||||
|
"requestId": "%X{X-Request-ID}"
|
||||||
|
}
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
|
||||||
|
<http>
|
||||||
|
<url>http://localhost:3100/loki/api/v1/push</url>
|
||||||
|
</http>
|
||||||
|
<format>
|
||||||
|
<label>
|
||||||
|
<pattern>app=${name},host=${HOSTNAME},level=%level</pattern>
|
||||||
|
<readMarkers>true</readMarkers>
|
||||||
|
</label>
|
||||||
|
<message>
|
||||||
|
<pattern>
|
||||||
|
{
|
||||||
|
"level":"%level",
|
||||||
|
"class":"%logger{36}",
|
||||||
|
"thread":"%thread",
|
||||||
|
"message": "%message",
|
||||||
|
"requestId": "%X{X-Request-ID}"
|
||||||
|
}
|
||||||
|
</pattern>
|
||||||
|
</message>
|
||||||
|
</format>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="CONSOLE"/>
|
||||||
|
<appender-ref ref="LOKI"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
@ -0,0 +1,86 @@
|
|||||||
|
package com.baeldung.loki;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = DemoService.class)
|
||||||
|
public class DemoServiceLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLokiContainerRunning_whenDemoServiceInvoked_thenLokiAppenderCollectLogs() throws JsonProcessingException, InterruptedException {
|
||||||
|
DemoService service = new DemoService();
|
||||||
|
service.log();
|
||||||
|
Thread.sleep(2000);
|
||||||
|
String baseUrl = "http://localhost:3100/loki/api/v1/query_range";
|
||||||
|
// Set up query parameters
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
String query = "{level=\"INFO\"} |= `DemoService.log invoked!!!`";
|
||||||
|
// Get current time in UTC
|
||||||
|
LocalDateTime currentDateTime = LocalDateTime.now(ZoneOffset.UTC);
|
||||||
|
String current_time_est = currentDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
|
||||||
|
|
||||||
|
LocalDateTime tenMinsAgo = currentDateTime.minusMinutes(10);
|
||||||
|
String start_time_est = tenMinsAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
|
||||||
|
System.out.println(start_time_est);
|
||||||
|
|
||||||
|
URI uri = UriComponentsBuilder.fromUriString(baseUrl)
|
||||||
|
.queryParam("query", query)
|
||||||
|
.queryParam("start", start_time_est)
|
||||||
|
.queryParam("end", current_time_est)
|
||||||
|
.build()
|
||||||
|
.toUri();
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(
|
||||||
|
uri,
|
||||||
|
HttpMethod.GET,
|
||||||
|
new HttpEntity<>(headers),
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
if (response.getStatusCode() == HttpStatus.OK) {
|
||||||
|
String responseBody = response.getBody();
|
||||||
|
JsonNode jsonNode = objectMapper.readTree(responseBody);
|
||||||
|
JsonNode result = jsonNode.get("data").get("result").get(0).get("values");
|
||||||
|
result.iterator().forEachRemaining(e-> {
|
||||||
|
Iterator<JsonNode> elements = e.elements();
|
||||||
|
elements.forEachRemaining(f ->
|
||||||
|
messages.add(f.toString())
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
System.out.println("Error: " + response.getStatusCodeValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
String expected = "DemoService.log invoked!!!";
|
||||||
|
assertTrue(messages.stream().anyMatch(e -> e.contains(expected)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user