-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseApplication.kt
More file actions
123 lines (107 loc) · 4.55 KB
/
BaseApplication.kt
File metadata and controls
123 lines (107 loc) · 4.55 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.example.androidobservability
import android.app.Application
import android.util.Log
import android.widget.ImageView
import com.launchdarkly.observability.api.ObservabilityOptions
import com.launchdarkly.observability.client.TelemetryInspector
import com.launchdarkly.observability.plugin.Observability
import com.launchdarkly.observability.replay.PrivacyProfile
import com.launchdarkly.observability.replay.ReplayOptions
import com.launchdarkly.observability.replay.plugin.SessionReplay
import com.launchdarkly.observability.replay.view
import com.launchdarkly.sdk.ContextKind
import com.launchdarkly.sdk.LDContext
import com.launchdarkly.sdk.android.Components
import com.launchdarkly.sdk.android.LDAndroidLogging
import com.launchdarkly.sdk.android.LDClient
import com.launchdarkly.sdk.android.LDConfig
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import com.launchdarkly.observability.sdk.LDReplay
import com.launchdarkly.sdk.android.FeatureFlagChangeListener
open class BaseApplication : Application() {
companion object {
const val LAUNCHDARKLY_MOBILE_KEY = BuildConfig.LAUNCHDARKLY_MOBILE_KEY
}
var observabilityOptions = ObservabilityOptions(
resourceAttributes = Attributes.of(
AttributeKey.stringKey("example"), "value"
),
debug = true,
otlpEndpoint = BuildConfig.OTLP_ENDPOINT,
backendUrl = BuildConfig.BACKEND_URL,
tracesApi = ObservabilityOptions.TracesApi.enabled(),
metricsApi = ObservabilityOptions.MetricsApi.enabled(),
instrumentations = ObservabilityOptions.Instrumentations(
crashReporting = true, launchTime = true, activityLifecycle = true
),
logAdapter = LDAndroidLogging.adapter(),
)
var telemetryInspector: TelemetryInspector? = null
var testUrl: String? = null
open fun realInit() {
val observabilityPlugin = Observability(
application = this@BaseApplication,
mobileKey = LAUNCHDARKLY_MOBILE_KEY,
options = testUrl?.let { observabilityOptions.copy(backendUrl = it, otlpEndpoint = it) } ?: observabilityOptions
)
val sessionReplayPlugin = SessionReplay(
options = ReplayOptions(
enabled = false,
privacyProfile = PrivacyProfile(
maskText = false,
maskWebViews = true,
maskViews = listOf(
view(ImageView::class.java),
),
maskXMLViewIds = listOf("smoothieTitle")
)
)
)
// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly mobile key found on the LaunchDarkly
// dashboard in the start guide.
// If you want to disable the Auto EnvironmentAttributes functionality.
// Use AutoEnvAttributes.Disabled as the argument to the Builder
val ldConfig = LDConfig.Builder(LDConfig.Builder.AutoEnvAttributes.Enabled)
.mobileKey(LAUNCHDARKLY_MOBILE_KEY)
.plugins(
Components.plugins().setPlugins(
listOf(
observabilityPlugin,
sessionReplayPlugin
)
)
)
.build()
// Set up the context properties. This context should appear on your LaunchDarkly context
// dashboard soon after you run the demo.
val context = LDContext.builder(ContextKind.DEFAULT, "example-user-key")
.anonymous(true)
.build()
LDClient.init(this@BaseApplication, ldConfig, context, 1)
val anonContext = LDContext.builder(ContextKind.DEFAULT, "anonymous-userkey")
.anonymous(true)
.build()
//LDClient.get().identify(anonContext)
telemetryInspector = observabilityPlugin.getTelemetryInspector()
if (testUrl == null) {
// intervenes in E2E tests by trigger spans
flagEvaluation()
}
LDReplay.start()
}
fun flagEvaluation() {
val flagKey = "feature1"
val value = LDClient.get().boolVariation(flagKey, false)
Log.i("flag", "sync ${flagKey} value= ${value}")
val listener = FeatureFlagChangeListener {
val newValue = LDClient.get().boolVariation(flagKey, false)
Log.i("flag", "listened ${flagKey} value= ${newValue}")
}
LDClient.get().registerFeatureFlagListener(flagKey, listener)
}
override fun onCreate() {
super.onCreate()
realInit()
}
}