Skip to content

Commit

Permalink
Cache HttpUrl for exporters that use OkHttp (#4616)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Jul 19, 2022
1 parent 514ae2c commit d84a111
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand All @@ -65,7 +66,7 @@ public final class OkHttpGrpcExporter<T extends Marshaler> implements GrpcExport
private final String type;
private final ExporterMetrics exporterMetrics;
private final OkHttpClient client;
private final String endpoint;
private final HttpUrl url;
private final Headers headers;
private final boolean compressionEnabled;

Expand All @@ -81,7 +82,7 @@ public final class OkHttpGrpcExporter<T extends Marshaler> implements GrpcExport
this.type = type;
this.exporterMetrics = ExporterMetrics.createGrpcOkHttp(exporterName, type, meterProvider);
this.client = client;
this.endpoint = endpoint;
this.url = HttpUrl.get(endpoint);
this.headers = headers;
this.compressionEnabled = compressionEnabled;
}
Expand All @@ -90,7 +91,7 @@ public final class OkHttpGrpcExporter<T extends Marshaler> implements GrpcExport
public CompletableResultCode export(T exportRequest, int numItems) {
exporterMetrics.addSeen(numItems);

Request.Builder requestBuilder = new Request.Builder().url(endpoint).headers(headers);
Request.Builder requestBuilder = new Request.Builder().url(url).headers(headers);

RequestBody requestBody = new GrpcRequestBody(exportRequest, compressionEnabled);
requestBuilder.post(requestBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -45,7 +46,7 @@ public final class OkHttpExporter<T extends Marshaler> {

private final String type;
private final OkHttpClient client;
private final String endpoint;
private final HttpUrl url;
@Nullable private final Headers headers;
private final boolean compressionEnabled;
private final Function<T, RequestBody> requestBodyCreator;
Expand All @@ -63,7 +64,7 @@ public final class OkHttpExporter<T extends Marshaler> {
boolean exportAsJson) {
this.type = type;
this.client = client;
this.endpoint = endpoint;
this.url = HttpUrl.get(endpoint);
this.headers = headers;
this.compressionEnabled = compressionEnabled;
this.requestBodyCreator = exportAsJson ? JsonRequestBody::new : ProtoRequestBody::new;
Expand All @@ -73,7 +74,7 @@ public final class OkHttpExporter<T extends Marshaler> {
public CompletableResultCode export(T exportRequest, int numItems) {
exporterMetrics.addSeen(numItems);

Request.Builder requestBuilder = new Request.Builder().url(endpoint);
Request.Builder requestBuilder = new Request.Builder().url(url);
if (headers != null) {
requestBuilder.headers(headers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.logging.Logger;
import javax.annotation.Nullable;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand All @@ -37,7 +38,7 @@ final class OkHttpGrpcService<ReqMarshalerT extends Marshaler, ResUnMarshalerT e

private final String type;
private final OkHttpClient client;
private final String endpoint;
private final HttpUrl url;
private final Headers headers;
private final boolean compressionEnabled;

Expand All @@ -50,15 +51,15 @@ final class OkHttpGrpcService<ReqMarshalerT extends Marshaler, ResUnMarshalerT e
boolean compressionEnabled) {
this.type = type;
this.client = client;
this.endpoint = endpoint;
this.url = HttpUrl.get(endpoint);
this.headers = headers;
this.compressionEnabled = compressionEnabled;
}

@Override
public ResUnMarshalerT execute(
ReqMarshalerT exportRequest, ResUnMarshalerT responseUnmarshaller) {
Request.Builder requestBuilder = new Request.Builder().url(endpoint).headers(headers);
Request.Builder requestBuilder = new Request.Builder().url(url).headers(headers);

RequestBody requestBody = new GrpcRequestBody(exportRequest, compressionEnabled);
requestBuilder.post(requestBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.util.HashMap;
import java.util.ServiceLoader;
import okhttp3.HttpUrl;
import org.junit.jupiter.api.Test;

public class JaegerRemoteSamplerProviderTest {
Expand Down Expand Up @@ -53,9 +54,10 @@ void serviceProvider() {
assertThat(s).extracting("serviceName").isEqualTo("test_service");
assertThat(s)
.extracting("delegate")
.extracting("endpoint")
.extracting("url")
.isEqualTo(
"http://localhost:9999/jaeger.api_v2.SamplingManager/GetSamplingStrategy");
HttpUrl.get(
"http://localhost:9999/jaeger.api_v2.SamplingManager/GetSamplingStrategy"));
}
});
}
Expand Down

0 comments on commit d84a111

Please sign in to comment.