Skip to content

Commit

Permalink
Add the Otel parent span for OkHttp request use case in sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusc83 committed Jun 5, 2024
1 parent 6a975fc commit 178a467
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class ViewModelFactory(
WebViewModel(localServer) as T
}
OtelTracesViewModel::class.java -> {
OtelTracesViewModel() as T
OtelTracesViewModel(okHttpClient, localServer) as T
}
else -> {
modelClass.newInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ internal class OtelTracesFragment : Fragment(), View.OnClickListener {
return rootView
}

override fun onResume() {
super.onResume()
viewModel.onResume()
}

override fun onPause() {
viewModel.onPause()
super.onPause()
}

@Suppress("UnsafeCallOnNullableType") // not an issue in the sample
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@ package com.datadog.android.sample.traces
import android.os.AsyncTask
import androidx.lifecycle.ViewModel
import com.datadog.android.log.Logger
import com.datadog.android.okhttp.otel.addParentSpan
import com.datadog.android.sample.BuildConfig
import com.datadog.android.vendor.sample.LocalServer
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.context.Scope
import okhttp3.OkHttpClient
import okhttp3.Request

@Suppress("DEPRECATION")
internal class OtelTracesViewModel : ViewModel() {
internal class OtelTracesViewModel(
private val okHttpClient: OkHttpClient,
private val localServer: LocalServer

) : ViewModel() {

private var asyncOperationTask: AsyncTask<Unit, Unit, Unit>? = null
private var chainedContextsTask: AsyncTask<Unit, Unit, Unit>? = null
private var linkedSpansTask: AsyncTask<Unit, Unit, Unit>? = null

fun onResume() {
localServer.start("https://www.datadoghq.com/")
}

fun onPause() {
localServer.stop()
}

fun startAsyncOperation(
onProgress: (Int) -> Unit = {},
onDone: () -> Unit = {}
Expand All @@ -34,7 +50,11 @@ internal class OtelTracesViewModel : ViewModel() {
}

fun startChainedContexts(onDone: () -> Unit = {}) {
chainedContextsTask = ChainedContextsTask(onDone)
chainedContextsTask = ChainedContextsTask(
localServer.getUrl(),
okHttpClient,
onDone
)
chainedContextsTask?.execute()
}

Expand Down Expand Up @@ -123,6 +143,8 @@ internal class OtelTracesViewModel : ViewModel() {
// region ChainedContextsTask

private class ChainedContextsTask(
private val url: String,
private val okHttpClient: OkHttpClient,
val onDone: () -> Unit
) : AsyncTask<Unit, Unit, Unit>() {
private val tracer: Tracer = GlobalOpenTelemetry.get()
Expand Down Expand Up @@ -167,7 +189,12 @@ internal class OtelTracesViewModel : ViewModel() {
logger.v("Sanitizing username: $username")
Thread.sleep(2000)
processingSanitization.end()
Thread.sleep(5000)
val request = Request.Builder()
.get()
.url(url)
.addParentSpan(processingFormSpan)
.build()
okHttpClient.newCall(request).execute()
formScope.close()
processingFormSpan.end()
}
Expand Down

0 comments on commit 178a467

Please sign in to comment.