|
| 1 | +package datadog.communication.otlp; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.OptionalInt; |
| 5 | + |
| 6 | +/** |
| 7 | + * Encapsulates the result of an OTLP export attempt. |
| 8 | + * |
| 9 | + * <p>If communication fails or times out, the response will NOT be successful and will lack a |
| 10 | + * status code, but will have an exception. |
| 11 | + * |
| 12 | + * <p>If communication succeeds, the response will have a status code and will be marked as success |
| 13 | + * or failure in accordance with the code. |
| 14 | + * |
| 15 | + * <p>NOTE: A successful communication may still contain an exception if there was a problem parsing |
| 16 | + * the response. |
| 17 | + */ |
| 18 | +public final class OtlpResponse { |
| 19 | + /** Factory method for a successful request with a trivial response body */ |
| 20 | + public static OtlpResponse success(final int status) { |
| 21 | + return new OtlpResponse(true, status, null, null); |
| 22 | + } |
| 23 | + |
| 24 | + /** Factory method for a successful request with a response body */ |
| 25 | + public static OtlpResponse success(final int status, String response) { |
| 26 | + return new OtlpResponse(true, status, null, response); |
| 27 | + } |
| 28 | + |
| 29 | + /** Factory method for a successful request with a malformed response body */ |
| 30 | + public static OtlpResponse successWithParseError(final int status, final Throwable exception) { |
| 31 | + return new OtlpResponse(true, status, exception, null); |
| 32 | + } |
| 33 | + |
| 34 | + /** Factory method for a request that received an error status in response */ |
| 35 | + public static OtlpResponse failed(final int status) { |
| 36 | + return new OtlpResponse(false, status, null, null); |
| 37 | + } |
| 38 | + |
| 39 | + /** Factory method for a request that received an error status and a response body */ |
| 40 | + public static OtlpResponse failed(final int status, String response) { |
| 41 | + return new OtlpResponse(false, status, null, response); |
| 42 | + } |
| 43 | + |
| 44 | + /** Factory method for a failed communication attempt */ |
| 45 | + public static OtlpResponse failed(final Throwable exception) { |
| 46 | + return new OtlpResponse(false, null, exception, null); |
| 47 | + } |
| 48 | + |
| 49 | + private final boolean success; |
| 50 | + private final Integer status; |
| 51 | + private final Throwable exception; |
| 52 | + private final String response; |
| 53 | + |
| 54 | + private OtlpResponse( |
| 55 | + final boolean success, final Integer status, final Throwable exception, String response) { |
| 56 | + this.success = success; |
| 57 | + this.status = status; |
| 58 | + this.exception = exception; |
| 59 | + this.response = response; |
| 60 | + } |
| 61 | + |
| 62 | + public boolean success() { |
| 63 | + return success; |
| 64 | + } |
| 65 | + |
| 66 | + public OptionalInt status() { |
| 67 | + return status == null ? OptionalInt.empty() : OptionalInt.of(status); |
| 68 | + } |
| 69 | + |
| 70 | + public Optional<Throwable> exception() { |
| 71 | + return Optional.ofNullable(exception); |
| 72 | + } |
| 73 | + |
| 74 | + /** The raw response body, or empty for failed communication attempts (no body received) */ |
| 75 | + public Optional<String> response() { |
| 76 | + return Optional.ofNullable(response); |
| 77 | + } |
| 78 | +} |
0 commit comments