From 5d471c2f70281a95d1c65bbc9b62b930d792e172 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:51:43 +0100 Subject: [PATCH 1/7] Adding initialization listener to the rum config --- .../android/config/OtelRumConfig.java | 15 ++++++++++++ .../startup/InitializationListener.kt | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt diff --git a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index 7051c79db..c82aca9a8 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -7,7 +7,11 @@ import io.opentelemetry.android.ScreenAttributesSpanProcessor; import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider; +import io.opentelemetry.android.instrumentation.startup.InitializationListener; import io.opentelemetry.api.common.Attributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.function.Supplier; /** @@ -23,7 +27,9 @@ public class OtelRumConfig { private boolean includeScreenAttributes = true; private DiskBufferingConfiguration diskBufferingConfiguration = DiskBufferingConfiguration.builder().build(); + private boolean networkChangeMonitoringEnabled = true; + private List initializationListeners = new ArrayList<>(); /** * Configures the set of global attributes to emit with every span and event. Any existing @@ -119,4 +125,13 @@ public void disableNetworkChangeMonitoring() { public boolean isNetworkChangeMonitoringEnabled() { return this.networkChangeMonitoringEnabled; } + + /** Adds a new initialization listener. It will be called during the RUM initialization. */ + public void addInitializationListener(InitializationListener listener) { + initializationListeners.add(listener); + } + + public List getInitializationListeners() { + return Collections.unmodifiableList(initializationListeners); + } } diff --git a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt new file mode 100644 index 000000000..62fe047b7 --- /dev/null +++ b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt @@ -0,0 +1,24 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.android.instrumentation.startup + +/** + * Provides callbacks to know the sate of the initialization. + */ +interface InitializationListener { + companion object { + } + + /** + * Called when the RUM initialization starts. + */ + fun onStart() + + /** + * Called when the RUM initialization ends. + */ + fun onEnd() +} From b4e129f4559eec19b5ff56d1ba7ac53ddb402582 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:56:14 +0100 Subject: [PATCH 2/7] Notifying initialization listeners --- .../android/OpenTelemetryRumBuilder.java | 16 ++++++++++++++++ .../android/config/OtelRumConfig.java | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index a18d68605..07c114295 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -17,6 +17,7 @@ import io.opentelemetry.android.instrumentation.network.NetworkAttributesSpanAppender; import io.opentelemetry.android.instrumentation.network.NetworkChangeMonitor; import io.opentelemetry.android.instrumentation.startup.InitializationEvents; +import io.opentelemetry.android.instrumentation.startup.InitializationListener; import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents; import io.opentelemetry.android.internal.features.persistence.DiskManager; import io.opentelemetry.android.internal.features.persistence.SimpleTemporaryFileProvider; @@ -249,6 +250,7 @@ public SessionId getSessionId() { * @return A new {@link OpenTelemetryRum} instance. */ public OpenTelemetryRum build() { + notifyInitializationStart(); applyConfiguration(); @@ -263,9 +265,23 @@ public OpenTelemetryRum build() { SdkPreconfiguredRumBuilder delegate = new SdkPreconfiguredRumBuilder(application, sdk, sessionId); instrumentationInstallers.forEach(delegate::addInstrumentation); + + notifyInitializationEnd(); return delegate.build(); } + private void notifyInitializationStart() { + for (InitializationListener listener : config.getInitializationListeners()) { + listener.onStart(); + } + } + + private void notifyInitializationEnd() { + for (InitializationListener listener : config.getInitializationListeners()) { + listener.onEnd(); + } + } + /** Leverage the configuration to wire up various instrumentation components. */ private void applyConfiguration() { if (config.shouldGenerateSdkInitializationEvents()) { diff --git a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index c82aca9a8..d6156e318 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -29,7 +29,7 @@ public class OtelRumConfig { DiskBufferingConfiguration.builder().build(); private boolean networkChangeMonitoringEnabled = true; - private List initializationListeners = new ArrayList<>(); + private final List initializationListeners = new ArrayList<>(); /** * Configures the set of global attributes to emit with every span and event. Any existing @@ -128,7 +128,9 @@ public boolean isNetworkChangeMonitoringEnabled() { /** Adds a new initialization listener. It will be called during the RUM initialization. */ public void addInitializationListener(InitializationListener listener) { - initializationListeners.add(listener); + if (!initializationListeners.contains(listener)) { + initializationListeners.add(listener); + } } public List getInitializationListeners() { From 6aa6de5d5cb4f64cb14b84e1323aaf2e042f4162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:41:40 +0100 Subject: [PATCH 3/7] Update instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> --- .../android/instrumentation/startup/InitializationListener.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt index 62fe047b7..ac5a7b22b 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt +++ b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt @@ -9,8 +9,6 @@ package io.opentelemetry.android.instrumentation.startup * Provides callbacks to know the sate of the initialization. */ interface InitializationListener { - companion object { - } /** * Called when the RUM initialization starts. From 5483388f0bdcb10787b690201ff0912e2130d1cb Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:45:09 +0100 Subject: [PATCH 4/7] Added javadoc --- .../java/io/opentelemetry/android/config/OtelRumConfig.java | 4 ++++ .../android/instrumentation/startup/InitializationListener.kt | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index d6156e318..047b65cab 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -133,6 +133,10 @@ public void addInitializationListener(InitializationListener listener) { } } + /** + * Provides an unmodifiable list of initialization listeners. This is mean to be called during + * RUM initialization. + */ public List getInitializationListeners() { return Collections.unmodifiableList(initializationListeners); } diff --git a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt index ac5a7b22b..85970c90f 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt +++ b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt @@ -9,7 +9,6 @@ package io.opentelemetry.android.instrumentation.startup * Provides callbacks to know the sate of the initialization. */ interface InitializationListener { - /** * Called when the RUM initialization starts. */ From d234fa576431b6172648b5074d9605e0c128b00b Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:46:10 +0100 Subject: [PATCH 5/7] Using Set for initialization listeners config --- .../io/opentelemetry/android/config/OtelRumConfig.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index 047b65cab..dfca95755 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -11,7 +11,9 @@ import io.opentelemetry.api.common.Attributes; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.function.Supplier; /** @@ -29,7 +31,7 @@ public class OtelRumConfig { DiskBufferingConfiguration.builder().build(); private boolean networkChangeMonitoringEnabled = true; - private final List initializationListeners = new ArrayList<>(); + private final Set initializationListeners = new HashSet<>(); /** * Configures the set of global attributes to emit with every span and event. Any existing @@ -128,9 +130,7 @@ public boolean isNetworkChangeMonitoringEnabled() { /** Adds a new initialization listener. It will be called during the RUM initialization. */ public void addInitializationListener(InitializationListener listener) { - if (!initializationListeners.contains(listener)) { - initializationListeners.add(listener); - } + initializationListeners.add(listener); } /** @@ -138,6 +138,6 @@ public void addInitializationListener(InitializationListener listener) { * RUM initialization. */ public List getInitializationListeners() { - return Collections.unmodifiableList(initializationListeners); + return Collections.unmodifiableList(new ArrayList<>(initializationListeners)); } } From e5b0b6b3c38a0499b412e192ac20d97e1a8e6224 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:49:06 +0100 Subject: [PATCH 6/7] Providing unmodifiable collection to avoid having to wrap a set into a list --- .../io/opentelemetry/android/config/OtelRumConfig.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index dfca95755..f438ce7d7 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -9,10 +9,9 @@ import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider; import io.opentelemetry.android.instrumentation.startup.InitializationListener; import io.opentelemetry.api.common.Attributes; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.function.Supplier; @@ -137,7 +136,7 @@ public void addInitializationListener(InitializationListener listener) { * Provides an unmodifiable list of initialization listeners. This is mean to be called during * RUM initialization. */ - public List getInitializationListeners() { - return Collections.unmodifiableList(new ArrayList<>(initializationListeners)); + public Collection getInitializationListeners() { + return Collections.unmodifiableCollection(initializationListeners); } } From 1efda6d70f0bfde57ee8a33e256e313953e86f36 Mon Sep 17 00:00:00 2001 From: Cesar Munoz <56847527+LikeTheSalad@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:16:57 +0100 Subject: [PATCH 7/7] Passing the initialized OpenTelemetry instance to the initialization listeners --- .../io/opentelemetry/android/OpenTelemetryRumBuilder.java | 7 ++++--- .../instrumentation/startup/InitializationListener.kt | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java b/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java index 07c114295..009d799fe 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java +++ b/instrumentation/src/main/java/io/opentelemetry/android/OpenTelemetryRumBuilder.java @@ -21,6 +21,7 @@ import io.opentelemetry.android.instrumentation.startup.SdkInitializationEvents; import io.opentelemetry.android.internal.features.persistence.DiskManager; import io.opentelemetry.android.internal.features.persistence.SimpleTemporaryFileProvider; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.propagation.ContextPropagators; @@ -266,7 +267,7 @@ public OpenTelemetryRum build() { new SdkPreconfiguredRumBuilder(application, sdk, sessionId); instrumentationInstallers.forEach(delegate::addInstrumentation); - notifyInitializationEnd(); + notifyInitializationEnd(sdk); return delegate.build(); } @@ -276,9 +277,9 @@ private void notifyInitializationStart() { } } - private void notifyInitializationEnd() { + private void notifyInitializationEnd(OpenTelemetry openTelemetry) { for (InitializationListener listener : config.getInitializationListeners()) { - listener.onEnd(); + listener.onEnd(openTelemetry); } } diff --git a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt index 85970c90f..413d3284d 100644 --- a/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt +++ b/instrumentation/src/main/java/io/opentelemetry/android/instrumentation/startup/InitializationListener.kt @@ -5,6 +5,8 @@ package io.opentelemetry.android.instrumentation.startup +import io.opentelemetry.api.OpenTelemetry + /** * Provides callbacks to know the sate of the initialization. */ @@ -16,6 +18,7 @@ interface InitializationListener { /** * Called when the RUM initialization ends. + * @param openTelemetry - The initialized OpenTelemetry instance. */ - fun onEnd() + fun onEnd(openTelemetry: OpenTelemetry) }