Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(n8n): trace workflow and node execution
  • Loading branch information
jabrailkhalil committed Oct 20, 2026
commit 595cf34bf04569c0c7f42993365a86b7d04ab401
69 changes: 69 additions & 0 deletions .github/workflows/typescript-n8n.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: TypeScript n8n

on:
pull_request:
branches: [main, dev]
paths:
- 'typescript/packages/traceai_n8n/**'
- 'typescript/packages/fi-core/**'
- 'typescript/packages/fi-semantic-conventions/**'
- 'typescript/package.json'
- 'typescript/pnpm-lock.yaml'
- 'typescript/pnpm-workspace.yaml'
- 'typescript/tsconfig*.json'
- '.github/workflows/typescript-n8n.yml'
push:
paths:
- 'typescript/packages/traceai_n8n/**'
- 'typescript/packages/fi-core/**'
- 'typescript/packages/fi-semantic-conventions/**'
- 'typescript/package.json'
- 'typescript/pnpm-lock.yaml'
- 'typescript/pnpm-workspace.yaml'
- 'typescript/tsconfig*.json'
- '.github/workflows/typescript-n8n.yml'
workflow_dispatch:

permissions:
contents: read

concurrency:
group: typescript-n8n-${{ github.ref }}
cancel-in-progress: true

