|
| 1 | +import { InstrumentationBase,InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; |
| 2 | +import type { HandlerInterface, Hono, HonoInstance, MiddlewareHandlerInterface, OnHandlerInterface } from './types'; |
| 3 | + |
| 4 | +const PACKAGE_NAME = '@sentry/instrumentation-hono'; |
| 5 | +const PACKAGE_VERSION = '0.0.1'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Hono instrumentation for OpenTelemetry |
| 9 | + */ |
| 10 | +export class HonoInstrumentation extends InstrumentationBase { |
| 11 | + public constructor() { |
| 12 | + super(PACKAGE_NAME, PACKAGE_VERSION, {}); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Initialize the instrumentation. |
| 17 | + */ |
| 18 | + public init(): InstrumentationNodeModuleDefinition[] { |
| 19 | + return [ |
| 20 | + new InstrumentationNodeModuleDefinition( |
| 21 | + 'hono', |
| 22 | + ['^4.0.0'], |
| 23 | + moduleExports => this._patch(moduleExports), |
| 24 | + ), |
| 25 | + ]; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Patches the module exports to instrument Hono. |
| 30 | + */ |
| 31 | + private _patch(moduleExports: { Hono: Hono }): { Hono: Hono } { |
| 32 | + // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 33 | + const instrumentation = this; |
| 34 | + |
| 35 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 36 | + function Hono(this: HonoInstance, ...args: any): HonoInstance { |
| 37 | + const app: HonoInstance = moduleExports.Hono.apply(this, args); |
| 38 | + |
| 39 | + instrumentation._wrap(app, 'get', instrumentation._patchHandler()); |
| 40 | + instrumentation._wrap(app, 'post', instrumentation._patchHandler()); |
| 41 | + instrumentation._wrap(app, 'put', instrumentation._patchHandler()); |
| 42 | + instrumentation._wrap(app, 'delete', instrumentation._patchHandler()); |
| 43 | + instrumentation._wrap(app, 'options', instrumentation._patchHandler()); |
| 44 | + instrumentation._wrap(app, 'patch', instrumentation._patchHandler()); |
| 45 | + instrumentation._wrap(app, 'all', instrumentation._patchHandler()); |
| 46 | + instrumentation._wrap(app, 'on', instrumentation._patchOnHandler()); |
| 47 | + instrumentation._wrap(app, 'use', instrumentation._patchMiddlewareHandler()); |
| 48 | + |
| 49 | + return app; |
| 50 | + } |
| 51 | + |
| 52 | + moduleExports.Hono = Hono; |
| 53 | + return moduleExports; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Patches the route handler to instrument it. |
| 58 | + */ |
| 59 | + private _patchHandler(): (original: HandlerInterface) => HandlerInterface { |
| 60 | + return function(original: HandlerInterface) { |
| 61 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | + return function wrappedHandler(this: HonoInstance, ...args: any) { |
| 63 | + // TODO: Add OpenTelemetry tracing logic here |
| 64 | + return original.apply(this, args); |
| 65 | + }; |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Patches the 'on' handler to instrument it. |
| 71 | + */ |
| 72 | + private _patchOnHandler(): (original: OnHandlerInterface) => OnHandlerInterface { |
| 73 | + return function(original: OnHandlerInterface) { |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 75 | + return function wrappedHandler(this: HonoInstance, ...args: any) { |
| 76 | + // TODO: Add OpenTelemetry tracing logic here |
| 77 | + return original.apply(this, args); |
| 78 | + }; |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Patches the middleware handler to instrument it. |
| 84 | + */ |
| 85 | + private _patchMiddlewareHandler(): (original: MiddlewareHandlerInterface) => MiddlewareHandlerInterface { |
| 86 | + return function(original: MiddlewareHandlerInterface) { |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 88 | + return function wrappedHandler(this: HonoInstance, ...args: any) { |
| 89 | + // TODO: Add OpenTelemetry tracing logic here |
| 90 | + return original.apply(this, args); |
| 91 | + }; |
| 92 | + }; |
| 93 | + } |
| 94 | +} |
0 commit comments