cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dynatrace AppMon Android App Instrumentation

nirdoshs
Newcomer

Please advise on the following.

Trying to add the Dynatrace
header to our service calls. We make use of the Retrofit library in order
to do service calls. We can add the header easy enough with a request
interceptor.

But I don’t know when the
request was “handled” and therefore I do not kwow where/when to call action.leaveAction();

DTXAction
action = Dynatrace.enterAction("<action_name>");

Request
request = new Request.Builder()


.url("http://myhost.mydomain.com/example")


.addHeader(Dynatrace.getRequestTagHeader(), action.getRequestTag())


.build();

// handle web
request

action.leaveAction();

5 REPLIES 5

Thomas_Wirth1
Dynatrace Pro
Dynatrace Pro

Hi Nirdosh

The web request is sent to the server, when you call the method Call.enqueue or Call.execute. The leaveAction method should be called after the Call.execute method or in the Callback methods from the Call.enqueue method. There are also two (okhttp3) samples in the 7.0 documentation. It should be very easy to reuse the instrumentation logic for your retrofit2 web requests, because retrofit2 is based on okhttp3.

Another option would be to use the auto-instrumentor. The 6.5 auto-instrumentor automatically instruments retrofit2 web requests.

Best regards

Thomas

Thank you Thomas for the quick and detailed response. I've asked the developer to try and will get back to you soon.


Hi Thomas,


Thanks for the reply.

What we're trying to achieve is to have an interceptor on the okhttp3 client such as:


new Interceptor() {

@Override public Response intercept(Chain chain) throws IOException {
DTXAction action = Dynatrace.enterAction("<action_name>");
Request original = chain.request();
Request request = original.newBuilder()
.addHeader(Dynatrace.getRequestTagHeader(), action.getRequestTag())
.method(original.method(), original.body())
.build();
return chain.proceed(request);}
});


The problem with the above is that we do not have access to the DTXAction object on the callback so it's impossible to call action.leaveAction() since when the request has been completed.

Any suggestions welcomed.

You can stop the timing of the action after the proceed method call. See the following example:

new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
DTXAction action = Dynatrace.enterAction("<action_name>");
Request original = chain.request();
Request request = original.newBuilder()
.addHeader(Dynatrace.getRequestTagHeader(), action.getRequestTag())
.method(original.method(), original.body())
.build();
Response response = chain.proceed(request);
action.leaveAction();
return response;
}
});

But you also have to consider the error cases and therefore I recommend the following approach:

new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
DTXAction action = Dynatrace.enterAction("<action_name>");

try {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader(Dynatrace.getRequestTagHeader(), action.getRequestTag())
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
finally {
action.leaveAction();
}
}
});

Thank you @Thomas W. This is exactly what we needed 🙂