11import { isWrapped } from '@opentelemetry/core' ;
22import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect' ;
3- import { captureException , defineIntegration , isEnabled } from '@sentry/core' ;
3+ import {
4+ SEMANTIC_ATTRIBUTE_SENTRY_OP ,
5+ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
6+ captureException ,
7+ defineIntegration ,
8+ getClient ,
9+ isEnabled ,
10+ spanToJSON ,
11+ } from '@sentry/core' ;
412import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry' ;
5- import type { IntegrationFn } from '@sentry/types' ;
13+ import type { IntegrationFn , Span } from '@sentry/types' ;
614import { consoleSandbox } from '@sentry/utils' ;
715
816type ConnectApp = {
@@ -30,6 +38,16 @@ function connectErrorMiddleware(err: any, req: any, res: any, next: any): void {
3038export const setupConnectErrorHandler = ( app : ConnectApp ) : void => {
3139 app . use ( connectErrorMiddleware ) ;
3240
41+ // Sadly, ConnectInstrumentation has no requestHook, so we need to add the attributes here
42+ // We register this hook in this method, because if we register it in the integration `setup`,
43+ // it would always run even for users that are not even using fastify
44+ const client = getClient ( ) ;
45+ if ( client ) {
46+ client . on ( 'spanStart' , span => {
47+ addConnectSpanAttributes ( span ) ;
48+ } ) ;
49+ }
50+
3351 if ( ! isWrapped ( app . use ) && isEnabled ( ) ) {
3452 consoleSandbox ( ( ) => {
3553 // eslint-disable-next-line no-console
@@ -39,3 +57,26 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {
3957 } ) ;
4058 }
4159} ;
60+
61+ function addConnectSpanAttributes ( span : Span ) : void {
62+ const attributes = spanToJSON ( span ) . data || { } ;
63+
64+ // this is one of: middleware, request_handler
65+ const type = attributes [ 'connect.type' ] ;
66+
67+ // If this is already set, or we have no connect span, no need to process again...
68+ if ( attributes [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] || ! type ) {
69+ return ;
70+ }
71+
72+ span . setAttributes ( {
73+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.http.otel.connect' ,
74+ [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : `${ type } .connect` ,
75+ } ) ;
76+
77+ // Also update the name, we don't need to "middleware - " prefix
78+ const name = attributes [ 'connect.name' ] ;
79+ if ( typeof name === 'string' ) {
80+ span . updateName ( name ) ;
81+ }
82+ }
0 commit comments