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

Add network change monitoring #176

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 @@ -13,6 +13,7 @@
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider;
import io.opentelemetry.android.instrumentation.network.NetworkAttributesSpanAppender;
import io.opentelemetry.android.instrumentation.network.NetworkChangeMonitor;
import io.opentelemetry.android.instrumentation.startup.InitializationEvents;
import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents;
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
Expand All @@ -36,6 +37,7 @@
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nullable;

/**
* A builder of {@link OpenTelemetryRum}. It enabled configuring the OpenTelemetry SDK and disabling
Expand Down Expand Up @@ -65,6 +67,7 @@ public final class OpenTelemetryRumBuilder {
(a) -> a;

private Resource resource;
@Nullable private CurrentNetworkProvider currentNetworkProvider = null;
private InitializationEvents initializationEvents = InitializationEvents.NO_OP;

private static TextMapPropagator buildDefaultPropagator() {
Expand All @@ -91,6 +94,18 @@ public OpenTelemetryRumBuilder setResource(Resource resource) {
return this;
}

/**
* Call this to pass an existing CurrentNetworkProvider instance to share with the underlying
* OpenTelemetry Rum instrumentation.
*
* @return {@code this}
*/
public OpenTelemetryRumBuilder setCurrentNetworkProvider(
CurrentNetworkProvider currentNetworkProvider) {
this.currentNetworkProvider = currentNetworkProvider;
return this;
}

/**
* Merges a new {@link Resource} with any existing {@link Resource} in this builder. The
* resulting {@link Resource} will be attached to all telemetry emitted by the {@link
Expand Down Expand Up @@ -265,8 +280,7 @@ private void applyConfiguration() {
// Network specific attributes
if (config.shouldIncludeNetworkAttributes()) {
// Add span processor that appends network attributes.
CurrentNetworkProvider currentNetworkProvider =
CurrentNetworkProvider.createAndStart(application);
CurrentNetworkProvider currentNetworkProvider = getOrCreateCurrentNetworkProvider();
addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
SpanProcessor networkAttributesSpanAppender =
Expand All @@ -277,6 +291,16 @@ private void applyConfiguration() {
initializationEvents.currentNetworkProviderInitialized();
}

// Add network change monitor if enabled (default = = true)
if (config.isNetworkChangeMonitoringEnabled()) {
addInstrumentation(
app -> {
NetworkChangeMonitor.create(getOrCreateCurrentNetworkProvider())
.installOn(app);
initializationEvents.networkMonitorInitialized();
});
}

// Add span processor that appends screen attribute(s)
if (config.shouldIncludeScreenAttributes()) {
addTracerProviderCustomizer(
Expand All @@ -288,6 +312,13 @@ private void applyConfiguration() {
}
}

private CurrentNetworkProvider getOrCreateCurrentNetworkProvider() {
if (currentNetworkProvider == null) {
this.currentNetworkProvider = CurrentNetworkProvider.createAndStart(application);
}
return currentNetworkProvider;
}

private SdkTracerProvider buildTracerProvider(SessionId sessionId, Application application) {
SdkTracerProviderBuilder tracerProviderBuilder =
SdkTracerProvider.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class OtelRumConfig {
private boolean includeScreenAttributes = true;
private DiskBufferingConfiguration diskBufferingConfiguration =
DiskBufferingConfiguration.builder().build();
private boolean networkChangeMonitoringEnabled = true;

/**
* Configures the set of global attributes to emit with every span and event. Any existing
Expand Down Expand Up @@ -103,4 +104,12 @@ public void setDiskBufferingConfiguration(
DiskBufferingConfiguration diskBufferingConfiguration) {
this.diskBufferingConfiguration = diskBufferingConfiguration;
}

public void disableNetworkChangeMonitoring() {
this.networkChangeMonitoringEnabled = false;
}

public boolean isNetworkChangeMonitoringEnabled() {
return this.networkChangeMonitoringEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface InitializationEvents {

void currentNetworkProviderInitialized();

void networkMonitorInitialized();

InitializationEvents NO_OP =
new InitializationEvents() {
@Override
Expand All @@ -25,5 +27,8 @@ public void recordConfiguration(OtelRumConfig config) {}

@Override
public void currentNetworkProviderInitialized() {}

@Override
public void networkMonitorInitialized() {}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public void recordConfiguration(OtelRumConfig config) {
public void currentNetworkProviderInitialized() {
// TODO: Build me
}

@Override
public void networkMonitorInitialized() {
// TOOD: Build me "networkMonitorInitialized"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.time.Duration;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -49,11 +50,17 @@ class OpenTelemetryRumBuilderTest {
final InMemorySpanExporter spanExporter = InMemorySpanExporter.create();

@Mock Application application;
@Mock android.content.Context applicationContext;
@Mock Activity activity;
@Mock ApplicationStateListener listener;

@Captor ArgumentCaptor<Application.ActivityLifecycleCallbacks> activityCallbacksCaptor;

@BeforeEach
void setup() {
when(application.getApplicationContext()).thenReturn(applicationContext);
}

@Test
void shouldRegisterApplicationStateWatcher() {
makeBuilder().build();
Expand Down