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

Add SpanData.Builder to allow modifying the information in a SpanData… #1502

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions exporters/inmemory/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
api project(':opentelemetry-sdk')

annotationProcessor libraries.auto_value
testImplementation project(':opentelemetry-testing-internal')

signature "org.codehaus.mojo.signature:java17:1.0@signature"
signature "net.sf.androidscents.signature:android-api-level-24:7.0_r2@signature"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.TracerSdkProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span.Kind;
Expand Down
2 changes: 2 additions & 0 deletions exporters/jaeger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dependencies {
libraries.protobuf,
libraries.protobuf_util

testImplementation project(':opentelemetry-testing-internal')

testImplementation "io.grpc:grpc-testing:${grpcVersion}",
libraries.testcontainers,
libraries.awaitility,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import io.opentelemetry.exporters.jaeger.proto.api_v2.Model;
import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.SpanData.Event;
import io.opentelemetry.sdk.trace.data.SpanData.Link;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import io.opentelemetry.exporters.jaeger.proto.api_v2.Model;
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.Status;
Expand Down
2 changes: 2 additions & 0 deletions exporters/logging/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ext.moduleName = "io.opentelemetry.exporters.logging"
dependencies {
api project(':opentelemetry-sdk')

testImplementation project(':opentelemetry-testing-internal')

signature "org.codehaus.mojo.signature:java17:1.0@signature"
signature "net.sf.androidscents.signature:android-api-level-24:7.0_r2@signature"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
Expand Down
2 changes: 2 additions & 0 deletions exporters/otlp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
libraries.protobuf,
libraries.protobuf_util

testImplementation project(':opentelemetry-testing-internal')

testImplementation "io.grpc:grpc-testing:${grpcVersion}"
testRuntime "io.grpc:grpc-netty-shaded:${grpcVersion}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse;
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
import io.opentelemetry.proto.trace.v1.ResourceSpans;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import io.opentelemetry.proto.trace.v1.Span.SpanKind;
import io.opentelemetry.proto.trace.v1.Status;
import io.opentelemetry.proto.trace.v1.Status.StatusCode;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData.Link;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
Expand Down
2 changes: 2 additions & 0 deletions exporters/zipkin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
implementation libraries.zipkin_reporter,
libraries.zipkin_okhttp

testImplementation project(':opentelemetry-testing-internal')

testImplementation libraries.guava,
libraries.zipkin_junit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import com.google.common.collect.ImmutableList;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.SpanData.Event;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
Expand Down Expand Up @@ -149,7 +149,7 @@ private void exportAndVerify(ZipkinSpanExporter zipkinSpanExporter) {
assertThat(zipkinSpans.get(0)).isEqualTo(buildZipkinSpan());
}

private static TestSpanData.Builder buildStandardSpan() {
private static SpanData.Builder buildStandardSpan() {
return TestSpanData.newBuilder()
.setTraceId(TraceId.fromLowerBase16(TRACE_ID, 0))
.setSpanId(SpanId.fromLowerBase16(SPAN_ID, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.SpanData.Event;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
Expand Down Expand Up @@ -271,7 +271,7 @@ void testShutdown() throws IOException {
verify(mockSender).close();
}

private static TestSpanData.Builder buildStandardSpan() {
private static SpanData.Builder buildStandardSpan() {
return TestSpanData.newBuilder()
.setTraceId(TraceId.fromLowerBase16(TRACE_ID, 0))
.setSpanId(SpanId.fromLowerBase16(SPAN_ID, 0))
Expand Down
3 changes: 3 additions & 0 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ dependencies {

testImplementation libraries.junit_pioneer

testImplementation project(':opentelemetry-testing-internal')
jmh project(':opentelemetry-testing-internal')

signature "org.codehaus.mojo.signature:java17:1.0@signature"
signature "net.sf.androidscents.signature:android-api-level-24:7.0_r2@signature"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package io.opentelemetry.sdk.trace.export;

import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.test.TestSpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanId;
Expand Down Expand Up @@ -73,7 +73,7 @@ public final void setup() {
Arrays.fill(exporter, new NoopSpanExporter());
this.exporter = MultiSpanExporter.create(Arrays.asList(exporter));

TestSpanData[] spans = new TestSpanData[spanCount];
SpanData[] spans = new SpanData[spanCount];
for (int i = 0; i < spans.length; i++) {
spans[i] =
TestSpanData.newBuilder()
Expand Down
179 changes: 179 additions & 0 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanDataImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2020, OpenTelemetry 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 io.opentelemetry.sdk.trace;

import com.google.auto.value.AutoValue;
import io.opentelemetry.common.ReadableAttributes;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.Status;
import io.opentelemetry.trace.TraceFlags;
import io.opentelemetry.trace.TraceId;
import io.opentelemetry.trace.TraceState;
import java.util.List;
import javax.annotation.concurrent.Immutable;

@Immutable
@AutoValue
// AutoValue generated hashCode is fine but we need to define equals to accept the base SpanData
// type.
@SuppressWarnings("EqualsHashCode")
abstract class SpanDataImpl implements SpanData {

static SpanData.Builder newBuilder(SpanData spanData) {
return new AutoValue_SpanDataImpl.Builder()
.setTraceId(spanData.getTraceId())
.setSpanId(spanData.getSpanId())
.setTraceFlags(spanData.getTraceFlags())
.setTraceState(spanData.getTraceState())
.setParentSpanId(spanData.getParentSpanId())
.setResource(spanData.getResource())
.setInstrumentationLibraryInfo(spanData.getInstrumentationLibraryInfo())
.setName(spanData.getName())
.setKind(spanData.getKind())
.setStartEpochNanos(spanData.getStartEpochNanos())
.setAttributes(spanData.getAttributes())
.setEvents(spanData.getEvents())
.setLinks(spanData.getLinks())
.setStatus(spanData.getStatus())
.setEndEpochNanos(spanData.getEndEpochNanos())
.setHasRemoteParent(spanData.getHasRemoteParent())
.setHasEnded(spanData.getHasEnded())
.setTotalRecordedEvents(spanData.getTotalRecordedEvents())
.setTotalRecordedLinks(spanData.getTotalRecordedLinks())
.setTotalAttributeCount(spanData.getTotalAttributeCount());
}

@Override
public final SpanData.Builder toBuilder() {
return autoToBuilder();
}

abstract Builder autoToBuilder();

// AutoValue won't generate equals that compares with SpanData interface.
@Override
public final boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof SpanData) {
SpanData that = (SpanData) o;
return getTraceId().equals(that.getTraceId())
&& getSpanId().equals(that.getSpanId())
&& getTraceFlags().equals(that.getTraceFlags())
&& getTraceState().equals(that.getTraceState())
&& getParentSpanId().equals(that.getParentSpanId())
&& getResource().equals(that.getResource())
&& getInstrumentationLibraryInfo().equals(that.getInstrumentationLibraryInfo())
&& getName().equals(that.getName())
&& getKind().equals(that.getKind())
&& getStartEpochNanos() == that.getStartEpochNanos()
&& getAttributes().equals(that.getAttributes())
&& getEvents().equals(that.getEvents())
&& getLinks().equals(that.getLinks())
&& getStatus().equals(that.getStatus())
&& getEndEpochNanos() == that.getEndEpochNanos()
&& getHasRemoteParent() == that.getHasRemoteParent()
&& getHasEnded() == that.getHasEnded()
&& getTotalRecordedEvents() == that.getTotalRecordedEvents()
&& getTotalRecordedLinks() == that.getTotalRecordedLinks()
&& getTotalAttributeCount() == that.getTotalAttributeCount();
}
return false;
}

/**
* A {@code Builder} class for {@link SpanDataImpl}.
*
* @since 0.1.0
*/
@AutoValue.Builder
abstract static class Builder implements SpanData.Builder {

@Override
public final SpanData build() {
return autoBuild();
}

abstract SpanDataImpl autoBuild();

@Override
public abstract Builder setTraceId(TraceId traceId);

@Override
public abstract Builder setSpanId(SpanId spanId);

@Override
public abstract Builder setTraceFlags(TraceFlags traceFlags);

@Override
public abstract Builder setTraceState(TraceState traceState);

@Override
public abstract Builder setParentSpanId(SpanId parentSpanId);

@Override
public abstract Builder setResource(Resource resource);

@Override
public abstract Builder setInstrumentationLibraryInfo(
InstrumentationLibraryInfo instrumentationLibraryInfo);

@Override
public abstract Builder setName(String name);

@Override
public abstract Builder setStartEpochNanos(long epochNanos);

@Override
public abstract Builder setEndEpochNanos(long epochNanos);

@Override
public abstract Builder setAttributes(ReadableAttributes attributes);

@Override
public abstract Builder setEvents(List<Event> events);

@Override
public abstract Builder setStatus(Status status);

@Override
public abstract Builder setKind(Kind kind);

@Override
public abstract Builder setLinks(List<Link> links);

@Override
public abstract Builder setHasRemoteParent(boolean hasRemoteParent);

@Override
public abstract Builder setHasEnded(boolean hasEnded);

@Override
public abstract Builder setTotalRecordedEvents(int totalRecordedEvents);

@Override
public abstract Builder setTotalRecordedLinks(int totalRecordedLinks);

@Override
public abstract Builder setTotalAttributeCount(int totalAttributeCount);
}
}
5 changes: 5 additions & 0 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ static SpanWrapper create(
hasEnded);
}

@Override
public SpanData.Builder toBuilder() {
return SpanDataImpl.newBuilder(this);
}

@Override
public TraceId getTraceId() {
return delegate().getContext().getTraceId();
Expand Down
Loading