jobs:
integration:
name: n8n real-engine integration
runs-on: ubuntu-latest
timeout-minutes: 15
defaults:
run:
working-directory: typescript
env:
N8N_DIAGNOSTICS_ENABLED: 'false'
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: pnpm/action-setup@v4
with:
version: 10.30.1
- uses: actions/setup-node@v4
with:
node-version: '22.21.1'
cache: pnpm
cache-dependency-path: typescript/pnpm-lock.yaml
- name: Install locked dependencies without lifecycle scripts
run: pnpm --filter '@traceai/n8n...' install --frozen-lockfile --ignore-scripts
- name: Build shared dependencies
run: pnpm run build:deps
- name: Build CommonJS and ESM entries
run: pnpm --filter @traceai/n8n build
- name: Check types
run: pnpm --filter @traceai/n8n typecheck
- name: Check formatting
run: pnpm --filter @traceai/n8n lint
- name: Test the real engine and automatic loading
run: pnpm --filter @traceai/n8n test:coverage
- name: Pack the package
run: pnpm --filter @traceai/n8n pack --pack-destination "$RUNNER_TEMP"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ var tracer = TraceAI.Register(opts =>
| [`@traceai/instructor`](https://www.npmjs.com/package/@traceai/instructor) | Instructor | [![npm](https://img.shields.io/npm/v/@traceai/instructor)](https://www.npmjs.com/package/@traceai/instructor) |
| [`@traceai/portkey`](https://www.npmjs.com/package/@traceai/portkey) | Portkey | [![npm](https://img.shields.io/npm/v/@traceai/portkey)](https://www.npmjs.com/package/@traceai/portkey) |
| [`@traceai/mcp`](https://www.npmjs.com/package/@traceai/mcp) | Model Context Protocol | [![npm](https://img.shields.io/npm/v/@traceai/mcp)](https://www.npmjs.com/package/@traceai/mcp) |
| [`@traceai/n8n`](typescript/packages/traceai_n8n) | Self-hosted n8n workflow and node execution tracing | Unreleased (source) |
| [`@traceai/fi-instrumentation-pipecat`](https://www.npmjs.com/package/@traceai/fi-instrumentation-pipecat) | Pipecat (Voice AI) | [![npm](https://img.shields.io/npm/v/@traceai/fi-instrumentation-pipecat)](https://www.npmjs.com/package/@traceai/fi-instrumentation-pipecat) |
| [`@traceai/fi-instrumentation-livekit`](https://www.npmjs.com/package/@traceai/fi-instrumentation-livekit) | LiveKit (Real-time) | [![npm](https://img.shields.io/npm/v/@traceai/fi-instrumentation-livekit)](https://www.npmjs.com/package/@traceai/fi-instrumentation-livekit) |

Expand Down
2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "pnpm run build:ordered",
"build:deps": "pnpm --filter @traceai/fi-core --filter @traceai/fi-semantic-conventions run build",
"build:consumers": "pnpm --filter @traceai/openai --filter @traceai/anthropic --filter @traceai/langchain --filter @traceai/bedrock --filter @traceai/vercel --filter @traceai/chromadb --filter @traceai/pinecone --filter @traceai/qdrant --filter @traceai/weaviate --filter @traceai/milvus --filter @traceai/lancedb --filter @traceai/mongodb --filter @traceai/pgvector --filter @traceai/redis --filter @traceai/together --filter @traceai/huggingface --filter @traceai/vllm --filter @traceai/deepseek --filter @traceai/fireworks --filter @traceai/cerebras --filter @traceai/xai run build",
"build:consumers": "pnpm --filter @traceai/openai --filter @traceai/anthropic --filter @traceai/langchain --filter @traceai/bedrock --filter @traceai/vercel --filter @traceai/chromadb --filter @traceai/pinecone --filter @traceai/qdrant --filter @traceai/weaviate --filter @traceai/milvus --filter @traceai/lancedb --filter @traceai/mongodb --filter @traceai/pgvector --filter @traceai/redis --filter @traceai/together --filter @traceai/huggingface --filter @traceai/vllm --filter @traceai/deepseek --filter @traceai/fireworks --filter @traceai/cerebras --filter @traceai/xai --filter @traceai/n8n run build",
"build:ordered": "pnpm run build:deps && pnpm run build:consumers",
"test": "pnpm run -r test",
"test:coverage": "pnpm run -r test:coverage",
Expand Down
10 changes: 10 additions & 0 deletions typescript/packages/traceai_n8n/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

## 0.1.0 (unreleased)

- Add workflow and node execution attempt spans for `n8n-core >=2.16.1 <3`.
- Preserve workflow results, cancellable promises, parent context and retry
behavior; handle resolved errors and cancellation without duplicate span ends.
- Export execution metadata without item payloads, credentials or error messages.
- Support automatic CommonJS loading and manual instrumentation, with real-engine
integration tests and a self-hosted setup guide.
129 changes: 129 additions & 0 deletions typescript/packages/traceai_n8n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# @traceai/n8n

OpenTelemetry instrumentation for the self-hosted n8n execution engine. It adds a
workflow span around each engine run and child spans around node execution
attempts, including retries. Existing provider instrumentors can add LLM spans
under the active node span.

## Compatibility

- Node.js 22.16 or newer.
- `n8n-core >=2.16.1 <3`; integration tests use `n8n-core@2.16.1` and its
`n8n-workflow@2.16.0` dependency. Earlier engine versions are not supported.
- Automatic instrumentation uses the CommonJS module loader. Both CommonJS and
ESM package entry points are available, but native ESM loading of n8n itself is
not covered by the automatic loader hook.

This package is a source contribution and has not been released to npm yet. The
commands below build it from this repository:

```sh
cd typescript
pnpm --filter '@traceai/n8n...' install --frozen-lockfile --ignore-scripts
pnpm run build:deps
pnpm --filter @traceai/n8n build
pnpm --filter @traceai/n8n pack --pack-destination /path/to/artifacts
```

Install the resulting tarball in the same application installation that resolves
`n8n-core`. The workspace dependencies must also be available there; when building
them from source, pack and install `@traceai/fi-semantic-conventions` and
`@traceai/fi-core` first. On Windows, use Git Bash for the existing dependency
build scripts.

## Self-hosted setup

Load instrumentation **before** n8n loads its engine, in every process that
executes workflows. For example, create `tracing.cjs` alongside your n8n
installation. It requires `@traceai/fi-core`, `@opentelemetry/api`,
`@opentelemetry/context-async-hooks` and `@opentelemetry/instrumentation` in addition
to this package.

```js
const { context } = require("@opentelemetry/api");
const {
AsyncLocalStorageContextManager,
} = require("@opentelemetry/context-async-hooks");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");
const { register, ProjectType } = require("@traceai/fi-core");
const { N8nInstrumentation } = require("@traceai/n8n");

// If the application already has an OTel context manager/provider, reuse them.
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
const tracerProvider = register({
projectName: "n8n-workflows",
projectType: ProjectType.OBSERVE,
});
const instrumentation = new N8nInstrumentation();
registerInstrumentations({
tracerProvider,
instrumentations: [instrumentation],
});

// Flush on normal process exit; integrate shutdown with the host's lifecycle
// when it has its own shutdown hooks.
process.once("beforeExit", () => tracerProvider.shutdown());
```

Configure `FI_API_KEY` and `FI_SECRET_KEY` in the host environment, then preload
the file when starting the n8n process, for example on Linux:

```sh
NODE_OPTIONS="--require=/absolute/path/to/tracing.cjs" n8n start
```

Preserve any existing `NODE_OPTIONS` when adding the preload. In queue mode,
configure each worker as well as any process that runs manual executions. This
integration creates traces inside an engine process; it does **not** propagate
context through the queue or connect separate processes into one trace.

If the engine was already loaded, use the public manual entry point instead:

```js
instrumentation.manuallyInstrument(require("n8n-core"));
```

Call `instrumentation.disable()` to restore patched engine methods. Runs already
being observed still finish their open spans. OpenTelemetry tracing suppression
is respected.

## Captured data

| Span | Attributes |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| Workflow (`CHAIN`) | `n8n.workflow.id`, `n8n.workflow.name`, `n8n.execution.id` when available, `n8n.execution.mode`, `n8n.execution.status` |
| Node attempt (`TOOL`) | `n8n.node.id`, `n8n.node.name`, `n8n.node.type`, `n8n.node.type_version`, `n8n.node.run_index`, `n8n.node.status`, execution ID and mode |

Span duration records elapsed execution time. Workflow status comes from n8n's
final run result, including errors returned as resolved promises. Rejected node
attempts are marked as errors even if n8n subsequently retries or continues.
Cancellation ends open spans once and preserves n8n's cancellable promise.

The integration does not capture workflow items, node parameters, credentials,
input/output payloads, exception messages or stack traces. Exceptions retain only
their type. Names and identifiers are exported, so avoid putting secrets in them.
Provider instrumentors have their own payload settings; this package does not
change those settings.

This first integration observes engine runs and calls to `runNode`. Pinned nodes
that bypass that method have no node span. Nodes that handle errors internally
and return them as ordinary item data are not classified by inspecting that data.
Waiting and resumed runs are separate spans; there is no persisted trace context
across a restart. It does not install an n8n community node, configure n8n Cloud,
or replace provider-specific LLM instrumentation.

## Development

From `typescript/`, after building dependencies:

```sh
pnpm --filter @traceai/n8n build
pnpm --filter @traceai/n8n typecheck
pnpm --filter @traceai/n8n test:coverage
pnpm --filter @traceai/n8n lint
```

The tests use the actual n8n engine and custom local nodes, with an in-memory
OpenTelemetry exporter. They require no credentials, external services or mocked
n8n modules. A fresh Node process also tests automatic module patching. The
`lint` script checks formatting with Prettier.
69 changes: 69 additions & 0 deletions typescript/packages/traceai_n8n/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@traceai/n8n",
"version": "0.1.0",
"description": "OpenTelemetry instrumentation for n8n workflow execution",
"main": "./dist/src/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/src/index.d.ts",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/esm/index.js",
"require": "./dist/src/index.js"
}
},
"files": [
"dist/**/*.js",
"dist/**/*.d.ts",
"dist/**/*.map",
"dist/esm/package.json",
"README.md",
"CHANGELOG.md"
],
"scripts": {
"build": "tsc --build tsconfig.json && tsc -p tsconfig.esm.json && node scripts/esm-package.cjs",
"test": "node --test test/*.test.cjs",
"test:coverage": "node --experimental-test-coverage --test test/*.test.cjs",
"typecheck": "tsc --build tsconfig.json",
"lint": "prettier --check src test scripts *.json *.md",
"prepublishOnly": "pnpm run build && pnpm test && pnpm run lint"
},
"dependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/core": "^2.0.1",
"@opentelemetry/instrumentation": "^0.202.0",
"@traceai/fi-core": "workspace:^",
"@traceai/fi-semantic-conventions": "workspace:^"
},
"peerDependencies": {
"n8n-core": ">=2.16.1 <3"
},
"peerDependenciesMeta": {
"n8n-core": {
"optional": true
}
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^2.0.1",
"@opentelemetry/sdk-trace-base": "^2.0.1",
"@opentelemetry/sdk-trace-node": "^2.0.1",
"@types/node": "^22.15.29",
"n8n-core": "2.16.1",
"n8n-workflow": "2.16.0",
"prettier": "^3.5.3",
"typescript": "^5.8.3"
},
"engines": {
"node": ">=22.16"
},
"author": "Future AGI <no-reply@futureagi.com>",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/future-agi/traceAI.git",
"directory": "typescript/packages/traceai_n8n"
},
"publishConfig": {
"access": "public"
}
}
3 changes: 3 additions & 0 deletions typescript/packages/traceai_n8n/scripts/esm-package.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { writeFileSync } = require("node:fs");

writeFileSync("dist/esm/package.json", '{"type":"module"}\n');
2 changes: 2 additions & 0 deletions typescript/packages/traceai_n8n/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { N8nInstrumentation } from "./instrumentation.js";
export type { N8nInstrumentationConfig } from "./instrumentation.js";
Loading