Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.3 KB
Raw Blame
import type {
  TelemetryInitConfig,
  TelemetryStartupOptions,
} from '../src/config';
import { Telemetry } from '../src/index';
import { createWebTracingProvider } from '../src/web';

export interface ProbeResult {
  nativeCalls: number;
  failed: boolean;
  requests: {
    url: string;
    credentials?: RequestCredentials;
    mode?: RequestMode;
  }[];
}

declare global {
  interface Window {
    runExporterProbe: (collector: string) => Promise<ProbeResult>;
  }
}

window.runExporterProbe = async (collector) => {
  const browserFetch = globalThis.fetch.bind(globalThis);
  const result: ProbeResult = { nativeCalls: 0, failed: false, requests: [] };
  const rejectNativeFetch = async () => {
    result.nativeCalls++;
    throw new Error(
      'Telemetry reached the authenticated application transport'
    );
  };
  globalThis.fetch = Object.assign(rejectNativeFetch, browserFetch);
  const startup = Promise.resolve().then(
    (): TelemetryStartupOptions => ({
      recording: true,
      tracesUrl: `${collector}/v1/traces`,
      logsUrl: `${collector}/v1/logs`,
    })
  );
  const config: TelemetryInitConfig = {
    serviceName: 'webview-export-probe',
    environment: 'test',
    enabled: async () => true,
    sampling: { perf: 0 },
    samplingKey: () => 'webview-export-probe',
    tracesUrl: 'http://invalid.example/fallback/v1/traces',
    logsUrl: 'http://invalid.example/fallback/v1/logs',
    startup,
    exportFetch: (input, init) => {
      const options: RequestInit = { ...init, credentials: 'omit' };
      result.requests.push({
        url: String(input),
        credentials: options.credentials,
        mode: options.mode,
      });
      return browserFetch(input, options);
    },
  };
  try {
    const ready = Telemetry.init({
      ...config,
      tracingProvider: (resource, getUserId, sampling) =>
        createWebTracingProvider(config, resource, getUserId, sampling),
    });
    Telemetry.span('export.early', { sampling: 'perf' }).end();
    await ready;
    Telemetry.span('export.ready', { sampling: 'perf' }).end();
    Telemetry.info('export.log');
    await Telemetry.flush();
  } catch {
    result.failed = true;
  } finally {
    await Telemetry.shutdown().catch(() => {
      result.failed = true;
    });
    globalThis.fetch = browserFetch;
  }
  return result;
};