-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat : grafana LGTM sample #1189
Conversation
Warning Rate Limit Exceeded@rajadilipkolli has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 26 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. WalkthroughThe Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
@SpringBootTest(classes = {TestGrafanaLGTMApplication.class}) | ||
@AutoConfigureMockMvc | ||
class GrafanaLGTMApplicationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
void prometheus(){ | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test configuration is set up correctly. However, the prometheus
test method is empty and needs to be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai , help me to write integration test which calls below curl using spring boots mockmvc
curl --user "admin:admin" \
--data-urlencode 'query=http_server_request_duration_seconds_count' \
http://localhost:3000/api/datasources/proxy/uid/prometheus/api/v1/query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, here's how you can write the integration test using Spring Boot's MockMvc to simulate the curl command you provided. This test will perform a GET request to the specified Grafana endpoint, including basic authentication and the required query parameter:
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SpringBootTest(classes = {TestGrafanaLGTMApplication.class})
@AutoConfigureMockMvc
class GrafanaLGTMApplicationTest {
@Autowired
private MockMvc mockMvc;
@Test
void prometheus() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/datasources/proxy/uid/prometheus/api/v1/query")
.with(httpBasic("admin", "admin"))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("query", "http_server_request_duration_seconds_count"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.result").exists());
}
}
This test checks that the endpoint is reachable, returns a 200 OK status, and the response is JSON containing a data.result
field. Adjust the assertions as needed based on the actual expected response structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, expand above example to clearly have imports or method of httpBasic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, I've expanded the example to include the necessary imports and a brief explanation of the httpBasic
method used for authentication in the test. Here's the updated test code:
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.http.MediaType;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SpringBootTest(classes = {TestGrafanaLGTMApplication.class})
@AutoConfigureMockMvc
class GrafanaLGTMApplicationTest {
@Autowired
private MockMvc mockMvc;
@Test
void prometheus() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/datasources/proxy/uid/prometheus/api/v1/query")
.with(SecurityMockMvcRequestPostProcessors.httpBasic("admin", "admin"))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("query", "http_server_request_duration_seconds_count"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.result").exists());
}
}
The httpBasic
method is part of the SecurityMockMvcRequestPostProcessors
class, which is used here to add basic authentication to the request. This method takes two parameters: the username and the password. In this test, it's used to authenticate as "admin" with the password "admin". This setup is necessary to test secured endpoints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, here's how you can rewrite the test using RestAssured and the necessary dependencies to add in your POM.xml.
Updated Test Code with RestAssured
package com.learning.grafanalgtm;
import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
class GrafanaLGTMApplicationTest {
@BeforeAll
static void setup() {
RestAssured.baseURI = "http://localhost:3000";
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("admin");
authScheme.setPassword("admin");
RestAssured.authentication = authScheme;
}
@Test
void prometheus() {
given()
.contentType("application/x-www-form-urlencoded")
.body("query=http_server_request_duration_seconds_count")
.when()
.post("/api/datasources/proxy/uid/prometheus/api/v1/query")
.then()
.statusCode(200)
.contentType("application/json")
.body("data.result", not(empty()));
}
}
Required Dependencies for POM.xml
Add the following dependencies to your pom.xml
to use RestAssured in your project:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
This setup configures RestAssured with basic authentication and performs a POST request similar to your curl command. The test checks for a 200 status code, correct content type, and non-empty result data. Adjust the base URI and other parameters as necessary for your environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, it has worked, thank you. Can you please change to below POST CURL
curl --user "admin:admin"
--data-urlencode 'q={span.http.response.status_code="200"}'
http://localhost:3000/api/datasources/proxy/uid/tempo/api/search
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, I've updated the RestAssured test to match the new POST curl command you provided. Here's the revised test code:
package com.learning.grafanalgtm;
import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
class GrafanaLGTMApplicationTest {
@BeforeAll
static void setup() {
RestAssured.baseURI = "http://localhost:3000";
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("admin");
authScheme.setPassword("admin");
RestAssured.authentication = authScheme;
}
@Test
void tempoQuery() {
given()
.contentType("application/x-www-form-urlencoded")
.body("q={span.http.response.status_code=\"200\"}")
.when()
.post("/api/datasources/proxy/uid/tempo/api/search")
.then()
.statusCode(200)
.contentType("application/json")
.body("data.result", not(empty()));
}
}
This test now targets the /api/datasources/proxy/uid/tempo/api/search
endpoint and sends a POST request with the query q={span.http.response.status_code="200"}
. It checks for a 200 status code, correct content type, and non-empty result data in the response. Adjust the base URI and other parameters as necessary for your environment.
If you need further adjustments or have any more requests, feel free to let me know!
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, I have improve readablity by tweaking above code as
.statusCode(HttpStatus.SC_OK)
.contentType(ContentType.JSON)
where imports are from
import io.restassured.http.ContentType;
import org.apache.http.HttpStatus;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, the improvements you've made to the test code enhance its readability and maintainability significantly. Using HttpStatus.SC_OK
and ContentType.JSON
not only makes the code cleaner but also less prone to errors associated with raw values. Great choice!
If there's anything more you need help with or any other adjustments you'd like to discuss, feel free to let me know!
🐰
(\__/)
(•ㅅ•) CodeRabbit is happy to see your code becoming cleaner and more robust!
/ づ
New Learnings Added
User: rajadilipkolli"
URL: https://github.com/rajadilipkolli/my-spring-boot-experiments/pull/1189
Timestamp: 2024-04-13T07:17:05.492Z
Learning: User prefers using constants like `HttpStatus.SC_OK` and `ContentType.JSON` for status codes and content types in tests to improve readability and maintainability.
Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Quality Gate passedIssues Measures |
Summary by CodeRabbit
New Features
/greetings
endpoint.Documentation
Configuration
Tests