Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OT API using Brave 4 #14

Merged
merged 17 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Some example code demonstrating how the OpenTracing API is to be used.

Code in the first process…

BraveTracer tracer = new BraveTracer();
Tracer brave4 = Tracer.newBuilder()....build();
io.opentracing.Tracer tracer = BraveTracer.wrap(brave4);

// start a span
try ( Span span0 = tracer.buildSpan("span-0")
Expand Down Expand Up @@ -76,14 +77,3 @@ Code in the second process
}

This code repeats from process to process, as far through the stack as required.

## Status

OpenTracing spans created are represented by Brave's local spans. When an OpenTracing span is injected into a protocol's wire a client Brave span is created. Likewise when an OpenTracing span is extracted from a protocol wire a server Brave span is created.

There is a mismatch between the two APIs in how state of the current span is held. OpenTracing does not hold any such state and passes in the
current span intended to be a parent is an explicit action in the API, while in Brave this state is known and such action not required. OpenTracing's
expectations are honoured here and Brave's internal state is overridden as needed.

It is noted that it probably would have been simpler to have implemented against lower APIs in brave, like directly against the collector and spans,
than the top level Brave api. Hopefully this improvement happens in the short-term, otherwise the API usage for the user will not change.
54 changes: 42 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>

<!-- default bytecode version for src/main -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add or replace the root README with one that looks like the one from openzipkin/brave/brave (particularly the section about converting in-out from brave 3, as this is similar to converting in and out of opentracing)

<main.java.version>1.7</main.java.version>
<main.signature.artifact>java17</main.signature.artifact>

<!-- default bytecode version for src/test -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<main.basedir>${project.basedir}</main.basedir>
</properties>

Expand Down Expand Up @@ -80,19 +88,16 @@
<dependencies>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-impl</artifactId>
<artifactId>opentracing-api</artifactId>
<version>0.20.7</version>
</dependency>

<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-core</artifactId>
<version>3.17.0</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-http</artifactId>
<version>3.17.0</version>
<artifactId>brave</artifactId>
<version>4.0.3</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -105,6 +110,12 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -113,9 +124,10 @@
<plugin>
<inherited>true</inherited>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
<executions>
<execution>
Expand All @@ -125,12 +137,30 @@
<goal>compile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<goals>
<goal>process-main</goal>
</goals>
<configuration>
<target>${main.java.version}</target>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

<fork>false</fork>
</configuration>
</execution>
</executions>
</plugin>

<!-- Language-level aside, make sure Java 9 types and methods aren't used -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
218 changes: 218 additions & 0 deletions src/main/java/brave/opentracing/BraveSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/**
* Copyright 2016-2017 The OpenZipkin Authors
*
* 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 brave.opentracing;

import io.opentracing.Span;
import io.opentracing.SpanContext;

import java.util.Iterator;
import java.util.Map;

/**
* Holds the {@linkplain brave.Span} used by the underlying {@linkplain brave.Tracer}.\
*
* <p>This type also includes hooks to integrate with the underlying {@linkplain brave.Tracer}.
* Ex you can access the underlying span with {@link #unwrap}
*/
public final class BraveSpan implements Span {

/**
* Converts an existing {@linkplain brave.Span} for use in OpenTracing apis
*/
static BraveSpan wrap(brave.Span span) {
if (span == null) throw new NullPointerException("span == null");
return new BraveSpan(span);
}

/**
* Converts an existing {@linkplain brave.Span} for use in OpenTracing apis
*/
public final brave.Span unwrap() {
return delegate;
}

private final brave.Span delegate;
private final SpanContext context;

private BraveSpan(brave.Span delegate) {
this.delegate = delegate;
this.context = BraveSpanContext.wrap(delegate.context());
}

/**
* {@inheritDoc}
*/
@Override
public SpanContext context() {
return context;
}

/**
* {@inheritDoc}
*/
@Override
public void finish() {
delegate.finish();
}

/**
* {@inheritDoc}
*/
@Override
public void finish(long finishMicros) {
delegate.finish(finishMicros);
}

/**
* {@inheritDoc}
*/
@Override
public void close() {
delegate.close();
}

/**
* {@inheritDoc}
*/
@Override
public Span setTag(String key, String value) {
delegate.tag(key, value);
return this;
}

/**
* {@inheritDoc}
*/
@Override
public Span setTag(String key, boolean value) {
return setTag(key, Boolean.toString(value));
}

/**
* {@inheritDoc}
*/
@Override
public Span setTag(String key, Number value) {
return setTag(key, value.toString());
}

/**
* {@inheritDoc}
*/
@Override
public Span log(Map<String, ?> fields) {
if (fields.isEmpty()) return this;
// in real life, do like zipkin-go-opentracing: "key1=value1 key2=value2"
return log(toAnnotation(fields));
}

/**
* {@inheritDoc}
*/
@Override
public Span log(long timestampMicroseconds, Map<String, ?> fields) {
if (fields.isEmpty()) return this;
// in real life, do like zipkin-go-opentracing: "key1=value1 key2=value2"
return log(timestampMicroseconds, toAnnotation(fields));
}

/**
* Converts a map to a string of form: "key1=value1 key2=value2"
*/
static String toAnnotation(Map<String, ?> fields) {
// special-case the "event" field which is similar to the semantics of a zipkin annotation
Object event = fields.get("event");
if (event != null && fields.size() == 1) return event.toString();

return joinOnEqualsSpace(fields);
}

static String joinOnEqualsSpace(Map<String, ?> fields) {
if (fields.isEmpty()) return "";

StringBuilder result = new StringBuilder();
for (Iterator<? extends Map.Entry<String, ?>> i = fields.entrySet().iterator(); i.hasNext(); ) {
Map.Entry<String, ?> next = i.next();
result.append(next.getKey()).append('=').append(next.getValue());
if (i.hasNext()) result.append(' ');
}
return result.toString();
}

/**
* {@inheritDoc}
*/
@Override
public Span log(String event) {
delegate.annotate(event);
return this;
}

/**
* {@inheritDoc}
*/
@Override
public Span log(long timestampMicroseconds, String event) {
delegate.annotate(timestampMicroseconds, event);
return this;
}

/**
* {@inheritDoc}
*/
@Override
public Span log(String eventName, Object ignored) {
return log(eventName);
}

/**
* {@inheritDoc}
*/
@Override
public Span log(long timestampMicroseconds, String eventName, Object ignored) {
return log(timestampMicroseconds, eventName);
}

/**
* This is a NOOP as neither <a href="https://github.com/openzipkin/b3-propagation">B3</a>
* nor Brave include baggage support.
*/
// OpenTracing could one day define a way to plug-in arbitrary baggage handling similar to how
// it has feature-specific apis like active-span
@Override
public Span setBaggageItem(String key, String value) {
// brave does not support baggage
return this;
}

/**
* Returns null as neither <a href="https://github.com/openzipkin/b3-propagation">B3</a>
* nor Brave include baggage support.
*/
// OpenTracing could one day define a way to plug-in arbitrary baggage handling similar to how
// it has feature-specific apis like active-span
@Override
public String getBaggageItem(String key) {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public Span setOperationName(String operationName) {
delegate.name(operationName);
return this;
}
}
Loading