|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.android.rum.internal.net |
| 8 | + |
| 9 | +import com.datadog.android.api.InternalLogger |
| 10 | +import com.datadog.android.api.instrumentation.network.HttpRequestInfo |
| 11 | +import com.datadog.android.internal.network.GraphQLHeaders |
| 12 | +import com.datadog.android.internal.utils.fromBase64 |
| 13 | +import com.datadog.android.lint.InternalApi |
| 14 | +import com.datadog.android.rum.RumAttributes |
| 15 | +import org.json.JSONArray |
| 16 | +import org.json.JSONException |
| 17 | +import org.json.JSONObject |
| 18 | + |
| 19 | +/** |
| 20 | + * Extracts GraphQL-related attributes and errors from network requests and responses. |
| 21 | + */ |
| 22 | +@InternalApi |
| 23 | +class GraphQLExtractor { |
| 24 | + |
| 25 | + /** |
| 26 | + * Extracts GraphQL attributes (operation name, type, variables, payload) from the request headers. |
| 27 | + * @param requestInfo the HTTP request info containing headers |
| 28 | + * @return a map of GraphQL attributes decoded from Base64 header values |
| 29 | + */ |
| 30 | + fun extractGraphQLAttributes(requestInfo: HttpRequestInfo): Map<String, Any?> { |
| 31 | + return buildMap { |
| 32 | + GRAPHQL_HEADER_TO_ATTRIBUTE.forEach { (header, attribute) -> |
| 33 | + requestInfo.headers[header.headerValue]?.firstOrNull()?.let { |
| 34 | + put(attribute, it.fromBase64()) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Extracts and normalizes GraphQL errors from a JSON response body. |
| 42 | + * @param contentType the content type of the response |
| 43 | + * @param body the response body string |
| 44 | + * @param internalLogger the logger for reporting parsing failures |
| 45 | + * @return a JSON array string of normalized errors, or null if none found |
| 46 | + */ |
| 47 | + fun extractGraphQLErrors( |
| 48 | + contentType: String?, |
| 49 | + body: String?, |
| 50 | + internalLogger: InternalLogger |
| 51 | + ): String? { |
| 52 | + if (body == null || contentType !in JSON_CONTENT_TYPES) return null |
| 53 | + |
| 54 | + return try { |
| 55 | + val json = JSONObject(body) |
| 56 | + val errorsArray = if (json.has(ERRORS_KEY)) json.getJSONArray(ERRORS_KEY) else null |
| 57 | + |
| 58 | + if (errorsArray == null || errorsArray.length() == 0) { |
| 59 | + null |
| 60 | + } else { |
| 61 | + val normalizedErrors = JSONArray() |
| 62 | + for (i in 0 until errorsArray.length()) { |
| 63 | + @Suppress("UnsafeThirdPartyFunctionCall") // JSONException is caught and we are in array bounds |
| 64 | + val error = errorsArray.getJSONObject(i) |
| 65 | + normalizeErrorCode(error) |
| 66 | + normalizedErrors.put(error) |
| 67 | + } |
| 68 | + normalizedErrors.toString() |
| 69 | + } |
| 70 | + } catch (e: JSONException) { |
| 71 | + internalLogger.log( |
| 72 | + InternalLogger.Level.WARN, |
| 73 | + InternalLogger.Target.MAINTAINER, |
| 74 | + { ERROR_EXTRACT_GRAPHQL_ERRORS }, |
| 75 | + e |
| 76 | + ) |
| 77 | + null |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private fun normalizeErrorCode(error: JSONObject) { |
| 82 | + if (!error.has(CODE_KEY)) { |
| 83 | + val code = error.optJSONObject(EXTENSIONS_KEY)?.opt(CODE_KEY) |
| 84 | + if (code != null) { |
| 85 | + @Suppress("UnsafeThirdPartyFunctionCall") // caught by caller's catch blocks |
| 86 | + error.put(CODE_KEY, code) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + companion object { |
| 92 | + |
| 93 | + /** Maximum number of bytes to peek from a response body when extracting GraphQL errors. */ |
| 94 | + const val MAX_GRAPHQL_BODY_PEEK: Long = 512L * 1024L |
| 95 | + |
| 96 | + internal const val ERROR_EXTRACT_GRAPHQL_ERRORS = |
| 97 | + "Failed to extract GraphQL errors from response body." |
| 98 | + |
| 99 | + private const val ERRORS_KEY = "errors" |
| 100 | + private const val EXTENSIONS_KEY = "extensions" |
| 101 | + private const val CODE_KEY = "code" |
| 102 | + |
| 103 | + private val GRAPHQL_HEADER_TO_ATTRIBUTE = mapOf( |
| 104 | + GraphQLHeaders.DD_GRAPHQL_NAME_HEADER to RumAttributes.GRAPHQL_OPERATION_NAME, |
| 105 | + GraphQLHeaders.DD_GRAPHQL_TYPE_HEADER to RumAttributes.GRAPHQL_OPERATION_TYPE, |
| 106 | + GraphQLHeaders.DD_GRAPHQL_VARIABLES_HEADER to RumAttributes.GRAPHQL_VARIABLES, |
| 107 | + GraphQLHeaders.DD_GRAPHQL_PAYLOAD_HEADER to RumAttributes.GRAPHQL_PAYLOAD |
| 108 | + ) |
| 109 | + |
| 110 | + internal val JSON_CONTENT_TYPES = setOf( |
| 111 | + "application/json", |
| 112 | + "application/graphql+json", |
| 113 | + "application/graphql-response+json" |
| 114 | + ) |
| 115 | + } |
| 116 | +} |
0 commit comments