-
Notifications
You must be signed in to change notification settings - Fork 1
/
OtelExamples.kt
166 lines (153 loc) · 4.93 KB
/
OtelExamples.kt
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
package io.opentelemetry.demo.android
import android.annotation.SuppressLint
import android.util.Log
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.demo.android.tracing.error
import io.opentelemetry.demo.android.tracing.span
import io.opentelemetry.demo.android.tracing.spanStart
import io.opentelemetry.demo.android.tracing.tong
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTelemetry
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.http.GET
import toothpick.ktp.KTP
import toothpick.ktp.delegate.inject
/**
* @author Ponomarev Leonid ([email protected])
* @since 17.03.2023
*/
class OtelExamples {
private val tracer: Tracer by inject()
private val telemetry: OpenTelemetry by inject()
init {
KTP.openRootScope().inject(this)
}
interface ApiSpec {
@GET("/search?q=opentelemetry+android+example")
fun query(): Completable
@GET("/404")
fun error(): Completable
}
private val api: ApiSpec =
Retrofit.Builder()
.baseUrl("https://google.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.callFactory(
OkHttpTelemetry.builder(telemetry).build()
.newCallFactory(OkHttpClient.Builder().build())
)
.build()
.create(ApiSpec::class.java)
fun run() {
sync()
rxSync()
rxAsync()
retrofit()
}
fun sync() {
tracer.span("sync") {
try {
Thread.sleep(150)
tracer.span("sync.subspan.1") {
Thread.sleep(100)
}
tracer.span("sync.subspan.2") {
Thread.sleep(200)
}
tracer.span("sync.subspan.3") {
Thread.sleep(50)
throw RuntimeException("sync error")
}
} catch (t: Throwable) {
tracer.error(t)
}
}
}
@SuppressLint("CheckResult")
fun rxSync() {
tracer.span("rx.sync") {
Observable.just("label")
.doOnNext {
tracer.span("rx.sync.subspan.1") {
Thread.sleep(250)
}
}
.flatMapSingle {
Single.fromCallable {
tracer.span("rx.sync.subspan.2") {
Thread.sleep(50)
}
}
}
.subscribe({
tracer.span("rx.sync.subspan.3") {
Thread.sleep(250)
}
throw RuntimeException("rx.sync exception")
})
{ throwable: Throwable ->
Log.w(TAG, "", throwable)
tracer.error(throwable)
}
}
}
@SuppressLint("CheckResult")
fun rxAsync() {
tracer.spanStart("rx.scheduled") { root ->
val obs = Observable.create<Unit> { emitter ->
emitter.onNext(Unit)
emitter.onNext(Unit)
emitter.onError(RuntimeException("rx.scheduled exception"))
}
obs
.doOnNext {
tracer.tong("rx.scheduled.subspan.1", root) {
Thread.sleep(250)
}
}
.doOnNext {
tracer.tong("rx.scheduled.subspan.2", root) {
Thread.sleep(150)
}
}
.flatMapSingle {
Single.fromCallable {
tracer.span("rx.scheduled.subspan.3", root) {
Thread.sleep(50)
}
}
}
.doOnTerminate { root.end() }
.subscribeOn(Schedulers.io())
.subscribe(
{
tracer.tong("rx.scheduled.subspan.subscribe", root) {
Thread.sleep(150)
}
},
{ throwable ->
tracer.error(throwable)
Log.w(TAG, "", throwable)
}
)
}
}
@SuppressLint("CheckResult")
fun retrofit() {
tracer.spanStart("retrofit") {
api.query()
.andThen(api.error())
.doOnTerminate { it.end() }
.subscribe({/* ignore */ }, {/* swallow */ })
}
}
companion object {
const val TAG = "OTelExamples"
}
}