Quellcode durchsuchen

feat(telemetry): Respect dev mode for log processors

David Höck vor 9 Monaten
Ursprung
Commit
14bf4022a9

+ 1 - 0
package-lock.json

@@ -4915,6 +4915,7 @@
     },
     "node_modules/@clack/prompts/node_modules/is-unicode-supported": {
       "version": "1.3.0",
+      "extraneous": true,
       "inBundle": true,
       "license": "MIT",
       "engines": {

+ 4 - 2
packages/core/src/cache/cache.service.ts

@@ -49,16 +49,18 @@ export class CacheService {
     @Span('CacheService.get')
     async get<T extends JsonCompatible<T>>(key: string): Promise<T | undefined> {
         const span = getActiveSpan();
-        span?.setAttribute('cache.key', key);
+
         try {
             const result = await this.cacheStrategy.get(key);
             if (result) {
                 span?.setAttribute('cache.hit', true);
+                span?.addEvent('cache.hit', { key });
                 Logger.debug(`CacheService hit for key [${key}]`);
             }
-            span?.end();
             return result as T;
         } catch (e: any) {
+            span?.setAttribute('cache.hit', false);
+            span?.addEvent('cache.miss', { key });
             Logger.error(`Could not get key [${key}] from CacheService`, undefined, e.stack);
         }
     }

+ 1 - 1
packages/core/src/service/services/product-option.service.ts

@@ -13,9 +13,9 @@ import { Translated } from '../../common/types/locale-types';
 import { assertFound } from '../../common/utils';
 import { Logger } from '../../config/logger/vendure-logger';
 import { TransactionalConnection } from '../../connection/transactional-connection';
+import { ProductOptionGroup } from '../../entity/product-option-group/product-option-group.entity';
 import { ProductOptionTranslation } from '../../entity/product-option/product-option-translation.entity';
 import { ProductOption } from '../../entity/product-option/product-option.entity';
-import { ProductOptionGroup } from '../../entity/product-option-group/product-option-group.entity';
 import { ProductVariant } from '../../entity/product-variant/product-variant.entity';
 import { EventBus } from '../../event-bus';
 import { ProductOptionEvent } from '../../event-bus/events/product-option-event';

+ 3 - 0
packages/core/src/service/services/product.service.ts

@@ -79,6 +79,9 @@ export class ProductService {
     ): Promise<PaginatedList<Translated<Product>>> {
         const span = getActiveSpan();
         span?.setAttribute('channelId', ctx.channelId.toString());
+        span?.addEvent('channelId', {
+            channelId: ctx.channelId.toString(),
+        });
 
         const effectiveRelations = relations || this.relations.slice();
         const customPropertyMap: { [name: string]: string } = {};

+ 2 - 2
packages/dev-server/instrumentation.ts

@@ -15,9 +15,9 @@ process.env.OTEL_LOGS_EXPORTER = 'otlp';
 
 const logExporter = new OTLPLogExporter();
 
-const config = getSdkConfiguration(false, {
+const config = getSdkConfiguration(true, {
     spanProcessors: [new BatchSpanProcessor(traceExporter)],
-    logRecordProcessor: new BatchLogRecordProcessor(logExporter),
+    logRecordProcessors: [new BatchLogRecordProcessor(logExporter)],
     resource: resourceFromAttributes({
         'service.name': 'vendure-dev-server',
     }),

+ 4 - 1
packages/telemetry/src/instrumentation.ts

@@ -1,6 +1,7 @@
 import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
 import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
 import { resourceFromAttributes } from '@opentelemetry/resources';
+import { ConsoleLogRecordExporter, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
 import { NodeSDKConfiguration } from '@opentelemetry/sdk-node';
 import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
 
@@ -8,14 +9,16 @@ export function getSdkConfiguration(
     devMode: boolean = false,
     config: Partial<NodeSDKConfiguration> = {},
 ): Partial<NodeSDKConfiguration> {
-    const { spanProcessors, ...rest } = config;
+    const { spanProcessors, logRecordProcessors, ...rest } = config;
 
     const devModeAwareConfig: Partial<NodeSDKConfiguration> = devMode
         ? {
               spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())],
+              logRecordProcessors: [new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())],
           }
         : {
               spanProcessors,
+              logRecordProcessors,
           };
 
     return {