Skip to content

Commit cfd6d53

Browse files
committed
feat: Add OpenTelemetry Support
feat: Add OpenTelemetry Support feat: Add Support for Tracing Propagation Headers
1 parent 936d075 commit cfd6d53

17 files changed

+1764
-1194
lines changed

pom.xml

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@
4242
</repository>
4343
</distributionManagement>
4444

45-
<developers>
46-
<developer>
47-
<id>henriquetruta</id>
48-
<name>Henrique Truta</name>
49-
<email>[email protected]</email>
50-
<organization>Spotify AB</organization>
51-
<organizationUrl>http://www.spotify.com</organizationUrl>
52-
</developer>
53-
<developer>
54-
<id>hewhomustnotbenamed</id>
55-
<name>Abhimanyu Shegokar</name>
56-
<email>[email protected]</email>
57-
<organization>Spotify AB</organization>
58-
<organizationUrl>http://www.spotify.com</organizationUrl>
59-
</developer>
60-
</developers>
61-
6245
<repositories>
6346
<repository>
6447
<id>apache.snapshots</id>
@@ -102,6 +85,7 @@
10285
<objenesis.version>3.3</objenesis.version>
10386
<opencensus.version>0.31.1</opencensus.version>
10487
<okhttp.version>4.11.0</okhttp.version>
88+
<opentelemetry.version>1.42.1</opentelemetry.version>
10589

10690
<shade.id>${project.groupId}.githubclient.shade</shade.id>
10791
</properties>
@@ -120,6 +104,13 @@
120104
<scope>import</scope>
121105
<type>pom</type>
122106
</dependency>
107+
<dependency>
108+
<groupId>io.opentelemetry</groupId>
109+
<artifactId>opentelemetry-bom</artifactId>
110+
<version>${opentelemetry.version}</version>
111+
<type>pom</type>
112+
<scope>import</scope>
113+
</dependency>
123114
</dependencies>
124115
</dependencyManagement>
125116

@@ -179,6 +170,25 @@
179170
<artifactId>opencensus-api</artifactId>
180171
<version>${opencensus.version}</version>
181172
</dependency>
173+
174+
<dependency>
175+
<groupId>io.opentelemetry</groupId>
176+
<artifactId>opentelemetry-api</artifactId>
177+
</dependency>
178+
<dependency>
179+
<groupId>io.opentelemetry</groupId>
180+
<artifactId>opentelemetry-sdk</artifactId>
181+
</dependency>
182+
<dependency>
183+
<groupId>io.opentelemetry</groupId>
184+
<artifactId>opentelemetry-sdk-testing</artifactId>
185+
</dependency>
186+
<dependency>
187+
<groupId>commons-io</groupId>
188+
<artifactId>commons-io</artifactId>
189+
<version>2.7</version>
190+
<scope>compile</scope>
191+
</dependency>
182192
<dependency>
183193
<groupId>io.jsonwebtoken</groupId>
184194
<artifactId>jjwt-impl</artifactId>
@@ -246,12 +256,6 @@
246256
<version>${okhttp.version}</version>
247257
<scope>test</scope>
248258
</dependency>
249-
<dependency>
250-
<groupId>commons-io</groupId>
251-
<artifactId>commons-io</artifactId>
252-
<version>2.7</version>
253-
<scope>compile</scope>
254-
</dependency>
255259
</dependencies>
256260

257261
<profiles>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* -\-\-
3+
* github-client
4+
* --
5+
* Copyright (C) 2016 - 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.tracing;
22+
23+
import okhttp3.Request;
24+
25+
import java.util.concurrent.CompletionStage;
26+
27+
public abstract class BaseTracer implements Tracer {
28+
@Override
29+
public Span span(final String name, final String method, final CompletionStage<?> future) {
30+
return internalSpan(name, method, future);
31+
}
32+
33+
@Override
34+
public Span span(final String path, final String method) {
35+
return internalSpan(path, method, null);
36+
}
37+
38+
@Override
39+
public Span span(final Request request) {
40+
return internalSpan(request.url().toString(), request.method(), null);
41+
}
42+
43+
protected abstract Span internalSpan(
44+
String path,
45+
String method,
46+
CompletionStage<?> future);
47+
48+
@Override
49+
public void attachSpanToFuture(final Span span, final CompletionStage<?> future) {
50+
future.whenComplete(
51+
(result, t) -> {
52+
if (t == null) {
53+
span.success();
54+
} else {
55+
span.failure(t);
56+
}
57+
span.close();
58+
});
59+
}
60+
}

src/main/java/com/spotify/github/Span.java renamed to src/main/java/com/spotify/github/tracing/Span.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* -/-/-
1919
*/
2020

21-
package com.spotify.github;
21+
package com.spotify.github.tracing;
22+
23+
import okhttp3.Request;
2224

