async start()

in common/lib/utils/telemetry/xray_telemetry_context.ts [34:84]


  async start(func: () => any): Promise<any> {
    try {
      this.currentSegment = getSegment();
    } catch (error: any) {
      // Ignore.
    }

    let effectiveTraceLevel = this.traceLevel;
    if (!this.currentSegment && this.traceLevel === TelemetryTraceLevel.NESTED) {
      effectiveTraceLevel = TelemetryTraceLevel.TOP_LEVEL;
    }

    const ns = getNamespace();

    switch (effectiveTraceLevel) {
      case TelemetryTraceLevel.FORCE_TOP_LEVEL:
      case TelemetryTraceLevel.TOP_LEVEL:
        return await ns.runAndReturn(async () => {
          const segment = new Segment(this.name);
          if (this.currentSegment) {
            const parentId = this.currentSegment.id;
            this.currentSegment = segment;
            this.setAttribute(TelemetryConst.PARENT_TRACE_ANNOTATION, parentId);
          } else {
            this.currentSegment = segment;
          }
          setSegment(this.currentSegment!);

          this.setAttribute(TelemetryConst.TRACE_NAME_ANNOTATION, this.name);
          logger.info(`[XRay] Telemetry '${this.name}' trace ID: ${this.currentSegment?.id}`);

          return await this.executeMethod(func);
        });
      case TelemetryTraceLevel.NESTED:
        return await ns.runAndReturn(async () => {
          const subsegment = this.currentSegment!.addNewSubsegment(this.name);
          const parentId = this.currentSegment!.id;
          setSegment(subsegment);
          this.currentSegment = subsegment;
          this.setAttribute(TelemetryConst.PARENT_TRACE_ANNOTATION, parentId);
          this.setAttribute(TelemetryConst.TRACE_NAME_ANNOTATION, this.name);

          return await this.executeMethod(func);
        });
      case TelemetryTraceLevel.NO_TRACE:
        // Do not post this trace.
        return await func();
      default:
        return await func();
    }
  }