|
| 1 | +package com.sap.ai.sdk.core.common; |
| 2 | + |
| 3 | +import com.google.common.annotations.Beta; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.UUID; |
| 6 | +import javax.annotation.Nonnull; |
| 7 | +import lombok.Getter; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.experimental.UtilityClass; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import lombok.val; |
| 12 | +import org.apache.hc.core5.http.ClassicHttpResponse; |
| 13 | +import org.apache.hc.core5.http.HttpEntity; |
| 14 | +import org.slf4j.MDC; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility for managing MDC (Mapped Diagnostic Context) for logging of AI Core requests. |
| 18 | + * |
| 19 | + * <p>This class is intended for internal use only. |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +@UtilityClass |
| 23 | +@Beta |
| 24 | +public class RequestLogContext { |
| 25 | + |
| 26 | + private static void setRequestId(@Nonnull final String requestId) { |
| 27 | + MDC.put(MdcKeys.REQUEST_ID, requestId); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Set the endpoint for the current request context. |
| 32 | + * |
| 33 | + * @param endpoint the endpoint URL |
| 34 | + */ |
| 35 | + public static void setEndpoint(@Nonnull final String endpoint) { |
| 36 | + MDC.put(MdcKeys.ENDPOINT, endpoint); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Set the destination for the current request context. |
| 41 | + * |
| 42 | + * @param destination the destination name |
| 43 | + */ |
| 44 | + public static void setDestination(@Nonnull final String destination) { |
| 45 | + MDC.put(MdcKeys.DESTINATION, destination); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Set the mode for the current request context. |
| 50 | + * |
| 51 | + * @param mode the request mode |
| 52 | + */ |
| 53 | + public static void setMode(@Nonnull final Mode mode) { |
| 54 | + MDC.put(MdcKeys.MODE, mode.getValue()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set the service for the current request context. |
| 59 | + * |
| 60 | + * @param service the service type |
| 61 | + */ |
| 62 | + public static void setService(@Nonnull final Service service) { |
| 63 | + MDC.put(MdcKeys.SERVICE, service.getValue()); |
| 64 | + } |
| 65 | + |
| 66 | + /** Clear all MDC request context information. */ |
| 67 | + public static void clear() { |
| 68 | + MDC.remove(MdcKeys.REQUEST_ID); |
| 69 | + MDC.remove(MdcKeys.ENDPOINT); |
| 70 | + MDC.remove(MdcKeys.DESTINATION); |
| 71 | + MDC.remove(MdcKeys.MODE); |
| 72 | + MDC.remove(MdcKeys.SERVICE); |
| 73 | + } |
| 74 | + |
| 75 | + /** Log the start of a request with generated request ID. */ |
| 76 | + public static void logRequestStart() { |
| 77 | + val reqId = UUID.randomUUID().toString().substring(0, 8); |
| 78 | + RequestLogContext.setRequestId(reqId); |
| 79 | + |
| 80 | + val message = "[reqId={}] Starting {} {} request to {}, destination={}"; |
| 81 | + log.debug( |
| 82 | + message, |
| 83 | + reqId, |
| 84 | + MDC.get(MdcKeys.SERVICE), |
| 85 | + MDC.get(MdcKeys.MODE), |
| 86 | + MDC.get(MdcKeys.ENDPOINT), |
| 87 | + MDC.get(MdcKeys.DESTINATION)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Log successful response with duration and size information. |
| 92 | + * |
| 93 | + * @param response the HTTP response |
| 94 | + */ |
| 95 | + public static void logResponseSuccess(@Nonnull final ClassicHttpResponse response) { |
| 96 | + if (!log.isDebugEnabled()) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + val headerTime = Optional.ofNullable(response.getFirstHeader("x-upstream-service-time")); |
| 101 | + val duration = headerTime.map(h -> h.getValue() + "ms").orElse("unknown"); |
| 102 | + val sizeInfo = |
| 103 | + Optional.ofNullable(response.getEntity()) |
| 104 | + .map(HttpEntity::getContentLength) |
| 105 | + .filter(length -> length >= 0) |
| 106 | + .map(length -> "%.1fKB".formatted(length / 1024.0)) |
| 107 | + .orElse("unknown"); |
| 108 | + val message = "[reqId={}] {} request completed successfully with duration={}, size={}."; |
| 109 | + log.debug(message, MDC.get(MdcKeys.REQUEST_ID), MDC.get(MdcKeys.SERVICE), duration, sizeInfo); |
| 110 | + } |
| 111 | + |
| 112 | + @UtilityClass |
| 113 | + private static class MdcKeys { |
| 114 | + private static final String REQUEST_ID = "reqId"; |
| 115 | + private static final String ENDPOINT = "endpoint"; |
| 116 | + private static final String DESTINATION = "destination"; |
| 117 | + private static final String MODE = "mode"; |
| 118 | + private static final String SERVICE = "service"; |
| 119 | + } |
| 120 | + |
| 121 | + /** Request execution modes. */ |
| 122 | + @RequiredArgsConstructor |
| 123 | + public enum Mode { |
| 124 | + /** Synchronous request mode */ |
| 125 | + SYNCHRONOUS("synchronous"), |
| 126 | + /** Streaming request mode */ |
| 127 | + STREAMING("streaming"); |
| 128 | + @Getter private final String value; |
| 129 | + } |
| 130 | + |
| 131 | + /** AI service types. */ |
| 132 | + @RequiredArgsConstructor |
| 133 | + public enum Service { |
| 134 | + /** OpenAI service */ |
| 135 | + OPENAI("openai"), |
| 136 | + /** Orchestration service */ |
| 137 | + ORCHESTRATION("orchestration"); |
| 138 | + @Getter private final String value; |
| 139 | + } |
| 140 | +} |
0 commit comments