-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add OTLP/HTTP collector #13
Merged
codefromthecrypt
merged 14 commits into
openzipkin-contrib:main
from
making:otlp-collector
Sep 16, 2024
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b23b75
Add OTLP/HTTP collector
making b5015c0
Remove unused opentelemetry-sdk-common
making 6f2bec2
Update collector-http/src/main/java/zipkin2/collector/otel/http/OpenT…
making 88b3977
Update collector-http/src/main/java/zipkin2/collector/otel/http/OpenT…
making 88a5f95
Update collector-http/src/main/java/zipkin2/collector/otel/http/OpenT…
making 9ac6f61
Fix import
making e94d942
Fix test
making ce708bc
Use long to set trace/span id and remove OtelEncodingUtils
making b162311
Unwrap UncheckedIOException
making 420507b
Use comma separator instead of json to encode attributes
making 3dbd79a
Makes no sense to set an id with zeros, as this will throw an Illegal…
making 9f67806
Remove unused INVALID_TRACE
making c869d69
Increment spanDropped if the received span is invalid
making 5195c30
Handling invalid span exceptions outside the Translator
making File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# zipkin-collector-otel-http | ||
# collector-http | ||
|
||
This component implements | ||
the [OTLP/HTTP protocol](https://opentelemetry.io/docs/specs/otlp/#otlphttp) | ||
with [Armeria](https://armeria.dev/). | ||
This component implements the [OTLP/HTTP protocol](https://opentelemetry.io/docs/specs/otlp/#otlphttp) with [Armeria](https://armeria.dev/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,30 @@ | |
*/ | ||
package zipkin2.collector.otel.http; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import com.google.protobuf.CodedInputStream; | ||
import com.linecorp.armeria.common.AggregationOptions; | ||
import com.linecorp.armeria.common.HttpData; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.ResponseHeaders; | ||
import com.linecorp.armeria.common.encoding.StreamDecoderFactory; | ||
import com.linecorp.armeria.server.AbstractHttpService; | ||
import com.linecorp.armeria.server.ServerBuilder; | ||
import com.linecorp.armeria.server.ServerConfigurator; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
import com.linecorp.armeria.server.encoding.DecodingService; | ||
import io.netty.buffer.ByteBufAllocator; | ||
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest; | ||
import zipkin2.Callback; | ||
import zipkin2.Span; | ||
import zipkin2.collector.Collector; | ||
import zipkin2.collector.CollectorComponent; | ||
import zipkin2.collector.CollectorMetrics; | ||
|
@@ -18,32 +36,40 @@ | |
|
||
public final class OpenTelemetryHttpCollector extends CollectorComponent | ||
implements ServerConfigurator { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static final class Builder extends CollectorComponent.Builder { | ||
|
||
Collector.Builder delegate = Collector.newBuilder(OpenTelemetryHttpCollector.class); | ||
|
||
CollectorMetrics metrics = CollectorMetrics.NOOP_METRICS; | ||
|
||
@Override public Builder storage(StorageComponent storageComponent) { | ||
@Override | ||
public Builder storage(StorageComponent storageComponent) { | ||
delegate.storage(storageComponent); | ||
return this; | ||
} | ||
|
||
@Override public Builder metrics(CollectorMetrics metrics) { | ||
if (metrics == null) throw new NullPointerException("metrics == null"); | ||
@Override | ||
public Builder metrics(CollectorMetrics metrics) { | ||
if (metrics == null) { | ||
throw new NullPointerException("metrics == null"); | ||
} | ||
delegate.metrics(this.metrics = metrics.forTransport("otel/http")); | ||
return this; | ||
} | ||
|
||
@Override public Builder sampler(CollectorSampler sampler) { | ||
@Override | ||
public Builder sampler(CollectorSampler sampler) { | ||
delegate.sampler(sampler); | ||
return this; | ||
} | ||
|
||
@Override public OpenTelemetryHttpCollector build() { | ||
@Override | ||
public OpenTelemetryHttpCollector build() { | ||
return new OpenTelemetryHttpCollector(this); | ||
} | ||
|
||
|
@@ -52,38 +78,87 @@ public static final class Builder extends CollectorComponent.Builder { | |
} | ||
|
||
final Collector collector; | ||
|
||
final CollectorMetrics metrics; | ||
|
||
OpenTelemetryHttpCollector(Builder builder) { | ||
collector = builder.delegate.build(); | ||
metrics = builder.metrics; | ||
} | ||
|
||
@Override public OpenTelemetryHttpCollector start() { | ||
@Override | ||
public OpenTelemetryHttpCollector start() { | ||
return this; | ||
} | ||
|
||
@Override public String toString() { | ||
@Override | ||
public String toString() { | ||
return "OpenTelemetryHttpCollector{}"; | ||
} | ||
|
||
/** | ||
* Reconfigures the service per https://opentelemetry.io/docs/specs/otlp/#otlphttp-request | ||
*/ | ||
@Override public void reconfigure(ServerBuilder sb) { | ||
@Override | ||
public void reconfigure(ServerBuilder sb) { | ||
sb.decorator(DecodingService.newDecorator(StreamDecoderFactory.gzip())); | ||
sb.service("/v1/traces", new HttpService(this)); | ||
} | ||
|
||
static final class HttpService extends AbstractHttpService { | ||
private static final Logger LOG = Logger.getLogger(HttpService.class.getName()); | ||
|
||
final OpenTelemetryHttpCollector collector; | ||
|
||
HttpService(OpenTelemetryHttpCollector collector) { | ||
this.collector = collector; | ||
} | ||
|
||
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) | ||
throws Exception { | ||
throw new RuntimeException("Implement me!"); | ||
@Override | ||
protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) { | ||
CompletableCallback result = new CompletableCallback(); | ||
req.aggregate(AggregationOptions.usePooledObjects(ByteBufAllocator.DEFAULT, ctx.eventLoop() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
making marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)).handle((msg, t) -> { | ||
if (t != null) { | ||
result.onError(t); | ||
return null; | ||
} | ||
try (HttpData content = msg.content()) { | ||
if (content.isEmpty()) { | ||
result.onSuccess(null); | ||
return null; | ||
} | ||
|
||
try { | ||
ExportTraceServiceRequest request = ExportTraceServiceRequest.parseFrom(CodedInputStream.newInstance(content.byteBuf().nioBuffer())); | ||
making marked this conversation as resolved.
Show resolved
Hide resolved
|
||
List<Span> spans = SpanTranslator.translate(request); | ||
collector.collector.accept(spans, result); | ||
} | ||
catch (IOException e) { | ||
LOG.log(Level.WARNING, "Unable to parse the request:", e); | ||
throw new UncheckedIOException(e); | ||
making marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return null; | ||
} | ||
}); | ||
return HttpResponse.of(result); | ||
} | ||
} | ||
|
||
static final class CompletableCallback extends CompletableFuture<HttpResponse> | ||
implements Callback<Void> { | ||
|
||
static final ResponseHeaders ACCEPTED_RESPONSE = ResponseHeaders.of(HttpStatus.ACCEPTED); | ||
|
||
@Override | ||
public void onSuccess(Void value) { | ||
complete(HttpResponse.of(ACCEPTED_RESPONSE)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
completeExceptionally(t); | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
collector-http/src/main/java/zipkin2/collector/otel/http/ProtoUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package zipkin2.collector.otel.http; | ||
|
||
import java.util.List; | ||
|
||
import com.google.protobuf.TextFormat; | ||
import io.opentelemetry.proto.common.v1.AnyValue; | ||
import io.opentelemetry.proto.common.v1.KeyValue; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
final class ProtoUtils { | ||
static String kvListToJson(List<KeyValue> attributes) { | ||
return attributes.stream() | ||
.map(entry -> "\"" + entry.getKey() + "\":" + valueToJson(entry.getValue())) | ||
.collect(joining(",", "{", "}")); | ||
} | ||
|
||
static String valueToString(AnyValue value) { | ||
if (value.hasStringValue()) { | ||
return value.getStringValue(); | ||
} | ||
return valueToJson(value); | ||
} | ||
|
||
static String valueToJson(AnyValue value) { | ||
if (value.hasStringValue()) { | ||
return "\"" + value.getStringValue() + "\""; | ||
} | ||
if (value.hasArrayValue()) { | ||
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/README.md#attribute | ||
return value.getArrayValue().getValuesList().stream() | ||
.map(ProtoUtils::valueToJson) | ||
.collect(joining(",", "[", "]")); | ||
} | ||
if (value.hasKvlistValue()) { | ||
return kvListToJson(value.getKvlistValue().getValuesList()); | ||
} | ||
if (value.hasBoolValue()) { | ||
return String.valueOf(value.getBoolValue()); | ||
} | ||
if (value.hasDoubleValue()) { | ||
return String.valueOf(value.getDoubleValue()); | ||
} | ||
if (value.hasIntValue()) { | ||
return String.valueOf(value.getIntValue()); | ||
} | ||
if (value.hasBytesValue()) { | ||
// TODO | ||
return TextFormat.escapeBytes(value.getBytesValue()); | ||
} | ||
return value.toString(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to name this similar to the sender?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate this?