-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : adds prometheus and tempo test cases (#1192)
* feat : grafana LGTM sample * adds workflow * Update boot-grafana-lgtm/ReadMe.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * feat : fetch grafana port * feat : adds prometheus and tempo tests * adds grafana-lgtm label * Update SUMMARY.md * fix : broken link --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2b8946b
commit 224d8f5
Showing
8 changed files
with
200 additions
and
102 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
60 changes: 29 additions & 31 deletions
60
boot-grafana-lgtm/src/main/java/com/learning/grafanalgtm/GrafanaLGTMApplication.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 |
---|---|---|
@@ -1,31 +1,29 @@ | ||
package com.learning.grafanalgtm; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import java.util.TimeZone; | ||
|
||
|
||
@SpringBootApplication | ||
public class GrafanaLGTMApplication { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(GrafanaLGTMApplication.class); | ||
|
||
public static void main(String[] args) { | ||
final Runtime r = Runtime.getRuntime(); | ||
|
||
log.info("[APP] Active processors: {}", r.availableProcessors()); | ||
log.info("[APP] Total memory: {}", r.totalMemory()); | ||
log.info("[APP] Free memory: {}", r.freeMemory()); | ||
log.info("[APP] Max memory: {}", r.maxMemory()); | ||
SpringApplication.run(GrafanaLGTMApplication.class, args); | ||
} | ||
|
||
@PostConstruct | ||
void started() { | ||
TimeZone.setDefault(TimeZone.getTimeZone("GMT")); | ||
} | ||
} | ||
package com.learning.grafanalgtm; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import java.util.TimeZone; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class GrafanaLGTMApplication { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(GrafanaLGTMApplication.class); | ||
|
||
public static void main(String[] args) { | ||
final Runtime r = Runtime.getRuntime(); | ||
|
||
log.info("[APP] Active processors: {}", r.availableProcessors()); | ||
log.info("[APP] Total memory: {}", r.totalMemory()); | ||
log.info("[APP] Free memory: {}", r.freeMemory()); | ||
log.info("[APP] Max memory: {}", r.maxMemory()); | ||
SpringApplication.run(GrafanaLGTMApplication.class, args); | ||
} | ||
|
||
@PostConstruct | ||
void started() { | ||
TimeZone.setDefault(TimeZone.getTimeZone("GMT")); | ||
} | ||
} |
37 changes: 20 additions & 17 deletions
37
boot-grafana-lgtm/src/main/java/com/learning/grafanalgtm/controller/GreetingsController.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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
package com.learning.grafanalgtm.controller; | ||
|
||
import io.micrometer.core.annotation.Counted; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class GreetingsController { | ||
|
||
@Counted | ||
@GetMapping("/greetings") | ||
public String greetings() { | ||
return "Hello, World!"; | ||
} | ||
|
||
} | ||
package com.learning.grafanalgtm.controller; | ||
|
||
import io.micrometer.core.annotation.Counted; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class GreetingsController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GreetingsController.class); | ||
|
||
@Counted | ||
@GetMapping("/greetings") | ||
public String greetings() { | ||
LOGGER.info("Inside Greetings method"); | ||
return "Hello, World!"; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
spring.application.name=boot-grafana-lgtm | ||
|
||
management.endpoints.web.exposure.include=env,health,info,logfile,loggers,metrics,prometheus | ||
management.tracing.sampling.probability=1.0 | ||
management.prometheus.metrics.export.step=1s | ||
management.metrics.distribution.percentiles-histogram.all=true | ||
management.metrics.distribution.percentiles-histogram.http.server.requests=true |
107 changes: 83 additions & 24 deletions
107
boot-grafana-lgtm/src/test/java/com/learning/grafanalgtm/GrafanaLGTMApplicationTest.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 |
---|---|---|
@@ -1,24 +1,83 @@ | ||
package com.learning.grafanalgtm; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
@SpringBootTest(classes = {TestGrafanaLGTMApplication.class}) | ||
@AutoConfigureMockMvc | ||
class GrafanaLGTMApplicationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private GenericContainer<?> lgtmContainer; | ||
|
||
@Test | ||
void prometheus(){ | ||
System.out.println("Configured grafana port "+ lgtmContainer.getMappedPort(3000)); | ||
} | ||
} | ||
package com.learning.grafanalgtm; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.authentication.PreemptiveBasicAuthScheme; | ||
import io.restassured.http.ContentType; | ||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
@SpringBootTest( | ||
classes = {TestGrafanaLGTMApplication.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class GrafanaLGTMApplicationTest { | ||
|
||
@Autowired | ||
private GenericContainer<?> lgtmContainer; | ||
|
||
@LocalServerPort | ||
private int localServerPort; | ||
|
||
@BeforeAll | ||
void setUp() { | ||
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme(); | ||
authScheme.setUserName("admin"); | ||
authScheme.setPassword("admin"); | ||
RestAssured.authentication = authScheme; | ||
} | ||
|
||
@BeforeEach | ||
void setPort() { | ||
RestAssured.port = localServerPort; | ||
} | ||
|
||
@Test | ||
void prometheus() { | ||
// calling endpoint to load metrics | ||
when().get("/greetings").then().statusCode(HttpStatus.SC_OK); | ||
|
||
RestAssured.port = lgtmContainer.getMappedPort(3000); | ||
given().contentType(ContentType.URLENC) | ||
.body("query=http_server_requests_seconds_count") | ||
.when() | ||
.post("/api/datasources/proxy/uid/prometheus/api/v1/query") | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.contentType(ContentType.JSON) | ||
.body("status", is("success")) | ||
.body("data.resultType", is("vector")) | ||
.body("data.result", empty()); | ||
} | ||
|
||
@Test | ||
void tempoQuery() { | ||
// calling endpoint to load metrics | ||
when().get("/greetings").then().statusCode(HttpStatus.SC_OK); | ||
|
||
RestAssured.port = lgtmContainer.getMappedPort(3000); | ||
|
||
given().contentType(ContentType.URLENC) | ||
.body("q={span.http.response.status_code=\"200\"}") | ||
.when() | ||
.post("/api/datasources/proxy/uid/tempo/api/search") | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.contentType(ContentType.JSON) | ||
.body("metrics.completedJobs", is(1)) | ||
.body("metrics.totalJobs", is(1)) | ||
.log() | ||
.all(); | ||
} | ||
} |
61 changes: 31 additions & 30 deletions
61
boot-grafana-lgtm/src/test/java/com/learning/grafanalgtm/TestGrafanaLGTMApplication.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 |
---|---|---|
@@ -1,30 +1,31 @@ | ||
package com.learning.grafanalgtm; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
|
||
import java.time.Duration; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestGrafanaLGTMApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(GrafanaLGTMApplication::main).with(TestGrafanaLGTMApplication.class).run(args); | ||
} | ||
|
||
@Bean | ||
@ServiceConnection("otel/opentelemetry-collector-contrib") | ||
GenericContainer<?> lgtmContainer() { | ||
return new GenericContainer<>("grafana/otel-lgtm:0.5.0") | ||
// grafana, otel grpc, otel http | ||
.withExposedPorts(3000, 4317, 4318) | ||
.withEnv("OTEL_METRIC_EXPORT_INTERVAL", "500") | ||
.waitingFor(Wait.forLogMessage(".*The OpenTelemetry collector and the Grafana LGTM stack are up and running.*\\s", 1)) | ||
.withStartupTimeout(Duration.ofMinutes(2)); | ||
} | ||
|
||
} | ||
package com.learning.grafanalgtm; | ||
|
||
import java.time.Duration; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestGrafanaLGTMApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(GrafanaLGTMApplication::main) | ||
.with(TestGrafanaLGTMApplication.class) | ||
.run(args); | ||
} | ||
|
||
@Bean | ||
@ServiceConnection("otel/opentelemetry-collector-contrib") | ||
GenericContainer<?> lgtmContainer() { | ||
return new GenericContainer<>("grafana/otel-lgtm:0.5.0") | ||
// grafana, otel grpc, otel http | ||
.withExposedPorts(3000, 4317, 4318) | ||
.withEnv("OTEL_METRIC_EXPORT_INTERVAL", "500") | ||
.waitingFor(Wait.forLogMessage( | ||
".*The OpenTelemetry collector and the Grafana LGTM stack are up and running.*\\s", 1)) | ||
.withStartupTimeout(Duration.ofMinutes(2)); | ||
} | ||
} |