Skip to content

Commit

Permalink
Split OpenTelemetry command (SeleniumHQ#10009)
Browse files Browse the repository at this point in the history
Split OpenTelemetry command in order to facilitate search by tags

Fixes SeleniumHQ#9942

Co-authored-by: Puja Jagani <puja.jagani93@gmail.com>
  • Loading branch information
2 people authored and elgatov committed Jun 27, 2022
1 parent c0b4883 commit 1eb859a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
13 changes: 12 additions & 1 deletion java/src/org/openqa/selenium/remote/TracedCommandExecutor.java
Expand Up @@ -21,6 +21,7 @@
import org.openqa.selenium.remote.tracing.Tracer;

import java.io.IOException;
import java.util.Map;

public class TracedCommandExecutor implements CommandExecutor {

Expand All @@ -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<String, ?> parameters = command.getParameters();
if (parameters != null && parameters.size() > 0) {
for (Map.Entry<String, ?> parameter : parameters.entrySet()) {
commandSpan.setAttribute("parameter." + parameter.getKey(), parameter.getValue().toString());
}
}
return delegate.execute(command);
}
}
Expand Down
102 changes: 102 additions & 0 deletions 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<String, Object> 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);
}
}

0 comments on commit 1eb859a

Please sign in to comment.