Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

1.7 KB
Raw Blame
import type { DequeuedJob } from "@karakeep/shared/queueing";
import {
  addLogFields,
  getTracer,
  withEventLog,
  withSpan,
  EventLogType,
} from "@karakeep/shared-server";

const tracer = getTracer("@karakeep/workers");

type WorkerRunFn<TData, TResult = void> = (
  job: DequeuedJob<TData>,
) => Promise<TResult>;

/**
 * Wraps a worker run function with OpenTelemetry tracing.
 * Creates a span for each job execution and automatically handles error recording.
 *
 * @param name - The name of the span (e.g., "feedWorker.run", "crawlerWorker.run")
 * @param fn - The worker run function to wrap
 * @returns A wrapped function that executes within a traced span
 *
 * @example
 * ```ts
 * const run = withWorkerTracing("feedWorker.run", async (job) => {
 *   // Your worker logic here
 * });
 * ```
 */
export function withWorkerTracing<TData, TResult = void>(
  name: string,
  fn: WorkerRunFn<TData, TResult>,
): WorkerRunFn<TData, TResult> {
  return async (job: DequeuedJob<TData>): Promise<TResult> => {
    return await withSpan(
      tracer,
      name,
      {
        attributes: {
          "job.id": job.id,
          "job.priority": job.priority,
          "job.runNumber": job.runNumber,
        },
      },
      () => fn(job),
    );
  };
}

export function withWorkerEventLog<TData, TResult = void>(
  name: EventLogType,
  fn: WorkerRunFn<TData, TResult>,
): WorkerRunFn<TData, TResult> {
  return async (job: DequeuedJob<TData>): Promise<TResult> => {
    return await withEventLog(name, async () => {
      addLogFields({
        "job.id": job.id,
        "job.priority": job.priority,
        "job.run_number": job.runNumber,
      });
      return fn(job);
    });
  };
}