_startEmbeddingsSpan()

in packages/instrumentation-openai/src/instrumentation.ts [688:715]


  _startEmbeddingsSpan(params: any, baseURL: string | undefined) {
    // Attributes common to span, metrics, log events.
    const commonAttrs: Attributes = {
      [ATTR_GEN_AI_OPERATION_NAME]: 'embeddings',
      [ATTR_GEN_AI_REQUEST_MODEL]: params.model,
      [ATTR_GEN_AI_SYSTEM]: 'openai',
    };
    Object.assign(commonAttrs, getAttrsFromBaseURL(baseURL, this._diag));

    // Span attributes.
    const attrs: Attributes = {
      ...commonAttrs,
    };
    if (params.encoding_format != null) {
      attrs[ATTR_GEN_AI_REQUEST_ENCODING_FORMATS] = [params.encoding_format];
    }

    const span = this.tracer.startSpan(
      `${attrs[ATTR_GEN_AI_OPERATION_NAME]} ${attrs[ATTR_GEN_AI_REQUEST_MODEL]}`,
      {
        kind: SpanKind.CLIENT,
        attributes: attrs,
      }
    );
    const ctx = trace.setSpan(context.active(), span);

    return { span, ctx, commonAttrs };
  }