Skip to content

Commit

Permalink
Adding spotless plugin, CI workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
benrady-aq committed Dec 10, 2024
1 parent a731945 commit 4f3c727
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 19 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Build

on: [push]

jobs:
test:
runs-on: ubuntu-22.10
steps:
- uses: actions/checkout@v1
- name: build
run: make publish
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spotless:

.PHONY: test
test: ## Run CI Checks
./gradlew cleanTest --warning-mode all test --continue
./gradlew cleanTest --warning-mode all test --continue spotlessCheck

.PHONY: watch
watch: ## Run tests continuously
Expand All @@ -29,4 +29,7 @@ todo: # Print a list of relevant TODOs in the code

.PHONY: reformat
reformat: ## Reformat all the code (as per pre-commit)
./gradlew spotlessApply
./gradlew spotlessApply

publish: test
# ./gradlew publish
21 changes: 21 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent
plugins {
id 'java'
id 'com.google.protobuf' version '0.9.4'
id 'com.diffplug.spotless' version "6.22.0"
}

group = 'com.aquatic'
Expand All @@ -13,6 +14,13 @@ repositories {
mavenCentral()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
withSourcesJar()
}

dependencies {
implementation 'com.google.protobuf:protobuf-java:3.25.5'
implementation 'org.xerial.snappy:snappy-java:1.1.10.7'
Expand All @@ -39,6 +47,19 @@ test {
}
}

