Skip to content

Commit 24a9c50

Browse files
llatinovpujagani
andauthored
Split OpenTelemetry command (#10009)
Split OpenTelemetry command in order to facilitate search by tags Fixes #9942 Co-authored-by: Puja Jagani <[email protected]>
1 parent 5e2fd23 commit 24a9c50

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

java/src/org/openqa/selenium/remote/TracedCommandExecutor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openqa.selenium.remote.tracing.Tracer;
2222

2323
import java.io.IOException;
24+
import java.util.Map;
2425

2526
public class TracedCommandExecutor implements CommandExecutor {
2627

@@ -35,7 +36,17 @@ public TracedCommandExecutor(CommandExecutor delegate, Tracer tracer) {
3536
@Override
3637
public Response execute(Command command) throws IOException {
3738
try (Span commandSpan = tracer.getCurrentContext().createSpan("command")) {
38-
commandSpan.setAttribute("command", command.toString());
39+
SessionId sessionId = command.getSessionId();
40+
if (sessionId != null) {
41+
commandSpan.setAttribute("sessionId", sessionId.toString());
42+
}
43+
commandSpan.setAttribute("command", command.getName());
44+
Map<String, ?> parameters = command.getParameters();
45+
if (parameters != null && parameters.size() > 0) {
46+
for (Map.Entry<String, ?> parameter : parameters.entrySet()) {
47+
commandSpan.setAttribute("parameter." + parameter.getKey(), parameter.getValue().toString());
48+
}
49+
}
3950
return delegate.execute(command);
4051
}
4152
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote;
19+
20+
import static org.mockito.Mockito.times;
21+
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.verifyNoMoreInteractions;
23+
import static org.mockito.Mockito.when;
24+
25+
import org.openqa.selenium.remote.tracing.Span;
26+
import org.openqa.selenium.remote.tracing.TraceContext;
27+
import org.openqa.selenium.remote.tracing.Tracer;
28+
import org.openqa.selenium.testing.UnitTests;
29+
import org.mockito.Mock;
30+
import org.mockito.MockitoAnnotations;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
35+
import java.io.IOException;
36+
import java.util.Map;
37+
import java.util.HashMap;
38+
import java.util.UUID;
39+
40+
@Category(UnitTests.class)
41+
public class TracedCommandExecutorTest {
42+
@Mock
43+
private CommandExecutor commandExecutor;
44+
@Mock
45+
private Tracer tracer;
46+
@Mock
47+
private TraceContext traceContext;
48+
@Mock
49+
private Span span;
50+
51+
private TracedCommandExecutor tracedCommandExecutor;
52+
53+
@Before
54+
public void createMocksAndTracedCommandExecutor() {
55+
MockitoAnnotations.initMocks(this);
56+
when(tracer.getCurrentContext()).thenReturn(traceContext);
57+
when(traceContext.createSpan("command")).thenReturn(span);
58+
tracedCommandExecutor = new TracedCommandExecutor(commandExecutor, tracer);
59+
}
60+
61+
@Test
62+
public void canCreateSpanWithAllAttributes() throws IOException {
63+
SessionId sessionId = new SessionId(UUID.randomUUID());
64+
Map<String, Object> parameters = new HashMap<>();
65+
parameters.put("param1", "value1");
66+
parameters.put("param2", "value2");
67+
Command command = new Command(sessionId, "findElement", parameters);
68+
69+
tracedCommandExecutor.execute(command);
70+
71+
verify(span, times(1)).setAttribute("sessionId", sessionId.toString());
72+
verify(span, times(1)).setAttribute("command", "findElement");
73+
verify(span, times(1)).setAttribute("parameter.param1", "value1");
74+
verify(span, times(1)).setAttribute("parameter.param2", "value2");
75+
verify(span, times(1)).close();
76+
verifyNoMoreInteractions(span);
77+
}
78+
79+
@Test
80+
public void canCreateSpanWithSessionIdAndCommandName() throws IOException {
81+
SessionId sessionId = new SessionId(UUID.randomUUID());
82+
Command command = new Command(sessionId, "findElement");
83+
84+
tracedCommandExecutor.execute(command);
85+
86+
verify(span, times(1)).setAttribute("sessionId", sessionId.toString());
87+
verify(span, times(1)).setAttribute("command", "findElement");
88+
verify(span, times(1)).close();
89+
verifyNoMoreInteractions(span);
90+
}
91+
92+
@Test
93+
public void canCreateSpanWithCommandName() throws IOException {
94+
Command command = new Command(null, "createSession");
95+
96+
tracedCommandExecutor.execute(command);
97+
98+
verify(span, times(1)).setAttribute("command", "createSession");
99+
verify(span, times(1)).close();
100+
verifyNoMoreInteractions(span);
101+
}
102+
}

0 commit comments

Comments
 (0)