From caa7e4426a7a15d6133b01c24f06a55e1be4d7b5 Mon Sep 17 00:00:00 2001 From: Laplie Anderson Date: Tue, 9 Jul 2019 15:08:08 -0400 Subject: [PATCH] Passing tests. Modify escaping of spaces in urls --- .../GoogleHttpClientDecorator.java | 6 +- .../GoogleHttpClientInstrumentation.java | 9 +-- .../test/groovy/GoogleHttpClientTest.groovy | 67 +++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 dd-java-agent/instrumentation/google-http-client/src/test/groovy/GoogleHttpClientTest.groovy diff --git a/dd-java-agent/instrumentation/google-http-client/src/main/java/datadog/trace/instrumentation/googlehttpclient/GoogleHttpClientDecorator.java b/dd-java-agent/instrumentation/google-http-client/src/main/java/datadog/trace/instrumentation/googlehttpclient/GoogleHttpClientDecorator.java index bebdddcf8c..4a6d03c1e5 100644 --- a/dd-java-agent/instrumentation/google-http-client/src/main/java/datadog/trace/instrumentation/googlehttpclient/GoogleHttpClientDecorator.java +++ b/dd-java-agent/instrumentation/google-http-client/src/main/java/datadog/trace/instrumentation/googlehttpclient/GoogleHttpClientDecorator.java @@ -16,7 +16,11 @@ public class GoogleHttpClientDecorator extends HttpClientDecorator { + + @Shared + def requestFactory = new NetHttpTransport().createRequestFactory(); + + @Override + int doRequest(String method, URI uri, Map headers, Closure callback) { + doRequest(method, uri, headers, callback, false); + } + + int doRequest(String method, URI uri, Map headers, Closure callback, boolean throwExceptionOnError) { + GenericUrl genericUrl = new GenericUrl(uri) + + HttpRequest request = requestFactory.buildRequest(method, genericUrl, null) + request.getHeaders().putAll(headers) + request.setThrowExceptionOnExecuteError(throwExceptionOnError) + + HttpResponse response = request.execute() + callback?.call() + + return response.getStatusCode() + } + + @Override + GoogleHttpClientDecorator decorator() { + return GoogleHttpClientDecorator.DECORATE + } + + @Override + boolean testRedirects() { + // Circular redirects don't throw an exception with Google Http Client + return false + } + + def "error traces when exception is not thrown"() { + given: + def uri = server.address.resolve("/error") + + when: + def status = doRequest(method, uri) + + then: + status == 500 + assertTraces(2) { + server.distributedRequestTrace(it, 0, trace(1).last()) + trace(1, size(1)) { + span(0) { + resourceName "$method $uri.path" + spanType DDSpanTypes.HTTP_CLIENT + errored true + } + } + } + + where: + method = "GET" + } +}