Skip to content

Commit

Permalink
Decouple session id name (#505)
Browse files Browse the repository at this point in the history
* Make default attribute "rum.session.id".

* spotless

* keep existing attribute

* spotless
  • Loading branch information
breedx-splk authored Mar 24, 2023
1 parent 69f92cf commit 7ff0dc2
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.rum.internal.RumConstants;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.DelegatingSpanData;
import io.opentelemetry.sdk.trace.data.EventData;
Expand Down Expand Up @@ -81,6 +82,10 @@ private SpanData modify(SpanData original) {
List<EventData> modifiedEvents = new ArrayList<>(original.getEvents().size());
AttributesBuilder modifiedAttributes = original.getAttributes().toBuilder();

// Copy the native session id name into the splunk name
String sessionId = original.getAttributes().get(RumConstants.SESSION_ID_KEY);
modifiedAttributes.put(StandardAttributes.SESSION_ID_KEY, sessionId);

SpanContext spanContext;
if (reactNativeEnabled) {
spanContext = extractReactNativeIdsIfPresent(original);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.splunk.rum;

import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.rum.internal.SpanFilterBuilder;
Expand All @@ -31,16 +33,15 @@ public final class StandardAttributes {
*
* @see SplunkRumBuilder#setGlobalAttributes(Attributes)
*/
public static final AttributeKey<String> APP_VERSION = AttributeKey.stringKey("app.version");
public static final AttributeKey<String> APP_VERSION = stringKey("app.version");

/**
* The build type of your app (typically one of debug or release). Useful for adding to global
* attributes.
*
* @see SplunkRumBuilder#setGlobalAttributes(Attributes)
*/
public static final AttributeKey<String> APP_BUILD_TYPE =
AttributeKey.stringKey("app.build.type");
public static final AttributeKey<String> APP_BUILD_TYPE = stringKey("app.build.type");

/**
* Full HTTP client request URL in the form {@code scheme://host[:port]/path?query[#fragment]}.
Expand All @@ -50,5 +51,8 @@ public final class StandardAttributes {
*/
public static final AttributeKey<String> HTTP_URL = SemanticAttributes.HTTP_URL;

public static final AttributeKey<? super String> SESSION_ID_KEY =
stringKey("splunk.rumSessionId");

private StandardAttributes() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class RumConstants {

public static final String OTEL_RUM_LOG_TAG = "OpenTelemetryRum";

public static final AttributeKey<String> SESSION_ID_KEY = stringKey("rum.session.id");

public static final AttributeKey<String> LAST_SCREEN_NAME_KEY =
AttributeKey.stringKey("last.screen.name");
public static final AttributeKey<String> SCREEN_NAME_KEY =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package io.opentelemetry.rum.internal;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.rum.internal.RumConstants.SESSION_ID_KEY;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

final class SessionIdSpanAppender implements SpanProcessor {

// TODO: rename to something that is not splunk specific
static final AttributeKey<String> SESSION_ID_KEY = stringKey("splunk.rumSessionId");

private final SessionId sessionId;

public SessionIdSpanAppender(SessionId sessionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.rum.internal.RumConstants;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.trace.TestSpanData;
Expand All @@ -54,16 +56,30 @@ class SplunkSpanDataModifierTest {
@Mock private SpanExporter delegate;
@Captor private ArgumentCaptor<Collection<SpanData>> exportedSpansCaptor;

@Test
void changesSpanIdAttrName() {
String sessionId = "abc123fonzie";
Attributes attrs = Attributes.of(RumConstants.SESSION_ID_KEY, sessionId);
SpanData original = startBuilder().setAttributes(attrs).build();

CompletableResultCode exportResult = CompletableResultCode.ofSuccess();
when(delegate.export(exportedSpansCaptor.capture())).thenReturn(exportResult);

SplunkSpanDataModifier underTest = new SplunkSpanDataModifier(delegate, false);
underTest.export(singletonList(original));

Collection<SpanData> exported = exportedSpansCaptor.getValue();
assertThat(exported).hasSize(1);
SpanData first = exported.iterator().next();
assertThat(first.getAttributes().get(StandardAttributes.SESSION_ID_KEY))
.isEqualTo(sessionId);
assertThat(first.getAttributes().get(RumConstants.SESSION_ID_KEY)).isEqualTo(sessionId);
}

@Test
void shouldConvertExceptionEventsToSpanAttributes() {
SpanData original =
TestSpanData.builder()
.setName("test")
.setKind(SpanKind.CLIENT)
.setStatus(StatusData.unset())
.setStartEpochNanos(12345)
.setEndEpochNanos(67890)
.setHasEnded(true)
startBuilder()
.setEvents(
Arrays.asList(
EventData.create(
Expand Down Expand Up @@ -119,15 +135,7 @@ void shouldConvertExceptionEventsToSpanAttributes() {

@Test
void shouldSetCaseSensitiveSpanNameToAttribute() {
SpanData original =
TestSpanData.builder()
.setName("SplunkRumSpan")
.setKind(SpanKind.CLIENT)
.setStatus(StatusData.unset())
.setStartEpochNanos(12345)
.setEndEpochNanos(67890)
.setHasEnded(true)
.build();
SpanData original = startBuilder("SplunkRumSpan").build();

CompletableResultCode exportResult = CompletableResultCode.ofSuccess();
when(delegate.export(exportedSpansCaptor.capture())).thenReturn(exportResult);
Expand Down Expand Up @@ -206,14 +214,8 @@ void shouldIgnoreReactIdsIfReactNativeSupportIsDisabled() {
TraceState.getDefault());

SpanData original =
TestSpanData.builder()
startBuilder("SplunkRumSpan")
.setSpanContext(spanContext)
.setName("SplunkRumSpan")
.setKind(SpanKind.CLIENT)
.setStatus(StatusData.unset())
.setStartEpochNanos(12345)
.setEndEpochNanos(67890)
.setHasEnded(true)
.setAttributes(
Attributes.builder()
.put(
Expand Down Expand Up @@ -286,4 +288,18 @@ void shouldReplaceTraceAndSpanIdWithReactNativeIds() {
.hasSpanId("8888888888888888")
.hasAttributesSatisfyingExactly(equalTo(SPLUNK_OPERATION_KEY, "SplunkRumSpan"));
}

private TestSpanData.Builder startBuilder() {
return startBuilder("test");
}

private TestSpanData.Builder startBuilder(String name) {
return TestSpanData.builder()
.setName(name)
.setKind(SpanKind.CLIENT)
.setStatus(StatusData.unset())
.setStartEpochNanos(12345)
.setEndEpochNanos(67890)
.setHasEnded(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.opentelemetry.rum.internal;

import static io.opentelemetry.rum.internal.RumConstants.SESSION_ID_KEY;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.mockito.ArgumentMatchers.isA;
Expand Down Expand Up @@ -80,8 +81,7 @@ void shouldBuildTracerProvider() {
assertThat(spans.get(0))
.hasName("test span")
.hasResource(resource)
.hasAttributesSatisfyingExactly(
equalTo(SessionIdSpanAppender.SESSION_ID_KEY, sessionId));
.hasAttributesSatisfyingExactly(equalTo(SESSION_ID_KEY, sessionId));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.opentelemetry.rum.internal;

import static io.opentelemetry.rum.internal.RumConstants.SESSION_ID_KEY;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -43,7 +44,7 @@ void shouldSetSessionIdAsSpanAttribute() {
assertTrue(underTest.isStartRequired());
underTest.onStart(Context.root(), span);

verify(span).setAttribute(SessionIdSpanAppender.SESSION_ID_KEY, "42");
verify(span).setAttribute(SESSION_ID_KEY, "42");

assertFalse(underTest.isEndRequired());
}
Expand Down

0 comments on commit 7ff0dc2

Please sign in to comment.