From 7a7d3a35e66678cf7d11fb9c89b8a26f2d4bfd6b Mon Sep 17 00:00:00 2001 From: Justin Carter Date: Mon, 8 Nov 2021 21:38:51 +0100 Subject: [PATCH] fix: adding nested input variables in graphql plugin (#720) --- .../src/instrumentation.ts | 5 ++-- .../src/utils.ts | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts index c4f0bc69df..e33c7cebee 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts @@ -43,6 +43,7 @@ import { Maybe, } from './types'; import { + addInputVariableAttributes, addSpanSource, endSpan, getOperation, @@ -419,9 +420,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { } if (processedArgs.variableValues && config.allowValues) { - Object.entries(processedArgs.variableValues).forEach(([key, value]) => { - span.setAttribute(`${AttributeNames.VARIABLES}${String(key)}`, value); - }); + addInputVariableAttributes(span, processedArgs.variableValues); } return span; diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts index 930dd11dbb..6edee1ea3e 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts @@ -32,6 +32,31 @@ import { const OPERATION_VALUES = Object.values(AllowedOperationTypes); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function addInputVariableAttribute(span: api.Span, key: string, variable: any) { + if (Array.isArray(variable)) { + variable.forEach((value, idx) => { + addInputVariableAttribute(span, `${key}.${idx}`, value); + }); + } else if (variable instanceof Object) { + Object.entries(variable).forEach(([nestedKey, value]) => { + addInputVariableAttribute(span, `${key}.${nestedKey}`, value); + }); + } else { + span.setAttribute(`${AttributeNames.VARIABLES}${String(key)}`, variable); + } +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function addInputVariableAttributes( + span: api.Span, + variableValues: { [key: string]: any } +) { + Object.entries(variableValues).forEach(([key, value]) => { + addInputVariableAttribute(span, key, value); + }); +} + export function addSpanSource( span: api.Span, loc?: graphqlTypes.Location,