spotless {
java {
palantirJavaFormat("2.35.0")
importOrder(
'com.aquatic',
'',
'java',
'\\#'
)
removeUnusedImports()
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.24.3'
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/aquatic/amp/exporter/AMPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public AMPClient(Region region, String workspaceId, AwsCredentialsProvider crede
this(region, workspaceId, credentialsProvider, HttpClient.newHttpClient()::send);
}

AMPClient(Region region, String workspaceId, AwsCredentialsProvider credentialsProvider, SimpleHttpClient<String> client) {
AMPClient(
Region region,
String workspaceId,
AwsCredentialsProvider credentialsProvider,
SimpleHttpClient<String> client) {
this.client = client;
workspaceUrl = "https://aps-workspaces.%s.amazonaws.com/workspaces/%s".formatted(region, workspaceId);
signer = new RequestSigner(credentialsProvider, Clock.system(UTC), Region.US_EAST_1);
Expand All @@ -65,15 +69,16 @@ public HttpResponse<String> writeSamples(Prometheus.WriteRequest req) throws IOE
return client.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
}

public HttpResponse<String> httpRequest(String path, Map<String, String> parameters) throws IOException, InterruptedException {
public HttpResponse<String> httpRequest(String path, Map<String, String> parameters)
throws IOException, InterruptedException {
URI endpoint = getParameterizedUri(path, parameters);
HttpRequest.Builder requestBuilder = createRequestBuilder(endpoint, signer.signGetRequest(endpoint));
return client.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
}

private static HttpRequest.Builder createRequestBuilder(URI endpoint, SdkHttpFullRequest signedRequest) {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder().uri(endpoint);
for(var header : signedRequest.headers().entrySet()) {
for (var header : signedRequest.headers().entrySet()) {
if (!header.getKey().equalsIgnoreCase("Host")) {
requestBuilder.header(header.getKey(), header.getValue().getFirst());
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/com/aquatic/amp/exporter/WriteReadExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ public static void main(String[] args) throws Exception {
long now = System.currentTimeMillis();
long value = now % 100;
System.out.println("Writing sample: " + i + "=" + value + " at timestamp " + now);
Prometheus.Sample sample = Prometheus.Sample.newBuilder().setTimestamp(now).setValue(value).build();
Prometheus.Label label = Prometheus.Label.newBuilder().setName("__name__").setValue(sampleName).build();
Prometheus.TimeSeries timeSeries = Prometheus.TimeSeries.newBuilder().addLabels(label).addSamples(sample).build();
var response = client.writeSamples(Prometheus.WriteRequest.newBuilder().addTimeseries(timeSeries).build());
Prometheus.Sample sample = Prometheus.Sample.newBuilder()
.setTimestamp(now)
.setValue(value)
.build();
Prometheus.Label label = Prometheus.Label.newBuilder()
.setName("__name__")
.setValue(sampleName)
.build();
Prometheus.TimeSeries timeSeries = Prometheus.TimeSeries.newBuilder()
.addLabels(label)
.addSamples(sample)
.build();
var response = client.writeSamples(Prometheus.WriteRequest.newBuilder()
.addTimeseries(timeSeries)
.build());
if (response.statusCode() != 200) {
throw new RuntimeException("Failed to write sample: " + i + "=" + response.body());
}
Thread.sleep(5000);
}
var end = Instant.now().toString();
var results = client.httpRequest("/api/v1/query_range", Map.of("query", sampleName, "start", start, "end", end, "step", "15s"));
var results = client.httpRequest(
"/api/v1/query_range", Map.of("query", sampleName, "start", start, "end", end, "step", "15s"));
if (results.statusCode() == 200) {
System.out.println(results.body());
} else {
Expand Down
23 changes: 16 additions & 7 deletions src/test/java/com/aquatic/amp/exporter/AMPClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

package com.aquatic.amp.exporter;


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xerial.snappy.Snappy;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;

import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
Expand All @@ -37,6 +35,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import javax.net.ssl.SSLSession;

import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -47,24 +46,34 @@ public class AMPClientTest {
@BeforeEach
public void beforeEach() {
SimpleHttpClient<String> httpClient = StubResponse::create;
client = new AMPClient(Region.US_EAST_1, "abc123", () -> AwsBasicCredentials.create("akid", "skid"), httpClient);
client =
new AMPClient(Region.US_EAST_1, "abc123", () -> AwsBasicCredentials.create("akid", "skid"), httpClient);
}

@Test
public void canWriteSamples() throws IOException, InterruptedException {
var writeRequest = Prometheus.WriteRequest.newBuilder().addTimeseries(Prometheus.TimeSeries.newBuilder().build()).build();
var writeRequest = Prometheus.WriteRequest.newBuilder()
.addTimeseries(Prometheus.TimeSeries.newBuilder().build())
.build();
HttpResponse<String> response = client.writeSamples(writeRequest);
HttpRequest request = response.request();
assertEquals(URI.create("https://aps-workspaces.us-east-1.amazonaws.com/workspaces/abc123/api/v1/remote_write"), request.uri());
assertEquals(Snappy.compress(writeRequest.toByteArray()).length, request.bodyPublisher().orElseThrow().contentLength());
assertEquals(
URI.create("https://aps-workspaces.us-east-1.amazonaws.com/workspaces/abc123/api/v1/remote_write"),
request.uri());
assertEquals(
Snappy.compress(writeRequest.toByteArray()).length,
request.bodyPublisher().orElseThrow().contentLength());
assertEquals(emptyList(), request.headers().allValues("Host"));
}

@Test
public void canGetDataViaHttpApi() throws IOException, InterruptedException {
var response = client.httpRequest("/api/v1/query", Map.of("query", "myLabel"));
HttpRequest request = response.request();
assertEquals(URI.create("https://aps-workspaces.us-east-1.amazonaws.com/workspaces/abc123/api/v1/query?query=myLabel"), request.uri());
assertEquals(
URI.create(
"https://aps-workspaces.us-east-1.amazonaws.com/workspaces/abc123/api/v1/query?query=myLabel"),
request.uri());
assertEquals(emptyList(), request.headers().allValues("Host"));
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/aquatic/amp/exporter/RequestSignerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class RequestSignerTest {
@BeforeEach
public void beforeEach() {
AwsCredentialsProvider credentialsProvider = () -> AwsBasicCredentials.create("akid", "skid");
signer = new RequestSigner(credentialsProvider, Clock.fixed(Instant.ofEpochMilli(1234567890123L), UTC), Region.US_EAST_1);
signer = new RequestSigner(
credentialsProvider, Clock.fixed(Instant.ofEpochMilli(1234567890123L), UTC), Region.US_EAST_1);
}

@Test
Expand Down Expand Up @@ -56,4 +57,4 @@ public void canSignPostRequests() {
assertEquals("SignedHeaders=content-encoding;content-type;host;x-amz-date,", authHeaderSections[2]);
assertThat(authHeaderSections[3], containsString("Signature="));
}
}
}

0 comments on commit 4f3c727

Please sign in to comment.