|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.opensearch.v3_0; |
| 7 | + |
| 8 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 9 | +import static java.util.logging.Level.FINE; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.core.JsonFactory; |
| 12 | +import jakarta.json.stream.JsonGenerator; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.Iterator; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import javax.annotation.Nullable; |
| 18 | +import org.opensearch.client.json.JsonpMapper; |
| 19 | +import org.opensearch.client.json.NdJsonpSerializable; |
| 20 | +import org.opensearch.client.json.jackson.JacksonJsonpGenerator; |
| 21 | +import org.opensearch.client.json.jackson.JacksonJsonpMapper; |
| 22 | + |
| 23 | +public final class OpenSearchBodyExtractor { |
| 24 | + |
| 25 | + private static final Logger logger = Logger.getLogger(OpenSearchBodyExtractor.class.getName()); |
| 26 | + private static final String QUERY_SEPARATOR = ";"; |
| 27 | + private static final JsonFactory JSON_FACTORY = new JsonFactory(); |
| 28 | + |
| 29 | + @Nullable |
| 30 | + public static String extractSanitized(JsonpMapper mapper, Object request) { |
| 31 | + try { |
| 32 | + if (request instanceof NdJsonpSerializable) { |
| 33 | + return serializeNdJsonSanitized(mapper, (NdJsonpSerializable) request); |
| 34 | + } |
| 35 | + |
| 36 | + return serializeSanitized(mapper, request); |
| 37 | + } catch (Exception exception) { |
| 38 | + logger.log(FINE, "Failure extracting body", exception); |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Nullable |
| 44 | + private static String serializeSanitized(JsonpMapper mapper, Object item) throws IOException { |
| 45 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 46 | + |
| 47 | + if (mapper instanceof JacksonJsonpMapper) { |
| 48 | + // Use Jackson-based sanitizing generator for JacksonJsonpMapper |
| 49 | + com.fasterxml.jackson.core.JsonGenerator jacksonGenerator = |
| 50 | + JSON_FACTORY.createGenerator(baos); |
| 51 | + com.fasterxml.jackson.core.JsonGenerator sanitizingGenerator = |
| 52 | + new SanitizingJacksonJsonGenerator(jacksonGenerator); |
| 53 | + try (JsonGenerator generator = new JacksonJsonpGenerator(sanitizingGenerator)) { |
| 54 | + mapper.serialize(item, generator); |
| 55 | + } |
| 56 | + } else { |
| 57 | + // Fallback for other mappers (may not work for all implementations) |
| 58 | + JsonGenerator rawGenerator = mapper.jsonProvider().createGenerator(baos); |
| 59 | + try (JsonGenerator generator = new SanitizingJsonGenerator(rawGenerator)) { |
| 60 | + mapper.serialize(item, generator); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + String result = baos.toString(UTF_8).trim(); |
| 65 | + return result.isEmpty() ? null : result; |
| 66 | + } |
| 67 | + |
| 68 | + @Nullable |
| 69 | + private static String serializeNdJsonSanitized(JsonpMapper mapper, NdJsonpSerializable value) |
| 70 | + throws IOException { |
| 71 | + StringBuilder result = new StringBuilder(); |
| 72 | + Iterator<?> values = value._serializables(); |
| 73 | + boolean first = true; |
| 74 | + |
| 75 | + while (values.hasNext()) { |
| 76 | + Object item = values.next(); |
| 77 | + String itemStr; |
| 78 | + |
| 79 | + if (item instanceof NdJsonpSerializable && item != value) { |
| 80 | + // Recursively handle nested NdJsonpSerializable |
| 81 | + itemStr = serializeNdJsonSanitized(mapper, (NdJsonpSerializable) item); |
| 82 | + } else { |
| 83 | + itemStr = serializeSanitized(mapper, item); |
| 84 | + } |
| 85 | + |
| 86 | + if (itemStr != null && !itemStr.isEmpty()) { |
| 87 | + if (!first) { |
| 88 | + result.append(QUERY_SEPARATOR); |
| 89 | + } |
| 90 | + result.append(itemStr); |
| 91 | + first = false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return result.length() == 0 ? null : result.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + private OpenSearchBodyExtractor() {} |
| 99 | +} |
0 commit comments