async start()

in common/lib/utils/telemetry/open_telemetry_context.ts [38:83]


  async start(func: () => any): Promise<any> {
    const activeContext = context.active();
    const isRoot = activeContext === api.ROOT_CONTEXT;

    let effectiveTraceLevel: TelemetryTraceLevel = this.traceLevel;
    if (isRoot && this.traceLevel === TelemetryTraceLevel.NESTED) {
      effectiveTraceLevel = TelemetryTraceLevel.TOP_LEVEL;
    }
    const key = createContextKey(`${this.name}-key`);
    this.span = trace.getActiveSpan();

    switch (effectiveTraceLevel) {
      case TelemetryTraceLevel.FORCE_TOP_LEVEL:
      case TelemetryTraceLevel.TOP_LEVEL:
        return await context.with(activeContext.setValue(key, "context"), async () => {
          return await this.tracer.startActiveSpan(this.name, async (span: APISpan) => {
            if (!isRoot && this.span) {
              const parentId = this.span.spanContext().spanId;
              this.span = span;
              this.setAttribute(TelemetryConst.PARENT_TRACE_ANNOTATION, parentId);
            } else {
              this.span = span;
            }

            this.setAttribute(TelemetryConst.TRACE_NAME_ANNOTATION, this.name);
            logger.info(`[OTLP] Telemetry '${this.name}' trace ID: ${this.span.spanContext().traceId}`);

            return await this.executeMethod(func);
          });
        });
      case TelemetryTraceLevel.NESTED:
        return await this.tracer.startActiveSpan(this.name, async (span: APISpan) => {
          const parentId = this.span!.spanContext().spanId;
          this.span = span;
          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 context.
        return await func();
      default:
        return await func();
    }
  }