Skip to content

Commit

Permalink
Add distributed tracing to the router and sessionmap
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 26, 2018
1 parent e0d6e84 commit 75ac1bf
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 52 deletions.
4 changes: 2 additions & 2 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public Executable configure(String... args) {
DistributedTracer tracer = new LoggingOptions(config).getTracer();
GlobalDistributedTracer.setInstance(tracer);

SessionMap sessions = new LocalSessionMap();
SessionMap sessions = new LocalSessionMap(tracer);
Distributor distributor = new LocalDistributor(tracer);
Router router = new Router(sessions, distributor);
Router router = new Router(tracer, sessions, distributor);

Server<?> server = new BaseServer<>(
new BaseServerOptions(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public Executable configure(String... args) {
DistributedTracer tracer = new LoggingOptions(config).getTracer();
GlobalDistributedTracer.setInstance(tracer);

SessionMap sessions = new LocalSessionMap();
SessionMap sessions = new LocalSessionMap(tracer);
Distributor distributor = new LocalDistributor(tracer);
Router router = new Router(sessions, distributor);
Router router = new Router(tracer, sessions, distributor);

String hostName;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java_library(
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/client/src/org/openqa/selenium/remote/tracing",
"//java/server/src/org/openqa/selenium/grid/distributor",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/injector",
Expand Down
35 changes: 25 additions & 10 deletions java/server/src/org/openqa/selenium/grid/router/HandleSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.openqa.selenium.remote.SessionId;
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.IOException;
import java.time.Duration;
Expand All @@ -37,8 +40,10 @@
class HandleSession implements CommandHandler {

private final LoadingCache<SessionId, CommandHandler> knownSessions;
private final DistributedTracer tracer;

public HandleSession(SessionMap sessions) {
public HandleSession(DistributedTracer tracer, SessionMap sessions) {
this.tracer = Objects.requireNonNull(tracer);
Objects.requireNonNull(sessions);

this.knownSessions = CacheBuilder.newBuilder()
Expand All @@ -57,17 +62,27 @@ public CommandHandler load(SessionId id) throws Exception {

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
String[] split = req.getUri().split("/", 4);
SessionId id = new SessionId(split[2]);
try (Span span = tracer.createSpan("router.webdriver-command", tracer.getActiveSpan())) {
span.addTag("http.method", req.getMethod());
span.addTag("http.url", req.getUri());

try {
knownSessions.get(id).execute(req, resp);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
String[] split = req.getUri().split("/", 4);
SessionId id = new SessionId(split[2]);

span.addTag("session.id", id);

try {
knownSessions.get(id).execute(req, resp);
span.addTag("http.status", resp.getStatus());
} catch (ExecutionException e) {
span.addTag("exception", e.getMessage());

Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
}
throw new RuntimeException(cause);
}
}
}
4 changes: 3 additions & 1 deletion java/server/src/org/openqa/selenium/grid/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.DistributedTracer;

import java.io.IOException;
import java.util.Optional;
Expand All @@ -42,8 +43,9 @@ public class Router implements Predicate<HttpRequest>, CommandHandler {
private final Injector injector;
private final Routes routes;

public Router(SessionMap sessions, Distributor distributor) {
public Router(DistributedTracer tracer, SessionMap sessions, Distributor distributor) {
injector = Injector.builder()
.register(tracer)
.register(sessions)
.register(distributor)
.register(new Json())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Executable configure(String... args) {
tracer,
HttpClient.Factory.createDefault().createClient(distributorUrl));

Router router = new Router(sessions, distributor);
Router router = new Router(tracer, sessions, distributor);

Server<?> server = new BaseServer<>(serverOptions);
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Executable configure(String... args) {
DistributedTracer tracer = new LoggingOptions(config).getTracer();
GlobalDistributedTracer.setInstance(tracer);

SessionMap sessions = new LocalSessionMap();
SessionMap sessions = new LocalSessionMap(tracer);

BaseServerOptions serverOptions = new BaseServerOptions(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/client/src/org/openqa/selenium/remote/tracing:tracing",
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
"//java/server/src/org/openqa/selenium/grid/data:data",
"//java/server/src/org/openqa/selenium/grid/web:web",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java_library(
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/client/src/org/openqa/selenium/remote/tracing",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.tracing.DistributedTracer;
import org.openqa.selenium.remote.tracing.Span;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -31,51 +33,74 @@

public class LocalSessionMap extends SessionMap {

private Map<SessionId, Session> knownSessions = new HashMap<>();
private ReadWriteLock lock = new ReentrantReadWriteLock(/* be fair */ true);
private final DistributedTracer tracer;
private final Map<SessionId, Session> knownSessions = new HashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock(/* be fair */ true);

public LocalSessionMap(DistributedTracer tracer) {
this.tracer = Objects.requireNonNull(tracer);
}

@Override
public boolean add(Session session) {
Objects.requireNonNull(session, "Session has not been set");

Lock writeLock = lock.writeLock();
writeLock.lock();
try {
knownSessions.put(session.getId(), session);
} finally {
writeLock.unlock();
}
try (Span span = tracer.createSpan("sessionmap.add", tracer.getActiveSpan())) {
span.addTag("session.id", session.getId());
span.addTag("session.capabilities", session.getCapabilities());
span.addTag("session.uri", session.getUri());

return true;
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
knownSessions.put(session.getId(), session);
} finally {
writeLock.unlock();
}

return true;
}
}

@Override
public Session get(SessionId id) {
Objects.requireNonNull(id, "Session ID has not been set");

Lock readLock = lock.readLock();
readLock.lock();
try {
Session session = knownSessions.get(id);
if (session == null) {
throw new NoSuchSessionException("Unable to find session with ID: " + id);
try (Span span = tracer.createSpan("sessionmap.get", tracer.getActiveSpan())) {
span.addTag("session.id", id);

Lock readLock = lock.readLock();
readLock.lock();
try {
Session session = knownSessions.get(id);
if (session == null) {
throw new NoSuchSessionException("Unable to find session with ID: " + id);
}

span.addTag("session.capabilities", session.getCapabilities());
span.addTag("session.uri", session.getUri());

return session;
} finally {
readLock.unlock();
}
return session;
} finally {
readLock.unlock();
}
}

@Override
public void remove(SessionId id) {
Objects.requireNonNull(id, "Session ID has not been set");

Lock writeLock = lock.writeLock();
writeLock.lock();
try {
knownSessions.remove(id);
} finally {
writeLock.unlock();
try (Span span = tracer.createSpan("sessionmap.remove", tracer.getActiveSpan())) {
span.addTag("session.id", id);

Lock writeLock = lock.writeLock();
writeLock.lock();
try {
knownSessions.remove(id);
} finally {
writeLock.unlock();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void shouldBeAbleToAddANodeAndCreateASession() throws URISyntaxException
URI nodeUri = new URI("http://example:5678");
URI routableUri = new URI("http://localhost:1234");

LocalSessionMap sessions = new LocalSessionMap();
LocalSessionMap sessions = new LocalSessionMap(tracer);
LocalNode node = LocalNode.builder(tracer, routableUri, sessions)
.add(caps, c -> new Session(new SessionId(UUID.randomUUID()), nodeUri, c))
.build();
Expand All @@ -91,7 +91,7 @@ public void shouldBeAbleToRemoveANode() throws URISyntaxException {
URI nodeUri = new URI("http://example:5678");
URI routableUri = new URI("http://localhost:1234");

LocalSessionMap sessions = new LocalSessionMap();
LocalSessionMap sessions = new LocalSessionMap(tracer);
LocalNode node = LocalNode.builder(tracer, routableUri, sessions)
.add(caps, c -> new Session(new SessionId(UUID.randomUUID()), nodeUri, c))
.build();
Expand All @@ -113,7 +113,7 @@ public void registeringTheSameNodeMultipleTimesOnlyCountsTheFirstTime()
URI nodeUri = new URI("http://example:5678");
URI routableUri = new URI("http://localhost:1234");

LocalSessionMap sessions = new LocalSessionMap();
LocalSessionMap sessions = new LocalSessionMap(tracer);
LocalNode node = LocalNode.builder(tracer, routableUri, sessions)
.add(caps, c -> new Session(new SessionId(UUID.randomUUID()), nodeUri, c))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setUp() throws URISyntaxException {

uri = new URI("http://localhost:1234");

sessions = new LocalSessionMap();
sessions = new LocalSessionMap(tracer);

class Handler extends Session implements CommandHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public class EndToEndTest {
@Test
public void inMemory() throws URISyntaxException {

SessionMap sessions = new LocalSessionMap();
SessionMap sessions = new LocalSessionMap(tracer);
Distributor distributor = new LocalDistributor(tracer);
URI nodeUri = new URI("http://localhost:4444");
LocalNode node = LocalNode.builder(tracer, nodeUri, sessions)
.add(driverCaps, createFactory(nodeUri))
.build();
distributor.add(node);

Router router = new Router(sessions, distributor);
Router router = new Router(tracer, sessions, distributor);

Server<?> server = createServer();
server.addRoute(Routes.matching(router).using(router));
Expand All @@ -86,7 +86,7 @@ private void exerciseDriver(Server<?> server) {
@Test
public void withServers() throws URISyntaxException {

LocalSessionMap localSessions = new LocalSessionMap();
LocalSessionMap localSessions = new LocalSessionMap(tracer);
Server<?> sessionServer = createServer();
sessionServer.addRoute(Routes.matching(localSessions).using(localSessions));
sessionServer.start();
Expand All @@ -113,7 +113,7 @@ public void withServers() throws URISyntaxException {

distributor.add(localNode);

Router router = new Router(sessions, distributor);
Router router = new Router(tracer, sessions, distributor);
Server<?> routerServer = createServer();
routerServer.addRoute(Routes.matching(router).using(router));
routerServer.start();
Expand Down
1 change: 1 addition & 0 deletions java/server/test/org/openqa/selenium/grid/sessionmap/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ java_test(
],
deps = [
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/client/src/org/openqa/selenium/remote/tracing:tracing",
"//java/server/src/org/openqa/selenium/grid/data:data",
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/local:local",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gen_java_tests(
size = "small",
srcs = glob(["*.java"]),
deps = [
"//java/client/src/org/openqa/selenium/remote/tracing",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/local",
"//java/server/src/org/openqa/selenium/grid/sessionmap/remote",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.grid.sessionmap;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -32,6 +31,7 @@
import org.openqa.selenium.grid.web.PassthroughHttpClient;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.DistributedTracer;

import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -57,7 +57,7 @@ public void setUp() throws URISyntaxException {
new URI("http://localhost:1234"),
new ImmutableCapabilities());

local = new LocalSessionMap();
local = new LocalSessionMap(DistributedTracer.builder().build());
client = new PassthroughHttpClient<>(local);
remote = new RemoteSessionMap(client);
}
Expand Down

0 comments on commit 75ac1bf

Please sign in to comment.