|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.awssdk.v2_2; |
| 7 | + |
| 8 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Base64; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
| 13 | +import javax.annotation.Nullable; |
| 14 | +import software.amazon.awssdk.core.SdkRequest; |
| 15 | +import software.amazon.awssdk.protocols.jsoncore.JsonNode; |
| 16 | +import software.amazon.awssdk.protocols.jsoncore.internal.ObjectJsonNode; |
| 17 | +import software.amazon.awssdk.protocols.jsoncore.internal.StringJsonNode; |
| 18 | +import software.amazon.awssdk.services.lambda.model.InvokeRequest; |
| 19 | + |
| 20 | +// this class is only used from LambdaAccess from method with @NoMuzzle annotation |
| 21 | + |
| 22 | +// Direct lambda invocations (e.g., not through an api gateway) currently strip |
| 23 | +// away the otel propagation headers (but leave x-ray ones intact). Use the |
| 24 | +// custom client context header as an additional propagation mechanism for this |
| 25 | +// very specific scenario. For reference, the header is named "X-Amz-Client-Context" but the api to |
| 26 | +// manipulate it abstracts that away. The client context field is documented in |
| 27 | +// https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html#API_Invoke_RequestParameters |
| 28 | + |
| 29 | +final class LambdaImpl { |
| 30 | + static { |
| 31 | + // Force loading of InvokeRequest; this ensures that an exception is thrown at this point when |
| 32 | + // the Lambda library is not present, which will cause DirectLambdaAccess to have |
| 33 | + // enabled=false in library mode. |
| 34 | + @SuppressWarnings("unused") |
| 35 | + String invokeRequestName = InvokeRequest.class.getName(); |
| 36 | + // was added in 2.17.0 |
| 37 | + @SuppressWarnings("unused") |
| 38 | + String jsonNodeName = JsonNode.class.getName(); |
| 39 | + } |
| 40 | + |
| 41 | + private static final String CLIENT_CONTEXT_CUSTOM_FIELDS_KEY = "custom"; |
| 42 | + static final int MAX_CLIENT_CONTEXT_LENGTH = 3583; // visible for testing |
| 43 | + |
| 44 | + private LambdaImpl() {} |
| 45 | + |
| 46 | + @Nullable |
| 47 | + static SdkRequest modifyRequest( |
| 48 | + SdkRequest request, io.opentelemetry.context.Context otelContext) { |
| 49 | + if (isDirectLambdaInvocation(request)) { |
| 50 | + return modifyOrAddCustomContextHeader((InvokeRequest) request, otelContext); |
| 51 | + } |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + static boolean isDirectLambdaInvocation(SdkRequest request) { |
| 56 | + return request instanceof InvokeRequest; |
| 57 | + } |
| 58 | + |
| 59 | + static SdkRequest modifyOrAddCustomContextHeader( |
| 60 | + InvokeRequest request, io.opentelemetry.context.Context otelContext) { |
| 61 | + InvokeRequest.Builder builder = request.toBuilder(); |
| 62 | + // Unfortunately the value of this thing is a base64-encoded json with a character limit; also |
| 63 | + // therefore not comma-composable like many http headers |
| 64 | + String clientContextString = request.clientContext(); |
| 65 | + String clientContextJsonString = "{}"; |
| 66 | + if (clientContextString != null && !clientContextString.isEmpty()) { |
| 67 | + clientContextJsonString = |
| 68 | + new String(Base64.getDecoder().decode(clientContextString), StandardCharsets.UTF_8); |
| 69 | + } |
| 70 | + JsonNode jsonNode = JsonNode.parser().parse(clientContextJsonString); |
| 71 | + if (!jsonNode.isObject()) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + JsonNode customNode = |
| 75 | + jsonNode |
| 76 | + .asObject() |
| 77 | + .computeIfAbsent( |
| 78 | + CLIENT_CONTEXT_CUSTOM_FIELDS_KEY, (k) -> new ObjectJsonNode(new LinkedHashMap<>())); |
| 79 | + if (!customNode.isObject()) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + Map<String, JsonNode> map = customNode.asObject(); |
| 83 | + GlobalOpenTelemetry.getPropagators() |
| 84 | + .getTextMapPropagator() |
| 85 | + .inject(otelContext, map, (nodes, key, value) -> nodes.put(key, new StringJsonNode(value))); |
| 86 | + if (map.isEmpty()) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + // turn it back into a string (json encode) |
| 91 | + String newJson = jsonNode.toString(); |
| 92 | + |
| 93 | + // turn it back into a base64 string |
| 94 | + String newJson64 = Base64.getEncoder().encodeToString(newJson.getBytes(StandardCharsets.UTF_8)); |
| 95 | + // check it for length (err on the safe side with >=) |
| 96 | + if (newJson64.length() >= MAX_CLIENT_CONTEXT_LENGTH) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + builder.clientContext(newJson64); |
| 100 | + return builder.build(); |
| 101 | + } |
| 102 | +} |
0 commit comments