-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathViewModel.kt
More file actions
210 lines (181 loc) · 6.91 KB
/
ViewModel.kt
File metadata and controls
210 lines (181 loc) · 6.91 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.example.androidobservability
import android.app.Application
import android.content.Intent
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.launchdarkly.observability.interfaces.Metric
import com.launchdarkly.observability.sdk.LDObserve
import com.launchdarkly.sdk.ContextKind
import com.launchdarkly.sdk.LDContext
import com.launchdarkly.sdk.android.LDClient
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.logs.Severity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL
class ViewModel(application: Application) : AndroidViewModel(application) {
fun triggerMetric() {
LDObserve.recordMetric(Metric("test-gauge", 50.0))
}
fun triggerHistogramMetric() {
LDObserve.recordHistogram(Metric("test-histogram", 15.0))
}
fun triggerCountMetric() {
LDObserve.recordCount(Metric("test-counter", 10.0))
}
fun triggerIncrementalMetric() {
LDObserve.recordIncr(Metric("test-incremental-counter", 12.0))
}
fun triggerUpDownCounterMetric() {
LDObserve.recordUpDownCounter(Metric("test-up-down-counter", 25.0))
}
fun triggerError() {
LDObserve.recordError(
Error("Manual error womp womp", Error("The error that caused the other error.")),
Attributes.of(AttributeKey.stringKey("FakeAttribute"), "FakeVal")
)
}
fun triggerLog() {
LDObserve.recordLog(
"Test Log",
Severity.INFO,
Attributes.builder()
.put(AttributeKey.stringKey("test-string"), "maui")
.put(AttributeKey.booleanKey("test-true"), true)
.put(AttributeKey.booleanKey("test-false"), false)
.put(AttributeKey.longKey("test-integer"), 42L)
.put(AttributeKey.doubleKey("test-double"), 3.14)
.put(AttributeKey.doubleArrayKey("test-array"), listOf(3.14))
.put(AttributeKey.longArrayKey("test-nested.array"), listOf(1L))
.build()
)
}
fun triggerCustomLog(
message: String,
severity: Severity = Severity.INFO,
attributes: Attributes = Attributes.empty()
) {
if (message.isNotEmpty()) {
LDObserve.recordLog(
message = message,
severity = severity,
attributes = attributes
)
}
}
fun triggerCustomSpan(spanName: String) {
if (spanName.isNotEmpty()) {
viewModelScope.launch(Dispatchers.IO) {
val customSpan = LDObserve.startSpan(
name = spanName,
attributes = Attributes.of(
AttributeKey.stringKey("custom_span"), "true"
)
)
customSpan.end()
}
}
}
fun triggerNestedSpans() {
viewModelScope.launch(Dispatchers.IO) {
val newSpan0 = LDObserve.startSpan("NestedSpan", Attributes.empty())
newSpan0.makeCurrent().use {
val newSpan1 = LDObserve.startSpan("NestedSpan1", Attributes.empty())
newSpan1.makeCurrent().use {
val newSpan2 = LDObserve.startSpan("NestedSpan2", Attributes.empty())
newSpan2.makeCurrent().use {
LDObserve.recordCount(Metric("test-counter", 10.0))
LDObserve.recordLog("NestedLog", Severity.INFO, Attributes.empty())
sendOkHttpRequest()
sendURLRequest()
newSpan2.end()
}
newSpan1.end()
}
newSpan0.end()
}
}
}
fun triggerCrash() {
throw RuntimeException("Failed to connect to bogus server.")
}
fun triggerHttpRequests() {
viewModelScope.launch(Dispatchers.IO) {
sendOkHttpRequest()
sendURLRequest()
}
}
fun identifyLDContext(contextKey: String = "test-context-key") {
val context = LDContext.builder(ContextKind.DEFAULT, contextKey)
.name("test-context-name")
.build()
LDClient.get().identify(context)
}
fun identifyUser() {
val userContext = LDContext.builder(ContextKind.DEFAULT, "single-userkey")
.name("Bob Bobberson")
.build()
LDClient.get().identify(userContext)
}
fun identifyAnonymous() {
val anonContext = LDContext.builder(ContextKind.DEFAULT, "anonymous-userkey")
.anonymous(true)
.build()
LDClient.get().identify(anonContext)
}
fun identifyMulti() {
val userContext = LDContext.builder(ContextKind.DEFAULT, "multi-username")
.name("multi-username")
.build()
val deviceContext = LDContext.builder(ContextKind.of("device"), "iphone")
.name("iphone")
.build()
val multiContext = LDContext.createMulti(userContext, deviceContext)
LDClient.get().identify(multiContext)
}
fun evaluateBooleanFlag(flagKey: String) {
if (flagKey.isNotEmpty()) {
val result = LDClient.get().boolVariation(flagKey, false)
Toast.makeText(getApplication(), "Flag $flagKey: $result", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(getApplication(), "Flag key cannot be empty", Toast.LENGTH_SHORT).show()
}
}
fun startForegroundService() {
val intent = Intent(getApplication(), ObservabilityForegroundService::class.java)
ContextCompat.startForegroundService(getApplication(), intent)
}
fun startBackgroundService() {
val intent = Intent(getApplication(), ObservabilityBackgroundService::class.java)
getApplication<Application>().startService(intent)
}
private fun sendOkHttpRequest() {
// Create HTTP client
val client = OkHttpClient()
// Build request
val request: Request = Request.Builder()
.url("https://www.google.com")
.build()
client.newCall(request).execute().use { response ->
println("Response code: " + response.code)
println("Response body: " + response.body?.string())
}
}
private fun sendURLRequest() {
val url = URL("https://www.android.com/")
val urlConnection = url.openConnection() as HttpURLConnection
try {
val output = BufferedInputStream(urlConnection.inputStream).bufferedReader().use { it.readText() }
println("URLRequest output: $output")
} finally {
urlConnection.disconnect()
}
}
}