Skip to content

Commit

Permalink
Support reactor netty HttpClient.from construction (#2650)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Mar 29, 2021
1 parent dcd316d commit 202e038
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;

import com.google.auto.service.AutoService;
import io.netty.bootstrap.Bootstrap;
Expand Down Expand Up @@ -64,7 +65,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isStatic().and(named("create").or(named("newConnection"))),
isStatic().and(namedOneOf("create", "newConnection", "from")),
ReactorNettyInstrumentationModule.class.getName() + "$CreateAdvice");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.base.HttpClientTest
import reactor.netty.http.client.HttpClient
import reactor.netty.http.client.HttpClientResponse

abstract class AbstractReactorNettyHttpClientTest extends HttpClientTest implements AgentTestTrait {

@Override
boolean testRedirects() {
false
}

@Override
boolean testConnectionFailure() {
false
}

@Override
boolean testRemoteConnection() {
false
}

@Override
String userAgent() {
return "ReactorNetty"
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers = [:], Closure callback = null) {
HttpClientResponse resp = createHttpClient()
.followRedirect(true)
.headers({ h -> headers.each { k, v -> h.add(k, v) } })
.baseUrl(server.address.toString())
."${method.toLowerCase()}"()
.uri(uri.toString())
.response()
.block()
if (callback != null) {
callback.call()
}
return resp.status().code()
}

abstract HttpClient createHttpClient()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.base.HttpClientTest
import io.opentelemetry.instrumentation.test.base.SingleConnection
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeoutException
import reactor.netty.http.client.HttpClient
import reactor.netty.http.client.HttpClientResponse

class ReactorNettyHttpClientTest extends HttpClientTest implements AgentTestTrait {
class ReactorNettyHttpClientTest extends AbstractReactorNettyHttpClientTest {

@Override
boolean testRedirects() {
false
}

@Override
boolean testConnectionFailure() {
false
}

@Override
boolean testRemoteConnection() {
false
}

@Override
String userAgent() {
return "ReactorNetty"
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers = [:], Closure callback = null) {
HttpClientResponse resp = HttpClient.create()
.followRedirect(true)
.headers({ h -> headers.each { k, v -> h.add(k, v) } })
.baseUrl(server.address.toString())
."${method.toLowerCase()}"()
.uri(uri.toString())
.response()
.block()
if (callback != null) {
callback.call()
}
return resp.status().code()
HttpClient createHttpClient() {
return HttpClient.create()
}

@Override
Expand Down Expand Up @@ -76,5 +41,4 @@ class ReactorNettyHttpClientTest extends HttpClientTest implements AgentTestTrai
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import reactor.netty.http.client.HttpClient
import reactor.netty.tcp.TcpClient

class ReactorNettyHttpClientUsingFromTest extends AbstractReactorNettyHttpClientTest {

HttpClient createHttpClient() {
return HttpClient.from(TcpClient.create())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;

import com.google.auto.service.AutoService;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -63,7 +64,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isStatic().and(named("create").or(named("newConnection"))),
isStatic().and(namedOneOf("create", "newConnection", "from")),
ReactorNettyInstrumentationModule.class.getName() + "$CreateAdvice");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.base.HttpClientTest
import reactor.netty.http.client.HttpClient
import reactor.netty.http.client.HttpClientResponse

abstract class AbstractReactorNettyHttpClientTest extends HttpClientTest implements AgentTestTrait {

@Override
boolean testRedirects() {
false
}

@Override
boolean testConnectionFailure() {
false
}

@Override
boolean testRemoteConnection() {
false
}

@Override
String userAgent() {
return "ReactorNetty"
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers = [:], Closure callback = null) {
HttpClientResponse resp = createHttpClient()
.followRedirect(true)
.headers({ h -> headers.each { k, v -> h.add(k, v) } })
.baseUrl(server.address.toString())
."${method.toLowerCase()}"()
.uri(uri.toString())
.response()
.block()
if (callback != null) {
callback.call()
}
return resp.status().code()
}

abstract HttpClient createHttpClient()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.base.HttpClientTest
import io.opentelemetry.instrumentation.test.base.SingleConnection
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeoutException
import reactor.netty.http.client.HttpClient
import reactor.netty.http.client.HttpClientResponse

class ReactorNettyHttpClientTest extends HttpClientTest implements AgentTestTrait {
class ReactorNettyHttpClientTest extends AbstractReactorNettyHttpClientTest {

@Override
boolean testRedirects() {
false
}

@Override
boolean testConnectionFailure() {
false
}

@Override
boolean testRemoteConnection() {
false
}

@Override
String userAgent() {
return "ReactorNetty"
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers = [:], Closure callback = null) {
HttpClientResponse resp = HttpClient.create()
.followRedirect(true)
.headers({ h -> headers.each { k, v -> h.add(k, v) } })
.baseUrl(server.address.toString())
."${method.toLowerCase()}"()
.uri(uri.toString())
.response()
.block()
if (callback != null) {
callback.call()
}
return resp.status().code()
HttpClient createHttpClient() {
return HttpClient.create()
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import reactor.netty.http.client.HttpClient
import reactor.netty.tcp.TcpClient

class ReactorNettyHttpClientUsingFromTest extends AbstractReactorNettyHttpClientTest {

HttpClient createHttpClient() {
return HttpClient.from(TcpClient.create())
}
}

0 comments on commit 202e038

Please sign in to comment.