From ff5253543ae44ce6554fcc82dfcc1a963cb60c50 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 13 Oct 2023 10:36:00 -0700 Subject: [PATCH] fix semconv usages and convert new net names to old names. --- .../splunk/rum/SplunkSpanDataModifier.java | 55 +++++++++++++++++++ .../com/splunk/rum/LogToSpanBridgeTest.java | 2 +- .../com/splunk/rum/RumInitializerTest.java | 2 +- .../rum/SplunkSpanDataModifierTest.java | 4 +- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/SplunkSpanDataModifier.java b/splunk-otel-android/src/main/java/com/splunk/rum/SplunkSpanDataModifier.java index 20355ea7..5f7ffb78 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/SplunkSpanDataModifier.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/SplunkSpanDataModifier.java @@ -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; @@ -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()) { @@ -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(); @@ -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; + } + + AttributeReplacer update(AttributeKey currentName, AttributeKey replacementName){ + T value = original.get(currentName); + if(value != null){ + attributes.remove(currentName); + attributes.put(replacementName, value); + } + return this; + } + + AttributesBuilder finish(){ + return attributes; + } + } } diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/LogToSpanBridgeTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/LogToSpanBridgeTest.java index ebec61de..9ab98f89 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/LogToSpanBridgeTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/LogToSpanBridgeTest.java @@ -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; diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java index 129f00f8..c2ff8f61 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/RumInitializerTest.java @@ -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; diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/SplunkSpanDataModifierTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/SplunkSpanDataModifierTest.java index 7b0033a5..84161817 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/SplunkSpanDataModifierTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/SplunkSpanDataModifierTest.java @@ -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;