Skip to content

Commit

Permalink
fix semconv usages and convert new net names to old names.
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk committed Oct 13, 2023
1 parent 4b4af90 commit ff52535
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
import static io.opentelemetry.semconv.SemanticAttributes.EXCEPTION_MESSAGE;
import static io.opentelemetry.semconv.SemanticAttributes.EXCEPTION_STACKTRACE;
import static io.opentelemetry.semconv.SemanticAttributes.EXCEPTION_TYPE;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CARRIER_ICC;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CARRIER_MCC;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CARRIER_MNC;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CARRIER_NAME;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CONNECTION_SUBTYPE;
import static io.opentelemetry.semconv.SemanticAttributes.NETWORK_CONNECTION_TYPE;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CARRIER_ICC;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CARRIER_MCC;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CARRIER_MNC;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CARRIER_NAME;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE;
import static io.opentelemetry.semconv.SemanticAttributes.NET_HOST_CONNECTION_TYPE;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;

Expand Down Expand Up @@ -103,6 +115,9 @@ private SpanData modify(SpanData original) {
spanContext = original.getSpanContext();
}

// Convert new net semconv to old
modifiedAttributes = downgradeNetworkAttrNames(original.getAttributes(), modifiedAttributes);

// zipkin eats the event attributes that are recorded by default, so we need to convert
// the exception event to span attributes
for (EventData event : original.getEvents()) {
Expand All @@ -129,6 +144,19 @@ private SpanData modify(SpanData original) {
return new SplunkSpan(original, spanContext, modifiedEvents, modifiedAttributes.build());
}

// At least until we can leverage the new names...
private AttributesBuilder downgradeNetworkAttrNames(Attributes originalAttributes,
AttributesBuilder attributes) {
return AttributeReplacer.with(originalAttributes, attributes)
.update(NETWORK_CONNECTION_TYPE, NET_HOST_CONNECTION_TYPE)
.update(NETWORK_CONNECTION_SUBTYPE, NET_HOST_CONNECTION_SUBTYPE)
.update(NETWORK_CARRIER_ICC, NET_HOST_CARRIER_ICC)
.update(NETWORK_CARRIER_MCC, NET_HOST_CARRIER_MCC)
.update(NETWORK_CARRIER_MNC, NET_HOST_CARRIER_MNC)
.update(NETWORK_CARRIER_NAME, NET_HOST_CARRIER_NAME)
.finish();
}

private SpanContext extractReactNativeIdsIfPresent(SpanData original) {
Attributes attributes = original.getAttributes();
SpanContext originalSpanContext = original.getSpanContext();
Expand Down Expand Up @@ -230,4 +258,31 @@ public int getTotalAttributeCount() {
return modifiedAttributes.size();
}
}

private static class AttributeReplacer {
private final Attributes original;
private final AttributesBuilder attributes;

private static AttributeReplacer with(Attributes original, AttributesBuilder attributes) {
return new AttributeReplacer(original, attributes);
}

private AttributeReplacer(Attributes original, AttributesBuilder attributes) {
this.original = original;
this.attributes = attributes;
}

<T> AttributeReplacer update(AttributeKey<T> currentName, AttributeKey<T> replacementName){
T value = original.get(currentName);
if(value != null){
attributes.remove(currentName);
attributes.put(replacementName, value);
}
return this;
}

AttributesBuilder finish(){
return attributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.semconv.ResourceAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit ff52535

Please sign in to comment.