Skip to main content

Send Telemetry to Datadog from WebAssembly in Node.js

Observability in Datadog from WebAssembly

Overview

Enables the extraction of traces/spans from Wasm modules executing in a Node.js environment and emits them to a Datadog agent.

Installation

Install the Datadog Adapter via npm:

npm i @dylibso/observe-sdk-datadog

Example

A basic JavaScript program that loads a Wasm module and executes it using the native WebAssembly runtime.

const fs = require("fs");
const { WASI } = require("wasi");
const { env, argv } = require("node:process");
const { DatadogAdapter } = require("@dylibso/observe-sdk-datadog");

const wasi = new WASI({
version: "preview1",
args: argv.slice(1),
env,
});

// create a new instance of the Datadog adapter
// this assumes you have a datadog agent running on localhost, you may also pass a config
const adapter = new DatadogAdapter();

// create an optional filter that instructs the adapter to throw away any
// spans below a configured threshold
const opts = {
spanFilter: {
minDurationMicroseconds: 100,
},
};

// load up a Wasm module
const bytes = fs.readFileSync("your_module.wasm");

// instruct the adapter to start the trace
adapter.start(bytes, opts).then((traceContext) => {
const module = new WebAssembly.Module(bytes);

// instantiate and execute the module
WebAssembly.instantiate(module, {
...wasi.getImportObject(),
...traceContext.getImportObject(),
}).then((instance) => {
wasi.start(instance);
// associate some helpful metadata with the trace
traceContext.setMetadata({
http_status_code: 200,
http_url: "https://example.com",
});

// instruct the adapter to stop the trace
traceContext.stop();
});
});

Adapter Configuration

You may modify the behavior of your adapter by passing in a configuration when initializing the adapter. A configuration has the following fields:

const config = {
// the URL of the Datadog agent
agentHost: new URL("https://locationofyourdatadogagent.com:8126"),
// the service name to group your observability data under
serviceName: "my-wasm-service",
// default metadata that apply to every trace
defaultTags: new Map<string, string>(),
// the type of trace being captured
traceType: DatadogTraceType.Web,
// how often to send new traces to the agent
emitTracesInterval: 1000,
// the maximum number of traces to send to the agent per request
traceBatchMax: 100,
};
const adapter = new DatadogAdapter(config);