@loglayer/plugin-datadog-apm-trace-injector 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/index.cjs +28 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Theo Gravity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Datadog APM Trace Injector Plugin for LogLayer
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@loglayer/plugin-datadog-apm-trace-injector)
|
|
4
|
+
[](https://www.npmjs.com/package/@loglayer/plugin-datadog-apm-trace-injector)
|
|
5
|
+
[](http://www.typescriptlang.org/)
|
|
6
|
+
|
|
7
|
+
A plugin for [LogLayer](https://loglayer.dev) that automatically injects Datadog APM trace context into your logs. This enables correlation between your application logs and distributed traces in Datadog.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
This plugin requires the [`dd-trace`](https://github.com/DataDog/dd-trace-js) library to be installed in your project:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @loglayer/plugin-datadog-apm-trace-injector dd-trace
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// dd-trace generally needs to be the first import of any project
|
|
21
|
+
// as it needs to patch node_module packages before they are imported
|
|
22
|
+
import tracer from 'dd-trace';
|
|
23
|
+
import { LogLayer } from 'loglayer';
|
|
24
|
+
import { datadogTraceInjectorPlugin } from '@loglayer/plugin-datadog-apm-trace-injector';
|
|
25
|
+
|
|
26
|
+
// Initialize dd-trace (must be done before any other imports)
|
|
27
|
+
tracer.init();
|
|
28
|
+
|
|
29
|
+
// Create the plugin
|
|
30
|
+
const traceInjector = datadogTraceInjectorPlugin({
|
|
31
|
+
tracerInstance: tracer,
|
|
32
|
+
// Enable the plugin only if the Datadog API key is set
|
|
33
|
+
enabled: !!process.env.DD_API_KEY
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Add to your LogLayer instance
|
|
37
|
+
const log = new LogLayer({
|
|
38
|
+
plugins: [traceInjector],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Your logs will now automatically include trace context
|
|
42
|
+
log.info('User action completed');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Configuration
|
|
46
|
+
|
|
47
|
+
The plugin accepts the following configuration options:
|
|
48
|
+
|
|
49
|
+
| Option | Type | Description |
|
|
50
|
+
|--------|------|-------------|
|
|
51
|
+
| `id` | `string` | Optional. Unique identifier for the plugin |
|
|
52
|
+
| `tracerInstance` | `Tracer` | Required. The dd-trace tracer instance |
|
|
53
|
+
| `disabled` | `boolean` | Optional. Disable the plugin |
|
|
54
|
+
| `onError` | `(error: Error, data?: Record<string, any>) => void` | Optional. Error handler for tracer operation failures |
|
|
55
|
+
|
|
56
|
+
## Documentation
|
|
57
|
+
|
|
58
|
+
For more details, visit [https://loglayer.dev/plugins/datadog-apm-trace-injector](https://loglayer.dev/plugins/datadog-apm-trace-injector)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/plugin.ts
|
|
2
|
+
function datadogTraceInjectorPlugin(config) {
|
|
3
|
+
const tracer = config.tracerInstance;
|
|
4
|
+
return {
|
|
5
|
+
id: config.id,
|
|
6
|
+
disabled: config.disabled,
|
|
7
|
+
onBeforeDataOut({ data }) {
|
|
8
|
+
if (!data) {
|
|
9
|
+
data = {};
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const span = tracer.scope().active();
|
|
13
|
+
if (span) {
|
|
14
|
+
tracer.inject(span.context(), "log", data);
|
|
15
|
+
}
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (config.onError && error instanceof Error) {
|
|
18
|
+
config.onError(error, data);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.datadogTraceInjectorPlugin = datadogTraceInjectorPlugin;
|
|
28
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/loglayer/loglayer/packages/plugins/datadog-apm-trace-injector/dist/index.cjs","../src/plugin.ts"],"names":[],"mappings":"AAAA;ACWO,SAAS,0BAAA,CAA2B,MAAA,EAA0D;AACnG,EAAA,MAAM,OAAA,EAAS,MAAA,CAAO,cAAA;AAEtB,EAAA,OAAO;AAAA,IACL,EAAA,EAAI,MAAA,CAAO,EAAA;AAAA,IACX,QAAA,EAAU,MAAA,CAAO,QAAA;AAAA,IACjB,eAAA,CAAgB,EAAE,KAAK,CAAA,EAAmD;AACxE,MAAA,GAAA,CAAI,CAAC,IAAA,EAAM;AACT,QAAA,KAAA,EAAO,CAAC,CAAA;AAAA,MACV;AAEA,MAAA,IAAI;AAGF,QAAA,MAAM,KAAA,EAAO,MAAA,CAAO,KAAA,CAAM,CAAA,CAAE,MAAA,CAAO,CAAA;AAEnC,QAAA,GAAA,CAAI,IAAA,EAAM;AACR,UAAA,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,OAAA,CAAQ,CAAA,EAAG,KAAA,EAAO,IAAI,CAAA;AAAA,QAC3C;AAAA,MACF,EAAA,MAAA,CAAS,KAAA,EAAO;AAEd,QAAA,GAAA,CAAI,MAAA,CAAO,QAAA,GAAW,MAAA,WAAiB,KAAA,EAAO;AAC5C,UAAA,MAAA,CAAO,OAAA,CAAQ,KAAA,EAAO,IAAI,CAAA;AAAA,QAC5B;AAAA,MAGF;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA;AACF;ADlBA;AACE;AACF,gEAAC","file":"/home/runner/work/loglayer/loglayer/packages/plugins/datadog-apm-trace-injector/dist/index.cjs","sourcesContent":[null,"import type { LogLayerPlugin, PluginBeforeDataOutParams } from \"@loglayer/plugin\";\nimport type { DatadogTraceInjectorPluginParams } from \"./types.js\";\n\n/**\n * Creates a new Datadog trace injector plugin.\n *\n * This plugin injects the current Datadog APM trace context into the log data.\n *\n * @param config - The datadog trace injector plugin configuration\n * @returns A LogLayer plugin instance\n */\nexport function datadogTraceInjectorPlugin(config: DatadogTraceInjectorPluginParams): LogLayerPlugin {\n const tracer = config.tracerInstance;\n\n return {\n id: config.id,\n disabled: config.disabled,\n onBeforeDataOut({ data }: PluginBeforeDataOutParams): Record<string, any> {\n if (!data) {\n data = {};\n }\n\n try {\n // Associates log with APM trace\n // https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/nodejs/\n const span = tracer.scope().active();\n\n if (span) {\n tracer.inject(span.context(), \"log\", data);\n }\n } catch (error) {\n // Call error handler if provided, otherwise silently continue\n if (config.onError && error instanceof Error) {\n config.onError(error, data);\n }\n // Continue logging even if tracer operations fail\n // This ensures logging doesn't break if dd-trace has issues\n }\n\n return data;\n },\n };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { LogLayerPluginParams, LogLayerPlugin } from '@loglayer/plugin';
|
|
2
|
+
import { Tracer } from 'dd-trace';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration parameters for the datadog trace injector plugin.
|
|
6
|
+
*/
|
|
7
|
+
interface DatadogTraceInjectorPluginParams extends LogLayerPluginParams {
|
|
8
|
+
/**
|
|
9
|
+
* dd-trace tracer instance
|
|
10
|
+
*/
|
|
11
|
+
tracerInstance: Tracer;
|
|
12
|
+
/**
|
|
13
|
+
* Optional error handler for tracer operation failures
|
|
14
|
+
* @param error - The error that occurred during tracer operations
|
|
15
|
+
* @param data - The log data that was being processed when the error occurred
|
|
16
|
+
*/
|
|
17
|
+
onError?: (error: Error, data?: Record<string, any>) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Datadog trace injector plugin.
|
|
22
|
+
*
|
|
23
|
+
* This plugin injects the current Datadog APM trace context into the log data.
|
|
24
|
+
*
|
|
25
|
+
* @param config - The datadog trace injector plugin configuration
|
|
26
|
+
* @returns A LogLayer plugin instance
|
|
27
|
+
*/
|
|
28
|
+
declare function datadogTraceInjectorPlugin(config: DatadogTraceInjectorPluginParams): LogLayerPlugin;
|
|
29
|
+
|
|
30
|
+
export { type DatadogTraceInjectorPluginParams, datadogTraceInjectorPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { LogLayerPluginParams, LogLayerPlugin } from '@loglayer/plugin';
|
|
2
|
+
import { Tracer } from 'dd-trace';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration parameters for the datadog trace injector plugin.
|
|
6
|
+
*/
|
|
7
|
+
interface DatadogTraceInjectorPluginParams extends LogLayerPluginParams {
|
|
8
|
+
/**
|
|
9
|
+
* dd-trace tracer instance
|
|
10
|
+
*/
|
|
11
|
+
tracerInstance: Tracer;
|
|
12
|
+
/**
|
|
13
|
+
* Optional error handler for tracer operation failures
|
|
14
|
+
* @param error - The error that occurred during tracer operations
|
|
15
|
+
* @param data - The log data that was being processed when the error occurred
|
|
16
|
+
*/
|
|
17
|
+
onError?: (error: Error, data?: Record<string, any>) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Datadog trace injector plugin.
|
|
22
|
+
*
|
|
23
|
+
* This plugin injects the current Datadog APM trace context into the log data.
|
|
24
|
+
*
|
|
25
|
+
* @param config - The datadog trace injector plugin configuration
|
|
26
|
+
* @returns A LogLayer plugin instance
|
|
27
|
+
*/
|
|
28
|
+
declare function datadogTraceInjectorPlugin(config: DatadogTraceInjectorPluginParams): LogLayerPlugin;
|
|
29
|
+
|
|
30
|
+
export { type DatadogTraceInjectorPluginParams, datadogTraceInjectorPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
function datadogTraceInjectorPlugin(config) {
|
|
3
|
+
const tracer = config.tracerInstance;
|
|
4
|
+
return {
|
|
5
|
+
id: config.id,
|
|
6
|
+
disabled: config.disabled,
|
|
7
|
+
onBeforeDataOut({ data }) {
|
|
8
|
+
if (!data) {
|
|
9
|
+
data = {};
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const span = tracer.scope().active();
|
|
13
|
+
if (span) {
|
|
14
|
+
tracer.inject(span.context(), "log", data);
|
|
15
|
+
}
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (config.onError && error instanceof Error) {
|
|
18
|
+
config.onError(error, data);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
datadogTraceInjectorPlugin
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["import type { LogLayerPlugin, PluginBeforeDataOutParams } from \"@loglayer/plugin\";\nimport type { DatadogTraceInjectorPluginParams } from \"./types.js\";\n\n/**\n * Creates a new Datadog trace injector plugin.\n *\n * This plugin injects the current Datadog APM trace context into the log data.\n *\n * @param config - The datadog trace injector plugin configuration\n * @returns A LogLayer plugin instance\n */\nexport function datadogTraceInjectorPlugin(config: DatadogTraceInjectorPluginParams): LogLayerPlugin {\n const tracer = config.tracerInstance;\n\n return {\n id: config.id,\n disabled: config.disabled,\n onBeforeDataOut({ data }: PluginBeforeDataOutParams): Record<string, any> {\n if (!data) {\n data = {};\n }\n\n try {\n // Associates log with APM trace\n // https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/nodejs/\n const span = tracer.scope().active();\n\n if (span) {\n tracer.inject(span.context(), \"log\", data);\n }\n } catch (error) {\n // Call error handler if provided, otherwise silently continue\n if (config.onError && error instanceof Error) {\n config.onError(error, data);\n }\n // Continue logging even if tracer operations fail\n // This ensures logging doesn't break if dd-trace has issues\n }\n\n return data;\n },\n };\n}\n"],"mappings":";AAWO,SAAS,2BAA2B,QAA0D;AACnG,QAAM,SAAS,OAAO;AAEtB,SAAO;AAAA,IACL,IAAI,OAAO;AAAA,IACX,UAAU,OAAO;AAAA,IACjB,gBAAgB,EAAE,KAAK,GAAmD;AACxE,UAAI,CAAC,MAAM;AACT,eAAO,CAAC;AAAA,MACV;AAEA,UAAI;AAGF,cAAM,OAAO,OAAO,MAAM,EAAE,OAAO;AAEnC,YAAI,MAAM;AACR,iBAAO,OAAO,KAAK,QAAQ,GAAG,OAAO,IAAI;AAAA,QAC3C;AAAA,MACF,SAAS,OAAO;AAEd,YAAI,OAAO,WAAW,iBAAiB,OAAO;AAC5C,iBAAO,QAAQ,OAAO,IAAI;AAAA,QAC5B;AAAA,MAGF;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loglayer/plugin-datadog-apm-trace-injector",
|
|
3
|
+
"description": "Injects DataDog APM traces to logs in LogLayer.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.cts",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/loglayer/loglayer.git",
|
|
23
|
+
"directory": "packages/plugins/datadog-apm-trace-injector"
|
|
24
|
+
},
|
|
25
|
+
"author": "Theo Gravity <theo@suteki.nu>",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"logging",
|
|
28
|
+
"log",
|
|
29
|
+
"loglayer",
|
|
30
|
+
"plugin",
|
|
31
|
+
"datadog",
|
|
32
|
+
"trace",
|
|
33
|
+
"injector",
|
|
34
|
+
"stamp",
|
|
35
|
+
"apm",
|
|
36
|
+
"dd-trace"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@loglayer/plugin": "2.1.1",
|
|
40
|
+
"@loglayer/shared": "2.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "24.2.1",
|
|
44
|
+
"express": "5.1.0",
|
|
45
|
+
"dd-trace": "5.62.0",
|
|
46
|
+
"tsup": "8.5.0",
|
|
47
|
+
"tsx": "4.20.4",
|
|
48
|
+
"typescript": "5.9.2",
|
|
49
|
+
"vitest": "3.2.4",
|
|
50
|
+
"@internal/tsconfig": "2.1.0",
|
|
51
|
+
"loglayer": "6.6.0"
|
|
52
|
+
},
|
|
53
|
+
"bugs": "https://github.com/loglayer/loglayer/issues",
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=18"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"dist"
|
|
59
|
+
],
|
|
60
|
+
"homepage": "https://loglayer.dev",
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsup src/index.ts",
|
|
63
|
+
"test": "vitest --run",
|
|
64
|
+
"livetest": "tsx src/__tests__/livetest.ts",
|
|
65
|
+
"clean": "rm -rf .turbo node_modules dist",
|
|
66
|
+
"lint": "biome check --no-errors-on-unmatched --write --unsafe src",
|
|
67
|
+
"lint:staged": "biome check --no-errors-on-unmatched --write --unsafe --staged src",
|
|
68
|
+
"verify-types": "tsc --noEmit"
|
|
69
|
+
}
|
|
70
|
+
}
|