|
| 1 | +/*- |
| 2 | + * -\-\- |
| 3 | + * github-api |
| 4 | + * -- |
| 5 | + * Copyright (C) 2021 Spotify AB |
| 6 | + * -- |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * -/-/- |
| 19 | + */ |
| 20 | + |
| 21 | +package com.spotify.github.http.okhttp; |
| 22 | + |
| 23 | +import static okhttp3.MediaType.parse; |
| 24 | + |
| 25 | +import com.spotify.github.http.HttpClient; |
| 26 | +import com.spotify.github.http.HttpRequest; |
| 27 | +import com.spotify.github.http.HttpResponse; |
| 28 | +import com.spotify.github.http.ImmutableHttpRequest; |
| 29 | +import com.spotify.github.tracing.NoopTracer; |
| 30 | +import com.spotify.github.tracing.Span; |
| 31 | +import com.spotify.github.tracing.TraceHelper; |
| 32 | +import com.spotify.github.tracing.Tracer; |
| 33 | +import com.spotify.github.tracing.opencensus.OpenCensusTracer; |
| 34 | +import com.spotify.github.tracing.opentelemetry.OpenTelemetryTracer; |
| 35 | +import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTelemetry; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Optional; |
| 38 | +import java.util.concurrent.CompletableFuture; |
| 39 | +import okhttp3.*; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | + |
| 42 | +public class OkHttpHttpClient implements HttpClient { |
| 43 | + private final OkHttpClient client; |
| 44 | + private Tracer tracer; |
| 45 | + private Call.Factory callFactory; |
| 46 | + |
| 47 | + public OkHttpHttpClient(final OkHttpClient client) { |
| 48 | + this.client = client; |
| 49 | + this.tracer = NoopTracer.INSTANCE; |
| 50 | + this.callFactory = createTracedClient(); |
| 51 | + } |
| 52 | + |
| 53 | + public OkHttpHttpClient(final OkHttpClient client, final Tracer tracer) { |
| 54 | + this.client = client; |
| 55 | + this.tracer = tracer; |
| 56 | + this.callFactory = createTracedClient(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public CompletableFuture<HttpResponse> send(final HttpRequest httpRequest) { |
| 61 | + Request request = buildRequest(httpRequest); |
| 62 | + CompletableFuture<HttpResponse> future = new CompletableFuture<>(); |
| 63 | + if (this.callFactory == null) { |
| 64 | + this.callFactory = createTracedClient(); |
| 65 | + } |
| 66 | + this.callFactory |
| 67 | + .newCall(request) |
| 68 | + .enqueue( |
| 69 | + new Callback() { |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onResponse(@NotNull final Call call, @NotNull final Response response) |
| 73 | + throws IOException { |
| 74 | + future.complete(new OkHttpHttpResponse(httpRequest, response)); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onFailure(@NotNull final Call call, @NotNull final IOException e) { |
| 79 | + future.completeExceptionally(e); |
| 80 | + } |
| 81 | + }); |
| 82 | + return future; |
| 83 | + } |
| 84 | + |
| 85 | + // try (Span span = tracer.span(request)) { |
| 86 | + // if (this.callFactory == null) { |
| 87 | + // this.callFactory = this.tracer.createTracedClient(this.client); |
| 88 | + // } |
| 89 | + // final Call call = this.callFactory.newCall(request); |
| 90 | + // |
| 91 | + // final CompletableFuture<Response> future = new CompletableFuture<>(); |
| 92 | + // |
| 93 | + // // avoid multiple redirects |
| 94 | + // final AtomicBoolean redirected = new AtomicBoolean(false); |
| 95 | + // |
| 96 | + // call.enqueue( |
| 97 | + // new Callback() { |
| 98 | + // @Override |
| 99 | + // public void onFailure(@NotNull final Call call, final IOException e) { |
| 100 | + // future.completeExceptionally(e); |
| 101 | + // } |
| 102 | + // |
| 103 | + // @Override |
| 104 | + // public void onResponse(@NotNull final Call call, final Response response) { |
| 105 | + // processPossibleRedirects(response, redirected) |
| 106 | + // .handle( |
| 107 | + // (res, ex) -> { |
| 108 | + // if (Objects.nonNull(ex)) { |
| 109 | + // future.completeExceptionally(ex); |
| 110 | + // } else if (!res.isSuccessful()) { |
| 111 | + // try { |
| 112 | + // future.completeExceptionally(mapException(res, request)); |
| 113 | + // } catch (final Throwable e) { |
| 114 | + // future.completeExceptionally(e); |
| 115 | + // } finally { |
| 116 | + // if (res.body() != null) { |
| 117 | + // res.body().close(); |
| 118 | + // } |
| 119 | + // } |
| 120 | + // } else { |
| 121 | + // future.complete(res); |
| 122 | + // } |
| 123 | + // return res; |
| 124 | + // }); |
| 125 | + // } |
| 126 | + // }); |
| 127 | + // tracer.attachSpanToFuture(span, future); |
| 128 | + // return future; |
| 129 | + // } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void setTracer(final Tracer tracer) { |
| 133 | + this.tracer = tracer; |
| 134 | + this.callFactory = createTracedClient(); |
| 135 | + } |
| 136 | + |
| 137 | + private Request buildRequest(final HttpRequest request) { |
| 138 | + Request.Builder requestBuilder = new Request.Builder().url(request.url()); |
| 139 | + request |
| 140 | + .headers() |
| 141 | + .forEach( |
| 142 | + (key, values) -> { |
| 143 | + values.forEach(value -> requestBuilder.addHeader(key, value)); |
| 144 | + }); |
| 145 | + requestBuilder.method( |
| 146 | + request.method(), |
| 147 | + RequestBody.create(parse(javax.ws.rs.core.MediaType.APPLICATION_JSON), request.body())); |
| 148 | + return requestBuilder.build(); |
| 149 | + } |
| 150 | + |
| 151 | + private HttpRequest buildHttpRequest(final Request request) { |
| 152 | + return ImmutableHttpRequest.builder() |
| 153 | + .url(request.url().toString()) |
| 154 | + .method(request.method()) |
| 155 | + .headers(request.headers().toMultimap()) |
| 156 | + .body(Optional.ofNullable(request.body()).map(RequestBody::toString).orElse("")) |
| 157 | + .build(); |
| 158 | + } |
| 159 | + |
| 160 | + private Call.Factory createTracedClient() { |
| 161 | + if (this.tracer == null || this.tracer instanceof NoopTracer) { |
| 162 | + return createTracedClientNoopTracer(); |
| 163 | + } |
| 164 | + if (this.tracer instanceof OpenCensusTracer) { |
| 165 | + return createTracedClientOpenCensus(); |
| 166 | + } |
| 167 | + if (this.tracer instanceof OpenTelemetryTracer) { |
| 168 | + return createTracedClientOpenTelemetry(); |
| 169 | + } |
| 170 | + return createTracedClientNoopTracer(); |
| 171 | + } |
| 172 | + |
| 173 | + private Call.Factory createTracedClientNoopTracer() { |
| 174 | + return new Call.Factory() { |
| 175 | + @NotNull |
| 176 | + @Override |
| 177 | + public Call newCall(@NotNull final Request request) { |
| 178 | + return client.newCall(request); |
| 179 | + } |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + private Call.Factory createTracedClientOpenTelemetry() { |
| 184 | + return OkHttpTelemetry.builder(((OpenTelemetryTracer) this.tracer).getOpenTelemetry()) |
| 185 | + .build() |
| 186 | + .newCallFactory(client); |
| 187 | + } |
| 188 | + |
| 189 | + private Call.Factory createTracedClientOpenCensus() { |
| 190 | + return new Call.Factory() { |
| 191 | + @NotNull |
| 192 | + @Override |
| 193 | + public Call newCall(@NotNull final Request request) { |
| 194 | + CompletableFuture<Response> future = new CompletableFuture<>(); |
| 195 | + Span span = |
| 196 | + OkHttpHttpClient.this |
| 197 | + .tracer |
| 198 | + .span(buildHttpRequest(request)) |
| 199 | + .addTag(TraceHelper.TraceTags.HTTP_URL, request.url().toString()); |
| 200 | + OkHttpClient.Builder okBuilder = client.newBuilder(); |
| 201 | + okBuilder |
| 202 | + .networkInterceptors() |
| 203 | + .add( |
| 204 | + 0, |
| 205 | + new Interceptor() { |
| 206 | + @NotNull |
| 207 | + @Override |
| 208 | + public Response intercept(@NotNull final Chain chain) throws IOException { |
| 209 | + try { |
| 210 | + Response response = chain.proceed(chain.request()); |
| 211 | + span.addTag(TraceHelper.TraceTags.HTTP_STATUS_CODE, response.code()) |
| 212 | + .addTag(TraceHelper.TraceTags.HTTP_STATUS_MESSAGE, response.message()) |
| 213 | + .success(); |
| 214 | + future.complete(response); |
| 215 | + return response; |
| 216 | + } catch (Exception ex) { |
| 217 | + span.failure(ex); |
| 218 | + future.completeExceptionally(ex); |
| 219 | + throw ex; |
| 220 | + } finally { |
| 221 | + span.close(); |
| 222 | + } |
| 223 | + } |
| 224 | + }); |
| 225 | + |
| 226 | + return okBuilder.build().newCall(request); |
| 227 | + } |
| 228 | + }; |
| 229 | + } |
| 230 | +} |
0 commit comments