Explorar el Código

feat: Add more spans to services

David Höck hace 9 meses
padre
commit
bf16f82452

+ 5 - 5
packages/core/src/cache/cache.service.ts

@@ -34,7 +34,7 @@ export class CacheService {
      * The `Cache` instance provides a convenience wrapper around the `CacheService`
      * methods.
      */
-    @Span('vendure.cache.create-cache')
+    @Span('CacheService.createCache')
     createCache(config: CacheConfig): Cache {
         const span = getActiveSpan();
         span?.setAttribute('cache.config', JSON.stringify(config));
@@ -46,7 +46,7 @@ export class CacheService {
      * Gets an item from the cache, or returns undefined if the key is not found, or the
      * item has expired.
      */
-    @Span('vendure.cache.get')
+    @Span('CacheService.get')
     async get<T extends JsonCompatible<T>>(key: string): Promise<T | undefined> {
         const span = getActiveSpan();
         span?.setAttribute('cache.key', key);
@@ -71,7 +71,7 @@ export class CacheService {
      * Optionally a "time to live" (ttl) can be specified, which means that the key will
      * be considered stale after that many milliseconds.
      */
-    @Span('vendure.cache.set')
+    @Span('CacheService.set')
     async set<T extends JsonCompatible<T>>(
         key: string,
         value: T,
@@ -95,7 +95,7 @@ export class CacheService {
      * @description
      * Deletes an item from the cache.
      */
-    @Span('vendure.cache.delete')
+    @Span('CacheService.delete')
     async delete(key: string): Promise<void> {
         const span = getActiveSpan();
         span?.setAttribute('cache.key', key);
@@ -115,7 +115,7 @@ export class CacheService {
      * @description
      * Deletes all items from the cache which contain at least one matching tag.
      */
-    @Span('vendure.cache.invalidate-tags')
+    @Span('CacheService.invalidateTags')
     async invalidateTags(tags: string[]): Promise<void> {
         const span = getActiveSpan();
         span?.setAttribute('cache.tags', tags.join(', '));

+ 5 - 0
packages/core/src/service/services/channel.service.ts

@@ -11,6 +11,7 @@ import {
 import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
 import { ID, PaginatedList, Type } from '@vendure/common/lib/shared-types';
 import { unique } from '@vendure/common/lib/unique';
+import { getActiveSpan } from '@vendure/telemetry';
 import { FindOptionsWhere } from 'typeorm';
 
 import { RelationPaths } from '../../api';
@@ -270,12 +271,16 @@ export class ChannelService {
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
             ctxOrToken instanceof RequestContext ? [ctxOrToken, token!] : [undefined, ctxOrToken];
 
+        const span = getActiveSpan();
         const allChannels = await this.allChannels.value(ctx);
 
         if (allChannels.length === 1 || channelToken === '') {
             // there is only the default channel, so return it
+            span?.setAttribute('channel.token', 'default');
             return this.getDefaultChannel(ctx);
         }
+
+        span?.setAttribute('channel.token', channelToken);
         const channel = allChannels.find(c => c.token === channelToken);
         if (!channel) {
             throw new ChannelNotFoundError(channelToken);

+ 36 - 1
packages/core/src/service/services/facet.service.ts

@@ -11,6 +11,7 @@ import {
     UpdateFacetInput,
 } from '@vendure/common/lib/generated-types';
 import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
+import { getActiveSpan } from '@vendure/telemetry';
 import { In } from 'typeorm';
 
 import { RequestContext } from '../../api/common/request-context';
@@ -21,11 +22,12 @@ import { Translated } from '../../common/types/locale-types';
 import { assertFound, idsAreEqual } from '../../common/utils';
 import { ConfigService } from '../../config/config.service';
 import { TransactionalConnection } from '../../connection/transactional-connection';
+import { FacetValue } from '../../entity/facet-value/facet-value.entity';
 import { FacetTranslation } from '../../entity/facet/facet-translation.entity';
 import { Facet } from '../../entity/facet/facet.entity';
-import { FacetValue } from '../../entity/facet-value/facet-value.entity';
 import { EventBus } from '../../event-bus';
 import { FacetEvent } from '../../event-bus/events/facet-event';
+import { Span } from '../../instrumentation';
 import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
 import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
 import { TranslatableSaver } from '../helpers/translatable-saver/translatable-saver';
@@ -57,6 +59,7 @@ export class FacetService {
         private roleService: RoleService,
     ) {}
 
+    @Span('FacetService.findAll')
     findAll(
         ctx: RequestContext,
         options?: ListQueryOptions<Facet>,
@@ -80,6 +83,7 @@ export class FacetService {
             });
     }
 
+    @Span('FacetService.findOne')
     findOne(
         ctx: RequestContext,
         facetId: ID,
@@ -105,6 +109,7 @@ export class FacetService {
         facetCode: string,
         lang: LanguageCode,
     ): Promise<Translated<Facet> | undefined>;
+    @Span('FacetService.findByCode')
     findByCode(
         ctxOrFacetCode: RequestContext | string,
         facetCodeOrLang: string | LanguageCode,
@@ -140,6 +145,7 @@ export class FacetService {
      * @description
      * Returns the Facet which contains the given FacetValue id.
      */
+    @Span('FacetService.findByFacetValueId')
     async findByFacetValueId(ctx: RequestContext, id: ID): Promise<Translated<Facet> | undefined> {
         const facet = await this.connection
             .getRepository(ctx, Facet)
@@ -153,6 +159,7 @@ export class FacetService {
         }
     }
 
+    @Span('FacetService.create')
     async create(ctx: RequestContext, input: CreateFacetInput): Promise<Translated<Facet>> {
         const facet = await this.translatableSaver.create({
             ctx,
@@ -174,6 +181,7 @@ export class FacetService {
         return assertFound(this.findOne(ctx, facet.id));
     }
 
+    @Span('FacetService.update')
     async update(ctx: RequestContext, input: UpdateFacetInput): Promise<Translated<Facet>> {
         const facet = await this.translatableSaver.update({
             ctx,
@@ -191,7 +199,11 @@ export class FacetService {
         return assertFound(this.findOne(ctx, facet.id));
     }
 
+    @Span('FacetService.delete')
     async delete(ctx: RequestContext, id: ID, force: boolean = false): Promise<DeletionResponse> {
+        const span = getActiveSpan();
+        span?.setAttribute('force', force.toString());
+
         const facet = await this.connection.getEntityOrThrow(ctx, Facet, id, {
             relations: ['values'],
             channelId: ctx.channelId,
@@ -218,14 +230,29 @@ export class FacetService {
             await this.connection.getRepository(ctx, Facet).remove(facet);
             await this.eventBus.publish(new FacetEvent(ctx, deletedFacet, 'deleted', id));
             result = DeletionResult.DELETED;
+            span?.addEvent('facet-deleted', {
+                facetCode: facet.code,
+                productCount,
+                variantCount,
+            });
         } else if (force) {
             await this.connection.getRepository(ctx, Facet).remove(facet);
             await this.eventBus.publish(new FacetEvent(ctx, deletedFacet, 'deleted', id));
             message = ctx.translate('message.facet-force-deleted', i18nVars);
             result = DeletionResult.DELETED;
+            span?.addEvent('facet-deleted', {
+                facetCode: facet.code,
+                productCount,
+                variantCount,
+            });
         } else {
             message = ctx.translate('message.facet-used', i18nVars);
             result = DeletionResult.NOT_DELETED;
+            span?.addEvent('facet-not-deleted', {
+                facetCode: facet.code,
+                productCount,
+                variantCount,
+            });
         }
 
         return {
@@ -266,6 +293,7 @@ export class FacetService {
      * @description
      * Assigns Facets to the specified Channel
      */
+    @Span('FacetService.assignFacetsToChannel')
     async assignFacetsToChannel(
         ctx: RequestContext,
         input: AssignFacetsToChannelInput,
@@ -294,6 +322,13 @@ export class FacetService {
             ),
         ]);
 
+        const span = getActiveSpan();
+
+        span?.addEvent('facets-assigned-to-channel', {
+            facetIds: facetsToAssign.map(f => f.id).join(','),
+            channelId: input.channelId,
+        });
+
         return this.connection
             .findByIdsInChannel(
                 ctx,