|
| 1 | +package datadog.telemetry; |
| 2 | + |
| 3 | +import static datadog.telemetry.RequestBuilderProvider.DD_TELEMETRY_REQUEST_TYPE; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.Request; |
| 8 | +import okhttp3.Response; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +public class HttpClient { |
| 13 | + public enum Result { |
| 14 | + SUCCESS, |
| 15 | + FAILURE, |
| 16 | + NOT_FOUND |
| 17 | + } |
| 18 | + |
| 19 | + private static final Logger log = LoggerFactory.getLogger(HttpClient.class); |
| 20 | + |
| 21 | + private final OkHttpClient httpClient; |
| 22 | + |
| 23 | + public HttpClient(OkHttpClient httpClient) { |
| 24 | + this.httpClient = httpClient; |
| 25 | + } |
| 26 | + |
| 27 | + public Result sendRequest(Request request) { |
| 28 | + String requestType = request.header(DD_TELEMETRY_REQUEST_TYPE); |
| 29 | + try (Response response = httpClient.newCall(request).execute()) { |
| 30 | + if (response.code() == 404) { |
| 31 | + log.debug("Telemetry endpoint is disabled, dropping {} message", requestType); |
| 32 | + return Result.NOT_FOUND; |
| 33 | + } |
| 34 | + if (!response.isSuccessful()) { |
| 35 | + log.debug( |
| 36 | + "Telemetry message {} failed with: {} {} ", |
| 37 | + requestType, |
| 38 | + response.code(), |
| 39 | + response.message()); |
| 40 | + return Result.FAILURE; |
| 41 | + } |
| 42 | + } catch (IOException e) { |
| 43 | + log.debug("Telemetry message {} failed with exception: {}", requestType, e.toString()); |
| 44 | + return Result.FAILURE; |
| 45 | + } |
| 46 | + |
| 47 | + log.debug("Telemetry message {} sent successfully", requestType); |
| 48 | + return Result.SUCCESS; |
| 49 | + } |
| 50 | +} |
0 commit comments