traceApp

tcmts.cli.web.traceApp · #sgstq7jv · term · runs on the server

uses process

Trace an app's server (tcm start, tcm dev): its spans kept in .tcm/data/traces.sqlite for tcm traces, and sent over OTLP where OpenTelemetry's variables say (otlpTarget; the service is the app's name unless OTEL_SERVICE_NAME says). OTEL_SDK_DISABLED=true turns it off. deployed says the build answering now.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { thrownIn } from "#codebase/tcmts/cli/main";
import { dim, yellow } from "#codebase/tcmts/cli/style";
import { dataDir, otlpTarget, serverApiHost } from "#codebase/tcmts/cli/web";

export async function traceApp(
  cb: Codebase,
  appName: string,
  build: string,
  env: Record<string, string | undefined> = process.env,
): Promise<{ deployed: (build: string) => void; stop: () => Promise<void> }> {
  if (env.OTEL_SDK_DISABLED?.trim().toLowerCase() === "true") {
    console.log(`  traces   ${dim("off (OTEL_SDK_DISABLED)")}`);
    return { deployed: () => {}, stop: async () => {} };
  }
  const host = await serverApiHost(cb)();
  const target = otlpTarget(env);
  if (target && "problem" in target) console.log(yellow(`  ${target.problem}`));
  const otlp = target && !("problem" in target) ? target : undefined;
  const deployed = (b: string) =>
    host.startTracing({
      dataDir: dataDir(cb),
      build: b,
      service: env.OTEL_SERVICE_NAME?.trim() || appName,
      otlp,
      log: (line) => console.log(yellow(`  ${line}`)),
      // where an error was thrown: the definition, by hash, so errors group by it
      nameOf: (h) => cb.store.pathsOf(h)[0],
      describe: (e): Record<string, string> => {
        const at = thrownIn(cb, (e as Error | undefined)?.stack);
        return at ? { "tcm.thrown_in": at } : {};
      },
    });
  deployed(build);
  console.log(
    `  traces   ${otlp ? `kept (tcm traces) and sent to ${otlp.url}` : dim("kept: tcm traces (OTEL_EXPORTER_OTLP_ENDPOINT sends them too)")}`,
  );
  return { deployed, stop: () => host.stopTracing() };
}

uses

used by