Skip to content

Commit aecc5bd

Browse files
authored
[grid] Add spans to new session queue remove related functions
1 parent 83f4a19 commit aecc5bd

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

java/server/src/org/openqa/selenium/grid/sessionqueue/GetNextMatchingRequest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,43 @@
2525
import org.openqa.selenium.remote.http.HttpHandler;
2626
import org.openqa.selenium.remote.http.HttpRequest;
2727
import org.openqa.selenium.remote.http.HttpResponse;
28+
import org.openqa.selenium.remote.tracing.Span;
29+
import org.openqa.selenium.remote.tracing.Tracer;
2830

2931
import java.io.UncheckedIOException;
3032
import java.lang.reflect.Type;
3133
import java.util.Optional;
3234
import java.util.Set;
3335

3436
import static java.util.Collections.singletonMap;
37+
import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;
38+
import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;
39+
import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;
3540

3641
class GetNextMatchingRequest implements HttpHandler {
3742
private static final Type SET_OF_CAPABILITIES = new TypeToken<Set<Capabilities>>() {}.getType();
3843

44+
private final Tracer tracer;
3945
private final NewSessionQueue queue;
4046

41-
public GetNextMatchingRequest(NewSessionQueue queue) {
47+
public GetNextMatchingRequest(Tracer tracer, NewSessionQueue queue) {
48+
this.tracer = Require.nonNull("Tracer", tracer);
4249
this.queue = Require.nonNull("New session queue", queue);
4350
}
4451

4552
@Override
4653
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
47-
Set<Capabilities> stereotypes = Contents.fromJson(req, SET_OF_CAPABILITIES);
54+
try (Span span = newSpanAsChildOf(tracer, req, "sessionqueue.getrequest")) {
55+
HTTP_REQUEST.accept(span, req);
56+
Set<Capabilities> stereotypes = Contents.fromJson(req, SET_OF_CAPABILITIES);
4857

49-
Optional<SessionRequest> maybeRequest = queue.getNextAvailable(stereotypes);
58+
Optional<SessionRequest> maybeRequest = queue.getNextAvailable(stereotypes);
5059

51-
return new HttpResponse().setContent(Contents.asJson(singletonMap("value", maybeRequest.orElse(null))));
60+
HttpResponse response = new HttpResponse().setContent(Contents.asJson(singletonMap("value", maybeRequest.orElse(null))));
61+
62+
HTTP_RESPONSE.accept(span, response);
63+
64+
return response;
65+
}
5266
}
5367
}

java/server/src/org/openqa/selenium/grid/sessionqueue/NewSessionQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected NewSessionQueue(Tracer tracer, Secret registrationSecret) {
9494
.to(params -> new RemoveFromSessionQueue(tracer, this, requestIdFrom(params)))
9595
.with(requiresSecret),
9696
post("/se/grid/newsessionqueue/session/next")
97-
.to(() -> new GetNextMatchingRequest(this))
97+
.to(() -> new GetNextMatchingRequest(tracer, this))
9898
.with(requiresSecret),
9999
get("/se/grid/newsessionqueue/queue")
100100
.to(() -> new GetSessionQueue(tracer, this)),

java/server/src/org/openqa/selenium/grid/sessionqueue/local/LocalNewSessionQueue.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.openqa.selenium.internal.Require;
2727
import org.openqa.selenium.remote.http.Contents;
2828
import org.openqa.selenium.remote.http.HttpResponse;
29+
import org.openqa.selenium.remote.tracing.AttributeKey;
30+
import org.openqa.selenium.remote.tracing.Span;
2931
import org.openqa.selenium.remote.tracing.Tracer;
3032

3133
import java.io.Closeable;
@@ -265,8 +267,10 @@ public boolean retryAddToQueue(SessionRequest request) {
265267

266268
@Override
267269
public Optional<SessionRequest> remove(RequestId reqId) {
270+
Span span = tracer.getCurrentContext().createSpan("sessionqueue.remove");
268271
Require.nonNull("Request ID", reqId);
269272

273+
span.setAttribute(AttributeKey.REQUEST_ID.getKey(), reqId.toString());
270274
Lock writeLock = lock.writeLock();
271275
writeLock.lock();
272276
try {
@@ -276,17 +280,21 @@ public Optional<SessionRequest> remove(RequestId reqId) {
276280
if (reqId.equals(req.getRequestId())) {
277281
iterator.remove();
278282

283+
span.setAttribute("removed", true);
279284
return Optional.of(req);
280285
}
281286
}
287+
span.setAttribute("removed", false);
282288
return Optional.empty();
283289
} finally {
284290
writeLock.unlock();
291+
span.close();
285292
}
286293
}
287294

288295
@Override
289296
public Optional<SessionRequest> getNextAvailable(Set<Capabilities> stereotypes) {
297+
Span span = tracer.getCurrentContext().createSpan("sessionqueue.stereotypematch");
290298
Require.nonNull("Stereotypes", stereotypes);
291299

292300
Predicate<Capabilities> matchesStereotype =
@@ -300,11 +308,15 @@ public Optional<SessionRequest> getNextAvailable(Set<Capabilities> stereotypes)
300308
.filter(req -> req.getDesiredCapabilities().stream().anyMatch(matchesStereotype))
301309
.findFirst();
302310

303-
maybeRequest.ifPresent(req -> this.remove(req.getRequestId()));
311+
maybeRequest.ifPresent(req -> {
312+
span.setAttribute("match", true);
313+
this.remove(req.getRequestId());
314+
});
304315

305316
return maybeRequest;
306317
} finally {
307318
writeLock.unlock();
319+
span.close();
308320
}
309321
}
310322

0 commit comments

Comments
 (0)