Skip to content

Commit

Permalink
Merge pull request #176 from breedx-splk/add_network_change_monitor
Browse files Browse the repository at this point in the history
Add network change monitoring
  • Loading branch information
breedx-splk authored Dec 11, 2023
2 parents 1bc0f9f + f137f5a commit fac5ec1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
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

0 comments on commit fac5ec1

Please sign in to comment.