-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathSdkPreconfiguredRumBuilder.java
74 lines (63 loc) · 2.89 KB
/
SdkPreconfiguredRumBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.android;
import android.app.Application;
import io.opentelemetry.android.instrumentation.common.InstrumentedApplication;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public final class SdkPreconfiguredRumBuilder {
private final Application application;
private final OpenTelemetrySdk sdk;
private final SessionId sessionId;
private final List<Consumer<InstrumentedApplication>> instrumentationInstallers =
new ArrayList<>();
SdkPreconfiguredRumBuilder(Application application, OpenTelemetrySdk openTelemetrySdk) {
this(application, openTelemetrySdk, new SessionId(new SessionIdTimeoutHandler()));
}
SdkPreconfiguredRumBuilder(
Application application, OpenTelemetrySdk openTelemetrySdk, SessionId sessionId) {
this.application = application;
this.sdk = openTelemetrySdk;
this.sessionId = sessionId;
}
/**
* Adds an instrumentation installer function that will be run on an {@link
* InstrumentedApplication} instance as a part of the {@link #build()} method call.
*
* @return {@code this}
*/
public SdkPreconfiguredRumBuilder addInstrumentation(
Consumer<InstrumentedApplication> instrumentationInstaller) {
instrumentationInstallers.add(instrumentationInstaller);
return this;
}
/**
* Creates a new instance of {@link OpenTelemetryRum} with the settings of this {@link
* OpenTelemetryRumBuilder}.
*
* <p>This method uses a preconfigured OpenTelemetry SDK and install built-in system
* instrumentations in the passed Android {@link Application}.
*
* @return A new {@link OpenTelemetryRum} instance.
*/
public OpenTelemetryRum build() {
// the app state listeners need to be run in the first ActivityLifecycleCallbacks since they
// might turn off/on additional telemetry depending on whether the app is active or not
ApplicationStateWatcher applicationStateWatcher = new ApplicationStateWatcher();
application.registerActivityLifecycleCallbacks(applicationStateWatcher);
applicationStateWatcher.registerListener(sessionId.getTimeoutHandler());
Tracer tracer = sdk.getTracer(OpenTelemetryRum.class.getSimpleName());
sessionId.setSessionIdChangeListener(new SessionIdChangeTracer(tracer));
InstrumentedApplication instrumentedApplication =
new InstrumentedApplicationImpl(application, sdk, applicationStateWatcher);
for (Consumer<InstrumentedApplication> installer : instrumentationInstallers) {
installer.accept(instrumentedApplication);
}
return new OpenTelemetryRumImpl(sdk, sessionId);
}
}