@forinda/kickjs-otel 2.0.1 → 2.2.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.
@@ -0,0 +1,70 @@
1
+
2
+ import { AdapterContext, AdapterMiddleware, AppAdapter } from "@forinda/kickjs";
3
+
4
+ //#region src/types.d.ts
5
+ interface OtelAdapterOptions {
6
+ /** Service name reported to the OTel backend (default: 'kickjs-app') */
7
+ serviceName?: string;
8
+ /** Service version (default: '0.0.0') */
9
+ serviceVersion?: string;
10
+ /** Enable HTTP request tracing (default: true) */
11
+ tracing?: boolean;
12
+ /** Enable request metrics — counter, histogram (default: true) */
13
+ metrics?: boolean;
14
+ /**
15
+ * Custom span attributes added to every request span.
16
+ * Receives the Express request object.
17
+ */
18
+ customAttributes?: (req: any) => Record<string, string | number | boolean>;
19
+ /**
20
+ * Routes to ignore from tracing (e.g., health checks).
21
+ * Supports exact match or prefix match with trailing *.
22
+ * @example ['/health', '/_debug/*', '/favicon.ico']
23
+ */
24
+ ignoreRoutes?: string[];
25
+ }
26
+ //#endregion
27
+ //#region src/otel.adapter.d.ts
28
+ /**
29
+ * OpenTelemetry adapter for KickJS — automatic tracing and metrics.
30
+ *
31
+ * Creates spans for each HTTP request with route, method, status code,
32
+ * and duration. Optionally records request count and latency histograms.
33
+ *
34
+ * Works with any OTel-compatible backend: Jaeger, Grafana Tempo, Datadog,
35
+ * Honeycomb, etc. Configure exporters via the OTel SDK before bootstrapping.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { OtelAdapter } from '@forinda/kickjs-otel'
40
+ *
41
+ * // Set up OTel SDK (e.g., with Jaeger exporter) before bootstrap
42
+ * bootstrap({
43
+ * modules,
44
+ * adapters: [
45
+ * new OtelAdapter({
46
+ * serviceName: 'my-api',
47
+ * serviceVersion: '1.0.0',
48
+ * ignoreRoutes: ['/health', '/_debug/*'],
49
+ * }),
50
+ * ],
51
+ * })
52
+ * ```
53
+ */
54
+ declare class OtelAdapter implements AppAdapter {
55
+ name: string;
56
+ private options;
57
+ private tracer;
58
+ private meter;
59
+ private requestCounter;
60
+ private requestDuration;
61
+ constructor(options?: OtelAdapterOptions);
62
+ beforeStart({}: AdapterContext): void;
63
+ middleware(): AdapterMiddleware[];
64
+ private onFinish;
65
+ private shouldIgnore;
66
+ shutdown(): Promise<void>;
67
+ }
68
+ //#endregion
69
+ export { OtelAdapter, type OtelAdapterOptions };
70
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/otel.adapter.ts"],"mappings":";;;;UAAiB,kBAAA;;EAEf,WAAA;EAFe;EAKf,cAAA;;EAGA,OAAA;EANA;EASA,OAAA;EAHA;;;;EASA,gBAAA,IAAoB,GAAA,UAAa,MAAA;EAOjC;;;;;EAAA,YAAA;AAAA;;;;AAxBF;;;;;;;;;;;;;;;;;ACqCA;;;;;;;;cAAa,WAAA,YAAuB,UAAA;EAClC,IAAA;EAAA,QACQ,OAAA;EAAA,QAIA,MAAA;EAAA,QACA,KAAA;EAAA,QACA,cAAA;EAAA,QACA,eAAA;cAEI,OAAA,GAAS,kBAAA;EAUrB,WAAA,CAAA,IAAgB,cAAA;EA+BhB,UAAA,CAAA,GAAc,iBAAA;EAAA,QA2CN,QAAA;EAAA,QAgCA,YAAA;EAUF,QAAA,CAAA,GAAY,OAAA;AAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,147 @@
1
+ /**
2
+ * @forinda/kickjs-otel v2.2.0
3
+ *
4
+ * Copyright (c) Felix Orinda
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ import { createRequire } from "node:module";
12
+ import { Logger } from "@forinda/kickjs";
13
+ //#region \0rolldown/runtime.js
14
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
15
+ //#endregion
16
+ //#region src/otel.adapter.ts
17
+ const log = Logger.for("OtelAdapter");
18
+ /**
19
+ * OpenTelemetry adapter for KickJS — automatic tracing and metrics.
20
+ *
21
+ * Creates spans for each HTTP request with route, method, status code,
22
+ * and duration. Optionally records request count and latency histograms.
23
+ *
24
+ * Works with any OTel-compatible backend: Jaeger, Grafana Tempo, Datadog,
25
+ * Honeycomb, etc. Configure exporters via the OTel SDK before bootstrapping.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { OtelAdapter } from '@forinda/kickjs-otel'
30
+ *
31
+ * // Set up OTel SDK (e.g., with Jaeger exporter) before bootstrap
32
+ * bootstrap({
33
+ * modules,
34
+ * adapters: [
35
+ * new OtelAdapter({
36
+ * serviceName: 'my-api',
37
+ * serviceVersion: '1.0.0',
38
+ * ignoreRoutes: ['/health', '/_debug/*'],
39
+ * }),
40
+ * ],
41
+ * })
42
+ * ```
43
+ */
44
+ var OtelAdapter = class {
45
+ name = "OtelAdapter";
46
+ options;
47
+ tracer = null;
48
+ meter = null;
49
+ requestCounter = null;
50
+ requestDuration = null;
51
+ constructor(options = {}) {
52
+ this.options = {
53
+ serviceName: options.serviceName ?? "kickjs-app",
54
+ serviceVersion: options.serviceVersion ?? "0.0.0",
55
+ tracing: options.tracing ?? true,
56
+ metrics: options.metrics ?? true,
57
+ ...options
58
+ };
59
+ }
60
+ beforeStart({}) {
61
+ try {
62
+ const otelApi = __require("@opentelemetry/api");
63
+ if (this.options.tracing) {
64
+ this.tracer = otelApi.trace.getTracer(this.options.serviceName, this.options.serviceVersion);
65
+ log.info(`Tracing enabled for ${this.options.serviceName}`);
66
+ }
67
+ if (this.options.metrics) {
68
+ this.meter = otelApi.metrics.getMeter(this.options.serviceName, this.options.serviceVersion);
69
+ this.requestCounter = this.meter.createCounter("http.server.request.count", { description: "Total number of HTTP requests" });
70
+ this.requestDuration = this.meter.createHistogram("http.server.request.duration", {
71
+ description: "HTTP request duration in milliseconds",
72
+ unit: "ms"
73
+ });
74
+ log.info("Metrics enabled — http.server.request.count, http.server.request.duration");
75
+ }
76
+ } catch {
77
+ log.warn("OpenTelemetry API not found. Install @opentelemetry/api to enable tracing and metrics.");
78
+ }
79
+ }
80
+ middleware() {
81
+ return [{
82
+ handler: (req, res, next) => {
83
+ if (this.shouldIgnore(req.path)) return next();
84
+ const startTime = performance.now();
85
+ let span = null;
86
+ if (this.tracer) {
87
+ const otelApi = __require("@opentelemetry/api");
88
+ span = this.tracer.startSpan(`${req.method} ${req.route?.path ?? req.path}`, { attributes: {
89
+ "http.method": req.method,
90
+ "http.url": req.originalUrl,
91
+ "http.target": req.path,
92
+ "http.user_agent": req.get("user-agent") ?? "",
93
+ "net.host.name": req.hostname,
94
+ ...this.options.customAttributes?.(req) ?? {}
95
+ } });
96
+ const ctx = otelApi.trace.setSpan(otelApi.context.active(), span);
97
+ otelApi.context.with(ctx, () => {
98
+ this.onFinish(req, res, startTime, span);
99
+ next();
100
+ });
101
+ return;
102
+ }
103
+ this.onFinish(req, res, startTime, null);
104
+ next();
105
+ },
106
+ phase: "beforeGlobal"
107
+ }];
108
+ }
109
+ onFinish(req, res, startTime, span) {
110
+ res.on("finish", () => {
111
+ const duration = performance.now() - startTime;
112
+ const route = req.route?.path ?? req.path;
113
+ const attributes = {
114
+ "http.method": req.method,
115
+ "http.route": route,
116
+ "http.status_code": res.statusCode
117
+ };
118
+ if (span) {
119
+ span.setAttributes({
120
+ "http.status_code": res.statusCode,
121
+ "http.route": route
122
+ });
123
+ if (res.statusCode >= 400) span.setStatus({
124
+ code: 2,
125
+ message: `HTTP ${res.statusCode}`
126
+ });
127
+ span.end();
128
+ }
129
+ if (this.requestCounter) this.requestCounter.add(1, attributes);
130
+ if (this.requestDuration) this.requestDuration.record(duration, attributes);
131
+ });
132
+ }
133
+ shouldIgnore(path) {
134
+ if (!this.options.ignoreRoutes) return false;
135
+ return this.options.ignoreRoutes.some((pattern) => {
136
+ if (pattern.endsWith("*")) return path.startsWith(pattern.slice(0, -1));
137
+ return path === pattern;
138
+ });
139
+ }
140
+ async shutdown() {
141
+ log.info("OTel adapter shutdown");
142
+ }
143
+ };
144
+ //#endregion
145
+ export { OtelAdapter };
146
+
147
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/otel.adapter.ts"],"sourcesContent":["import {\n Logger,\n type AppAdapter,\n type AdapterContext,\n type AdapterMiddleware,\n} from '@forinda/kickjs'\nimport type { Request, Response, NextFunction } from 'express'\nimport type { OtelAdapterOptions } from './types'\n\nconst log = Logger.for('OtelAdapter')\n\n/**\n * OpenTelemetry adapter for KickJS — automatic tracing and metrics.\n *\n * Creates spans for each HTTP request with route, method, status code,\n * and duration. Optionally records request count and latency histograms.\n *\n * Works with any OTel-compatible backend: Jaeger, Grafana Tempo, Datadog,\n * Honeycomb, etc. Configure exporters via the OTel SDK before bootstrapping.\n *\n * @example\n * ```ts\n * import { OtelAdapter } from '@forinda/kickjs-otel'\n *\n * // Set up OTel SDK (e.g., with Jaeger exporter) before bootstrap\n * bootstrap({\n * modules,\n * adapters: [\n * new OtelAdapter({\n * serviceName: 'my-api',\n * serviceVersion: '1.0.0',\n * ignoreRoutes: ['/health', '/_debug/*'],\n * }),\n * ],\n * })\n * ```\n */\nexport class OtelAdapter implements AppAdapter {\n name = 'OtelAdapter'\n private options: Required<\n Pick<OtelAdapterOptions, 'serviceName' | 'serviceVersion' | 'tracing' | 'metrics'>\n > &\n OtelAdapterOptions\n private tracer: any = null\n private meter: any = null\n private requestCounter: any = null\n private requestDuration: any = null\n\n constructor(options: OtelAdapterOptions = {}) {\n this.options = {\n serviceName: options.serviceName ?? 'kickjs-app',\n serviceVersion: options.serviceVersion ?? '0.0.0',\n tracing: options.tracing ?? true,\n metrics: options.metrics ?? true,\n ...options,\n }\n }\n\n beforeStart({}: AdapterContext): void {\n try {\n // Dynamically import OTel API — it's a peer dependency\n const otelApi = require('@opentelemetry/api')\n\n if (this.options.tracing) {\n this.tracer = otelApi.trace.getTracer(this.options.serviceName, this.options.serviceVersion)\n log.info(`Tracing enabled for ${this.options.serviceName}`)\n }\n\n if (this.options.metrics) {\n this.meter = otelApi.metrics.getMeter(this.options.serviceName, this.options.serviceVersion)\n\n this.requestCounter = this.meter.createCounter('http.server.request.count', {\n description: 'Total number of HTTP requests',\n })\n\n this.requestDuration = this.meter.createHistogram('http.server.request.duration', {\n description: 'HTTP request duration in milliseconds',\n unit: 'ms',\n })\n\n log.info('Metrics enabled — http.server.request.count, http.server.request.duration')\n }\n } catch {\n log.warn(\n 'OpenTelemetry API not found. Install @opentelemetry/api to enable tracing and metrics.',\n )\n }\n }\n\n middleware(): AdapterMiddleware[] {\n return [\n {\n handler: (req: Request, res: Response, next: NextFunction) => {\n // Skip ignored routes\n if (this.shouldIgnore(req.path)) {\n return next()\n }\n\n const startTime = performance.now()\n\n // Start a span if tracing is enabled\n let span: any = null\n if (this.tracer) {\n const otelApi = require('@opentelemetry/api')\n span = this.tracer.startSpan(`${req.method} ${req.route?.path ?? req.path}`, {\n attributes: {\n 'http.method': req.method,\n 'http.url': req.originalUrl,\n 'http.target': req.path,\n 'http.user_agent': req.get('user-agent') ?? '',\n 'net.host.name': req.hostname,\n ...(this.options.customAttributes?.(req) ?? {}),\n },\n })\n\n // Set span on context so downstream code can add attributes\n const ctx = otelApi.trace.setSpan(otelApi.context.active(), span)\n otelApi.context.with(ctx, () => {\n this.onFinish(req, res, startTime, span)\n next()\n })\n return\n }\n\n this.onFinish(req, res, startTime, null)\n next()\n },\n phase: 'beforeGlobal',\n },\n ]\n }\n\n private onFinish(req: Request, res: Response, startTime: number, span: any): void {\n res.on('finish', () => {\n const duration = performance.now() - startTime\n const route = (req as any).route?.path ?? req.path\n const attributes = {\n 'http.method': req.method,\n 'http.route': route,\n 'http.status_code': res.statusCode,\n }\n\n // End span\n if (span) {\n span.setAttributes({\n 'http.status_code': res.statusCode,\n 'http.route': route,\n })\n if (res.statusCode >= 400) {\n span.setStatus({ code: 2, message: `HTTP ${res.statusCode}` })\n }\n span.end()\n }\n\n // Record metrics\n if (this.requestCounter) {\n this.requestCounter.add(1, attributes)\n }\n if (this.requestDuration) {\n this.requestDuration.record(duration, attributes)\n }\n })\n }\n\n private shouldIgnore(path: string): boolean {\n if (!this.options.ignoreRoutes) return false\n return this.options.ignoreRoutes.some((pattern) => {\n if (pattern.endsWith('*')) {\n return path.startsWith(pattern.slice(0, -1))\n }\n return path === pattern\n })\n }\n\n async shutdown(): Promise<void> {\n log.info('OTel adapter shutdown')\n }\n}\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forinda/kickjs-otel",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "OpenTelemetry adapter for KickJS — automatic tracing, metrics, and export to any OTel backend",
5
5
  "keywords": [
6
6
  "kickjs",
@@ -40,20 +40,38 @@
40
40
  "vite"
41
41
  ],
42
42
  "type": "module",
43
- "main": "dist/index.js",
44
- "types": "dist/index.d.ts",
43
+ "main": "dist/index.mjs",
44
+ "types": "dist/index.d.mts",
45
45
  "exports": {
46
46
  ".": {
47
- "import": "./dist/index.js",
48
- "types": "./dist/index.d.ts"
47
+ "import": "./dist/index.mjs",
48
+ "types": "./dist/index.d.mts"
49
49
  }
50
50
  },
51
51
  "files": [
52
52
  "dist"
53
53
  ],
54
+ "wireit": {
55
+ "build": {
56
+ "command": "tsdown",
57
+ "files": [
58
+ "src/**/*.ts",
59
+ "tsdown.config.ts",
60
+ "tsconfig.json",
61
+ "package.json"
62
+ ],
63
+ "output": [
64
+ "dist/**"
65
+ ],
66
+ "dependencies": [
67
+ "../core:build",
68
+ "../http:build"
69
+ ]
70
+ }
71
+ },
54
72
  "dependencies": {
55
73
  "reflect-metadata": "^0.2.2",
56
- "@forinda/kickjs": "2.0.1"
74
+ "@forinda/kickjs": "2.2.0"
57
75
  },
58
76
  "peerDependencies": {
59
77
  "@opentelemetry/api": ">=1.4.0",
@@ -99,11 +117,10 @@
99
117
  "url": "https://github.com/forinda/kick-js/issues"
100
118
  },
101
119
  "scripts": {
102
- "build": "vite build && pnpm build:types",
103
- "build:types": "tsc -p tsconfig.build.json",
104
- "dev": "vite build --watch",
120
+ "build": "wireit",
121
+ "dev": "tsdown --watch",
105
122
  "typecheck": "tsc --noEmit",
106
- "clean": "rm -rf dist .turbo",
123
+ "clean": "rm -rf dist .wireit",
107
124
  "lint": "tsc --noEmit"
108
125
  }
109
126
  }
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export { OtelAdapter } from './otel.adapter';
2
- export type { OtelAdapterOptions } from './types';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,YAAY,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA"}
package/dist/index.js DELETED
@@ -1,84 +0,0 @@
1
- import { Logger as h } from "@forinda/kickjs";
2
- var a = /* @__PURE__ */ ((t) => typeof require < "u" ? require : typeof Proxy < "u" ? new Proxy(t, { get: (e, r) => (typeof require < "u" ? require : e)[r] }) : t)(function(t) {
3
- if (typeof require < "u") return require.apply(this, arguments);
4
- throw Error('Calling `require` for "' + t + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
5
- }), u = h.for("OtelAdapter"), p = class {
6
- name = "OtelAdapter";
7
- options;
8
- tracer = null;
9
- meter = null;
10
- requestCounter = null;
11
- requestDuration = null;
12
- constructor(t = {}) {
13
- this.options = {
14
- serviceName: t.serviceName ?? "kickjs-app",
15
- serviceVersion: t.serviceVersion ?? "0.0.0",
16
- tracing: t.tracing ?? !0,
17
- metrics: t.metrics ?? !0,
18
- ...t
19
- };
20
- }
21
- beforeStart({}) {
22
- try {
23
- const t = a("@opentelemetry/api");
24
- this.options.tracing && (this.tracer = t.trace.getTracer(this.options.serviceName, this.options.serviceVersion), u.info(`Tracing enabled for ${this.options.serviceName}`)), this.options.metrics && (this.meter = t.metrics.getMeter(this.options.serviceName, this.options.serviceVersion), this.requestCounter = this.meter.createCounter("http.server.request.count", { description: "Total number of HTTP requests" }), this.requestDuration = this.meter.createHistogram("http.server.request.duration", {
25
- description: "HTTP request duration in milliseconds",
26
- unit: "ms"
27
- }), u.info("Metrics enabled — http.server.request.count, http.server.request.duration"));
28
- } catch {
29
- u.warn("OpenTelemetry API not found. Install @opentelemetry/api to enable tracing and metrics.");
30
- }
31
- }
32
- middleware() {
33
- return [{
34
- handler: (t, e, r) => {
35
- if (this.shouldIgnore(t.path)) return r();
36
- const i = performance.now();
37
- let o = null;
38
- if (this.tracer) {
39
- const s = a("@opentelemetry/api");
40
- o = this.tracer.startSpan(`${t.method} ${t.route?.path ?? t.path}`, { attributes: {
41
- "http.method": t.method,
42
- "http.url": t.originalUrl,
43
- "http.target": t.path,
44
- "http.user_agent": t.get("user-agent") ?? "",
45
- "net.host.name": t.hostname,
46
- ...this.options.customAttributes?.(t) ?? {}
47
- } });
48
- const n = s.trace.setSpan(s.context.active(), o);
49
- s.context.with(n, () => {
50
- this.onFinish(t, e, i, o), r();
51
- });
52
- return;
53
- }
54
- this.onFinish(t, e, i, null), r();
55
- },
56
- phase: "beforeGlobal"
57
- }];
58
- }
59
- onFinish(t, e, r, i) {
60
- e.on("finish", () => {
61
- const o = performance.now() - r, s = t.route?.path ?? t.path, n = {
62
- "http.method": t.method,
63
- "http.route": s,
64
- "http.status_code": e.statusCode
65
- };
66
- i && (i.setAttributes({
67
- "http.status_code": e.statusCode,
68
- "http.route": s
69
- }), e.statusCode >= 400 && i.setStatus({
70
- code: 2,
71
- message: `HTTP ${e.statusCode}`
72
- }), i.end()), this.requestCounter && this.requestCounter.add(1, n), this.requestDuration && this.requestDuration.record(o, n);
73
- });
74
- }
75
- shouldIgnore(t) {
76
- return this.options.ignoreRoutes ? this.options.ignoreRoutes.some((e) => e.endsWith("*") ? t.startsWith(e.slice(0, -1)) : t === e) : !1;
77
- }
78
- async shutdown() {
79
- u.info("OTel adapter shutdown");
80
- }
81
- };
82
- export {
83
- p as OtelAdapter
84
- };
@@ -1,43 +0,0 @@
1
- import { type AppAdapter, type AdapterContext, type AdapterMiddleware } from '@forinda/kickjs';
2
- import type { OtelAdapterOptions } from './types';
3
- /**
4
- * OpenTelemetry adapter for KickJS — automatic tracing and metrics.
5
- *
6
- * Creates spans for each HTTP request with route, method, status code,
7
- * and duration. Optionally records request count and latency histograms.
8
- *
9
- * Works with any OTel-compatible backend: Jaeger, Grafana Tempo, Datadog,
10
- * Honeycomb, etc. Configure exporters via the OTel SDK before bootstrapping.
11
- *
12
- * @example
13
- * ```ts
14
- * import { OtelAdapter } from '@forinda/kickjs-otel'
15
- *
16
- * // Set up OTel SDK (e.g., with Jaeger exporter) before bootstrap
17
- * bootstrap({
18
- * modules,
19
- * adapters: [
20
- * new OtelAdapter({
21
- * serviceName: 'my-api',
22
- * serviceVersion: '1.0.0',
23
- * ignoreRoutes: ['/health', '/_debug/*'],
24
- * }),
25
- * ],
26
- * })
27
- * ```
28
- */
29
- export declare class OtelAdapter implements AppAdapter {
30
- name: string;
31
- private options;
32
- private tracer;
33
- private meter;
34
- private requestCounter;
35
- private requestDuration;
36
- constructor(options?: OtelAdapterOptions);
37
- beforeStart({}: AdapterContext): void;
38
- middleware(): AdapterMiddleware[];
39
- private onFinish;
40
- private shouldIgnore;
41
- shutdown(): Promise<void>;
42
- }
43
- //# sourceMappingURL=otel.adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"otel.adapter.d.ts","sourceRoot":"","sources":["../src/otel.adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACvB,MAAM,iBAAiB,CAAA;AAExB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAIjD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,WAAY,YAAW,UAAU;IAC5C,IAAI,SAAgB;IACpB,OAAO,CAAC,OAAO,CAGK;IACpB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,KAAK,CAAY;IACzB,OAAO,CAAC,cAAc,CAAY;IAClC,OAAO,CAAC,eAAe,CAAY;gBAEvB,OAAO,GAAE,kBAAuB;IAU5C,WAAW,CAAC,EAAE,EAAE,cAAc,GAAG,IAAI;IA+BrC,UAAU,IAAI,iBAAiB,EAAE;IA2CjC,OAAO,CAAC,QAAQ;IAgChB,OAAO,CAAC,YAAY;IAUd,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAGhC"}
package/dist/types.d.ts DELETED
@@ -1,22 +0,0 @@
1
- export interface OtelAdapterOptions {
2
- /** Service name reported to the OTel backend (default: 'kickjs-app') */
3
- serviceName?: string;
4
- /** Service version (default: '0.0.0') */
5
- serviceVersion?: string;
6
- /** Enable HTTP request tracing (default: true) */
7
- tracing?: boolean;
8
- /** Enable request metrics — counter, histogram (default: true) */
9
- metrics?: boolean;
10
- /**
11
- * Custom span attributes added to every request span.
12
- * Receives the Express request object.
13
- */
14
- customAttributes?: (req: any) => Record<string, string | number | boolean>;
15
- /**
16
- * Routes to ignore from tracing (e.g., health checks).
17
- * Supports exact match or prefix match with trailing *.
18
- * @example ['/health', '/_debug/*', '/favicon.ico']
19
- */
20
- ignoreRoutes?: string[];
21
- }
22
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB,yCAAyC;IACzC,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB,kDAAkD;IAClD,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB,kEAAkE;IAClE,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAA;IAE1E;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CACxB"}