@forinda/kickjs-otel 1.3.2 → 1.4.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/dist/index.d.ts CHANGED
@@ -1,66 +1,3 @@
1
- import { AppAdapter, Container, AdapterMiddleware } from '@forinda/kickjs-core';
2
-
3
- interface OtelAdapterOptions {
4
- /** Service name reported to the OTel backend (default: 'kickjs-app') */
5
- serviceName?: string;
6
- /** Service version (default: '0.0.0') */
7
- serviceVersion?: string;
8
- /** Enable HTTP request tracing (default: true) */
9
- tracing?: boolean;
10
- /** Enable request metrics — counter, histogram (default: true) */
11
- metrics?: boolean;
12
- /**
13
- * Custom span attributes added to every request span.
14
- * Receives the Express request object.
15
- */
16
- customAttributes?: (req: any) => Record<string, string | number | boolean>;
17
- /**
18
- * Routes to ignore from tracing (e.g., health checks).
19
- * Supports exact match or prefix match with trailing *.
20
- * @example ['/health', '/_debug/*', '/favicon.ico']
21
- */
22
- ignoreRoutes?: string[];
23
- }
24
-
25
- /**
26
- * OpenTelemetry adapter for KickJS — automatic tracing and metrics.
27
- *
28
- * Creates spans for each HTTP request with route, method, status code,
29
- * and duration. Optionally records request count and latency histograms.
30
- *
31
- * Works with any OTel-compatible backend: Jaeger, Grafana Tempo, Datadog,
32
- * Honeycomb, etc. Configure exporters via the OTel SDK before bootstrapping.
33
- *
34
- * @example
35
- * ```ts
36
- * import { OtelAdapter } from '@forinda/kickjs-otel'
37
- *
38
- * // Set up OTel SDK (e.g., with Jaeger exporter) before bootstrap
39
- * bootstrap({
40
- * modules,
41
- * adapters: [
42
- * new OtelAdapter({
43
- * serviceName: 'my-api',
44
- * serviceVersion: '1.0.0',
45
- * ignoreRoutes: ['/health', '/_debug/*'],
46
- * }),
47
- * ],
48
- * })
49
- * ```
50
- */
51
- declare class OtelAdapter implements AppAdapter {
52
- name: string;
53
- private options;
54
- private tracer;
55
- private meter;
56
- private requestCounter;
57
- private requestDuration;
58
- constructor(options?: OtelAdapterOptions);
59
- beforeStart(_app: any, _container: Container): void;
60
- middleware(): AdapterMiddleware[];
61
- private onFinish;
62
- private shouldIgnore;
63
- shutdown(): Promise<void>;
64
- }
65
-
66
- export { OtelAdapter, type OtelAdapterOptions };
1
+ export { OtelAdapter } from './otel.adapter';
2
+ export type { OtelAdapterOptions } from './types';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
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 CHANGED
@@ -1 +1,84 @@
1
- var l=Object.defineProperty;var h=(s,t)=>l(s,"name",{value:t,configurable:!0}),p=(s=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(s,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):s)(function(s){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+s+'" is not supported')});import{Logger as d}from"@forinda/kickjs-core";var u=d.for("OtelAdapter"),c=class{static{h(this,"OtelAdapter")}name="OtelAdapter";options;tracer=null;meter=null;requestCounter=null;requestDuration=null;constructor(t={}){this.options={serviceName:t.serviceName??"kickjs-app",serviceVersion:t.serviceVersion??"0.0.0",tracing:t.tracing??!0,metrics:t.metrics??!0,...t}}beforeStart(t,e){try{let r=p("@opentelemetry/api");this.options.tracing&&(this.tracer=r.trace.getTracer(this.options.serviceName,this.options.serviceVersion),u.info(`Tracing enabled for ${this.options.serviceName}`)),this.options.metrics&&(this.meter=r.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",{description:"HTTP request duration in milliseconds",unit:"ms"}),u.info("Metrics enabled \u2014 http.server.request.count, http.server.request.duration"))}catch{u.warn("OpenTelemetry API not found. Install @opentelemetry/api to enable tracing and metrics.")}}middleware(){return[{handler:h((t,e,r)=>{if(this.shouldIgnore(t.path))return r();let i=performance.now(),n=null;if(this.tracer){let o=p("@opentelemetry/api");n=this.tracer.startSpan(`${t.method} ${t.route?.path??t.path}`,{attributes:{"http.method":t.method,"http.url":t.originalUrl,"http.target":t.path,"http.user_agent":t.get("user-agent")??"","net.host.name":t.hostname,...this.options.customAttributes?.(t)??{}}});let a=o.trace.setSpan(o.context.active(),n);o.context.with(a,()=>{this.onFinish(t,e,i,n),r()});return}this.onFinish(t,e,i,null),r()},"handler"),phase:"beforeGlobal"}]}onFinish(t,e,r,i){e.on("finish",()=>{let n=performance.now()-r,o=t.route?.path??t.path,a={"http.method":t.method,"http.route":o,"http.status_code":e.statusCode};i&&(i.setAttributes({"http.status_code":e.statusCode,"http.route":o}),e.statusCode>=400&&i.setStatus({code:2,message:`HTTP ${e.statusCode}`}),i.end()),this.requestCounter&&this.requestCounter.add(1,a),this.requestDuration&&this.requestDuration.record(n,a)})}shouldIgnore(t){return this.options.ignoreRoutes?this.options.ignoreRoutes.some(e=>e.endsWith("*")?t.startsWith(e.slice(0,-1)):t===e):!1}async shutdown(){u.info("OTel adapter shutdown")}};export{c as OtelAdapter};
1
+ import { Logger as h } from "@forinda/kickjs-core";
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(t, e) {
22
+ try {
23
+ const r = a("@opentelemetry/api");
24
+ this.options.tracing && (this.tracer = r.trace.getTracer(this.options.serviceName, this.options.serviceVersion), u.info(`Tracing enabled for ${this.options.serviceName}`)), this.options.metrics && (this.meter = r.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
+ };
@@ -0,0 +1,43 @@
1
+ import { type AppAdapter, type AdapterMiddleware, type Container } from '@forinda/kickjs-core';
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(_app: any, _container: Container): void;
38
+ middleware(): AdapterMiddleware[];
39
+ private onFinish;
40
+ private shouldIgnore;
41
+ shutdown(): Promise<void>;
42
+ }
43
+ //# sourceMappingURL=otel.adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel.adapter.d.ts","sourceRoot":"","sources":["../src/otel.adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACf,MAAM,sBAAsB,CAAA;AAE7B,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,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,GAAG,IAAI;IA+BnD,UAAU,IAAI,iBAAiB,EAAE;IA2CjC,OAAO,CAAC,QAAQ;IAgChB,OAAO,CAAC,YAAY;IAUd,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAGhC"}
@@ -0,0 +1,22 @@
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
@@ -0,0 +1 @@
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forinda/kickjs-otel",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "OpenTelemetry adapter for KickJS — automatic tracing, metrics, and export to any OTel backend",
5
5
  "keywords": [
6
6
  "kickjs",
@@ -36,7 +36,8 @@
36
36
  "@forinda/kickjs-queue",
37
37
  "@forinda/kickjs-multi-tenant",
38
38
  "@forinda/kickjs-devtools",
39
- "@forinda/kickjs-notifications"
39
+ "@forinda/kickjs-notifications",
40
+ "vite"
40
41
  ],
41
42
  "type": "module",
42
43
  "main": "dist/index.js",
@@ -52,8 +53,8 @@
52
53
  ],
53
54
  "dependencies": {
54
55
  "reflect-metadata": "^0.2.2",
55
- "@forinda/kickjs-core": "1.3.2",
56
- "@forinda/kickjs-http": "1.3.2"
56
+ "@forinda/kickjs-core": "1.4.0",
57
+ "@forinda/kickjs-http": "1.4.0"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "@opentelemetry/api": ">=1.4.0",
@@ -74,12 +75,11 @@
74
75
  }
75
76
  },
76
77
  "devDependencies": {
77
- "@opentelemetry/api": "^1.9.0",
78
- "@opentelemetry/sdk-trace-base": "^1.30.1",
79
- "@opentelemetry/sdk-metrics": "^1.30.1",
80
- "@opentelemetry/semantic-conventions": "^1.28.0",
78
+ "@opentelemetry/api": "^1.9.1",
79
+ "@opentelemetry/sdk-trace-base": "^2.6.1",
80
+ "@opentelemetry/sdk-metrics": "^2.6.1",
81
+ "@opentelemetry/semantic-conventions": "^1.4.0",
81
82
  "@types/node": "^24.5.2",
82
- "tsup": "^8.5.0",
83
83
  "typescript": "^5.9.2"
84
84
  },
85
85
  "publishConfig": {
@@ -100,8 +100,9 @@
100
100
  "url": "https://github.com/forinda/kick-js/issues"
101
101
  },
102
102
  "scripts": {
103
- "build": "tsup",
104
- "dev": "tsup --watch",
103
+ "build": "vite build && pnpm build:types",
104
+ "build:types": "tsc -p tsconfig.build.json",
105
+ "dev": "vite build --watch",
105
106
  "typecheck": "tsc --noEmit",
106
107
  "clean": "rm -rf dist .turbo",
107
108
  "lint": "tsc --noEmit"