From 154077a11d962567a7cb72f7b5a5681671207125 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:22:22 +0200 Subject: [PATCH 01/14] Moving network provider tools to services --- android-agent/src/main/AndroidManifest.xml | 1 + .../android/OpenTelemetryRumBuilder.java | 2 +- .../android/config/OtelRumConfig.java | 2 +- .../services}/network/CarrierFinder.java | 9 +++++---- .../network/CurrentNetworkProvider.java | 9 ++++++--- .../network/NetworkChangeListener.java | 4 +++- .../services/network/data}/Carrier.java | 20 +++++++++---------- .../network/data}/CurrentNetwork.java | 12 +++++------ .../services/network/data}/NetworkState.java | 4 ++-- .../network/detector}/NetworkDetector.java | 6 ++++-- .../detector}/PostApi28NetworkDetector.java | 10 +++++++--- .../detector}/SimpleNetworkDetector.java | 8 +++++--- 12 files changed, 51 insertions(+), 36 deletions(-) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation => android-agent/src/main/java/io/opentelemetry/android/internal/services}/network/CarrierFinder.java (83%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation => android-agent/src/main/java/io/opentelemetry/android/internal/services}/network/CurrentNetworkProvider.java (93%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation => android-agent/src/main/java/io/opentelemetry/android/internal/services}/network/NetworkChangeListener.java (56%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data}/Carrier.java (87%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data}/CurrentNetwork.java (90%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data}/NetworkState.java (88%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector}/NetworkDetector.java (79%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector}/PostApi28NetworkDetector.java (88%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector}/SimpleNetworkDetector.java (78%) diff --git a/android-agent/src/main/AndroidManifest.xml b/android-agent/src/main/AndroidManifest.xml index fa49b6f45..298c8b6e8 100644 --- a/android-agent/src/main/AndroidManifest.xml +++ b/android-agent/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + diff --git a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index a59c74075..6d0c212ac 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -22,7 +22,6 @@ import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; import io.opentelemetry.android.instrumentation.crash.CrashReporter; import io.opentelemetry.android.instrumentation.crash.CrashReporterBuilder; -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.slowrendering.SlowRenderingDetector; @@ -34,6 +33,7 @@ import io.opentelemetry.android.internal.services.CacheStorage; import io.opentelemetry.android.internal.services.Preferences; import io.opentelemetry.android.internal.services.ServiceManager; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.propagation.ContextPropagators; diff --git a/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index f26ec6271..eb4d475c5 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -7,7 +7,7 @@ import io.opentelemetry.android.ScreenAttributesSpanProcessor; import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration; -import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; import io.opentelemetry.api.common.Attributes; import java.time.Duration; import java.util.function.Supplier; diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CarrierFinder.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java similarity index 83% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CarrierFinder.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java index 9bb68e07b..20e0ddab2 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CarrierFinder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java @@ -3,22 +3,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network; import android.os.Build; import android.telephony.TelephonyManager; import androidx.annotation.RequiresApi; +import io.opentelemetry.android.internal.services.network.data.Carrier; @RequiresApi(api = Build.VERSION_CODES.P) -class CarrierFinder { +public class CarrierFinder { private final TelephonyManager telephonyManager; - CarrierFinder(TelephonyManager telephonyManager) { + public CarrierFinder(TelephonyManager telephonyManager) { this.telephonyManager = telephonyManager; } - Carrier get() { + public Carrier get() { Carrier.Builder builder = Carrier.builder(); int id = telephonyManager.getSimCarrierId(); builder.id(id); diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProvider.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java similarity index 93% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProvider.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java index 1968ae36e..dd6141afc 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProvider.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network; import android.app.Application; import android.content.Context; @@ -15,6 +15,9 @@ import android.util.Log; import androidx.annotation.NonNull; import io.opentelemetry.android.common.RumConstants; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; +import io.opentelemetry.android.internal.services.network.detector.NetworkDetector; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Supplier; @@ -28,9 +31,9 @@ */ public final class CurrentNetworkProvider { - static final CurrentNetwork NO_NETWORK = + public static final CurrentNetwork NO_NETWORK = CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(); - static final CurrentNetwork UNKNOWN_NETWORK = + public static final CurrentNetwork UNKNOWN_NETWORK = CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build(); /** diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeListener.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java similarity index 56% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeListener.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java index 81e0255e8..5d842354a 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeListener.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java @@ -3,7 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network; + +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; interface NetworkChangeListener { diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/Carrier.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java similarity index 87% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/Carrier.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java index 4328d1710..c97db4e05 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/Carrier.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.data; import android.os.Build; import android.telephony.TelephonyManager; @@ -12,7 +12,7 @@ import java.util.Objects; @RequiresApi(api = Build.VERSION_CODES.P) -final class Carrier { +public final class Carrier { private final int id; private final @Nullable String name; @@ -20,7 +20,7 @@ final class Carrier { private final @Nullable String mobileNetworkCode; // 2 or 3 digits private final @Nullable String isoCountryCode; - static Builder builder() { + public static Builder builder() { return new Builder(); } @@ -93,38 +93,38 @@ public String toString() { + '}'; } - static class Builder { + public static class Builder { private int id = TelephonyManager.UNKNOWN_CARRIER_ID; private @Nullable String name = null; private @Nullable String mobileCountryCode = null; private @Nullable String mobileNetworkCode = null; private @Nullable String isoCountryCode = null; - Carrier build() { + public Carrier build() { return new Carrier(this); } - Builder id(int id) { + public Builder id(int id) { this.id = id; return this; } - Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - Builder mobileCountryCode(String countryCode) { + public Builder mobileCountryCode(String countryCode) { this.mobileCountryCode = countryCode; return this; } - Builder mobileNetworkCode(String networkCode) { + public Builder mobileNetworkCode(String networkCode) { this.mobileNetworkCode = networkCode; return this; } - Builder isoCountryCode(String isoCountryCode) { + public Builder isoCountryCode(String isoCountryCode) { this.isoCountryCode = isoCountryCode; return this; } diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetwork.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java similarity index 90% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetwork.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java index 87160e0ad..731699334 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetwork.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.data; import android.os.Build; import androidx.annotation.Nullable; @@ -92,11 +92,11 @@ public String toString() { + '}'; } - static Builder builder(NetworkState state) { + public static Builder builder(NetworkState state) { return new Builder(state); } - static class Builder { + public static class Builder { @Nullable private Carrier carrier; private final NetworkState state; @Nullable private String subType; @@ -105,17 +105,17 @@ private Builder(NetworkState state) { this.state = state; } - Builder carrier(@Nullable Carrier carrier) { + public Builder carrier(@Nullable Carrier carrier) { this.carrier = carrier; return this; } - Builder subType(@Nullable String subType) { + public Builder subType(@Nullable String subType) { this.subType = subType; return this; } - CurrentNetwork build() { + public CurrentNetwork build() { return new CurrentNetwork(this); } } diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkState.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java similarity index 88% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkState.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java index 4cecea28a..de36bfedf 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkState.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.data; import io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NetworkConnectionTypeValues; -enum NetworkState { +public enum NetworkState { NO_NETWORK_AVAILABLE(NetworkConnectionTypeValues.UNAVAILABLE), TRANSPORT_CELLULAR(NetworkConnectionTypeValues.CELL), TRANSPORT_WIFI(NetworkConnectionTypeValues.WIFI), diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java similarity index 79% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkDetector.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java index 34ef36469..3a18e39c6 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java @@ -3,14 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; import android.content.Context; import android.net.ConnectivityManager; import android.os.Build; import android.telephony.TelephonyManager; +import io.opentelemetry.android.internal.services.network.CarrierFinder; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; -interface NetworkDetector { +public interface NetworkDetector { CurrentNetwork detectCurrentNetwork(); static NetworkDetector create(Context context) { diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java similarity index 88% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetector.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java index 5f79de692..99c1dfdfd 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; -import static io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider.NO_NETWORK; -import static io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider.UNKNOWN_NETWORK; +import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.NO_NETWORK; +import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.UNKNOWN_NETWORK; import android.Manifest; import android.annotation.SuppressLint; @@ -18,6 +18,10 @@ import android.telephony.TelephonyManager; import androidx.annotation.RequiresApi; import androidx.core.app.ActivityCompat; +import io.opentelemetry.android.internal.services.network.CarrierFinder; +import io.opentelemetry.android.internal.services.network.data.Carrier; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; @RequiresApi(api = Build.VERSION_CODES.P) class PostApi28NetworkDetector implements NetworkDetector { diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java similarity index 78% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetector.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java index 618035ee0..106f8c064 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java @@ -3,13 +3,15 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; -import static io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider.NO_NETWORK; -import static io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider.UNKNOWN_NETWORK; +import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.NO_NETWORK; +import static io.opentelemetry.android.internal.services.network.CurrentNetworkProvider.UNKNOWN_NETWORK; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; class SimpleNetworkDetector implements NetworkDetector { private final ConnectivityManager connectivityManager; From 128be8dabe68461c0fc4807df3969387a072fd8c Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:31:36 +0200 Subject: [PATCH 02/14] Moving network service related tests --- android-agent/build.gradle.kts | 1 - .../opentelemetry/android/OpenTelemetryRumBuilder.java | 2 -- .../internal/services/network/data/Carrier.java | 10 +++++----- .../internal/services/network/data/CurrentNetwork.java | 2 +- .../internal/services}/network/CarrierFinderTest.java | 3 ++- .../services}/network/CurrentNetworkProviderTest.java | 5 ++++- .../network/detector}/NetworkDetectorTest.java | 2 +- .../detector}/PostApi28NetworkDetectorTest.java | 6 +++++- .../network/detector}/SimpleNetworkDetectorTest.java | 4 +++- 9 files changed, 21 insertions(+), 14 deletions(-) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation => android-agent/src/test/java/io/opentelemetry/android/internal/services}/network/CarrierFinderTest.java (93%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation => android-agent/src/test/java/io/opentelemetry/android/internal/services}/network/CurrentNetworkProviderTest.java (97%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network => android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector}/NetworkDetectorTest.java (93%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network => android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector}/PostApi28NetworkDetectorTest.java (93%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network => android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector}/SimpleNetworkDetectorTest.java (95%) diff --git a/android-agent/build.gradle.kts b/android-agent/build.gradle.kts index a10bb241c..9f5f6f545 100644 --- a/android-agent/build.gradle.kts +++ b/android-agent/build.gradle.kts @@ -61,7 +61,6 @@ dependencies { implementation(project(":instrumentation:anr")) implementation(project(":instrumentation:common-api")) implementation(project(":instrumentation:crash")) - implementation(project(":instrumentation:network")) implementation(project(":instrumentation:slowrendering")) implementation(libs.androidx.core) implementation(libs.androidx.navigation.fragment) diff --git a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index 6d0c212ac..e273a145e 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -22,8 +22,6 @@ import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; import io.opentelemetry.android.instrumentation.crash.CrashReporter; import io.opentelemetry.android.instrumentation.crash.CrashReporterBuilder; -import io.opentelemetry.android.instrumentation.network.NetworkAttributesSpanAppender; -import io.opentelemetry.android.instrumentation.network.NetworkChangeMonitor; import io.opentelemetry.android.instrumentation.slowrendering.SlowRenderingDetector; import io.opentelemetry.android.instrumentation.startup.InitializationEvents; import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents; diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java index c97db4e05..96a373eac 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java @@ -32,27 +32,27 @@ public static Builder builder() { this.isoCountryCode = builder.isoCountryCode; } - int getId() { + public int getId() { return id; } @Nullable - String getName() { + public String getName() { return name; } @Nullable - String getMobileCountryCode() { + public String getMobileCountryCode() { return mobileCountryCode; } @Nullable - String getMobileNetworkCode() { + public String getMobileNetworkCode() { return mobileNetworkCode; } @Nullable - String getIsoCountryCode() { + public String getIsoCountryCode() { return isoCountryCode; } diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java index 731699334..bdc52c968 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java @@ -56,7 +56,7 @@ String getCarrierNetworkCode() { @SuppressWarnings("NullAway") @Nullable - String getCarrierName() { + public String getCarrierName() { return haveCarrier() ? carrier.getName() : null; } diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CarrierFinderTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java similarity index 93% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CarrierFinderTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java index 815e81b01..6eeb1e996 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CarrierFinderTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java @@ -3,13 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import android.telephony.TelephonyManager; +import io.opentelemetry.android.internal.services.network.data.Carrier; import org.junit.jupiter.api.Test; class CarrierFinderTest { diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProviderTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java similarity index 97% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProviderTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java index f9ac963d0..a8288a35c 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkProviderTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -21,6 +21,9 @@ import android.net.Network; import android.net.NetworkRequest; import android.os.Build; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; +import io.opentelemetry.android.internal.services.network.detector.NetworkDetector; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; import org.junit.Test; diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkDetectorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetectorTest.java similarity index 93% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkDetectorTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetectorTest.java index b54150765..62aa6c6fe 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkDetectorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetectorTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; import static org.junit.Assert.assertTrue; diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetectorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java similarity index 93% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetectorTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java index 78507458d..784e3660c 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/PostApi28NetworkDetectorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -15,6 +15,10 @@ import android.net.NetworkCapabilities; import android.os.Build; import android.telephony.TelephonyManager; +import io.opentelemetry.android.internal.services.network.CarrierFinder; +import io.opentelemetry.android.internal.services.network.data.Carrier; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import org.junit.Assert; import org.junit.Before; import org.junit.Test; diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetectorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetectorTest.java similarity index 95% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetectorTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetectorTest.java index 54ca3fb98..b9c9213ab 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/SimpleNetworkDetectorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetectorTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.services.network.detector; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -13,6 +13,8 @@ import android.net.NetworkInfo; import android.os.Build; import androidx.test.core.app.ApplicationProvider; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; From f7b9bd770ecc32e4d1d5fe83ff15e2bdea932655 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:44:20 +0200 Subject: [PATCH 03/14] Moving network attr init feature to the agent --- .../android/OpenTelemetryRumBuilder.java | 11 +---------- .../CurrentNetworkAttributesExtractor.java | 3 ++- .../networkattrs}/NetworkAttributesSpanAppender.java | 4 +++- .../services/network/CurrentNetworkProvider.java | 4 ++-- .../services/network/data/CurrentNetwork.java | 10 +++++----- .../internal/services/network/data/NetworkState.java | 2 +- .../CurrentNetworkAttributesExtractorTest.java | 5 ++++- .../NetworkAttributesSpanAppenderTest.java | 5 ++++- 8 files changed, 22 insertions(+), 22 deletions(-) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs}/CurrentNetworkAttributesExtractor.java (92%) rename {instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network => android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs}/NetworkAttributesSpanAppender.java (88%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network => android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs}/CurrentNetworkAttributesExtractorTest.java (89%) rename {instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network => android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs}/NetworkAttributesSpanAppenderTest.java (84%) diff --git a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index e273a145e..2f0b963ca 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -25,6 +25,7 @@ import io.opentelemetry.android.instrumentation.slowrendering.SlowRenderingDetector; import io.opentelemetry.android.instrumentation.startup.InitializationEvents; import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents; +import io.opentelemetry.android.internal.features.networkattrs.NetworkAttributesSpanAppender; import io.opentelemetry.android.internal.features.persistence.DiskManager; import io.opentelemetry.android.internal.features.persistence.SimpleTemporaryFileProvider; import io.opentelemetry.android.internal.processors.GlobalAttributesLogRecordAppender; @@ -411,16 +412,6 @@ 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( diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractor.java b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java similarity index 92% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractor.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java index 0896a6bdb..91d70e6e1 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractor.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.features.networkattrs; import static io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NETWORK_CARRIER_ICC; import static io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NETWORK_CARRIER_MCC; @@ -13,6 +13,7 @@ import static io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NETWORK_CONNECTION_TYPE; import androidx.annotation.Nullable; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppender.java b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppender.java similarity index 88% rename from instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppender.java rename to android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppender.java index 05cbdafab..f71e29c2e 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppender.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppender.java @@ -3,8 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.features.networkattrs; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.trace.ReadWriteSpan; diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java index dd6141afc..7b4ee627d 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java @@ -110,11 +110,11 @@ private static NetworkRequest createNetworkMonitoringRequest() { .build(); } - CurrentNetwork getCurrentNetwork() { + public CurrentNetwork getCurrentNetwork() { return currentNetwork; } - void addNetworkChangeListener(NetworkChangeListener listener) { + public void addNetworkChangeListener(NetworkChangeListener listener) { listeners.add(listener); } diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java index bdc52c968..42ee59c46 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java @@ -27,30 +27,30 @@ public boolean isOnline() { return getState() != NetworkState.NO_NETWORK_AVAILABLE; } - NetworkState getState() { + public NetworkState getState() { return state; } @Nullable - String getSubType() { + public String getSubType() { return subType; } @SuppressWarnings("NullAway") @Nullable - String getCarrierCountryCode() { + public String getCarrierCountryCode() { return haveCarrier() ? carrier.getMobileCountryCode() : null; } @SuppressWarnings("NullAway") @Nullable - String getCarrierIsoCountryCode() { + public String getCarrierIsoCountryCode() { return haveCarrier() ? carrier.getIsoCountryCode() : null; } @SuppressWarnings("NullAway") @Nullable - String getCarrierNetworkCode() { + public String getCarrierNetworkCode() { return haveCarrier() ? carrier.getMobileNetworkCode() : null; } diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java index de36bfedf..7dc12b05e 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java @@ -21,7 +21,7 @@ public enum NetworkState { this.humanName = humanName; } - String getHumanName() { + public String getHumanName() { return humanName; } } diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java similarity index 89% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractorTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java index 0f0b2c4d2..cfac36c15 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/CurrentNetworkAttributesExtractorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java @@ -3,11 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.features.networkattrs; import static org.assertj.core.api.Assertions.entry; import android.os.Build; +import io.opentelemetry.android.internal.services.network.data.Carrier; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; import io.opentelemetry.semconv.SemanticAttributes; import org.junit.Test; diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppenderTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppenderTest.java similarity index 84% rename from instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppenderTest.java rename to android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppenderTest.java index 65f98d1cd..94c3ed422 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkAttributesSpanAppenderTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/NetworkAttributesSpanAppenderTest.java @@ -3,13 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.android.instrumentation.network; +package io.opentelemetry.android.internal.features.networkattrs; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.trace.ReadWriteSpan; From 9fad4b10c3ad22b0892bf0c84e10fdf06c1eebdc Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:47:41 +0200 Subject: [PATCH 04/14] Making network instrumentation depend on the agent --- .../networkattrs/CurrentNetworkAttributesExtractor.java | 4 ++-- .../internal/services/network/NetworkChangeListener.java | 2 +- instrumentation/network/build.gradle.kts | 2 +- .../instrumentation/network/NetworkApplicationListener.java | 3 +++ .../network/NetworkChangeAttributesExtractor.java | 3 +++ .../instrumentation/network/NetworkChangeMonitor.java | 2 ++ .../instrumentation/network/NetworkChangeMonitorBuilder.java | 2 ++ .../instrumentation/network/NetworkChangeMonitorTest.java | 5 +++++ 8 files changed, 19 insertions(+), 4 deletions(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java index 91d70e6e1..66eff3110 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractor.java @@ -18,9 +18,9 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; -final class CurrentNetworkAttributesExtractor { +public final class CurrentNetworkAttributesExtractor { - Attributes extract(CurrentNetwork network) { + public Attributes extract(CurrentNetwork network) { AttributesBuilder builder = Attributes.builder() .put(NETWORK_CONNECTION_TYPE, network.getState().getHumanName()); diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java index 5d842354a..d9305e1e0 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java @@ -7,7 +7,7 @@ import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; -interface NetworkChangeListener { +public interface NetworkChangeListener { void onNetworkChange(CurrentNetwork currentNetwork); } diff --git a/instrumentation/network/build.gradle.kts b/instrumentation/network/build.gradle.kts index 599795533..651b4afe2 100644 --- a/instrumentation/network/build.gradle.kts +++ b/instrumentation/network/build.gradle.kts @@ -21,7 +21,7 @@ android { dependencies { api(platform(libs.opentelemetry.platform)) api(libs.opentelemetry.api) - api(project(":common")) + api(project(":android-agent")) api(project(":instrumentation:common-api")) implementation(libs.androidx.core) implementation(libs.opentelemetry.semconv.incubating) diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkApplicationListener.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkApplicationListener.java index 06a33614c..35ba330ef 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkApplicationListener.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkApplicationListener.java @@ -8,6 +8,9 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey; import io.opentelemetry.android.instrumentation.common.ApplicationStateListener; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.NetworkChangeListener; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeAttributesExtractor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeAttributesExtractor.java index c003352ec..47c50e857 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeAttributesExtractor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeAttributesExtractor.java @@ -8,6 +8,9 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NETWORK_CONNECTION_TYPE; +import io.opentelemetry.android.internal.features.networkattrs.CurrentNetworkAttributesExtractor; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java index be2cfb73e..3fa5e62ed 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java @@ -6,6 +6,8 @@ package io.opentelemetry.android.instrumentation.network; import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java index c5c3f69fe..b1dbfe594 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java @@ -5,6 +5,8 @@ package io.opentelemetry.android.instrumentation.network; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import java.util.ArrayList; import java.util.List; diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java index 170a99184..0c199fae9 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java +++ b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java @@ -15,6 +15,11 @@ import android.os.Build; import io.opentelemetry.android.instrumentation.common.ApplicationStateListener; import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; +import io.opentelemetry.android.internal.services.network.NetworkChangeListener; +import io.opentelemetry.android.internal.services.network.data.Carrier; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.android.internal.services.network.data.NetworkState; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; import io.opentelemetry.sdk.trace.data.SpanData; From 060250c3fad24ea56879569762ad173291571d4e Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:44:34 +0200 Subject: [PATCH 05/14] Initializing CurrentNetworkProvider as service --- .../android/OpenTelemetryRumBuilder.java | 25 +---------- .../internal/services/ServiceManager.kt | 12 ++++-- .../internal/services/ServiceManagerImpl.kt | 5 +++ .../network/CurrentNetworkProvider.java | 43 +++++++++---------- .../services/ServiceManagerImplTest.kt | 2 + .../network/CurrentNetworkProviderTest.java | 34 +++++++++------ 6 files changed, 57 insertions(+), 64 deletions(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index 2f0b963ca..170958cdd 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -32,7 +32,6 @@ import io.opentelemetry.android.internal.services.CacheStorage; import io.opentelemetry.android.internal.services.Preferences; import io.opentelemetry.android.internal.services.ServiceManager; -import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.propagation.ContextPropagators; @@ -90,7 +89,6 @@ public final class OpenTelemetryRumBuilder { (a) -> a; private Resource resource; - @Nullable private CurrentNetworkProvider currentNetworkProvider = null; private InitializationEvents initializationEvents = InitializationEvents.NO_OP; private Consumer anrCustomizer = x -> {}; private Consumer crashReporterCustomizer = x -> {}; @@ -120,18 +118,6 @@ 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 @@ -401,11 +387,11 @@ private void applyConfiguration() { // Network specific attributes if (config.shouldIncludeNetworkAttributes()) { // Add span processor that appends network attributes. - CurrentNetworkProvider currentNetworkProvider = getOrCreateCurrentNetworkProvider(); addTracerProviderCustomizer( (tracerProviderBuilder, app) -> { SpanProcessor networkAttributesSpanAppender = - NetworkAttributesSpanAppender.create(currentNetworkProvider); + NetworkAttributesSpanAppender.create( + ServiceManager.get().getCurrentNetworkProvider()); return tracerProviderBuilder.addSpanProcessor( networkAttributesSpanAppender); }); @@ -466,13 +452,6 @@ private void applyConfiguration() { } } - private CurrentNetworkProvider getOrCreateCurrentNetworkProvider() { - if (currentNetworkProvider == null) { - this.currentNetworkProvider = CurrentNetworkProvider.createAndStart(application); - } - return currentNetworkProvider; - } - private SdkTracerProvider buildTracerProvider( SessionId sessionId, Application application, SpanExporter spanExporter) { SdkTracerProviderBuilder tracerProviderBuilder = diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt b/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt index 96f0e4c34..8c0f2732a 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManager.kt @@ -5,7 +5,8 @@ package io.opentelemetry.android.internal.services -import android.content.Context +import android.app.Application +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider import io.opentelemetry.android.internal.services.periodicwork.PeriodicWorkService /** @@ -18,22 +19,25 @@ interface ServiceManager : Startable { fun getPeriodicWorkService(): PeriodicWorkService + fun getCurrentNetworkProvider(): CurrentNetworkProvider + companion object { private var instance: ServiceManager? = null @JvmStatic - fun initialize(appContext: Context) { + fun initialize(application: Application) { if (instance != null) { return } instance = ServiceManagerImpl( listOf( - Preferences.create(appContext), + Preferences.create(application), CacheStorage( - appContext, + application, ), PeriodicWorkService(), + CurrentNetworkProvider.create(application), ), ) } diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManagerImpl.kt b/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManagerImpl.kt index 12f4aeea2..370e4db70 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManagerImpl.kt +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/ServiceManagerImpl.kt @@ -5,6 +5,7 @@ package io.opentelemetry.android.internal.services +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider import io.opentelemetry.android.internal.services.periodicwork.PeriodicWorkService import java.util.Collections @@ -31,6 +32,10 @@ internal class ServiceManagerImpl(services: List) : ServiceManager { return getService(PeriodicWorkService::class.java) } + override fun getCurrentNetworkProvider(): CurrentNetworkProvider { + return getService(CurrentNetworkProvider::class.java) + } + override fun start() { for (service in services.values) { if (service is Startable) { diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java index 7b4ee627d..42250e02e 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java @@ -15,6 +15,7 @@ import android.util.Log; import androidx.annotation.NonNull; import io.opentelemetry.android.common.RumConstants; +import io.opentelemetry.android.internal.services.Startable; import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.android.internal.services.network.data.NetworkState; import io.opentelemetry.android.internal.services.network.detector.NetworkDetector; @@ -29,44 +30,37 @@ * A provider of {@link CurrentNetwork} information. Registers itself in the Android {@link * ConnectivityManager} and listens for network changes. */ -public final class CurrentNetworkProvider { +public final class CurrentNetworkProvider implements Startable { public static final CurrentNetwork NO_NETWORK = CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build(); public static final CurrentNetwork UNKNOWN_NETWORK = CurrentNetwork.builder(NetworkState.TRANSPORT_UNKNOWN).build(); - /** - * Creates a new {@link CurrentNetworkProvider} instance and registers network callbacks in the - * Android {@link ConnectivityManager}. - */ - public static CurrentNetworkProvider createAndStart(Application application) { - Context context = application.getApplicationContext(); - CurrentNetworkProvider currentNetworkProvider = - new CurrentNetworkProvider(NetworkDetector.create(context)); - currentNetworkProvider.startMonitoring( - CurrentNetworkProvider::createNetworkMonitoringRequest, - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); - return currentNetworkProvider; + public static CurrentNetworkProvider create(Application application) { + return new CurrentNetworkProvider( + NetworkDetector.create(application), + (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE)); } private final NetworkDetector networkDetector; + private final ConnectivityManager connectivityManager; private volatile CurrentNetwork currentNetwork = UNKNOWN_NETWORK; - private final List listeners = new CopyOnWriteArrayList<>(); + private final List listeners = + new CopyOnWriteArrayList<>(); // visible for tests - // visible for tests - CurrentNetworkProvider(NetworkDetector networkDetector) { + CurrentNetworkProvider( + NetworkDetector networkDetector, ConnectivityManager connectivityManager) { + this.connectivityManager = connectivityManager; this.networkDetector = networkDetector; } // visible for tests - void startMonitoring( - Supplier createNetworkMonitoringRequest, - ConnectivityManager connectivityManager) { + void startMonitoring(Supplier createNetworkMonitoringRequest) { refreshNetworkStatus(); try { - registerNetworkCallbacks(createNetworkMonitoringRequest, connectivityManager); + registerNetworkCallbacks(createNetworkMonitoringRequest); } catch (Exception e) { // if this fails, we'll go without network change events. Log.w( @@ -76,9 +70,7 @@ void startMonitoring( } } - private void registerNetworkCallbacks( - Supplier createNetworkMonitoringRequest, - ConnectivityManager connectivityManager) { + private void registerNetworkCallbacks(Supplier createNetworkMonitoringRequest) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { connectivityManager.registerDefaultNetworkCallback(new ConnectionMonitor()); } else { @@ -124,6 +116,11 @@ private void notifyListeners(CurrentNetwork activeNetwork) { } } + @Override + public void start() { + startMonitoring(CurrentNetworkProvider::createNetworkMonitoringRequest); + } + private final class ConnectionMonitor extends ConnectivityManager.NetworkCallback { @Override diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/services/ServiceManagerImplTest.kt b/android-agent/src/test/java/io/opentelemetry/android/internal/services/ServiceManagerImplTest.kt index 5bede69ff..e76a7dcc9 100644 --- a/android-agent/src/test/java/io/opentelemetry/android/internal/services/ServiceManagerImplTest.kt +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/ServiceManagerImplTest.kt @@ -6,6 +6,7 @@ package io.opentelemetry.android.internal.services import io.opentelemetry.android.internal.services.ServiceManager.Companion.initialize +import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider import io.opentelemetry.android.internal.services.periodicwork.PeriodicWorkService import org.assertj.core.api.Assertions.assertThat import org.junit.Test @@ -28,6 +29,7 @@ class ServiceManagerImplTest { assertThat(serviceManager.getPeriodicWorkService()).isInstanceOf(PeriodicWorkService::class.java) assertThat(serviceManager.getCacheStorage()).isInstanceOf(CacheStorage::class.java) assertThat(serviceManager.getPreferences()).isInstanceOf(Preferences::class.java) + assertThat(serviceManager.getCurrentNetworkProvider()).isInstanceOf(CurrentNetworkProvider::class.java) } @Test diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java index a8288a35c..4c7f6a72f 100644 --- a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProviderTest.java @@ -53,8 +53,9 @@ public void lollipop() { .subType("LTE") .build()); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring(() -> networkRequest, connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> networkRequest); Assert.assertEquals( CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), @@ -103,8 +104,9 @@ public void modernSdks() { .subType("LTE") .build()); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring(() -> networkRequest, connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> networkRequest); Assert.assertEquals( CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), @@ -145,7 +147,8 @@ public void networkDetectorException() { NetworkDetector networkDetector = Mockito.mock(NetworkDetector.class); when(networkDetector.detectCurrentNetwork()).thenThrow(new SecurityException("bug")); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, Mockito.mock()); Assert.assertEquals( CurrentNetworkProvider.UNKNOWN_NETWORK, currentNetworkProvider.refreshNetworkStatus()); @@ -163,9 +166,9 @@ public void networkDetectorExceptionOnCallbackRegistration() { .when(connectivityManager) .registerDefaultNetworkCallback(isA(NetworkCallback.class)); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring( - () -> mock(NetworkRequest.class), connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> mock(NetworkRequest.class)); Assert.assertEquals( CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), currentNetworkProvider.refreshNetworkStatus()); @@ -184,8 +187,9 @@ public void networkDetectorExceptionOnCallbackRegistration_lollipop() { .when(connectivityManager) .registerNetworkCallback(eq(networkRequest), isA(NetworkCallback.class)); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring(() -> networkRequest, connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> networkRequest); Assert.assertEquals( CurrentNetwork.builder(NetworkState.TRANSPORT_WIFI).build(), currentNetworkProvider.refreshNetworkStatus()); @@ -207,8 +211,9 @@ public void shouldNotFailOnImmediateConnectionManagerCall_lollipop() { .when(connectivityManager) .registerNetworkCallback(eq(networkRequest), any(NetworkCallback.class)); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring(() -> networkRequest, connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> networkRequest); } @Test @@ -227,7 +232,8 @@ public void shouldNotFailOnImmediateConnectionManagerCall() { .when(connectivityManager) .registerDefaultNetworkCallback(any(NetworkCallback.class)); - CurrentNetworkProvider currentNetworkProvider = new CurrentNetworkProvider(networkDetector); - currentNetworkProvider.startMonitoring(() -> networkRequest, connectivityManager); + CurrentNetworkProvider currentNetworkProvider = + new CurrentNetworkProvider(networkDetector, connectivityManager); + currentNetworkProvider.startMonitoring(() -> networkRequest); } } From c11767bc67b0cdb3d13b233eaa9cb0421320fbb3 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:45:59 +0200 Subject: [PATCH 06/14] Clean up config --- .../android/config/OtelRumConfig.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index eb4d475c5..46b6c861a 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/android-agent/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -28,7 +28,6 @@ public class OtelRumConfig { private boolean includeScreenAttributes = true; private DiskBufferingConfiguration diskBufferingConfiguration = DiskBufferingConfiguration.builder().build(); - private boolean networkChangeMonitoringEnabled = true; private boolean anrDetectionEnabled = true; private boolean slowRenderingDetectionEnabled = true; private Duration slowRenderingDetectionPollInterval = @@ -121,20 +120,6 @@ public OtelRumConfig setDiskBufferingConfiguration( return this; } - /** - * Sets the configuration so that network change monitoring, which is enabled by default, will - * not be started. - */ - public OtelRumConfig disableNetworkChangeMonitoring() { - networkChangeMonitoringEnabled = false; - return this; - } - - /** Returns true if network change monitoring is enabled (default = true). */ - public boolean isNetworkChangeMonitoringEnabled() { - return networkChangeMonitoringEnabled; - } - /** Returns true if ANR (application not responding) detection is enabled (default = true). */ public boolean isAnrDetectionEnabled() { return anrDetectionEnabled; From 20aab442c5a5037f911d639833ad8c88916383c1 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:05:06 +0200 Subject: [PATCH 07/14] Created NetworkChangeInstrumentation --- .../network/NetworkChangeInstrumentation.java | 40 +++++++++++++++++++ .../network/NetworkChangeMonitor.java | 23 ++++------- .../network/NetworkChangeMonitorBuilder.java | 39 ------------------ 3 files changed, 47 insertions(+), 55 deletions(-) create mode 100644 instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java delete mode 100644 instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java new file mode 100644 index 000000000..9303b2e2e --- /dev/null +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java @@ -0,0 +1,40 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.android.instrumentation.network; + +import android.app.Application; +import androidx.annotation.NonNull; +import io.opentelemetry.android.OpenTelemetryRum; +import io.opentelemetry.android.instrumentation.AndroidInstrumentation; +import io.opentelemetry.android.internal.services.ServiceManager; +import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** Generates telemetry for when the network status changes. */ +public final class NetworkChangeInstrumentation implements AndroidInstrumentation { + + final List> additionalExtractors = new ArrayList<>(); + + /** Adds an {@link AttributesExtractor} that will extract additional attributes. */ + public NetworkChangeInstrumentation addAttributesExtractor( + AttributesExtractor extractor) { + additionalExtractors.add(extractor); + return this; + } + + @Override + public void install( + @NonNull Application application, @NonNull OpenTelemetryRum openTelemetryRum) { + NetworkChangeMonitor networkChangeMonitor = + new NetworkChangeMonitor( + ServiceManager.get().getCurrentNetworkProvider(), + Collections.unmodifiableList(additionalExtractors)); + networkChangeMonitor.installOn(openTelemetryRum.getOpenTelemetry()); + } +} diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java index 3fa5e62ed..8c4bec058 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java @@ -11,7 +11,6 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import java.util.ArrayList; import java.util.List; /** @@ -22,32 +21,24 @@ */ public final class NetworkChangeMonitor { - public static NetworkChangeMonitor create(CurrentNetworkProvider currentNetworkProvider) { - return builder(currentNetworkProvider).build(); - } - - public static NetworkChangeMonitorBuilder builder( - CurrentNetworkProvider currentNetworkProvider) { - return new NetworkChangeMonitorBuilder(currentNetworkProvider); + public NetworkChangeMonitor( + CurrentNetworkProvider currentNetworkProvider, + List> additionalExtractors) { + this.currentNetworkProvider = currentNetworkProvider; + this.additionalExtractors = additionalExtractors; } private final CurrentNetworkProvider currentNetworkProvider; private final List> additionalExtractors; - NetworkChangeMonitor(NetworkChangeMonitorBuilder builder) { - this.currentNetworkProvider = builder.currentNetworkProvider; - this.additionalExtractors = new ArrayList<>(builder.additionalExtractors); - } - /** * Installs the network change monitoring instrumentation on the given {@link * InstrumentedApplication}. */ - public void installOn(InstrumentedApplication instrumentedApplication) { + public void installOn(OpenTelemetry openTelemetry) { NetworkApplicationListener networkApplicationListener = new NetworkApplicationListener(currentNetworkProvider); - networkApplicationListener.startMonitoring( - buildInstrumenter(instrumentedApplication.getOpenTelemetrySdk())); + networkApplicationListener.startMonitoring(buildInstrumenter(openTelemetry)); instrumentedApplication.registerApplicationStateListener(networkApplicationListener); } diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java deleted file mode 100644 index b1dbfe594..000000000 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorBuilder.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.android.instrumentation.network; - -import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; -import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import java.util.ArrayList; -import java.util.List; - -/** - * A builder of {@link NetworkChangeMonitor}. - * - *

This class is internal and not for public use. Its APIs are unstable and can change at any - * time. - */ -public final class NetworkChangeMonitorBuilder { - - final CurrentNetworkProvider currentNetworkProvider; - final List> additionalExtractors = new ArrayList<>(); - - NetworkChangeMonitorBuilder(CurrentNetworkProvider currentNetworkProvider) { - this.currentNetworkProvider = currentNetworkProvider; - } - - /** Adds an {@link AttributesExtractor} that will extract additional attributes. */ - public NetworkChangeMonitorBuilder addAttributesExtractor( - AttributesExtractor extractor) { - additionalExtractors.add(extractor); - return this; - } - - public NetworkChangeMonitor build() { - return new NetworkChangeMonitor(this); - } -} From 8c7e9e18d65d2df4db729b37f04b51d88a4e0a17 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:19:06 +0200 Subject: [PATCH 08/14] Updated network monitor tests --- .../network/NetworkChangeMonitor.java | 4 ++- .../network/NetworkChangeMonitorTest.java | 27 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java index 8c4bec058..2ce3fc9dc 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java @@ -39,7 +39,9 @@ public void installOn(OpenTelemetry openTelemetry) { NetworkApplicationListener networkApplicationListener = new NetworkApplicationListener(currentNetworkProvider); networkApplicationListener.startMonitoring(buildInstrumenter(openTelemetry)); - instrumentedApplication.registerApplicationStateListener(networkApplicationListener); + // + // instrumentedApplication.registerApplicationStateListener(networkApplicationListener); + // TODO uncomment in part 3 } private Instrumenter buildInstrumenter(OpenTelemetry openTelemetry) { diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java index 0c199fae9..34940a0f8 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java +++ b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java @@ -10,23 +10,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import android.os.Build; import io.opentelemetry.android.instrumentation.common.ApplicationStateListener; -import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; import io.opentelemetry.android.internal.services.network.NetworkChangeListener; import io.opentelemetry.android.internal.services.network.data.Carrier; import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.android.internal.services.network.data.NetworkState; -import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.semconv.SemanticAttributes; +import java.util.Collections; import java.util.List; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,15 +46,12 @@ public class NetworkChangeMonitorTest { @Captor ArgumentCaptor networkChangeListenerCaptor; @Mock CurrentNetworkProvider currentNetworkProvider; - @Mock InstrumentedApplication instrumentedApplication; AutoCloseable mocks; @Before public void setUp() { mocks = MockitoAnnotations.openMocks(this); - when(instrumentedApplication.getOpenTelemetrySdk()) - .thenReturn((OpenTelemetrySdk) otelTesting.getOpenTelemetry()); } @After @@ -65,7 +61,7 @@ public void tearDown() throws Exception { @Test public void networkAvailable_wifi() { - NetworkChangeMonitor.create(currentNetworkProvider).installOn(instrumentedApplication); + create().installOn(otelTesting.getOpenTelemetry()); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -84,7 +80,7 @@ public void networkAvailable_wifi() { @Test public void networkAvailable_cellular() { - NetworkChangeMonitor.create(currentNetworkProvider).installOn(instrumentedApplication); + create().installOn(otelTesting.getOpenTelemetry()); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -121,7 +117,7 @@ public void networkAvailable_cellular() { @Test public void networkLost() { - NetworkChangeMonitor.create(currentNetworkProvider).installOn(instrumentedApplication); + create().installOn(otelTesting.getOpenTelemetry()); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -139,15 +135,18 @@ public void networkLost() { } @Test + @Ignore("Reintroduce in part 3") public void noEventsPlease() { - NetworkChangeMonitor.create(currentNetworkProvider).installOn(instrumentedApplication); + create().installOn(otelTesting.getOpenTelemetry()); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); NetworkChangeListener networkListener = networkChangeListenerCaptor.getValue(); - verify(instrumentedApplication) - .registerApplicationStateListener(applicationStateListenerCaptor.capture()); + // verify(instrumentedApplication) + // + // .registerApplicationStateListener(applicationStateListenerCaptor.capture()); TODO + // uncomment in part 3 ApplicationStateListener applicationListener = applicationStateListenerCaptor.getValue(); applicationListener.onApplicationBackgrounded(); @@ -165,4 +164,8 @@ public void noEventsPlease() { CurrentNetwork.builder(NetworkState.NO_NETWORK_AVAILABLE).build()); assertEquals(1, otelTesting.getSpans().size()); } + + private NetworkChangeMonitor create() { + return new NetworkChangeMonitor(currentNetworkProvider, Collections.emptyList()); + } } From 8f3bb42ae59e2ffca3c5df749c6e80987d96d369 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:19:39 +0200 Subject: [PATCH 09/14] Clean up --- .../android/instrumentation/network/NetworkChangeMonitor.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java index 2ce3fc9dc..a7d257965 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java @@ -5,7 +5,6 @@ package io.opentelemetry.android.instrumentation.network; -import io.opentelemetry.android.instrumentation.common.InstrumentedApplication; import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.api.OpenTelemetry; @@ -32,8 +31,7 @@ public NetworkChangeMonitor( private final List> additionalExtractors; /** - * Installs the network change monitoring instrumentation on the given {@link - * InstrumentedApplication}. + * Installs the network change monitoring instrumentation on the given {@link OpenTelemetry}. */ public void installOn(OpenTelemetry openTelemetry) { NetworkApplicationListener networkApplicationListener = From 129dd043340af02b4defd916a46d1c2e4c2c4d45 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:16:56 +0200 Subject: [PATCH 10/14] Renaming installOn by start --- .../network/NetworkChangeInstrumentation.java | 3 ++- .../instrumentation/network/NetworkChangeMonitor.java | 5 ++++- .../network/NetworkChangeMonitorTest.java | 11 ++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java index 9303b2e2e..c3b6c3147 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeInstrumentation.java @@ -33,8 +33,9 @@ public void install( @NonNull Application application, @NonNull OpenTelemetryRum openTelemetryRum) { NetworkChangeMonitor networkChangeMonitor = new NetworkChangeMonitor( + openTelemetryRum.getOpenTelemetry(), ServiceManager.get().getCurrentNetworkProvider(), Collections.unmodifiableList(additionalExtractors)); - networkChangeMonitor.installOn(openTelemetryRum.getOpenTelemetry()); + networkChangeMonitor.start(); } } diff --git a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java index a7d257965..331916f51 100644 --- a/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java +++ b/instrumentation/network/src/main/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitor.java @@ -19,10 +19,13 @@ * time. */ public final class NetworkChangeMonitor { + private final OpenTelemetry openTelemetry; public NetworkChangeMonitor( + OpenTelemetry openTelemetry, CurrentNetworkProvider currentNetworkProvider, List> additionalExtractors) { + this.openTelemetry = openTelemetry; this.currentNetworkProvider = currentNetworkProvider; this.additionalExtractors = additionalExtractors; } @@ -33,7 +36,7 @@ public NetworkChangeMonitor( /** * Installs the network change monitoring instrumentation on the given {@link OpenTelemetry}. */ - public void installOn(OpenTelemetry openTelemetry) { + public void start() { NetworkApplicationListener networkApplicationListener = new NetworkApplicationListener(currentNetworkProvider); networkApplicationListener.startMonitoring(buildInstrumenter(openTelemetry)); diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java index 34940a0f8..0868b5742 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java +++ b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java @@ -61,7 +61,7 @@ public void tearDown() throws Exception { @Test public void networkAvailable_wifi() { - create().installOn(otelTesting.getOpenTelemetry()); + create().start(); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -80,7 +80,7 @@ public void networkAvailable_wifi() { @Test public void networkAvailable_cellular() { - create().installOn(otelTesting.getOpenTelemetry()); + create().start(); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -117,7 +117,7 @@ public void networkAvailable_cellular() { @Test public void networkLost() { - create().installOn(otelTesting.getOpenTelemetry()); + create().start(); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -137,7 +137,7 @@ public void networkLost() { @Test @Ignore("Reintroduce in part 3") public void noEventsPlease() { - create().installOn(otelTesting.getOpenTelemetry()); + create().start(); verify(currentNetworkProvider) .addNetworkChangeListener(networkChangeListenerCaptor.capture()); @@ -166,6 +166,7 @@ public void noEventsPlease() { } private NetworkChangeMonitor create() { - return new NetworkChangeMonitor(currentNetworkProvider, Collections.emptyList()); + return new NetworkChangeMonitor( + otelTesting.getOpenTelemetry(), currentNetworkProvider, Collections.emptyList()); } } From f066a2c98734f10dd832a9ade449930e9d917506 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:20:38 +0200 Subject: [PATCH 11/14] Adding internal class docs --- .../android/internal/services/network/CarrierFinder.java | 3 +++ .../internal/services/network/CurrentNetworkProvider.java | 3 +++ .../internal/services/network/NetworkChangeListener.java | 3 +++ .../android/internal/services/network/data/Carrier.java | 3 +++ .../internal/services/network/data/CurrentNetwork.java | 7 ++++++- .../internal/services/network/data/NetworkState.java | 3 +++ .../services/network/detector/NetworkDetector.java | 3 +++ .../network/detector/PostApi28NetworkDetector.java | 3 +++ .../services/network/detector/SimpleNetworkDetector.java | 3 +++ 9 files changed, 30 insertions(+), 1 deletion(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java index 20e0ddab2..6a0924413 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java @@ -10,6 +10,9 @@ import androidx.annotation.RequiresApi; import io.opentelemetry.android.internal.services.network.data.Carrier; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ @RequiresApi(api = Build.VERSION_CODES.P) public class CarrierFinder { diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java index 42250e02e..af0d9192c 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java @@ -29,6 +29,9 @@ /** * A provider of {@link CurrentNetwork} information. Registers itself in the Android {@link * ConnectivityManager} and listens for network changes. + * + *

This class is internal and not for public use. Its APIs are unstable and can change at any + * time. */ public final class CurrentNetworkProvider implements Startable { diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java index d9305e1e0..c1b46ba7f 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/NetworkChangeListener.java @@ -7,6 +7,9 @@ import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ public interface NetworkChangeListener { void onNetworkChange(CurrentNetwork currentNetwork); diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java index 96a373eac..33407a7d7 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java @@ -11,6 +11,9 @@ import androidx.annotation.RequiresApi; import java.util.Objects; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ @RequiresApi(api = Build.VERSION_CODES.P) public final class Carrier { diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java index 42ee59c46..56675b0fc 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/CurrentNetwork.java @@ -9,7 +9,12 @@ import androidx.annotation.Nullable; import java.util.Objects; -/** A value class representing the current network that the device is connected to. */ +/** + * A value class representing the current network that the device is connected to. + * + *

This class is internal and not for public use. Its APIs are unstable and can change at any + * time. + */ public final class CurrentNetwork { @Nullable private final Carrier carrier; diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java index 7dc12b05e..9cfaf66bc 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/NetworkState.java @@ -7,6 +7,9 @@ import io.opentelemetry.semconv.incubating.NetworkIncubatingAttributes.NetworkConnectionTypeValues; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ public enum NetworkState { NO_NETWORK_AVAILABLE(NetworkConnectionTypeValues.UNAVAILABLE), TRANSPORT_CELLULAR(NetworkConnectionTypeValues.CELL), diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java index 3a18e39c6..27a6483e9 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/NetworkDetector.java @@ -12,6 +12,9 @@ import io.opentelemetry.android.internal.services.network.CarrierFinder; import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ public interface NetworkDetector { CurrentNetwork detectCurrentNetwork(); diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java index 99c1dfdfd..09edde22e 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetector.java @@ -23,6 +23,9 @@ import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.android.internal.services.network.data.NetworkState; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ @RequiresApi(api = Build.VERSION_CODES.P) class PostApi28NetworkDetector implements NetworkDetector { private final ConnectivityManager connectivityManager; diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java index 106f8c064..fd0cbaa6d 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/detector/SimpleNetworkDetector.java @@ -13,6 +13,9 @@ import io.opentelemetry.android.internal.services.network.data.CurrentNetwork; import io.opentelemetry.android.internal.services.network.data.NetworkState; +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ class SimpleNetworkDetector implements NetworkDetector { private final ConnectivityManager connectivityManager; From 2f2b7b02b52e594668c535e316f14e3d406f9de9 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:21:28 +0200 Subject: [PATCH 12/14] Fixing visible for tests comment --- .../internal/services/network/CurrentNetworkProvider.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java index af0d9192c..2da0fcc9a 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CurrentNetworkProvider.java @@ -50,9 +50,9 @@ public static CurrentNetworkProvider create(Application application) { private final ConnectivityManager connectivityManager; private volatile CurrentNetwork currentNetwork = UNKNOWN_NETWORK; - private final List listeners = - new CopyOnWriteArrayList<>(); // visible for tests + private final List listeners = new CopyOnWriteArrayList<>(); + // visible for tests CurrentNetworkProvider( NetworkDetector networkDetector, ConnectivityManager connectivityManager) { this.connectivityManager = connectivityManager; From eb5ad541fadee528018204539689d73c0f0a90f9 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:31:32 +0200 Subject: [PATCH 13/14] Rename .java to .kt --- .../services/network/data/Carrier.java | 135 ------------------ .../internal/services/network/data/Carrier.kt | 74 ++++++++++ 2 files changed, 74 insertions(+), 135 deletions(-) delete mode 100644 android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java create mode 100644 android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java deleted file mode 100644 index 33407a7d7..000000000 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.android.internal.services.network.data; - -import android.os.Build; -import android.telephony.TelephonyManager; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; -import java.util.Objects; - -/** - * This class is internal and not for public use. Its APIs are unstable and can change at any time. - */ -@RequiresApi(api = Build.VERSION_CODES.P) -public final class Carrier { - - private final int id; - private final @Nullable String name; - private final @Nullable String mobileCountryCode; // 3 digits - private final @Nullable String mobileNetworkCode; // 2 or 3 digits - private final @Nullable String isoCountryCode; - - public static Builder builder() { - return new Builder(); - } - - Carrier(Builder builder) { - this.id = builder.id; - this.name = builder.name; - this.mobileCountryCode = builder.mobileCountryCode; - this.mobileNetworkCode = builder.mobileNetworkCode; - this.isoCountryCode = builder.isoCountryCode; - } - - public int getId() { - return id; - } - - @Nullable - public String getName() { - return name; - } - - @Nullable - public String getMobileCountryCode() { - return mobileCountryCode; - } - - @Nullable - public String getMobileNetworkCode() { - return mobileNetworkCode; - } - - @Nullable - public String getIsoCountryCode() { - return isoCountryCode; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Carrier carrier = (Carrier) o; - return id == carrier.id - && Objects.equals(name, carrier.name) - && Objects.equals(mobileCountryCode, carrier.mobileCountryCode) - && Objects.equals(mobileNetworkCode, carrier.mobileNetworkCode) - && Objects.equals(isoCountryCode, carrier.isoCountryCode); - } - - @Override - public int hashCode() { - return Objects.hash(id, name, mobileCountryCode, mobileNetworkCode, isoCountryCode); - } - - @Override - public String toString() { - return "Carrier{" - + "id=" - + id - + ", name='" - + name - + '\'' - + ", mobileCountryCode='" - + mobileCountryCode - + '\'' - + ", mobileNetworkCode='" - + mobileNetworkCode - + '\'' - + ", isoCountryCode='" - + isoCountryCode - + '\'' - + '}'; - } - - public static class Builder { - private int id = TelephonyManager.UNKNOWN_CARRIER_ID; - private @Nullable String name = null; - private @Nullable String mobileCountryCode = null; - private @Nullable String mobileNetworkCode = null; - private @Nullable String isoCountryCode = null; - - public Carrier build() { - return new Carrier(this); - } - - public Builder id(int id) { - this.id = id; - return this; - } - - public Builder name(String name) { - this.name = name; - return this; - } - - public Builder mobileCountryCode(String countryCode) { - this.mobileCountryCode = countryCode; - return this; - } - - public Builder mobileNetworkCode(String networkCode) { - this.mobileNetworkCode = networkCode; - return this; - } - - public Builder isoCountryCode(String isoCountryCode) { - this.isoCountryCode = isoCountryCode; - return this; - } - } -} diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt new file mode 100644 index 000000000..1e3d0c366 --- /dev/null +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt @@ -0,0 +1,74 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.android.internal.services.network.data + +import android.os.Build +import android.telephony.TelephonyManager +import androidx.annotation.RequiresApi + +/** + * This class is internal and not for public use. Its APIs are unstable and can change at any time. + */ +@RequiresApi(api = Build.VERSION_CODES.P) +data class Carrier internal constructor( + val id: Int, + val name: String?, + val mobileCountryCode: String?, + val mobileNetworkCode: String?, + val isoCountryCode: String?, +) { + constructor(builder: Builder) : this( + id = builder.id, + name = builder.name, + mobileCountryCode = builder.mobileCountryCode, + mobileNetworkCode = builder.mobileNetworkCode, + isoCountryCode = builder.isoCountryCode, + ) + + class Builder { + internal var id: Int = TelephonyManager.UNKNOWN_CARRIER_ID + var name: String? = null + var mobileCountryCode: String? = null + var mobileNetworkCode: String? = null + var isoCountryCode: String? = null + + fun build(): Carrier { + return Carrier(this) + } + + fun id(id: Int): Builder { + this.id = id + return this + } + + fun name(name: String?): Builder { + this.name = name + return this + } + + fun mobileCountryCode(countryCode: String?): Builder { + this.mobileCountryCode = countryCode + return this + } + + fun mobileNetworkCode(networkCode: String?): Builder { + this.mobileNetworkCode = networkCode + return this + } + + fun isoCountryCode(isoCountryCode: String?): Builder { + this.isoCountryCode = isoCountryCode + return this + } + } + + companion object { + @JvmStatic + fun builder(): Builder { + return Builder() + } + } +} From df79f91e10d6e4ee68ba8b776415ad972db40a21 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:39:12 +0200 Subject: [PATCH 14/14] Removing Carrier.Builder --- .../services/network/CarrierFinder.java | 25 +++---- .../internal/services/network/data/Carrier.kt | 66 +++---------------- ...CurrentNetworkAttributesExtractorTest.java | 11 +--- .../services/network/CarrierFinderTest.java | 11 +--- .../PostApi28NetworkDetectorTest.java | 2 +- .../network/NetworkChangeMonitorTest.java | 9 +-- 6 files changed, 27 insertions(+), 97 deletions(-) diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java index 6a0924413..7690e0540 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/CarrierFinder.java @@ -23,24 +23,25 @@ public CarrierFinder(TelephonyManager telephonyManager) { } public Carrier get() { - Carrier.Builder builder = Carrier.builder(); int id = telephonyManager.getSimCarrierId(); - builder.id(id); - CharSequence name = telephonyManager.getSimCarrierIdName(); - if (validString(name)) { - builder.name(name.toString()); + String name = null; + String mobileCountryCode = null; + String mobileNetworkCode = null; + String isoCountryCode = null; + CharSequence simCarrierIdName = telephonyManager.getSimCarrierIdName(); + if (validString(simCarrierIdName)) { + name = simCarrierIdName.toString(); } String simOperator = telephonyManager.getSimOperator(); if (validString(simOperator) && simOperator.length() >= 5) { - String countryCode = simOperator.substring(0, 3); - String networkCode = simOperator.substring(3); - builder.mobileCountryCode(countryCode).mobileNetworkCode(networkCode); + mobileCountryCode = simOperator.substring(0, 3); + mobileNetworkCode = simOperator.substring(3); } - String isoCountryCode = telephonyManager.getSimCountryIso(); - if (validString(isoCountryCode)) { - builder.isoCountryCode(isoCountryCode); + String providedIsoCountryCode = telephonyManager.getSimCountryIso(); + if (validString(providedIsoCountryCode)) { + isoCountryCode = providedIsoCountryCode; } - return builder.build(); + return new Carrier(id, name, mobileCountryCode, mobileNetworkCode, isoCountryCode); } private boolean validString(CharSequence str) { diff --git a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt index 1e3d0c366..5531892b1 100644 --- a/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt +++ b/android-agent/src/main/java/io/opentelemetry/android/internal/services/network/data/Carrier.kt @@ -13,62 +13,12 @@ import androidx.annotation.RequiresApi * This class is internal and not for public use. Its APIs are unstable and can change at any time. */ @RequiresApi(api = Build.VERSION_CODES.P) -data class Carrier internal constructor( - val id: Int, - val name: String?, - val mobileCountryCode: String?, - val mobileNetworkCode: String?, - val isoCountryCode: String?, -) { - constructor(builder: Builder) : this( - id = builder.id, - name = builder.name, - mobileCountryCode = builder.mobileCountryCode, - mobileNetworkCode = builder.mobileNetworkCode, - isoCountryCode = builder.isoCountryCode, +data class Carrier + @JvmOverloads + internal constructor( + val id: Int = TelephonyManager.UNKNOWN_CARRIER_ID, + val name: String? = null, + val mobileCountryCode: String? = null, + val mobileNetworkCode: String? = null, + val isoCountryCode: String? = null, ) - - class Builder { - internal var id: Int = TelephonyManager.UNKNOWN_CARRIER_ID - var name: String? = null - var mobileCountryCode: String? = null - var mobileNetworkCode: String? = null - var isoCountryCode: String? = null - - fun build(): Carrier { - return Carrier(this) - } - - fun id(id: Int): Builder { - this.id = id - return this - } - - fun name(name: String?): Builder { - this.name = name - return this - } - - fun mobileCountryCode(countryCode: String?): Builder { - this.mobileCountryCode = countryCode - return this - } - - fun mobileNetworkCode(networkCode: String?): Builder { - this.mobileNetworkCode = networkCode - return this - } - - fun isoCountryCode(isoCountryCode: String?): Builder { - this.isoCountryCode = isoCountryCode - return this - } - } - - companion object { - @JvmStatic - fun builder(): Builder { - return Builder() - } - } -} diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java index cfac36c15..907fe3e88 100644 --- a/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/features/networkattrs/CurrentNetworkAttributesExtractorTest.java @@ -29,14 +29,7 @@ public void getNetworkAttributes_withCarrier() { CurrentNetwork currentNetwork = CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) .subType("aaa") - .carrier( - Carrier.builder() - .id(206) - .name("ShadyTel") - .isoCountryCode("US") - .mobileCountryCode("usa") - .mobileNetworkCode("omg") - .build()) + .carrier(new Carrier(206, "ShadyTel", "usa", "omg", "US")) .build(); OpenTelemetryAssertions.assertThat(underTest.extract(currentNetwork)) @@ -55,7 +48,7 @@ public void getNetworkAttributes_withoutCarrier() { CurrentNetwork currentNetwork = CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) .subType("aaa") - .carrier(Carrier.builder().id(42).name("ShadyTel").build()) + .carrier(new Carrier(42, "ShadyTel")) .build(); OpenTelemetryAssertions.assertThat(underTest.extract(currentNetwork)) diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java index 6eeb1e996..4d94d512c 100644 --- a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/CarrierFinderTest.java @@ -17,14 +17,7 @@ class CarrierFinderTest { @Test void testSimpleGet() { - Carrier expected = - Carrier.builder() - .id(206) - .name("ShadyTel") - .isoCountryCode("US") - .mobileCountryCode("usa") - .mobileNetworkCode("omg") - .build(); + Carrier expected = new Carrier(206, "ShadyTel", "usa", "omg", "US"); TelephonyManager manager = mock(TelephonyManager.class); when(manager.getSimCarrierId()).thenReturn(expected.getId()); @@ -40,7 +33,7 @@ void testSimpleGet() { @Test void testMostlyInvalid() { - Carrier expected = Carrier.builder().build(); + Carrier expected = new Carrier(); TelephonyManager manager = mock(TelephonyManager.class); when(manager.getSimCarrierId()).thenReturn(-1); diff --git a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java index 784e3660c..7acb9bea5 100644 --- a/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java +++ b/android-agent/src/test/java/io/opentelemetry/android/internal/services/network/detector/PostApi28NetworkDetectorTest.java @@ -133,7 +133,7 @@ public void vpn() { @Test public void carrierIsSet() { - Carrier carrier = Carrier.builder().name("flib").build(); + Carrier carrier = new Carrier(0, "flib"); when(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) .thenReturn(true); when(carrierFinder.get()).thenReturn(carrier); diff --git a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java index 0868b5742..d2ddcb478 100644 --- a/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java +++ b/instrumentation/network/src/test/java/io/opentelemetry/android/instrumentation/network/NetworkChangeMonitorTest.java @@ -89,14 +89,7 @@ public void networkAvailable_cellular() { CurrentNetwork network = CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) .subType("LTE") - .carrier( - Carrier.builder() - .id(206) - .name("ShadyTel") - .isoCountryCode("US") - .mobileCountryCode("usa") - .mobileNetworkCode("omg") - .build()) + .carrier(new Carrier(206, "ShadyTel", "usa", "omg", "US")) .build(); listener.onNetworkChange(network);