Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up an OkHttp Dispatcher to propagate context into calls. #102

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.splunk.android.sample;

import static org.apache.http.conn.ssl.SSLSocketFactory.SSL;
import static io.opentelemetry.api.common.AttributeKey.longKey;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -35,6 +38,7 @@
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
Expand All @@ -43,15 +47,15 @@

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static io.opentelemetry.api.common.AttributeKey.longKey;
import static org.apache.http.conn.ssl.SSLSocketFactory.SSL;

public class FirstFragment extends Fragment {

private final MutableLiveData<String> httpResponse = new MutableLiveData<>();
Expand Down Expand Up @@ -103,22 +107,25 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
}

private void makeCall(String url, Span workflow) {
Call call = okHttpClient.newCall(new Request.Builder().url(url).get().build());
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
httpResponse.postValue("error");
workflow.setStatus(StatusCode.ERROR);
workflow.end();
}
//make sure the span is in the current context so it can be propagated into the async call.
try (Scope scope = workflow.makeCurrent()) {
Call call = okHttpClient.newCall(new Request.Builder().url(url).get().build());
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
httpResponse.postValue("error");
workflow.setStatus(StatusCode.ERROR);
workflow.end();
}

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
int responseCode = response.code();
httpResponse.postValue("" + responseCode);
workflow.end();
}
});
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
int responseCode = response.code();
httpResponse.postValue("" + responseCode);
workflow.end();
}
});
}
}

public LiveData<String> getHttpResponse() {
Expand All @@ -142,7 +149,11 @@ public void onResume() {
}

private OkHttpClient buildOkHttpClient(SplunkRum splunkRum) {
//grab the default executor service that okhttp uses, and wrap it with one that will propagate the otel context.
ExecutorService delegateExecutorService = new Dispatcher().executorService();
ExecutorService contextAwareExecutorService = Context.taskWrapping(delegateExecutorService);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dispatcher(new Dispatcher(contextAwareExecutorService))
.addInterceptor(splunkRum.createOkHttpRumInterceptor());
try {
// NOTE: This is really bad and dangerous. Don't ever do this in the real world.
Expand Down