1
- import { SEMANTIC_ATTRIBUTE_SENTRY_OP , defineIntegration , spanToJSON } from '@sentry/core' ;
1
+ import {
2
+ SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD ,
3
+ SEMANTIC_ATTRIBUTE_SENTRY_OP ,
4
+ SEMANTIC_ATTRIBUTE_URL_FULL ,
5
+ defineIntegration ,
6
+ spanToJSON ,
7
+ } from '@sentry/core' ;
2
8
import type { IntegrationFn } from '@sentry/types' ;
3
9
import { parseGraphQLQuery } from '@sentry/utils' ;
4
10
@@ -13,6 +19,10 @@ const _graphqlClientIntegration = ((options: GraphQLClientOptions) => {
13
19
name : INTEGRATION_NAME ,
14
20
setup ( client ) {
15
21
client . on ( 'spanStart' , span => {
22
+ client . emit ( 'outgoingRequestSpanStart' , span ) ;
23
+ } ) ;
24
+
25
+ client . on ( 'outgoingRequestSpanStart' , span => {
16
26
const spanJSON = spanToJSON ( span ) ;
17
27
18
28
const spanAttributes = spanJSON . data || { } ;
@@ -21,17 +31,21 @@ const _graphqlClientIntegration = ((options: GraphQLClientOptions) => {
21
31
const isHttpClientSpan = spanOp === 'http.client' ;
22
32
23
33
if ( isHttpClientSpan ) {
24
- const httpUrl = spanAttributes [ 'http.url' ] ;
34
+ const httpUrl = spanAttributes [ SEMANTIC_ATTRIBUTE_URL_FULL ] || spanAttributes [ 'http.url' ] ;
25
35
26
36
const { endpoints } = options ;
27
37
28
38
const isTracedGraphqlEndpoint = endpoints . includes ( httpUrl ) ;
29
39
30
40
if ( isTracedGraphqlEndpoint ) {
31
- const httpMethod = spanAttributes [ 'http.method' ] ;
32
- const graphqlQuery = spanAttributes [ 'body' ] ?. query as string ;
41
+ const httpMethod = spanAttributes [ SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD ] || spanAttributes [ 'http.method' ] ;
42
+ const graphqlBody = spanAttributes [ 'body' ] ;
43
+
44
+ // Standard graphql request shape: https://graphql.org/learn/serving-over-http/#post-request
45
+ const graphqlQuery = graphqlBody && ( graphqlBody [ 'query' ] as string ) ;
46
+ const graphqlOperationName = graphqlBody && ( graphqlBody [ 'operationName' ] as string ) ;
33
47
34
- const { operationName, operationType } = parseGraphQLQuery ( graphqlQuery ) ;
48
+ const { operationName = graphqlOperationName , operationType } = parseGraphQLQuery ( graphqlQuery ) ;
35
49
const newOperation = operationName ? `${ operationType } ${ operationName } ` : `${ operationType } ` ;
36
50
37
51
span . updateName ( `${ httpMethod } ${ httpUrl } (${ newOperation } )` ) ;
0 commit comments