( args: LoaderFunctionArgs | ActionFunctionArgs, span: Span, formDataKeys?: Record<string, string | boolean>, )
| 11 | * Store configured FormData keys as span attributes for Remix actions. |
| 12 | */ |
| 13 | export async function storeFormDataKeys( |
| 14 | args: LoaderFunctionArgs | ActionFunctionArgs, |
| 15 | span: Span, |
| 16 | formDataKeys?: Record<string, string | boolean>, |
| 17 | ): Promise<void> { |
| 18 | try { |
| 19 | // We clone the request for Remix be able to read the FormData later. |
| 20 | const clonedRequest = args.request.clone(); |
| 21 | |
| 22 | // This only will return the last name of multiple file uploads in a single FormData entry. |
| 23 | // We can switch to `unstable_parseMultipartFormData` when it's stable. |
| 24 | // https://remix.run/docs/en/main/utils/parse-multipart-form-data#unstable_parsemultipartformdata |
| 25 | const formData = await clonedRequest.formData(); |
| 26 | |
| 27 | formData.forEach((value, key) => { |
| 28 | let attrKey = key; |
| 29 | |
| 30 | if (formDataKeys?.[key]) { |
| 31 | if (typeof formDataKeys[key] === 'string') { |
| 32 | attrKey = formDataKeys[key]; |
| 33 | } |
| 34 | |
| 35 | span.setAttribute( |
| 36 | `remix.action_form_data.${attrKey}`, |
| 37 | typeof value === 'string' ? value : '[non-string value]', |
| 38 | ); |
| 39 | } |
| 40 | }); |
| 41 | } catch (e) { |
| 42 | DEBUG_BUILD && debug.warn('Failed to read FormData from request', e); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Converts Remix route IDs to parameterized paths at runtime. |
no test coverage detected