diff --git a/java/src/org/openqa/selenium/remote/TracedCommandExecutor.java b/java/src/org/openqa/selenium/remote/TracedCommandExecutor.java index dcc926cf9618b..889654325c946 100644 --- a/java/src/org/openqa/selenium/remote/TracedCommandExecutor.java +++ b/java/src/org/openqa/selenium/remote/TracedCommandExecutor.java @@ -21,6 +21,7 @@ import org.openqa.selenium.remote.tracing.Tracer; import java.io.IOException; +import java.util.Map; public class TracedCommandExecutor implements CommandExecutor { @@ -35,7 +36,17 @@ public TracedCommandExecutor(CommandExecutor delegate, Tracer tracer) { @Override public Response execute(Command command) throws IOException { try (Span commandSpan = tracer.getCurrentContext().createSpan("command")) { - commandSpan.setAttribute("command", command.toString()); + SessionId sessionId = command.getSessionId(); + if (sessionId != null) { + commandSpan.setAttribute("sessionId", sessionId.toString()); + } + commandSpan.setAttribute("command", command.getName()); + Map parameters = command.getParameters(); + if (parameters != null && parameters.size() > 0) { + for (Map.Entry parameter : parameters.entrySet()) { + commandSpan.setAttribute("parameter." + parameter.getKey(), parameter.getValue().toString()); + } + } return delegate.execute(command); } } diff --git a/java/test/org/openqa/selenium/remote/TracedCommandExecutorTest.java b/java/test/org/openqa/selenium/remote/TracedCommandExecutorTest.java new file mode 100644 index 0000000000000..7b8ddea84ef52 --- /dev/null +++ b/java/test/org/openqa/selenium/remote/TracedCommandExecutorTest.java @@ -0,0 +1,102 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.remote; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import org.openqa.selenium.remote.tracing.Span; +import org.openqa.selenium.remote.tracing.TraceContext; +import org.openqa.selenium.remote.tracing.Tracer; +import org.openqa.selenium.testing.UnitTests; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.IOException; +import java.util.Map; +import java.util.HashMap; +import java.util.UUID; + +@Category(UnitTests.class) +public class TracedCommandExecutorTest { + @Mock + private CommandExecutor commandExecutor; + @Mock + private Tracer tracer; + @Mock + private TraceContext traceContext; + @Mock + private Span span; + + private TracedCommandExecutor tracedCommandExecutor; + + @Before + public void createMocksAndTracedCommandExecutor() { + MockitoAnnotations.initMocks(this); + when(tracer.getCurrentContext()).thenReturn(traceContext); + when(traceContext.createSpan("command")).thenReturn(span); + tracedCommandExecutor = new TracedCommandExecutor(commandExecutor, tracer); + } + + @Test + public void canCreateSpanWithAllAttributes() throws IOException { + SessionId sessionId = new SessionId(UUID.randomUUID()); + Map parameters = new HashMap<>(); + parameters.put("param1", "value1"); + parameters.put("param2", "value2"); + Command command = new Command(sessionId, "findElement", parameters); + + tracedCommandExecutor.execute(command); + + verify(span, times(1)).setAttribute("sessionId", sessionId.toString()); + verify(span, times(1)).setAttribute("command", "findElement"); + verify(span, times(1)).setAttribute("parameter.param1", "value1"); + verify(span, times(1)).setAttribute("parameter.param2", "value2"); + verify(span, times(1)).close(); + verifyNoMoreInteractions(span); + } + + @Test + public void canCreateSpanWithSessionIdAndCommandName() throws IOException { + SessionId sessionId = new SessionId(UUID.randomUUID()); + Command command = new Command(sessionId, "findElement"); + + tracedCommandExecutor.execute(command); + + verify(span, times(1)).setAttribute("sessionId", sessionId.toString()); + verify(span, times(1)).setAttribute("command", "findElement"); + verify(span, times(1)).close(); + verifyNoMoreInteractions(span); + } + + @Test + public void canCreateSpanWithCommandName() throws IOException { + Command command = new Command(null, "createSession"); + + tracedCommandExecutor.execute(command); + + verify(span, times(1)).setAttribute("command", "createSession"); + verify(span, times(1)).close(); + verifyNoMoreInteractions(span); + } +}