Răsfoiți Sursa

fix(core): Create spans for method calls within same class (#3564)

David Höck 8 luni în urmă
părinte
comite
a2721d12d6

+ 13 - 4
packages/core/src/common/instrument-decorator.ts

@@ -1,4 +1,5 @@
 import { getConfig } from '../config/config-helpers';
+import { NoopInstrumentationStrategy } from '../config/system/noop-instrumentation-strategy';
 
 export const ENABLE_INSTRUMENTATION_ENV_VAR = 'VENDURE_ENABLE_INSTRUMENTATION';
 
@@ -59,17 +60,24 @@ export function Instrument(): ClassDecorator {
                 if (!instrumentationStrategy) {
                     return this;
                 }
+
+                if (instrumentationStrategy instanceof NoopInstrumentationStrategy) {
+                    throw new Error('Please add a TelemetryPlugin to your VendureConfig');
+                }
+
                 // eslint-disable-next-line @typescript-eslint/no-this-alias
                 const instance = this;
-                return new Proxy(this, {
+                const proxy = new Proxy(this, {
                     get: (obj, prop) => {
                         const original = obj[prop as string];
                         if (typeof original === 'function') {
+                            // Bind the method to the proxy instance to ensure internal calls go through the proxy
+                            const boundMethod = original.bind(proxy);
                             return function (...methodArgs: any[]) {
                                 const applyOriginalFunction =
-                                    original.constructor.name === 'AsyncFunction'
-                                        ? async () => await original.apply(obj, methodArgs)
-                                        : () => original.apply(obj, methodArgs);
+                                    boundMethod.constructor.name === 'AsyncFunction'
+                                        ? async () => await boundMethod(...methodArgs)
+                                        : () => boundMethod(...methodArgs);
                                 const wrappedMethodArgs = {
                                     instance,
                                     target,
@@ -83,6 +91,7 @@ export function Instrument(): ClassDecorator {
                         return original;
                     },
                 });
+                return proxy;
             }
         };
 

+ 5 - 6
packages/dev-server/dev-config.ts

@@ -5,23 +5,21 @@ import { ADMIN_API_PATH, API_PORT, SHOP_API_PATH } from '@vendure/common/lib/sha
 import {
     DefaultJobQueuePlugin,
     DefaultLogger,
+    DefaultSchedulerPlugin,
     DefaultSearchPlugin,
     dummyPaymentHandler,
     LanguageCode,
     LogLevel,
-    DefaultSchedulerPlugin,
     VendureConfig,
-    cleanSessionsTask,
 } from '@vendure/core';
-import { ScheduledTask } from '@vendure/core/dist/scheduler/scheduled-task';
 import { defaultEmailHandlers, EmailPlugin, FileBasedTemplateLoader } from '@vendure/email-plugin';
-import { BullMQJobQueuePlugin } from '@vendure/job-queue-plugin/package/bullmq';
-import 'dotenv/config';
 import { GraphiqlPlugin } from '@vendure/graphiql-plugin';
+import { TelemetryPlugin } from '@vendure/telemetry-plugin';
+import 'dotenv/config';
 import path from 'path';
 import { DataSourceOptions } from 'typeorm';
 
-import { MultivendorPlugin } from './example-plugins/multivendor-plugin/multivendor.plugin';
+const IS_INSTRUMENTED = process.env.IS_INSTRUMENTED === 'true';
 
 /**
  * Config settings used during development
@@ -128,6 +126,7 @@ export const devConfig: VendureConfig = {
                 changeEmailAddressUrl: 'http://localhost:4201/change-email-address',
             },
         }),
+        ...(IS_INSTRUMENTED ? [TelemetryPlugin.init({})] : []),
         AdminUiPlugin.init({
             route: 'admin',
             port: 5001,

+ 2 - 0
packages/dev-server/package.json

@@ -7,7 +7,9 @@
     "scripts": {
         "populate": "node -r ts-node/register -r dotenv/config populate-dev-server.ts",
         "dev:server": "node -r ts-node/register -r dotenv/config -r tsconfig-paths/register index.ts",
+        "dev:server:instrumented": "IS_INSTRUMENTED=true node -r ts-node/register -r dotenv/config -r tsconfig-paths/register -r ./instrumentation.ts index.ts",
         "dev:worker": "node -r ts-node/register -r dotenv/config -r tsconfig-paths/register index-worker.ts",
+        "dev:worker:instrumented": "IS_INSTRUMENTED=true node -r ts-node/register -r dotenv/config -r tsconfig-paths/register -r ./instrumentation.ts index-worker.ts",
         "dev": "concurrently npm:dev:*",
         "dashboard:dev": "vite dev",
         "load-test:1k": "node -r ts-node/register load-testing/run-load-test.ts 1000",