@@ -5,6 +5,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION } from '@sentry/opentelemet
5
5
import { generateInstrumentOnce } from '../../otel/instrument' ;
6
6
7
7
import { addOriginToSpan } from '../../utils/addOriginToSpan' ;
8
+ import type { AttributeValue } from '@opentelemetry/api' ;
8
9
9
10
interface GraphqlOptions {
10
11
/**
@@ -72,6 +73,16 @@ export const instrumentGraphql = generateInstrumentOnce(
72
73
} else {
73
74
rootSpan . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION , newOperation ) ;
74
75
}
76
+
77
+ if ( ! spanToJSON ( rootSpan ) . data [ 'original-description' ] ) {
78
+ rootSpan . setAttribute ( 'original-description' , spanToJSON ( rootSpan ) . description ) ;
79
+ }
80
+ // Important for e.g. @sentry /aws-serverless because this would otherwise overwrite the name again
81
+ rootSpan . updateName (
82
+ `${ spanToJSON ( rootSpan ) . data [ 'original-description' ] } (${ getGraphqlOperationNamesFromAttribute (
83
+ existingOperations ,
84
+ ) } )`,
85
+ ) ;
75
86
}
76
87
} ,
77
88
} ;
@@ -115,3 +126,20 @@ function getOptionsWithDefaults(options?: GraphqlOptions): GraphqlOptions {
115
126
...options ,
116
127
} ;
117
128
}
129
+
130
+ // copy from packages/opentelemetry/utils
131
+ function getGraphqlOperationNamesFromAttribute ( attr : AttributeValue ) : string {
132
+ if ( Array . isArray ( attr ) ) {
133
+ const sorted = attr . slice ( ) . sort ( ) ;
134
+
135
+ // Up to 5 items, we just add all of them
136
+ if ( sorted . length <= 5 ) {
137
+ return sorted . join ( ', ' ) ;
138
+ } else {
139
+ // Else, we add the first 5 and the diff of other operations
140
+ return `${ sorted . slice ( 0 , 5 ) . join ( ', ' ) } , +${ sorted . length - 5 } ` ;
141
+ }
142
+ }
143
+
144
+ return `${ attr } ` ;
145
+ }
0 commit comments