Skip to content

Commit

Permalink
Minor changes: Cleanup some static imports and rename a method
Browse files Browse the repository at this point in the history
Signed-off-by: Greg Schohn <greg.schohn@gmail.com>
  • Loading branch information
gregschohn committed Jun 6, 2024
1 parent ef62644 commit f45ddcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
12 changes: 6 additions & 6 deletions RFS/src/main/java/com/rfs/common/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RestClient(ConnectionDetails connectionDetails) {

public Mono<Response> getAsync(String path, IRfsContexts.IRequestContext context) {
return client
.doOnRequest(sizeMetricsHandler(context))
.doOnRequest(addSizeMetricsHandlers(context))
.get()
.uri("/" + path)
.responseSingle((response, bytes) -> bytes.asString()
Expand All @@ -61,7 +61,7 @@ public Response get(String path, IRfsContexts.IRequestContext context) {

public Mono<Response> postAsync(String path, String body, IRfsContexts.IRequestContext context) {
return client
.doOnRequest(sizeMetricsHandler(context))
.doOnRequest(addSizeMetricsHandlers(context))
.post()
.uri("/" + path)
.send(ByteBufMono.fromString(Mono.just(body)))
Expand All @@ -73,7 +73,7 @@ public Mono<Response> postAsync(String path, String body, IRfsContexts.IRequestC

public Mono<Response> putAsync(String path, String body, IRfsContexts.IRequestContext context) {
return client
.doOnRequest(sizeMetricsHandler(context))
.doOnRequest(addSizeMetricsHandlers(context))
.put()
.uri("/" + path)
.send(ByteBufMono.fromString(Mono.just(body)))
Expand All @@ -87,10 +87,10 @@ public Response put(String path, String body, IRfsContexts.IRequestContext conte
return putAsync(path, body, context).block();
}

private BiConsumer<HttpClientRequest, Connection> sizeMetricsHandler(final IRfsContexts.IRequestContext context) {
private BiConsumer<HttpClientRequest, Connection> addSizeMetricsHandlers(final IRfsContexts.IRequestContext ctx) {
return (r, conn) -> {
conn.channel().pipeline().addFirst(new WriteMeteringHandler(context::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(context::addBytesRead));
conn.channel().pipeline().addFirst(new WriteMeteringHandler(ctx::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(ctx::addBytesRead));
};
}
}
25 changes: 10 additions & 15 deletions RFS/src/test/java/com/rfs/common/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
import com.rfs.tracing.RfsContexts;
import com.rfs.tracing.TestContext;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.sdk.trace.data.SpanData;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.opensearch.migrations.testutils.HttpRequestFirstLine;
import org.opensearch.migrations.testutils.SimpleHttpResponse;
import org.opensearch.migrations.testutils.SimpleNettyHttpServer;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static java.util.stream.Collectors.toList;

class RestClientTest {
@Test
public void testGetEmitsInstrumentation() throws Exception{
Expand Down Expand Up @@ -57,24 +49,27 @@ public void testGetEmitsInstrumentation() throws Exception{
.filter(pd -> pd.getAttributes().asMap().values().stream().map(o -> (String) o).collect(Collectors.joining())
.equals(kvp.getKey()))
.reduce((a, b) -> b).get().getValue();
assertThat("Checking bytes {send, read} for context '" + kvp.getKey() + "'", new long[]{bytesSent, bytesRead}, equalTo(kvp.getValue()));
MatcherAssert.assertThat("Checking bytes {send, read} for context '" + kvp.getKey() + "'",
new long[]{bytesSent, bytesRead}, Matchers.equalTo(kvp.getValue()));
}

final var finishedSpans = rootContext.instrumentationBundle.getFinishedSpans();
final var finishedSpanNames = finishedSpans.stream().map(SpanData::getName).collect(toList());
assertThat(finishedSpanNames, containsInAnyOrder("httpRequest", "httpRequest", "createSnapshot"));
final var finishedSpanNames = finishedSpans.stream().map(SpanData::getName).collect(Collectors.toList());
MatcherAssert.assertThat(finishedSpanNames,
Matchers.containsInAnyOrder("httpRequest", "httpRequest", "createSnapshot"));

final var httpRequestSpansByTime = finishedSpans.stream()
.filter(sd -> sd.getName().equals("httpRequest"))
.sorted(Comparator.comparing(SpanData::getEndEpochNanos)).collect(toList());
.sorted(Comparator.comparing(SpanData::getEndEpochNanos)).collect(Collectors.toList());
int i = 0;
for (var expectedBytes : List.of(
new long[]{139,66},
new long[]{133,66})) {
var span = httpRequestSpansByTime.get(i++);
long bytesSent = span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_SENT_ATTR);
long bytesRead = span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_READ_ATTR);
assertThat("Checking bytes {send, read} for httpRequest " + i, new long[]{bytesSent, bytesRead}, equalTo(expectedBytes));
MatcherAssert.assertThat("Checking bytes {send, read} for httpRequest " + i,
new long[]{bytesSent, bytesRead}, Matchers.equalTo(expectedBytes));
}
}

Expand Down

0 comments on commit f45ddcb

Please sign in to comment.