2325
public interface Span extends AutoCloseable {
2426

@@ -29,5 +31,7 @@ public interface Span extends AutoCloseable {
2931
/** Close span. Must be called for any opened span. */
3032
@Override
3133
void close();
34+
35+
Request decorateRequest(Request request);
3236
}
3337

src/main/java/com/spotify/github/Tracer.java renamed to src/main/java/com/spotify/github/tracing/Tracer.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,15 +18,26 @@
1818
* -/-/-
1919
*/
2020

21-
package com.spotify.github;
21+
package com.spotify.github.tracing;
22+
23+
import okhttp3.Request;
2224

2325
import java.util.concurrent.CompletionStage;
2426

2527
public interface Tracer {
2628

27-
/** Create scoped span. Span will be closed when future completes. */
29+
/**
30+
* Create scoped span. Span will be closed when future completes.
31+
*/
2832
Span span(
2933
String path, String method, CompletionStage<?> future);
3034

35+
Span span(
36+
String path, String method);
37+
38+
Span span(
39+
Request request);
40+
41+
void attachSpanToFuture(Span span, CompletionStage<?> future);
3142
}
3243

src/main/java/com/spotify/github/opencensus/OpenCensusSpan.java renamed to src/main/java/com/spotify/github/tracing/opencensus/OpenCensusSpan.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,20 +18,23 @@
1818
* -/-/-
1919
*/
2020

21-
package com.spotify.github.opencensus;
21+
package com.spotify.github.tracing.opencensus;
22+
2223
import static java.util.Objects.requireNonNull;
23-
import com.spotify.github.Span;
24+
25+
import com.spotify.github.tracing.Span;
2426
import com.spotify.github.v3.exceptions.RequestNotOkException;
2527
import io.opencensus.trace.AttributeValue;
2628
import io.opencensus.trace.Status;
29+
import okhttp3.Request;
2730

28-
class OpenCensusSpan implements Span {
31+
public class OpenCensusSpan implements Span {
2932

3033
public static final int NOT_FOUND = 404;
3134
public static final int INTERNAL_SERVER_ERROR = 500;
3235
private final io.opencensus.trace.Span span;
3336

34-
OpenCensusSpan(final io.opencensus.trace.Span span) {
37+
public OpenCensusSpan(final io.opencensus.trace.Span span) {
3538
this.span = requireNonNull(span);
3639
}
3740

@@ -59,5 +62,13 @@ public Span failure(final Throwable t) {
5962
public void close() {
6063
span.end();
6164
}
65+
66+
@Override
67+
public Request decorateRequest(final Request request) {
68+
return request.newBuilder()
69+
.header("traceparent", span.getContext().getTraceId().toString())
70+
.header("tracestate", span.getContext().getTracestate().toString())
71+
.build();
72+
}
6273
}
6374

src/main/java/com/spotify/github/opencensus/OpenCensusTracer.java renamed to src/main/java/com/spotify/github/tracing/opencensus/OpenCensusTracer.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,10 +18,10 @@
1818
* -/-/-
1919
*/
2020

21-
package com.spotify.github.opencensus;
21+
package com.spotify.github.tracing.opencensus;
2222

23-
import com.spotify.github.Span;
24-
import com.spotify.github.Tracer;
23+
import com.spotify.github.tracing.BaseTracer;
24+
import com.spotify.github.tracing.Span;
2525
import io.opencensus.trace.Tracing;
2626

2727
import java.util.concurrent.CompletionStage;
@@ -30,22 +30,16 @@
3030
import static io.opencensus.trace.Span.Kind.CLIENT;
3131
import static java.util.Objects.requireNonNull;
3232

33-
public class OpenCensusTracer implements Tracer {
33+
public class OpenCensusTracer extends BaseTracer {
3434

3535
private static final io.opencensus.trace.Tracer TRACER = Tracing.getTracer();
3636

37-
@Override
38-
public Span span(final String name, final String method, final CompletionStage<?> future) {
39-
return internalSpan(name, method, future);
40-
}
41-
4237
@SuppressWarnings("MustBeClosedChecker")
43-
private Span internalSpan(
38+
protected Span internalSpan(
4439
final String path,
4540
final String method,
4641
final CompletionStage<?> future) {
4742
requireNonNull(path);
48-
requireNonNull(future);
4943

5044
final io.opencensus.trace.Span ocSpan =
5145
TRACER.spanBuilder("GitHub Request").setSpanKind(CLIENT).startSpan();
@@ -56,15 +50,9 @@ private Span internalSpan(
5650
ocSpan.putAttribute("method", stringAttributeValue(method));
5751
final Span span = new OpenCensusSpan(ocSpan);
5852

59-
future.whenComplete(
60-
(result, t) -> {
61-
if (t == null) {
62-
span.success();
63-
} else {
64-
span.failure(t);
65-
}
66-
span.close();
67-
});
53+
if (future != null) {
54+
attachSpanToFuture(span, future);
55+
}
6856

6957
return span;
7058
}

0 commit comments

Comments
 (0)