Skip to content

[#90] Add okhttp metrics #91

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ ext {
lombok : '1.18.2', // Code gen
sitemapgen4j : '1.0.6', // Sitemap generator for SEO
jbcrypt : '0.4', // BCrypt salted hashing library

okhttpMetrics : '0.4.0', // Metrics for OkHttp

junit : '4.12', // Unit Testing
]
libs = [
okhttp : "com.squareup.okhttp3:okhttp:$versions.okhttp",
okhttpUrlConnection : "com.squareup.okhttp3:okhttp-urlconnection:$versions.okhttp",
okhttpMetrics : "com.raskasa.metrics:metrics-okhttp:$versions.okhttpMetrics",
loggingInterceptor : "com.squareup.okhttp3:logging-interceptor:$versions.okhttp",
jacksonCore : "com.fasterxml.jackson.core:jackson-core:$versions.jackson",
jacksonDatabind : "com.fasterxml.jackson.core:jackson-databind:$versions.jackson",
Expand Down
1 change: 1 addition & 0 deletions stubbornjava-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
compile libs.jooqCodegen
compile libs.flyway
compile libs.connectorj
compile libs.okhttpMetrics

testCompile libs.junit
testCompile libs.hsqldb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.raskasa.metrics.okhttp.InstrumentedOkHttpClients;

import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.Interceptor.Chain;
Expand Down Expand Up @@ -56,28 +58,37 @@ public static Interceptor getHeaderInterceptor(String name, String value) {
};
}

public static OkHttpClient wrapWithMetircs(String name, OkHttpClient client) {
return InstrumentedOkHttpClients.create(Metrics.registry(), client, name);
}

// {{start:client}}
private static final OkHttpClient client;
private static final OkHttpClient globalClient;
static {
globalClient = wrapWithMetircs("GlobalClient", defaultClientBuilder().build());
}

/*
* Global client that can be shared for common HTTP tasks.
*/
public static OkHttpClient globalClient() {
return globalClient;
}

/*
* Global client base for extending defaults.
* This is the same as the global client but without metrics enabled yet.
*/
public static OkHttpClient.Builder defaultClientBuilder() {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost(15);

client = new OkHttpClient.Builder()
return new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.dispatcher(dispatcher)
.addNetworkInterceptor(loggingInterceptor)
.build();
}

;

/*
* Global client that can be shared for common HTTP tasks.
*/
public static OkHttpClient globalClient() {
return client;
.addNetworkInterceptor(loggingInterceptor);
}
// {{end:client}}

Expand All @@ -87,7 +98,7 @@ public static OkHttpClient globalClient() {
* a stateful cookie jar. This is useful when you need
* to access password protected sites.
*/
public static OkHttpClient newClientWithCookieJar() {
public static OkHttpClient newClientWithCookieJar(OkHttpClient client) {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class MetricsReporters {

public static void startReporters(MetricRegistry registry) {
// Graphite reporter to Grafana Cloud
OkHttpClient client = new OkHttpClient.Builder()
//.addNetworkInterceptor(HttpClient.getLoggingInterceptor())
.build();
OkHttpClient client = HttpClient.wrapWithMetircs("GraphiteReporter",
new OkHttpClient.Builder()
.build());

String graphiteHost = Configs.properties().getString("metrics.graphite.host");
String grafanaApiKey = Configs.properties().getString("metrics.grafana.api_key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public Builder clientSecret(String clientSecret) {
}

public GitHubApi build() {
OkHttpClient client = HttpClient.globalClient()
.newBuilder()
OkHttpClient client = HttpClient.defaultClientBuilder()
.addInterceptor(HttpClient.getHeaderInterceptor("Accept", VERSION_HEADER))
.addInterceptor(GitHubApi.gitHubAuth(clientId, clientSecret))
.build();
return new GitHubApi(client);
OkHttpClient metricsClient = HttpClient.wrapWithMetircs("GithubApiClient", client);
return new GitHubApi(metricsClient);
}
}

Expand Down