@yashshukla/observeiq-node 0.1.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,9 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+ export interface TraceContext {
3
+ traceId: string;
4
+ spanId: string;
5
+ }
6
+ export declare const storage: AsyncLocalStorage<TraceContext>;
7
+ export declare function getCurrentContext(): TraceContext | undefined;
8
+ export declare function generateId(): string;
9
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,eAAO,MAAM,OAAO,iCAAwC,CAAC;AAE7D,wBAAgB,iBAAiB,IAAI,YAAY,GAAG,SAAS,CAE5D;AAED,wBAAgB,UAAU,IAAI,MAAM,CAEnC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.storage = void 0;
4
+ exports.getCurrentContext = getCurrentContext;
5
+ exports.generateId = generateId;
6
+ const async_hooks_1 = require("async_hooks");
7
+ const uuid_1 = require("uuid");
8
+ exports.storage = new async_hooks_1.AsyncLocalStorage();
9
+ function getCurrentContext() {
10
+ return exports.storage.getStore();
11
+ }
12
+ function generateId() {
13
+ return (0, uuid_1.v4)();
14
+ }
15
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;;AAUA,8CAEC;AAED,gCAEC;AAhBD,6CAAgD;AAChD,+BAAoC;AAOvB,QAAA,OAAO,GAAG,IAAI,+BAAiB,EAAgB,CAAC;AAE7D,SAAgB,iBAAiB;IAC7B,OAAO,eAAO,CAAC,QAAQ,EAAE,CAAC;AAC9B,CAAC;AAED,SAAgB,UAAU;IACtB,OAAO,IAAA,SAAM,GAAE,CAAC;AACpB,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { Tracer } from './tracer';
2
+ export type { TracerConfig } from './tracer';
3
+ export type { Span } from './types';
4
+ export { getCurrentContext } from './context';
5
+ export type { TraceContext } from './context';
6
+ //# 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,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,YAAY,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCurrentContext = exports.Tracer = void 0;
4
+ var tracer_1 = require("./tracer");
5
+ Object.defineProperty(exports, "Tracer", { enumerable: true, get: function () { return tracer_1.Tracer; } });
6
+ var context_1 = require("./context");
7
+ Object.defineProperty(exports, "getCurrentContext", { enumerable: true, get: function () { return context_1.getCurrentContext; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AAGf,qCAA8C;AAArC,4GAAA,iBAAiB,OAAA"}
@@ -0,0 +1,12 @@
1
+ export interface TracerConfig {
2
+ serviceName: string;
3
+ collectorUrl: string;
4
+ }
5
+ export declare class Tracer {
6
+ private serviceName;
7
+ private collectorUrl;
8
+ constructor(config: TracerConfig);
9
+ trace<T>(operationName: string, fn: () => Promise<T>, tags?: Record<string, string | number | boolean>): Promise<T>;
10
+ private exportSpan;
11
+ }
12
+ //# sourceMappingURL=tracer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.d.ts","sourceRoot":"","sources":["../src/tracer.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,YAAY;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACxB;AAED,qBAAa,MAAM;IACf,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,YAAY,CAAS;gBAEjB,MAAM,EAAE,YAAY;IAM1B,KAAK,CAAC,CAAC,EACT,aAAa,EAAE,MAAM,EACrB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,IAAI,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAM,GACrD,OAAO,CAAC,CAAC,CAAC;YAyCC,UAAU;CAiB3B"}
package/dist/tracer.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tracer = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const context_1 = require("./context");
6
+ class Tracer {
7
+ constructor(config) {
8
+ this.serviceName = config.serviceName;
9
+ // Remove trailing slash to ensure clean URL joining
10
+ this.collectorUrl = config.collectorUrl.replace(/\/$/, '');
11
+ }
12
+ async trace(operationName, fn, tags = {}) {
13
+ const parent = (0, context_1.getCurrentContext)();
14
+ const traceId = parent?.traceId ?? (0, uuid_1.v4)();
15
+ const spanId = (0, uuid_1.v4)();
16
+ const startTime = Date.now();
17
+ return context_1.storage.run({ traceId, spanId }, async () => {
18
+ let status = 'ok';
19
+ let errorMessage;
20
+ let result;
21
+ try {
22
+ result = await fn();
23
+ }
24
+ catch (err) {
25
+ status = 'error';
26
+ errorMessage = err instanceof Error ? err.message : String(err);
27
+ throw err;
28
+ }
29
+ finally {
30
+ const span = {
31
+ traceId,
32
+ spanId,
33
+ parentSpanId: parent?.spanId ?? null,
34
+ operationName,
35
+ serviceName: this.serviceName,
36
+ startTime,
37
+ duration: Date.now() - startTime,
38
+ status,
39
+ tags,
40
+ ...(errorMessage && { error: errorMessage }),
41
+ };
42
+ // Fire and forget — don't let export failure break the caller
43
+ this.exportSpan(span).catch((e) => console.error('[observeiq] Failed to export span:', e));
44
+ }
45
+ return result;
46
+ });
47
+ }
48
+ async exportSpan(span) {
49
+ const controller = new AbortController();
50
+ const timeoutId = setTimeout(() => controller.abort(), 5000);
51
+ try {
52
+ const response = await fetch(this.collectorUrl + '/ingest', {
53
+ method: 'POST',
54
+ headers: { 'Content-Type': 'application/json' },
55
+ body: JSON.stringify(span),
56
+ signal: controller.signal,
57
+ });
58
+ if (!response.ok) {
59
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
60
+ }
61
+ }
62
+ finally {
63
+ clearTimeout(timeoutId);
64
+ }
65
+ }
66
+ }
67
+ exports.Tracer = Tracer;
68
+ //# sourceMappingURL=tracer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.js","sourceRoot":"","sources":["../src/tracer.ts"],"names":[],"mappings":";;;AAAA,+BAAoC;AACpC,uCAAuD;AAQvD,MAAa,MAAM;IAIf,YAAY,MAAoB;QAC5B,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACtC,oDAAoD;QACpD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC/D,CAAC;IAED,KAAK,CAAC,KAAK,CACP,aAAqB,EACrB,EAAoB,EACpB,OAAkD,EAAE;QAEpD,MAAM,MAAM,GAAG,IAAA,2BAAiB,GAAE,CAAC;QACnC,MAAM,OAAO,GAAG,MAAM,EAAE,OAAO,IAAI,IAAA,SAAM,GAAE,CAAC;QAC5C,MAAM,MAAM,GAAG,IAAA,SAAM,GAAE,C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,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,CAAC;QAC7D,IAAI,CAAC;YACD,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE;gBACxD,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,EAAC,cAAc,EAAE,kBAAkB,EAAC;gBAC7C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;gBAC1B,MAAM,EAAE,UAAU,CAAC,MAAM;aAC5B,CAAC,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACf,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;YACvE,CAAC;QACL,CAAC;gBAAS,CAAC;YACP,YAAY,CAAC,SAAS,CAAC,CAAC;QAC5B,CAAC;IACL,CAAC;CACJ;AAxED,wBAwEC"}
@@ -0,0 +1,13 @@
1
+ export interface Span {
2
+ traceId: string;
3
+ spanId: string;
4
+ parentSpanId: string | null;
5
+ operationName: string;
6
+ serviceName: string;
7
+ startTime: number;
8
+ duration: number;
9
+ status: 'ok' | 'error';
10
+ tags: Record<string, string | number | boolean>;
11
+ error?: string;
12
+ }
13
+ //# 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,IAAI;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IAChD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@yashshukla/observeiq-node",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "dev": "tsc --watch"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "dependencies": {
17
+ "uuid": "^14.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^20.19.41",
21
+ "@types/uuid": "^11.0.0",
22
+ "typescript": "^5.0.0"
23
+ }
24
+ }