Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.retrofit;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Invocation;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
public final class CustomAnnotation {
public interface Browse {
@GET("/")
@WarnIfSlow(max = 500, timeUnit = MILLISECONDS)
Call<ResponseBody> home();
}
@Documented
@Target(METHOD)
@Retention(RUNTIME)
public @interface WarnIfSlow {
long max();
TimeUnit timeUnit();
}
static final class WarnIfSlowInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long startNanos = System.nanoTime();
Response response = chain.proceed(request);
long elapsedNanos = System.nanoTime() - startNanos;
Invocation invocation = request.tag(Invocation.class);
WarnIfSlow warnIfSlow = invocation.method().getAnnotation(WarnIfSlow.class);
if (warnIfSlow != null && elapsedNanos > warnIfSlow.timeUnit().toNanos(warnIfSlow.max())) {
System.out.printf("SLOW METHOD WARNING: %s.%s %s HTTP %s (%.0f ms)%n",
invocation.method().getDeclaringClass().getSimpleName(),
invocation.method().getName(),
invocation.arguments(),
response.code(),
elapsedNanos / 1_000_000.0);
}
return response;
}
}
public static void main(String... args) throws IOException {
WarnIfSlowInterceptor warnIfSlowInterceptor = new WarnIfSlowInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(warnIfSlowInterceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://square.com/")
.callFactory(okHttpClient)
.build();
Browse browse = retrofit.create(Browse.class);
browse.home().execute();
}
}
@swankjesse

This comment has been minimized.

Copy link
Owner Author

@swankjesse swankjesse commented Nov 18, 2018

Though on second thought, this might not be the greatest example. You can already do timings in CallAdapter.Factory!

Invocation is cool ’cause you get the original parameters passed to the API method.

@blackberry2016

This comment has been minimized.

Copy link

@blackberry2016 blackberry2016 commented Dec 15, 2018

Invocation invocation = request.tag(Invocation.class);

invocation may be null, so add if (invocation != null) {}

When i share a singleton OkHttpClient with glide to load image, invocation is null
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.reflect.Method retrofit2.Invocation.method()' on a null object reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment