In both distributed, and complex systems, tracing events is crucial for understanding the flow of requests and identifying performance bottlenecks.

Products like Honeycomb, Datadog, Sentry and Grafana Tempo are popular choices for visualising and analysing traces.

Open Telemetry

OpenTelemetry, or OTEL, is a standard for instrumenting applications to generate telemetry data.

It provides a set of SDKs across multiple languages for producing telemetry data consumable by various observability tools.

Events sent over Sailhouse can be traced using OpenTelemetry, utilising their propogation API.

Although the examples below can be used in every invocation, setting up a global package/module which handles common orchestration logic like this will make it easier to modify this behaviour in the future.

Utilising metadata

Events on Sailhouse have an optional metadata object that can be used to add additional context to the event.

import { propagation, context } from '@opentelemetry/api';

const output = {};

// context.active() is the current context that a span is running in.
propagation.inject(context.active(), output);

await sailhouse.publish("document-analysed", {
  team_id: team.id,
  document_id: documentId,
}, {
  metadata: {
    // You could pass in the whole of output, but `traceparent` and
    // `tracestate` are the most important fields.
    tracedata: {
      traceparent: output.traceparent,
      tracestate: output.tracestate,
    },
  }
});

When processing an event, you can extract the trace information from the metadata object and use it to continue the trace.

const event = await sailhouse.pull(
  "document-analysed",
  "summarise-document",
);

const { tracedata } = event.metadata;

const traceContext = propagation.extract(context.active(), tracedata);