export function getAttrsFromBaseURL()

in packages/instrumentation-openai/src/utils.ts [61:88]


export function getAttrsFromBaseURL(
  baseURL: string | undefined,
  diag_: DiagLogger = diag
): Attributes | undefined {
  if (!baseURL) {
    return;
  }

  // TODO: would be nice to LRU cache this, but probably not significant perf
  let u;
  try {
    u = new URL(baseURL);
  } catch (ex) {
    // Note: We should never get to this point as openai should crash prior to this.
    // Even if it did, instrumentation will still work except lacking these attributes.
    diag_.debug(
      `could not determine server.{address,port} from baseURL: ${ex}`
    );
    return;
  }

  return {
    [ATTR_SERVER_ADDRESS]: u.hostname,
    [ATTR_SERVER_PORT]: u.port
      ? Number(u.port)
      : SERVER_PORT_FROM_URL_PROTOCOL[u.protocol],
  };
}