Skip to content

Commit

Permalink
Refactor react native classes (#385)
Browse files Browse the repository at this point in the history
* Refactor React Native utils and merge them into SplunkSpanDataModifier

* Update splunk-otel-android/src/main/java/com/splunk/rum/SplunkRumBuilder.java

Co-authored-by: jason plumb <[email protected]>

* junit5

Co-authored-by: jason plumb <[email protected]>
  • Loading branch information
Mateusz Rzeszutek and breedx-splk authored Oct 19, 2022
1 parent 507a793 commit 794e903
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,11 @@ private Resource buildResource(String applicationName, String rumVersion) {
// visible for testing
SpanExporter buildFilteringExporter(ConnectionUtil connectionUtil) {
SpanExporter exporter = buildExporter(connectionUtil);
SpanExporter splunkTranslatedExporter = new SplunkSpanDataModifier(exporter);
SpanExporter splunkTranslatedExporter =
new SplunkSpanDataModifier(exporter, builder.reactNativeSupportEnabled);
SpanExporter filteredExporter = builder.decorateWithSpanFilter(splunkTranslatedExporter);
initializationEvents.add(
new InitializationEvent("zipkin exporter initialized", timingClock.now()));

if (builder.reactNativeSupportEnabled) {
return builder.decorateWithReactNativeExporter(filteredExporter);
}
return filteredExporter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.app.Application;
import android.util.Log;
import androidx.annotation.Nullable;
import com.splunk.rum.reactnative.ReactNativeExporter;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.time.Duration;
Expand All @@ -39,6 +38,7 @@ public final class SplunkRumBuilder {
@Nullable private String realm;
boolean debugEnabled = false;
boolean diskBufferingEnabled = false;
boolean reactNativeSupportEnabled = false;
boolean crashReportingEnabled = true;
boolean networkMonitorEnabled = true;
boolean anrDetectionEnabled = true;
Expand All @@ -50,7 +50,6 @@ public final class SplunkRumBuilder {
int maxUsageMegabytes = DEFAULT_MAX_STORAGE_USE_MB;
boolean sessionBasedSamplerEnabled = false;
double sessionBasedSamplerRatio = 1.0;
boolean reactNativeSupportEnabled = false;

/**
* Sets the application name that will be used to identify your application in the Splunk RUM
Expand Down Expand Up @@ -139,6 +138,13 @@ public SplunkRumBuilder enableDiskBuffering() {
return this;
}

/**
* Enables support for the React Native instrumentation.
*
* <p>This feature is disabled by default. You can enable it by calling this method.
*
* @return {@code this}
*/
public SplunkRumBuilder enableReactNativeSupport() {
this.reactNativeSupportEnabled = true;
return this;
Expand Down Expand Up @@ -315,8 +321,4 @@ public SplunkRum build(Application application) {
SpanExporter decorateWithSpanFilter(SpanExporter exporter) {
return spanFilterBuilder.build().apply(exporter);
}

public SpanExporter decorateWithReactNativeExporter(SpanExporter exporter) {
return new ReactNativeExporter(exporter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.DelegatingSpanData;
import io.opentelemetry.sdk.trace.data.EventData;
Expand All @@ -45,6 +46,10 @@
final class SplunkSpanDataModifier implements SpanExporter {

static final AttributeKey<String> SPLUNK_OPERATION_KEY = stringKey("_splunk_operation");
static final AttributeKey<String> REACT_NATIVE_TRACE_ID_KEY =
AttributeKey.stringKey("_reactnative_traceId");
static final AttributeKey<String> REACT_NATIVE_SPAN_ID_KEY =
AttributeKey.stringKey("_reactnative_spanId");

private static final Set<AttributeKey<String>> resourceAttributesToCopy =
unmodifiableSet(
Expand All @@ -60,9 +65,11 @@ final class SplunkSpanDataModifier implements SpanExporter {
SplunkRum.RUM_VERSION_KEY)));

private final SpanExporter delegate;
private final boolean reactNativeEnabled;

SplunkSpanDataModifier(SpanExporter delegate) {
SplunkSpanDataModifier(SpanExporter delegate, boolean reactNativeEnabled) {
this.delegate = delegate;
this.reactNativeEnabled = reactNativeEnabled;
}

@Override
Expand All @@ -74,6 +81,15 @@ private SpanData modify(SpanData original) {
List<EventData> modifiedEvents = new ArrayList<>(original.getEvents().size());
AttributesBuilder modifiedAttributes = original.getAttributes().toBuilder();

SpanContext spanContext;
if (reactNativeEnabled) {
spanContext = extractReactNativeIdsIfPresent(original);
modifiedAttributes.remove(REACT_NATIVE_TRACE_ID_KEY);
modifiedAttributes.remove(REACT_NATIVE_SPAN_ID_KEY);
} else {
spanContext = original.getSpanContext();
}

// 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 @@ -97,7 +113,30 @@ private SpanData modify(SpanData original) {
}
}

return new SplunkSpan(original, modifiedEvents, modifiedAttributes.build());
return new SplunkSpan(original, spanContext, modifiedEvents, modifiedAttributes.build());
}

private SpanContext extractReactNativeIdsIfPresent(SpanData original) {
Attributes attributes = original.getAttributes();
SpanContext originalSpanContext = original.getSpanContext();

String reactNativeTraceId = attributes.get(REACT_NATIVE_TRACE_ID_KEY);
String reactNativeSpanId = attributes.get(REACT_NATIVE_SPAN_ID_KEY);
if (reactNativeTraceId == null || reactNativeSpanId == null) {
return originalSpanContext;
}

return originalSpanContext.isRemote()
? SpanContext.createFromRemoteParent(
reactNativeTraceId,
reactNativeSpanId,
originalSpanContext.getTraceFlags(),
originalSpanContext.getTraceState())
: SpanContext.create(
reactNativeTraceId,
reactNativeSpanId,
originalSpanContext.getTraceFlags(),
originalSpanContext.getTraceState());
}

private static Attributes extractExceptionAttributes(EventData event) {
Expand Down Expand Up @@ -138,16 +177,26 @@ public CompletableResultCode shutdown() {

private static final class SplunkSpan extends DelegatingSpanData {

private final SpanContext spanContext;
private final List<EventData> modifiedEvents;
private final Attributes modifiedAttributes;

private SplunkSpan(
SpanData delegate, List<EventData> modifiedEvents, Attributes modifiedAttributes) {
SpanData delegate,
SpanContext spanContext,
List<EventData> modifiedEvents,
Attributes modifiedAttributes) {
super(delegate);
this.spanContext = spanContext;
this.modifiedEvents = modifiedEvents;
this.modifiedAttributes = modifiedAttributes;
}

@Override
public SpanContext getSpanContext() {
return spanContext;
}

@Override
public List<EventData> getEvents() {
return modifiedEvents;
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 794e903

Please sign in to comment.