Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 3b59ca2

Browse files
authored
Add http propagation APIs. (#930)
* Add http propagation APIs. * Add an API in `PropagationComponent` for B3 format access. * Add contrib-http-util, with initial SD format support.
1 parent 72b7e39 commit 3b59ca2

File tree

12 files changed

+174
-8
lines changed

12 files changed

+174
-8
lines changed

all/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def subprojects = [
2020
project(':opencensus-contrib-agent'),
2121
project(':opencensus-contrib-grpc-util'),
2222
project(':opencensus-contrib-grpc-metrics'),
23+
project(':opencensus-contrib-http-util'),
2324
project(':opencensus-contrib-zpages'),
2425
project(':opencensus-exporter-trace-logging'),
2526
project(':opencensus-exporter-trace-stackdriver'),
@@ -35,6 +36,7 @@ def subprojects_javadoc = [
3536
project(':opencensus-contrib-agent'),
3637
project(':opencensus-contrib-grpc-util'),
3738
project(':opencensus-contrib-grpc-metrics'),
39+
project(':opencensus-contrib-http-util'),
3840
project(':opencensus-contrib-zpages'),
3941
project(':opencensus-exporter-trace-logging'),
4042
project(':opencensus-exporter-trace-stackdriver'),

api/src/main/java/io/opencensus/trace/propagation/PropagationComponent.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
/**
2020
* Container class for all the supported propagation formats. Currently supports only Binary format
21-
* see {@link BinaryFormat} but more formats will be added.
21+
* (see {@link BinaryFormat}) and B3 Text format (see {@link TextFormat}) but more formats will be
22+
* added.
2223
*/
2324
public abstract class PropagationComponent {
2425
private static final PropagationComponent NOOP_PROPAGATION_COMPONENT =
@@ -32,6 +33,16 @@ public abstract class PropagationComponent {
3233
*/
3334
public abstract BinaryFormat getBinaryFormat();
3435

36+
/**
37+
* Returns the B3 {@link TextFormat} with the provided implementations. See <a
38+
* href="https://github.com/openzipkin/b3-propagation">b3-propagation</a> for more information. If
39+
* no implementation is provided then no-op implementation will be used.
40+
*
41+
* @since 0.11.0
42+
* @return the B3 {@code TextFormat} implementation for B3.
43+
*/
44+
public abstract TextFormat getB3Format();
45+
3546
/**
3647
* Returns an instance that contains no-op implementations for all the instances.
3748
*
@@ -46,5 +57,10 @@ private static final class NoopPropagationComponent extends PropagationComponent
4657
public BinaryFormat getBinaryFormat() {
4758
return BinaryFormat.getNoopBinaryFormat();
4859
}
60+
61+
@Override
62+
public TextFormat getB3Format() {
63+
return TextFormat.getNoopTextFormat();
64+
}
4965
}
5066
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ subprojects {
320320
'opencensus-contrib-agent',
321321
'opencensus-contrib-grpc-metrics',
322322
'opencensus-contrib-grpc-util',
323+
'opencensus-contrib-http-util',
323324
'opencensus-contrib-zpages',
324325
'opencensus-exporter-stats-signalfx',
325326
'opencensus-exporter-stats-stackdriver',

contrib/http_util/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# OpenCensus HTTP Util
2+
[![Build Status][travis-image]][travis-url]
3+
[![Windows Build Status][appveyor-image]][appveyor-url]
4+
[![Maven Central][maven-image]][maven-url]
5+
6+
The *OpenCensus HTTP Util for Java* is a collection of utilities for trace instrumentation when
7+
working with HTTP.
8+
9+
## Quickstart
10+
11+
### Add the dependencies to your project
12+
13+
For Maven add to your `pom.xml`:
14+
```xml
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.opencensus</groupId>
18+
<artifactId>opencensus-api</artifactId>
19+
<version>0.11.0</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>io.opencensus</groupId>
23+
<artifactId>opencensus-contrib-http-util</artifactId>
24+
<version>0.11.0</version>
25+
</dependency>
26+
</dependencies>
27+
```
28+
29+
For Gradle add to your dependencies:
30+
```gradle
31+
compile 'io.opencensus:opencensus-api:0.11.0'
32+
compile 'io.opencensus:opencensus-contrib-http-util:0.11.0'
33+
```
34+
35+
[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master
36+
[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java
37+
[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true
38+
[appveyor-url]: https://ci.appveyor.com/project/opencensusjavateam/opencensus-java/branch/master
39+
[maven-image]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-contrib-grpc-util/badge.svg
40+
[maven-url]: https://maven-badges.herokuapp.com/maven-central/io.opencensus/opencensus-contrib-grpc-util
41+
[grpc-url]: https://github.com/grpc/grpc-java

contrib/http_util/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
description = 'OpenCensus HTTP Util'
2+
3+
apply plugin: 'java'
4+
5+
[compileJava, compileTestJava].each() {
6+
it.sourceCompatibility = 1.6
7+
it.targetCompatibility = 1.6
8+
}
9+
10+
dependencies {
11+
compile project(':opencensus-api'),
12+
libraries.guava
13+
14+
signature "org.codehaus.mojo.signature:java16:+@signature"
15+
}

impl_core/src/main/java/io/opencensus/implcore/trace/propagation/CloudTraceFormat.java renamed to contrib/http_util/src/main/java/io/opencensus/contrib/http/util/CloudTraceFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opencensus.implcore.trace.propagation;
17+
package io.opencensus.contrib.http.util;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.contrib.http.util;
18+
19+
import io.opencensus.trace.propagation.TextFormat;
20+
21+
/**
22+
* Utility class to get all supported {@link TextFormat}.
23+
*
24+
* @since 0.11.0
25+
*/
26+
public class HttpPropagationUtil {
27+
28+
private HttpPropagationUtil() {}
29+
30+
/**
31+
* Returns the Stack Driver format implementation. The header specification for this format is
32+
* "X-Cloud-Trace-Context: &lt;TRACE_ID&gt;/&lt;SPAN_ID&gt;[;o=&lt;TRACE_TRUE&gt;]". See this <a
33+
* href="https://cloud.google.com/trace/docs/support">page</a> for more information.
34+
*
35+
* @since 0.11.0
36+
* @return the Stack Driver format.
37+
*/
38+
public static TextFormat getCloudTraceFormat() {
39+
return new CloudTraceFormat();
40+
}
41+
}

impl_core/src/test/java/io/opencensus/implcore/trace/propagation/CloudTraceFormatTest.java renamed to contrib/http_util/src/test/java/io/opencensus/contrib/http/util/CloudTraceFormatTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opencensus.implcore.trace.propagation;
17+
package io.opencensus.contrib.http.util;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static io.opencensus.implcore.trace.propagation.CloudTraceFormat.HEADER_NAME;
21-
import static io.opencensus.implcore.trace.propagation.CloudTraceFormat.NOT_SAMPLED;
22-
import static io.opencensus.implcore.trace.propagation.CloudTraceFormat.SAMPLED;
23-
import static io.opencensus.implcore.trace.propagation.CloudTraceFormat.SPAN_ID_DELIMITER;
24-
import static io.opencensus.implcore.trace.propagation.CloudTraceFormat.TRACE_OPTION_DELIMITER;
20+
import static io.opencensus.contrib.http.util.CloudTraceFormat.HEADER_NAME;
21+
import static io.opencensus.contrib.http.util.CloudTraceFormat.NOT_SAMPLED;
22+
import static io.opencensus.contrib.http.util.CloudTraceFormat.SAMPLED;
23+
import static io.opencensus.contrib.http.util.CloudTraceFormat.SPAN_ID_DELIMITER;
24+
import static io.opencensus.contrib.http.util.CloudTraceFormat.TRACE_OPTION_DELIMITER;
2525

2626
import com.google.common.primitives.UnsignedLong;
2727
import io.opencensus.trace.SpanContext;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2018, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.contrib.http.util;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import io.opencensus.trace.propagation.TextFormat;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
/** Unit tests for {@link HttpPropagationUtil}. */
27+
@RunWith(JUnit4.class)
28+
public class HttpPropagationUtilTest {
29+
30+
@Test
31+
public void cloudTraceFormatNotNull() {
32+
TextFormat cloudTraceFormat = HttpPropagationUtil.getCloudTraceFormat();
33+
assertThat(cloudTraceFormat).isNotNull();
34+
assertThat(cloudTraceFormat).isInstanceOf(CloudTraceFormat.class);
35+
}
36+
}

impl_core/src/main/java/io/opencensus/implcore/trace/propagation/PropagationComponentImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818

1919
import io.opencensus.trace.propagation.BinaryFormat;
2020
import io.opencensus.trace.propagation.PropagationComponent;
21+
import io.opencensus.trace.propagation.TextFormat;
2122

2223
/** Implementation of the {@link PropagationComponent}. */
2324
public class PropagationComponentImpl extends PropagationComponent {
2425
private final BinaryFormat binaryFormat = new BinaryFormatImpl();
26+
private final B3Format b3Format = new B3Format();
2527

2628
@Override
2729
public BinaryFormat getBinaryFormat() {
2830
return binaryFormat;
2931
}
32+
33+
@Override
34+
public TextFormat getB3Format() {
35+
return b3Format;
36+
}
3037
}

0 commit comments

Comments
 (0)