Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization hooks #196

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
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;
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;
Expand Down Expand Up @@ -249,6 +251,7 @@ public SessionId getSessionId() {
* @return A new {@link OpenTelemetryRum} instance.
*/
public OpenTelemetryRum build() {
notifyInitializationStart();

applyConfiguration();

Expand All @@ -263,9 +266,23 @@ public OpenTelemetryRum build() {
SdkPreconfiguredRumBuilder delegate =
new SdkPreconfiguredRumBuilder(application, sdk, sessionId);
instrumentationInstallers.forEach(delegate::addInstrumentation);

notifyInitializationEnd(sdk);
return delegate.build();
}

private void notifyInitializationStart() {
for (InitializationListener listener : config.getInitializationListeners()) {
listener.onStart();
}
}

private void notifyInitializationEnd(OpenTelemetry openTelemetry) {
for (InitializationListener listener : config.getInitializationListeners()) {
listener.onEnd(openTelemetry);
}
}

/** Leverage the configuration to wire up various instrumentation components. */
private void applyConfiguration() {
if (config.shouldGenerateSdkInitializationEvents()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

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.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

/**
Expand All @@ -23,7 +28,9 @@ public class OtelRumConfig {
private boolean includeScreenAttributes = true;
private DiskBufferingConfiguration diskBufferingConfiguration =
DiskBufferingConfiguration.builder().build();

private boolean networkChangeMonitoringEnabled = true;
private final Set<InitializationListener> initializationListeners = new HashSet<>();

/**
* Configures the set of global attributes to emit with every span and event. Any existing
Expand Down Expand Up @@ -119,4 +126,17 @@ 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);
}

/**
* Provides an unmodifiable list of initialization listeners. This is mean to be called during
* RUM initialization.
*/
public Collection<InitializationListener> getInitializationListeners() {
return Collections.unmodifiableCollection(initializationListeners);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.instrumentation.startup

import io.opentelemetry.api.OpenTelemetry

/**
* Provides callbacks to know the sate of the initialization.
*/
interface InitializationListener {
/**
* Called when the RUM initialization starts.
*/
fun onStart()
Copy link
Contributor

Choose a reason for hiding this comment

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

We have found it helpful in upstream to have default no-op bodies for some of these, so that implementations only need to override the parts they actually care about. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! I tend to forget that interfaces' default functions exist 😅


/**
* Called when the RUM initialization ends.
* @param openTelemetry - The initialized OpenTelemetry instance.
*/
fun onEnd(openTelemetry: OpenTelemetry)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if this was onEnd(rum: OpenTelemetryRum) instead? The otel instance (like what you have here) as well as the session id are both obtainable from the OpenTelemetryRum instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, though now that I think about the whole thing after the proposal I wrote yesterday, it seems to me that these hooks might overlap with instrumentation installers. So maybe it's not needed to add another, similar mechanism if we decide to make that one public?

}