Skip to content

Commit

Permalink
Add encoder-otel-brave (#9)
Browse files Browse the repository at this point in the history
This pull request adds the encoder below:

# encoder-brave

This encodes brave spans into OTLP proto format.

```java
// Use OTLP encoder when sending to an OTLP backend
spanHandler = AsyncZipkinSpanHandler.newBuilder(sender).build(new OtlpProtoV1Encoder(Tags.ERROR));
```

---------

Co-authored-by: Marcin Grzejszczak <[email protected]>
  • Loading branch information
making and marcingrzejszczak authored Aug 19, 2024
1 parent 7a9d0c9 commit 442a436
Show file tree
Hide file tree
Showing 16 changed files with 1,631 additions and 29 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ persists them to a configured collector component.
|------------------------------------|-----------------------------------------------------------------------------------------|
| [collector-http](./collector-http) | Implements the [OTLP/HTTP protocol](https://opentelemetry.io/docs/specs/otlp/#otlphttp) |

### Encoders

The encoder encodes brave spans into OTLP proto format.

| Encoder | Description |
|-------------------------------------------|------------------------------------------------|
| [`OtlpProtoV1Encoder`](./encoder-brave) | zipkin-reporter-brave `AsyncZipkinSpanHandler` |

## Server integration

If you cannot use our [Docker image](./docker/README.md), you can still integrate
Expand Down
6 changes: 0 additions & 6 deletions benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>zipkin-collector-otel-http</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions encoder-brave/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# encoder-brave

This encodes brave spans into OTLP proto format.

```java
// Use OTLP encoder when sending to an OTLP backend
spanHandler = AsyncZipkinSpanHandler.newBuilder(sender).build(new OtlpProtoV1Encoder(Tags.ERROR));
```
119 changes: 119 additions & 0 deletions encoder-brave/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright The OpenZipkin Authors
SPDX-License-Identifier: Apache-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.zipkin.contrib.otel</groupId>
<artifactId>zipkin-otel-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>encoder-brave</artifactId>
<name>Brave Encoder: OpenTelemetry Trace</name>

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

<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-common</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.proto</groupId>
<artifactId>opentelemetry-proto</artifactId>
<version>${opentelemetry-proto.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>${opentelemetry-semconv.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
<!-- We use provided scope to avoid pinning a protobuf version -->
<scope>provided</scope>
</dependency>

<!-- Encoder/Data type deps -->
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>${zipkin-reporter.version}</version>
</dependency>
<dependency>
<groupId>${brave.groupId}</groupId>
<artifactId>brave</artifactId>
<version>${brave.version}</version>
<!-- Don't pin Brave -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${brave.groupId}</groupId>
<artifactId>brave-instrumentation-http</artifactId>
<version>${brave.version}</version>
<!-- Don't pin Brave -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
<version>${zipkin-reporter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${armeria.groupId}</groupId>
<artifactId>armeria-junit5</artifactId>
<version>${armeria.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package zipkin2.reporter.otel.brave;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Properties;

/**
* Define InstrumentationScope for encoder-brave
*/
final class BraveScope {
static final String NAME;

static final String VERSION;

static {
try (InputStream stream = BraveScope.class.getClassLoader().getResourceAsStream("scope.properties")) {
if (stream != null) {
Properties props = new Properties();
props.load(stream);
NAME = props.getProperty("name");
VERSION = props.getProperty("version");
}
else {
NAME = "unknown";
VERSION = "unknown";
}
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package zipkin2.reporter.otel.brave;

import brave.Tag;
import brave.handler.MutableSpan;
import io.opentelemetry.proto.trace.v1.TracesData;
import zipkin2.reporter.BytesEncoder;
import zipkin2.reporter.Encoding;

public final class OtlpProtoV1Encoder implements BytesEncoder<MutableSpan> {
final SpanTranslator spanTranslator;

public OtlpProtoV1Encoder(Tag<Throwable> errorTag) {
if (errorTag == null) throw new NullPointerException("errorTag == null");
this.spanTranslator = new SpanTranslator(errorTag);
}

@Override
public Encoding encoding() {
return Encoding.PROTO3;
}

@Override public int sizeInBytes(MutableSpan span) {
// TODO: Create a proto size function to avoid allocations here
TracesData convert = translate(span);
return encoding().listSizeInBytes(convert.getSerializedSize());
}

@Override public byte[] encode(MutableSpan span) {
return translate(span).toByteArray();
}

TracesData translate(MutableSpan span) {
return spanTranslator.translate(span);
}
}
Loading

0 comments on commit 442a436

Please sign in to comment.