/* * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ import { context, trace, Span, SpanKind, SpanStatusCode, } from '@opentelemetry/api'; import { InstrumentationBase, InstrumentationNodeModuleFile, InstrumentationNodeModuleDefinition, isWrapped, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; import { ATTR_MESSAGING_OPERATION, ATTR_MESSAGING_SYSTEM } from './semconv'; import { ATTR_MESSAGING_DESTINATION, ATTR_MESSAGING_DESTINATION_KIND, MESSAGING_OPERATION_VALUE_RECEIVE, MESSAGING_DESTINATION_KIND_VALUE_TOPIC, } from './semconv-obsolete'; import { SocketIoInstrumentationConfig } from './types'; import { SocketIoInstrumentationAttributes } from './AttributeNames'; /** @knipignore */ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; import { extractRoomsAttributeValue, isPromise, normalizeConfig, } from './utils'; const reservedEvents = [ 'connect', 'connect_error', 'disconnect', 'disconnecting', 'newListener', 'removeListener', ]; export class SocketIoInstrumentation extends InstrumentationBase { constructor(config: SocketIoInstrumentationConfig = {}) { super(PACKAGE_NAME, PACKAGE_VERSION, normalizeConfig(config)); } protected init() { const socketInstrumentation = new InstrumentationNodeModuleFile( 'socket.io/dist/socket.js', ['>=3 <5'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.Socket?.prototype?.on)) { this._unwrap(moduleExports.Socket.prototype, 'on'); } this._wrap( moduleExports.Socket.prototype, 'on', this._patchOn(moduleVersion) ); if (isWrapped(moduleExports?.Socket?.prototype?.emit)) { this._unwrap(moduleExports.Socket.prototype, 'emit'); } this._wrap( moduleExports.Socket.prototype, 'emit', this._patchEmit(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports?.Socket?.prototype?.on)) { this._unwrap(moduleExports.Socket.prototype, 'on'); } if (isWrapped(moduleExports?.Socket?.prototype?.emit)) { this._unwrap(moduleExports.Socket.prototype, 'emit'); } return moduleExports; } ); const broadcastOperatorInstrumentation = new InstrumentationNodeModuleFile( 'socket.io/dist/broadcast-operator.js', ['>=4 <5'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.BroadcastOperator?.prototype?.emit)) { this._unwrap(moduleExports.BroadcastOperator.prototype, 'emit'); } this._wrap( moduleExports.BroadcastOperator.prototype, 'emit', this._patchEmit(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports?.BroadcastOperator?.prototype?.emit)) { this._unwrap(moduleExports.BroadcastOperator.prototype, 'emit'); } return moduleExports; } ); const namespaceInstrumentation = new InstrumentationNodeModuleFile( 'socket.io/dist/namespace.js', ['<4'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.Namespace?.prototype?.emit)) { this._unwrap(moduleExports.Namespace.prototype, 'emit'); } this._wrap( moduleExports.Namespace.prototype, 'emit', this._patchEmit(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports?.Namespace?.prototype?.emit)) { this._unwrap(moduleExports.Namespace.prototype, 'emit'); } } ); const socketInstrumentationLegacy = new InstrumentationNodeModuleFile( 'socket.io/lib/socket.js', ['2'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports.prototype?.on)) { this._unwrap(moduleExports.prototype, 'on'); } this._wrap(moduleExports.prototype, 'on', this._patchOn(moduleVersion)); if (isWrapped(moduleExports.prototype?.emit)) { this._unwrap(moduleExports.prototype, 'emit'); } this._wrap( moduleExports.prototype, 'emit', this._patchEmit(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports.prototype?.on)) { this._unwrap(moduleExports.prototype, 'on'); } if (isWrapped(moduleExports.prototype?.emit)) { this._unwrap(moduleExports.prototype, 'emit'); } return moduleExports; } ); const namespaceInstrumentationLegacy = new InstrumentationNodeModuleFile( 'socket.io/lib/namespace.js', ['2'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.prototype?.emit)) { this._unwrap(moduleExports.prototype, 'emit'); } this._wrap( moduleExports.prototype, 'emit', this._patchEmit(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports?.prototype?.emit)) { this._unwrap(moduleExports.prototype, 'emit'); } } ); return [ new InstrumentationNodeModuleDefinition( 'socket.io', ['>=3 <5'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.Server?.prototype?.on)) { this._unwrap(moduleExports.Server.prototype, 'on'); } this._wrap( moduleExports.Server.prototype, 'on', this._patchOn(moduleVersion) ); return moduleExports; }, moduleExports => { if (isWrapped(moduleExports?.Server?.prototype?.on)) { this._unwrap(moduleExports.Server.prototype, 'on'); } return moduleExports; }, [ broadcastOperatorInstrumentation, namespaceInstrumentation, socketInstrumentation, ] ), new InstrumentationNodeModuleDefinition( 'socket.io', ['2'], (moduleExports, moduleVersion) => { if (moduleExports === undefined || moduleExports === null) { return moduleExports; } if (moduleVersion === undefined) { return moduleExports; } if (isWrapped(moduleExports?.prototype?.on)) { this._unwrap(moduleExports.prototype, 'on'); } this._wrap( moduleExports.prototype, 'on', this._patchOn(moduleVersion) ); return moduleExports; }, (moduleExports, moduleVersion) => { if (isWrapped(moduleExports?.prototype?.on)) { this._unwrap(moduleExports.prototype, 'on'); } return moduleExports; }, [namespaceInstrumentationLegacy, socketInstrumentationLegacy] ), ]; } override setConfig(config: SocketIoInstrumentationConfig = {}) { return super.setConfig(normalizeConfig(config)); } private _patchOn(moduleVersion: string) { const self = this; return (original: Function) => { return function (this: any, ev: any, originalListener: Function) { if (!self.getConfig().traceReserved && reservedEvents.includes(ev)) { return original.apply(this, arguments); } if (self.getConfig().onIgnoreEventList?.includes(ev)) { return original.apply(this, arguments); } const wrappedListener = function (this: any, ...args: any[]) { const eventName = ev; const namespace = this.name || this.adapter?.nsp?.name; const span: Span = self.tracer.startSpan( `${MESSAGING_OPERATION_VALUE_RECEIVE} ${namespace}`, { kind: SpanKind.CONSUMER, attributes: { [ATTR_MESSAGING_SYSTEM]: 'socket.io', [ATTR_MESSAGING_DESTINATION]: namespace, [ATTR_MESSAGING_OPERATION]: MESSAGING_OPERATION_VALUE_RECEIVE, [SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]: eventName, }, } ); const { onHook } = self.getConfig(); if (onHook) { safeExecuteInTheMiddle( () => onHook(span, { moduleVersion, payload: args }), e => { if (e) self._diag.error('onHook error', e); }, true ); } return context.with(trace.setSpan(context.active(), span), () => self.endSpan(() => originalListener.apply(this, arguments), span) ); }; return original.apply(this, [ev, wrappedListener]); }; }; } private endSpan(traced: () => any | Promise, span: Span) { try { const result = traced(); if (isPromise(result)) { return result.then( value => { span.end(); return value; }, err => { span.recordException(err); span.setStatus({ code: SpanStatusCode.ERROR, message: err?.message, }); span.end(); throw err; } ); } else { span.end(); return result; } } catch (error: any) { span.recordException(error); span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message }); span.end(); throw error; } } private _patchEmit(moduleVersion: string) { const self = this; return (original: Function) => { return function (this: any, ev: any, ...args: any[]) { if (!self.getConfig().traceReserved && reservedEvents.includes(ev)) { return original.apply(this, arguments); } if (self.getConfig().emitIgnoreEventList?.includes(ev)) { return original.apply(this, arguments); } const messagingSystem = 'socket.io'; const eventName = ev; const attributes: any = { [ATTR_MESSAGING_SYSTEM]: messagingSystem, [ATTR_MESSAGING_DESTINATION_KIND]: MESSAGING_DESTINATION_KIND_VALUE_TOPIC, [SocketIoInstrumentationAttributes.SOCKET_IO_EVENT_NAME]: eventName, }; const rooms = extractRoomsAttributeValue(this); if (rooms.length) { attributes[SocketIoInstrumentationAttributes.SOCKET_IO_ROOMS] = rooms; } const namespace = this.name || this.adapter?.nsp?.name || this.sockets?.name; if (namespace) { attributes[SocketIoInstrumentationAttributes.SOCKET_IO_NAMESPACE] = namespace; attributes[ATTR_MESSAGING_DESTINATION] = namespace; } const span = self.tracer.startSpan(`send ${namespace}`, { kind: SpanKind.PRODUCER, attributes, }); const { emitHook } = self.getConfig(); if (emitHook) { safeExecuteInTheMiddle( () => emitHook(span, { moduleVersion, payload: args }), e => { if (e) self._diag.error('emitHook error', e); }, true ); } try { return context.with(trace.setSpan(context.active(), span), () => original.apply(this, arguments) ); } catch (error: any) { span.setStatus({ code: SpanStatusCode.ERROR, message: error.message, }); throw error; } finally { span.end(); } }; }; } }