spans, final String name) {
+ return spans.stream().filter(s -> s.getName().equals(name)).findFirst().get();
+ }
+
+ @Before
+ public void setUpExporter() {
+ spanExporterHandler = new TestExportHandler();
+ Tracing.getExportComponent().getSpanExporter().registerHandler("test", spanExporterHandler);
+ }
+
+ @BeforeClass
+ public static void setupTracing() {
+ final TraceConfig traceConfig = Tracing.getTraceConfig();
+ final Sampler sampler = Samplers.alwaysSample();
+ final TraceParams newParams =
+ traceConfig.getActiveTraceParams().toBuilder().setSampler(sampler).build();
+ traceConfig.updateActiveTraceParams(newParams);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/spotify/github/opencensus/TestExportHandler.java b/src/test/java/com/spotify/github/opencensus/TestExportHandler.java
new file mode 100644
index 00000000..c1519572
--- /dev/null
+++ b/src/test/java/com/spotify/github/opencensus/TestExportHandler.java
@@ -0,0 +1,82 @@
+/*-
+ * -\-\-
+ * github-client
+ * --
+ * Copyright (C) 2016 - 2021 Spotify AB
+ * --
+ * Licensed 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 com.spotify.github.opencensus;
+
+import io.opencensus.trace.export.SpanData;
+import io.opencensus.trace.export.SpanExporter;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A dummy SpanExporter.Handler which keeps any exported Spans in memory, so we can query against
+ * them in tests.
+ *
+ * The opencensus-testing library has a TestHandler that can be used in tests like this, but the
+ * only method it exposes to gain access to the received spans is waitForExport(int) which blocks
+ * forever until the given number of spans is exported, which could be never. So instead we define
+ * our own very simple implementation.
+ */
+class TestExportHandler extends SpanExporter.Handler {
+ private static final Logger LOG = LoggerFactory.getLogger(TestExportHandler.class);
+
+ private final List receivedSpans = new ArrayList<>();
+ private final Object lock = new Object();
+
+ @Override
+ public void export(final Collection spanDataList) {
+ synchronized (lock) {
+ receivedSpans.addAll(spanDataList);
+ LOG.info("received {} spans, {} total", spanDataList.size(), receivedSpans.size());
+ }
+ }
+
+ List receivedSpans() {
+ synchronized (lock) {
+ return new ArrayList<>(receivedSpans);
+ }
+ }
+
+ /** Wait up to waitTime for at least `count` spans to be exported */
+ List waitForSpansToBeExported(final int count) throws InterruptedException {
+ // opencensus is hardcoded to export batches every 5 seconds (see
+ // io.opencensus.implcore.trace.export.ExportComponentImpl), so wait slightly longer than that
+ Duration waitTime = Duration.ofSeconds(7);
+ Instant deadline = Instant.now().plus(waitTime);
+
+ List spanData = receivedSpans();
+ while (spanData.size() < count) {
+ //noinspection BusyWait
+ Thread.sleep(100);
+ spanData = receivedSpans();
+
+ if (!Instant.now().isBefore(deadline)) {
+ LOG.warn("ending busy wait for spans because deadline passed");
+ break;
+ }
+ }
+ return spanData;
+ }
+}
diff --git a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java
index 182a7a96..8e4504d1 100644
--- a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java
+++ b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java
@@ -25,10 +25,9 @@
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
+import com.spotify.github.Tracer;
import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
import com.spotify.github.v3.exceptions.RequestNotOkException;
import com.spotify.github.v3.repos.CommitItem;
@@ -52,6 +51,7 @@ public class GitHubClientTest {
private GitHubClient github;
private OkHttpClient client;
+ private Tracer tracer = mock(Tracer.class);
@Before
public void setUp() {
@@ -88,10 +88,11 @@ public void testSearchIssue() throws Throwable {
when(client.newCall(any())).thenReturn(call);
IssueClient issueClient =
- github.createRepositoryClient("testorg", "testrepo").createIssueClient();
+ github.withTracer(tracer).createRepositoryClient("testorg", "testrepo").createIssueClient();
CompletableFuture maybeSucceeded = issueClient.editComment(1, "some comment");
capture.getValue().onResponse(call, response);
+ verify(tracer,times(1)).span(anyString(), anyString(),any());
try {
maybeSucceeded.get();
} catch (Exception e) {