_getPatchedEmbeddingsCreate()

in packages/instrumentation-openai/src/instrumentation.ts [640:679]


  _getPatchedEmbeddingsCreate() {
    const self = this;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (original: any) => {
      // https://platform.openai.com/docs/api-reference/embeddings/create
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      return function patchedCreate(this: any, ...args: unknown[]) {
        if (!self.isEnabled) {
          return original.apply(this, args);
        }

        debug('OpenAI.Chat.Embeddings.create args: %O', args);
        const params = args[0];
        const startNow = performance.now();

        let startInfo;
        try {
          startInfo = self._startEmbeddingsSpan(params, this?._client?.baseURL);
        } catch (err) {
          self._diag.error('unexpected error starting span:', err);
          return original.apply(this, args);
        }
        const { span, ctx, commonAttrs } = startInfo;

        /** @type {import('openai/core').APIPromise} */
        const apiPromise = context.with(ctx, () => original.apply(this, args));

        apiPromise
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          .then((result: any) => {
            self._onEmbeddingsCreateResult(span, startNow, commonAttrs, result);
          })
          .catch(
            self._createAPIPromiseRejectionHandler(startNow, span, commonAttrs)
          );

        return apiPromise;
      };
    };
  }