Skip to content

Commit

Permalink
Move tracing into the http client factory
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 10, 2018
1 parent b66fe3e commit c7c9ecb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public Response execute(Command command) throws IOException {
try (Span span = tracer.createSpan(command.getName(), tracer.getActiveSpan())) {
log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
span.addTag("selenium-sessionid", String.valueOf(command.getSessionId()));
HttpTracing.inject(span, httpRequest);
HttpResponse httpResponse = client.execute(httpRequest);
log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.DistributedTracer;
import org.openqa.selenium.remote.tracing.HttpTracing;
import org.openqa.selenium.remote.tracing.Span;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -100,9 +99,8 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession

HttpResponse response;
long start = System.currentTimeMillis();
try (Span span = DistributedTracer.getInstance().getActiveSpan()) {
HttpTracing.inject(span, request);

DistributedTracer tracer = DistributedTracer.getInstance();
try (Span span = tracer.createSpan("NEW_SESSION", tracer.getActiveSpan())) {
request.setHeader(CONTENT_LENGTH, String.valueOf(size));
request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
request.setContent(newSessionBlob);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.HttpTracing;

import okhttp3.ConnectionPool;
import okhttp3.Credentials;
Expand Down Expand Up @@ -155,7 +156,7 @@ public HttpClient createClient(URL url) {
: response;
});

return new OkHttpClient(client.build(), url);
return HttpTracing.decorate(new OkHttpClient(client.build(), url));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package org.openqa.selenium.remote.tracing;

import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import io.opentracing.tag.Tags;

import java.util.Objects;

Expand All @@ -33,6 +37,8 @@ public static void inject(Span span, HttpRequest request) {
return;
}

span.addTag(Tags.HTTP_METHOD.getKey(), request.getMethod().toString());
span.addTag(Tags.HTTP_URL.getKey(), request.getUri());
span.inject(request);
}

Expand All @@ -45,4 +51,25 @@ public static void extract(HttpRequest request, Span intoSpan) {
intoSpan.extract(request);
}

public static HttpClient decorate(HttpClient existing) {

return request -> {
Span span = DistributedTracer.getInstance().getActiveSpan();
inject(span, request);

try {
HttpResponse response = existing.execute(request);

if (span != null) {
span.addTag(Tags.HTTP_STATUS.getKey(), response.getStatus());
}

return response;
} catch (Throwable throwable) {
throw throwable;
}
};

}

}

0 comments on commit c7c9ecb

Please sign in to comment.