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

Leverage upstream improvements #710

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.splunk.rum.internal.GlobalAttributesSupplier;
import com.splunk.rum.internal.UInt32QuadXorTraceIdRatioSampler;
import io.opentelemetry.android.GlobalAttributesSpanAppender;
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.OpenTelemetryRumBuilder;
import io.opentelemetry.android.RuntimeDetailsExtractor;
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.anr.AnrDetector;
import io.opentelemetry.android.instrumentation.crash.CrashReporter;
Expand Down Expand Up @@ -92,7 +93,13 @@ SplunkRum initialize(
VisibleScreenTracker visibleScreenTracker = new VisibleScreenTracker();

initializationEvents.begin();
OpenTelemetryRumBuilder otelRumBuilder = OpenTelemetryRum.builder(application);

OtelRumConfig config = new OtelRumConfig();
GlobalAttributesSupplier globalAttributeSupplier =
new GlobalAttributesSupplier(builder.globalAttributes);
config.setGlobalAttributes(globalAttributeSupplier);

OpenTelemetryRumBuilder otelRumBuilder = OpenTelemetryRum.builder(application, config);

otelRumBuilder.mergeResource(createSplunkResource());
initializationEvents.emit("resourceInitialized");
Expand All @@ -104,14 +111,6 @@ SplunkRum initialize(
// TODO: How truly important is the order of these span processors? The location of event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I'll remove it in #713. 👍🏻

// generation should probably not be altered...

GlobalAttributesSpanAppender globalAttributesSpanAppender =
GlobalAttributesSpanAppender.create(builder.globalAttributes);

// Add span processor that appends global attributes.
otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) ->
tracerProviderBuilder.addSpanProcessor(globalAttributesSpanAppender));

// Add span processor that appends network attributes.
otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
Expand All @@ -120,15 +119,6 @@ SplunkRum initialize(
return tracerProviderBuilder.addSpanProcessor(networkAttributesSpanAppender);
});

// Add span processor that appends screen attributes and generate init event.
otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
ScreenAttributesAppender screenAttributesAppender =
new ScreenAttributesAppender(visibleScreenTracker);
initializationEvents.emit("attributeAppenderInitialized");
return tracerProviderBuilder.addSpanProcessor(screenAttributesAppender);
});

// Add batch span processor
otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
Expand Down Expand Up @@ -225,7 +215,7 @@ SplunkRum initialize(
builder.getConfigFlags(),
openTelemetryRum.getOpenTelemetry().getTracer(RUM_TRACER_NAME));

return new SplunkRum(openTelemetryRum, globalAttributesSpanAppender);
return new SplunkRum(openTelemetryRum, globalAttributeSupplier);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import android.util.Log;
import android.webkit.WebView;
import androidx.annotation.Nullable;
import io.opentelemetry.android.GlobalAttributesSpanAppender;
import com.splunk.rum.internal.GlobalAttributesSupplier;
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider;
import io.opentelemetry.android.instrumentation.startup.AppStartupTimer;
Expand Down Expand Up @@ -73,14 +73,14 @@ public class SplunkRum {
@Nullable private static SplunkRum INSTANCE;

private final OpenTelemetryRum openTelemetryRum;
private final GlobalAttributesSpanAppender globalAttributes;
private final GlobalAttributesSupplier globalAttributes;

static {
Handler handler = new Handler(Looper.getMainLooper());
startupTimer.detectBackgroundStart(handler);
}

SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSpanAppender globalAttributes) {
SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSupplier globalAttributes) {
this.openTelemetryRum = openTelemetryRum;
this.globalAttributes = globalAttributes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Splunk Inc.
*
* 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 com.splunk.rum.internal;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* Class to hold and update the set of global attributes that are appended to each span. This is
* used as the supplier to the GlobalAttributesSpanAppender. We need this because SplunkRum exposes
* a topmost update() method, and we can't break that contract, and there's no way to get a
* reference to the GlobalAttributesSpanAppender created by OtelRum.
*
* <p>When global attributes are more fleshed out in upstream, this will hopefully improve or go
* away.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class GlobalAttributesSupplier implements Supplier<Attributes> {
private Attributes attributes;

public GlobalAttributesSupplier(Attributes globalAttributes) {
this.attributes = globalAttributes;
}

@Override
public Attributes get() {
return attributes;
}

public void update(Consumer<AttributesBuilder> attributesUpdater) {
AttributesBuilder builder = attributes.toBuilder();
attributesUpdater.accept(builder);
attributes = builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import android.content.Context;
import android.location.Location;
import android.webkit.WebView;
import io.opentelemetry.android.GlobalAttributesSpanAppender;
import com.splunk.rum.internal.GlobalAttributesSupplier;
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider;
import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class SplunkRumTest {
private Tracer tracer;

@Mock private OpenTelemetryRum openTelemetryRum;
@Mock private GlobalAttributesSpanAppender globalAttributes;
@Mock private GlobalAttributesSupplier globalAttributes;

@BeforeEach
public void setup() {
Expand Down Expand Up @@ -255,7 +255,7 @@ void integrateWithBrowserRum() {
@Test
void updateLocation() {
AtomicReference<Attributes> updatedAttributes = new AtomicReference<>();
GlobalAttributesSpanAppender globalAttributes = mock(GlobalAttributesSpanAppender.class);
GlobalAttributesSupplier globalAttributes = mock(GlobalAttributesSupplier.class);
doAnswer(
invocation -> {
Consumer<AttributesBuilder> updater = invocation.getArgument(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Splunk Inc.
*
* 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 com.splunk.rum.internal;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.junit.jupiter.api.Assertions.*;

import io.opentelemetry.api.common.Attributes;
import org.junit.jupiter.api.Test;

class GlobalAttributesSupplierTest {

@Test
void update() {
Attributes initial = Attributes.of(stringKey("foo"), "bar", stringKey("bar"), "baz");
GlobalAttributesSupplier testClass = new GlobalAttributesSupplier(initial);
testClass.update(
builder -> {
builder.put("jimbo", "hutch");
builder.remove(stringKey("bar"));
});
Attributes result = testClass.get();
assertEquals("bar", result.get(stringKey("foo")));
assertEquals("hutch", result.get(stringKey("jimbo")));
assertNull(result.get(stringKey("bar")));
}
}