@traccia2/sdk 0.0.1

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.
Files changed (130) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +503 -0
  3. package/dist/auto.d.ts +27 -0
  4. package/dist/auto.d.ts.map +1 -0
  5. package/dist/auto.js +171 -0
  6. package/dist/auto.js.map +1 -0
  7. package/dist/config/env-config.d.ts +21 -0
  8. package/dist/config/env-config.d.ts.map +1 -0
  9. package/dist/config/env-config.js +111 -0
  10. package/dist/config/env-config.js.map +1 -0
  11. package/dist/config/pricing-config.d.ts +27 -0
  12. package/dist/config/pricing-config.d.ts.map +1 -0
  13. package/dist/config/pricing-config.js +74 -0
  14. package/dist/config/pricing-config.js.map +1 -0
  15. package/dist/config/runtime-config.d.ts +65 -0
  16. package/dist/config/runtime-config.d.ts.map +1 -0
  17. package/dist/config/runtime-config.js +97 -0
  18. package/dist/config/runtime-config.js.map +1 -0
  19. package/dist/context/context.d.ts +29 -0
  20. package/dist/context/context.d.ts.map +1 -0
  21. package/dist/context/context.js +48 -0
  22. package/dist/context/context.js.map +1 -0
  23. package/dist/exporter/console-exporter.d.ts +18 -0
  24. package/dist/exporter/console-exporter.d.ts.map +1 -0
  25. package/dist/exporter/console-exporter.js +39 -0
  26. package/dist/exporter/console-exporter.js.map +1 -0
  27. package/dist/exporter/http-exporter.d.ts +57 -0
  28. package/dist/exporter/http-exporter.d.ts.map +1 -0
  29. package/dist/exporter/http-exporter.js +181 -0
  30. package/dist/exporter/http-exporter.js.map +1 -0
  31. package/dist/exporter/index.d.ts +7 -0
  32. package/dist/exporter/index.d.ts.map +1 -0
  33. package/dist/exporter/index.js +12 -0
  34. package/dist/exporter/index.js.map +1 -0
  35. package/dist/index.d.ts +10 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +32 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/integrations/index.d.ts +9 -0
  40. package/dist/integrations/index.d.ts.map +1 -0
  41. package/dist/integrations/index.js +16 -0
  42. package/dist/integrations/index.js.map +1 -0
  43. package/dist/integrations/langchain-callback.d.ts +72 -0
  44. package/dist/integrations/langchain-callback.d.ts.map +1 -0
  45. package/dist/integrations/langchain-callback.js +201 -0
  46. package/dist/integrations/langchain-callback.js.map +1 -0
  47. package/dist/integrations/langgraph-instrumentation.d.ts +57 -0
  48. package/dist/integrations/langgraph-instrumentation.d.ts.map +1 -0
  49. package/dist/integrations/langgraph-instrumentation.js +162 -0
  50. package/dist/integrations/langgraph-instrumentation.js.map +1 -0
  51. package/dist/processor/batch-processor.d.ts +68 -0
  52. package/dist/processor/batch-processor.d.ts.map +1 -0
  53. package/dist/processor/batch-processor.js +150 -0
  54. package/dist/processor/batch-processor.js.map +1 -0
  55. package/dist/processor/cost-processor.d.ts +16 -0
  56. package/dist/processor/cost-processor.d.ts.map +1 -0
  57. package/dist/processor/cost-processor.js +50 -0
  58. package/dist/processor/cost-processor.js.map +1 -0
  59. package/dist/processor/index.d.ts +9 -0
  60. package/dist/processor/index.d.ts.map +1 -0
  61. package/dist/processor/index.js +18 -0
  62. package/dist/processor/index.js.map +1 -0
  63. package/dist/processor/logging-processor.d.ts +13 -0
  64. package/dist/processor/logging-processor.d.ts.map +1 -0
  65. package/dist/processor/logging-processor.js +26 -0
  66. package/dist/processor/logging-processor.js.map +1 -0
  67. package/dist/processor/sampler.d.ts +20 -0
  68. package/dist/processor/sampler.d.ts.map +1 -0
  69. package/dist/processor/sampler.js +33 -0
  70. package/dist/processor/sampler.js.map +1 -0
  71. package/dist/processor/token-counter.d.ts +13 -0
  72. package/dist/processor/token-counter.d.ts.map +1 -0
  73. package/dist/processor/token-counter.js +40 -0
  74. package/dist/processor/token-counter.js.map +1 -0
  75. package/dist/tracer/index.d.ts +8 -0
  76. package/dist/tracer/index.d.ts.map +1 -0
  77. package/dist/tracer/index.js +15 -0
  78. package/dist/tracer/index.js.map +1 -0
  79. package/dist/tracer/provider.d.ts +59 -0
  80. package/dist/tracer/provider.d.ts.map +1 -0
  81. package/dist/tracer/provider.js +114 -0
  82. package/dist/tracer/provider.js.map +1 -0
  83. package/dist/tracer/span-context.d.ts +23 -0
  84. package/dist/tracer/span-context.d.ts.map +1 -0
  85. package/dist/tracer/span-context.js +34 -0
  86. package/dist/tracer/span-context.js.map +1 -0
  87. package/dist/tracer/span.d.ts +49 -0
  88. package/dist/tracer/span.d.ts.map +1 -0
  89. package/dist/tracer/span.js +118 -0
  90. package/dist/tracer/span.js.map +1 -0
  91. package/dist/tracer/tracer.d.ts +28 -0
  92. package/dist/tracer/tracer.d.ts.map +1 -0
  93. package/dist/tracer/tracer.js +75 -0
  94. package/dist/tracer/tracer.js.map +1 -0
  95. package/dist/types.d.ts +135 -0
  96. package/dist/types.d.ts.map +1 -0
  97. package/dist/types.js +16 -0
  98. package/dist/types.js.map +1 -0
  99. package/package.json +79 -0
  100. package/src/__tests__/exporter.test.ts +62 -0
  101. package/src/__tests__/integrations-langchain.test.ts +384 -0
  102. package/src/__tests__/integrations-langgraph.test.ts +479 -0
  103. package/src/__tests__/processor.test.ts +89 -0
  104. package/src/__tests__/span.test.ts +103 -0
  105. package/src/__tests__/tracer.test.ts +89 -0
  106. package/src/auto.ts +198 -0
  107. package/src/config/env-config.ts +93 -0
  108. package/src/config/pricing-config.ts +84 -0
  109. package/src/config/runtime-config.ts +108 -0
  110. package/src/context/context.ts +52 -0
  111. package/src/exporter/console-exporter.ts +38 -0
  112. package/src/exporter/http-exporter.ts +188 -0
  113. package/src/exporter/index.ts +7 -0
  114. package/src/index.ts +51 -0
  115. package/src/integrations/README.md +287 -0
  116. package/src/integrations/index.ts +13 -0
  117. package/src/integrations/langchain-callback.ts +229 -0
  118. package/src/integrations/langgraph-instrumentation.ts +174 -0
  119. package/src/processor/batch-processor.ts +180 -0
  120. package/src/processor/cost-processor.ts +57 -0
  121. package/src/processor/index.ts +9 -0
  122. package/src/processor/logging-processor.ts +26 -0
  123. package/src/processor/sampler.ts +35 -0
  124. package/src/processor/token-counter.ts +42 -0
  125. package/src/tracer/index.ts +8 -0
  126. package/src/tracer/provider.ts +130 -0
  127. package/src/tracer/span-context.ts +46 -0
  128. package/src/tracer/span.ts +145 -0
  129. package/src/tracer/tracer.ts +100 -0
  130. package/src/types.ts +155 -0
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ /**
3
+ * Span implementation with lifecycle management.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Span = void 0;
7
+ const types_1 = require("../types");
8
+ const span_context_1 = require("./span-context");
9
+ const runtime_config_1 = require("../config/runtime-config");
10
+ /**
11
+ * Span implementation representing a unit of work.
12
+ */
13
+ class Span {
14
+ constructor(name, _tracer, context, parentSpanId, attributes) {
15
+ this.events = [];
16
+ this.status = types_1.SpanStatus.UNSET;
17
+ this.ended = false;
18
+ this.name = name;
19
+ this.context = context;
20
+ this.parentSpanId = parentSpanId;
21
+ this.attributes = attributes ? { ...attributes } : {};
22
+ this.startTimeNs = performance.now() * 1000000;
23
+ // Apply runtime metadata to tracestate
24
+ this.enrichTraceState();
25
+ }
26
+ /**
27
+ * Get the duration in nanoseconds.
28
+ */
29
+ get durationNs() {
30
+ if (this.endTimeNs === undefined) {
31
+ return undefined;
32
+ }
33
+ return this.endTimeNs - this.startTimeNs;
34
+ }
35
+ /**
36
+ * Set an attribute on the span.
37
+ */
38
+ setAttribute(key, value) {
39
+ if (this.ended) {
40
+ return;
41
+ }
42
+ this.attributes[key] = value;
43
+ }
44
+ /**
45
+ * Add an event to the span.
46
+ */
47
+ addEvent(name, attributes) {
48
+ if (this.ended) {
49
+ return;
50
+ }
51
+ this.events.push({
52
+ name,
53
+ timestamp: performance.now() * 1000000,
54
+ attributes: attributes ? { ...attributes } : {},
55
+ });
56
+ }
57
+ /**
58
+ * Record an exception on the span.
59
+ */
60
+ recordException(error, attributes) {
61
+ if (this.ended) {
62
+ return;
63
+ }
64
+ this.status = types_1.SpanStatus.ERROR;
65
+ this.statusDescription = error.message;
66
+ this.addEvent('exception', {
67
+ ...attributes,
68
+ 'exception.type': error.name,
69
+ 'exception.message': error.message,
70
+ 'exception.stacktrace': error.stack,
71
+ });
72
+ }
73
+ /**
74
+ * End the span.
75
+ */
76
+ end() {
77
+ if (this.ended) {
78
+ return;
79
+ }
80
+ this.ended = true;
81
+ this.endTimeNs = performance.now() * 1000000;
82
+ }
83
+ /**
84
+ * Check if the span is still recording.
85
+ */
86
+ isRecording() {
87
+ return !this.ended;
88
+ }
89
+ /**
90
+ * Enrich the trace state with runtime metadata.
91
+ */
92
+ enrichTraceState() {
93
+ try {
94
+ const config = (0, runtime_config_1.getConfig)();
95
+ let traceState = this.context.traceState || '';
96
+ const metadata = {};
97
+ if (config.tenantId) {
98
+ metadata['tenant'] = config.tenantId;
99
+ }
100
+ if (config.projectId) {
101
+ metadata['project'] = config.projectId;
102
+ }
103
+ if (config.debug) {
104
+ metadata['dbg'] = '1';
105
+ }
106
+ if (Object.keys(metadata).length > 0) {
107
+ const pairs = Object.entries(metadata).map(([k, v]) => `${k}=${v}`);
108
+ traceState = pairs.join(',');
109
+ this.context = new span_context_1.SpanContext(this.context.traceId, this.context.spanId, this.context.traceFlags, traceState);
110
+ }
111
+ }
112
+ catch {
113
+ // Silently fail on trace state enrichment
114
+ }
115
+ }
116
+ }
117
+ exports.Span = Span;
118
+ //# sourceMappingURL=span.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span.js","sourceRoot":"","sources":["../../src/tracer/span.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oCAA+E;AAC/E,iDAA6C;AAC7C,6DAAqD;AAErD;;GAEG;AACH,MAAa,IAAI;IAaf,YACE,IAAY,EACZ,OAAgB,EAChB,OAAqB,EACrB,YAAqB,EACrB,UAAoC;QAb/B,WAAM,GAAgB,EAAE,CAAC;QACzB,WAAM,GAAe,kBAAU,CAAC,KAAK,CAAC;QAKrC,UAAK,GAAG,KAAK,CAAC;QASpB,IAAI,CAAC,IA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Q,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBACpE,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAE7B,IAAI,CAAC,OAAO,GAAG,IAAI,0BAAW,CAC5B,IAAI,CAAC,OAAO,CAAC,OAAO,EACpB,IAAI,CAAC,OAAO,CAAC,MAAM,EACnB,IAAI,CAAC,OAAO,CAAC,UAAU,EACvB,UAAU,CACX,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,0CAA0C;QAC5C,CAAC;IACH,CAAC;CACF;AArID,oBAqIC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Tracer implementation for creating and managing spans.
3
+ */
4
+ import { ITracer, ISpan, ISpanContext } from '../types';
5
+ import { TracerProvider } from './provider';
6
+ /**
7
+ * Tracer for creating and managing spans.
8
+ */
9
+ export declare class Tracer implements ITracer {
10
+ private provider;
11
+ constructor(provider: TracerProvider, _instrumentationScope: string);
12
+ /**
13
+ * Start a new span.
14
+ */
15
+ startSpan(name: string, options?: {
16
+ attributes?: Record<string, unknown>;
17
+ parent?: ISpan | null;
18
+ parentContext?: ISpanContext | null;
19
+ }): ISpan;
20
+ /**
21
+ * Start an active span and run a function within it.
22
+ */
23
+ startActiveSpan<T = unknown>(name: string, fn: (span: ISpan) => Promise<T> | T, options?: {
24
+ attributes?: Record<string, unknown>;
25
+ parent?: ISpan | null;
26
+ }): Promise<T>;
27
+ }
28
+ //# sourceMappingURL=tracer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.d.ts","sourceRoot":"","sources":["../../src/tracer/tracer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAIxD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C;;GAEG;AACH,qBAAa,MAAO,YAAW,OAAO;IACpC,OAAO,CAAC,QAAQ,CAAiB;gBAErB,QAAQ,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM;IAInE;;OAEG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACrC,MAAM,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;QACtB,aAAa,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;KACrC,GACA,KAAK;IAyCR;;OAEG;IACG,eAAe,CAAC,CAAC,GAAG,OAAO,EAC/B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACnC,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACrC,MAAM,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;KACvB,GACA,OAAO,CAAC,CAAC,CAAC;CAiBd"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ /**
3
+ * Tracer implementation for creating and managing spans.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Tracer = void 0;
7
+ const span_1 = require("./span");
8
+ const span_context_1 = require("./span-context");
9
+ const context_1 = require("../context/context");
10
+ const runtime_config_1 = require("../config/runtime-config");
11
+ /**
12
+ * Tracer for creating and managing spans.
13
+ */
14
+ class Tracer {
15
+ constructor(provider, _instrumentationScope) {
16
+ this.provider = provider;
17
+ }
18
+ /**
19
+ * Start a new span.
20
+ */
21
+ startSpan(name, options) {
22
+ const parentSpan = options?.parent ?? (0, context_1.getCurrentSpan)();
23
+ const effectiveContext = options?.parentContext || parentSpan?.context;
24
+ let traceId;
25
+ let parentSpanId;
26
+ let traceFlags;
27
+ if (parentSpan) {
28
+ traceId = parentSpan.context.traceId;
29
+ parentSpanId = parentSpan.context.spanId;
30
+ traceFlags = parentSpan.context.traceFlags;
31
+ }
32
+ else if (effectiveContext && new span_context_1.SpanContext(effectiveContext.traceId, effectiveContext.spanId).isValid()) {
33
+ traceId = effectiveContext.traceId;
34
+ parentSpanId = effectiveContext.spanId;
35
+ traceFlags = effectiveContext.traceFlags;
36
+ }
37
+ else {
38
+ traceId = this.provider.generateTraceId();
39
+ parentSpanId = undefined;
40
+ const sampler = this.provider.getSampler();
41
+ const sampled = sampler ? sampler.shouldSample().sampled : true;
42
+ traceFlags = sampled ? 1 : 0;
43
+ // Debug override: force sampling for new root traces
44
+ const config = (0, runtime_config_1.getConfig)();
45
+ if (config.debug) {
46
+ traceFlags = 1;
47
+ }
48
+ }
49
+ const spanContext = new span_context_1.SpanContext(traceId, this.provider.generateSpanId(), traceFlags, effectiveContext?.traceState);
50
+ return new span_1.Span(name, this, spanContext, parentSpanId, options?.attributes);
51
+ }
52
+ /**
53
+ * Start an active span and run a function within it.
54
+ */
55
+ async startActiveSpan(name, fn, options) {
56
+ const span = this.startSpan(name, options);
57
+ try {
58
+ const result = await (0, context_1.runWithSpanAsync)(span, async () => {
59
+ return await Promise.resolve(fn(span));
60
+ });
61
+ return result;
62
+ }
63
+ catch (error) {
64
+ if (error instanceof Error) {
65
+ span.recordException(error);
66
+ }
67
+ throw error;
68
+ }
69
+ finally {
70
+ span.end();
71
+ }
72
+ }
73
+ }
74
+ exports.Tracer = Tracer;
75
+ //# sourceMappingURL=tracer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.js","sourceRoot":"","sources":["../../src/tracer/tracer.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAGH,iCAA8B;AAC9B,iDAA6C;AAC7C,gDAAsE;AAEtE,6DAAqD;AAErD;;GAEG;AACH,MAAa,MAAM;IAGjB,YAAY,QAAwB,EAAE,qBAA6B;QACjE,IAAI,CAAC,QAAQ,G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,CAAC,KAAK,EAAE,CAAC;gBACjB,UAAU,GAAG,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;QAED,MAAM,WAAW,GAAG,IAAI,0BAAW,CACjC,OAAO,EACP,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,EAC9B,UAAU,EACV,gBAAgB,EAAE,UAAU,CAC7B,CAAC;QAEF,OAAO,IAAI,WAAI,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;IAC9E,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,eAAe,CACnB,IAAY,EACZ,EAAmC,EACnC,OAGC;QAED,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAE3C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAA,0BAAgB,EAAC,IAAI,EAAE,KAAK,IAAI,EAAE;gBACrD,OAAO,MAAM,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;YACzC,CAAC,CAAC,CAAC;YACH,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;gBAC3B,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;YAC9B,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,GAAG,EAAE,CAAC;QACb,CAAC;IACH,CAAC;CACF;AArFD,wBAqFC"}
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Core types and interfaces for the tracing SDK.
3
+ */
4
+ /**
5
+ * Span status enumeration.
6
+ */
7
+ export declare enum SpanStatus {
8
+ UNSET = 0,
9
+ OK = 1,
10
+ ERROR = 2
11
+ }
12
+ /**
13
+ * Span context carries trace and span identification across process boundaries.
14
+ */
15
+ export interface ISpanContext {
16
+ traceId: string;
17
+ spanId: string;
18
+ traceFlags: number;
19
+ traceState?: string;
20
+ }
21
+ /**
22
+ * Span event for capturing events during span execution.
23
+ */
24
+ export interface SpanEvent {
25
+ name: string;
26
+ timestamp: number;
27
+ attributes?: Record<string, unknown>;
28
+ }
29
+ /**
30
+ * Span interface for creating and ending spans.
31
+ */
32
+ export interface ISpan {
33
+ context: ISpanContext;
34
+ name: string;
35
+ parentSpanId?: string;
36
+ attributes: Record<string, unknown>;
37
+ events: SpanEvent[];
38
+ status: SpanStatus;
39
+ statusDescription?: string;
40
+ startTimeNs: number;
41
+ endTimeNs?: number;
42
+ durationNs: number | undefined;
43
+ setAttribute(key: string, value: unknown): void;
44
+ addEvent(name: string, attributes?: Record<string, unknown>): void;
45
+ end(): void;
46
+ recordException(error: Error, attributes?: Record<string, unknown>): void;
47
+ isRecording(): boolean;
48
+ }
49
+ /**
50
+ * Tracer for creating and managing spans.
51
+ */
52
+ export interface ITracer {
53
+ startSpan(name: string, options?: {
54
+ attributes?: Record<string, unknown>;
55
+ parent?: ISpan | null;
56
+ parentContext?: ISpanContext | null;
57
+ }): ISpan;
58
+ startActiveSpan<T = unknown>(name: string, fn: (span: ISpan) => Promise<T> | T, options?: {
59
+ attributes?: Record<string, unknown>;
60
+ parent?: ISpan | null;
61
+ }): Promise<T> | T;
62
+ }
63
+ /**
64
+ * Span processor for handling spans at various lifecycle points.
65
+ */
66
+ export interface ISpanProcessor {
67
+ onStart?(span: ISpan): void;
68
+ onEnd(span: ISpan): void;
69
+ shutdown(): Promise<void> | void;
70
+ forceFlush(timeout?: number): Promise<void> | void;
71
+ }
72
+ /**
73
+ * Span exporter for sending spans to a backend.
74
+ */
75
+ export interface ISpanExporter {
76
+ export(spans: ISpan[]): Promise<boolean> | boolean;
77
+ shutdown(): Promise<void> | void;
78
+ }
79
+ /**
80
+ * Resource attributes describing the service.
81
+ */
82
+ export interface Resource {
83
+ [key: string]: string | number | boolean;
84
+ }
85
+ /**
86
+ * Tracer provider for managing tracers and processors.
87
+ */
88
+ export interface ITracerProvider {
89
+ getTracer(name: string, version?: string): ITracer;
90
+ addSpanProcessor(processor: ISpanProcessor): void;
91
+ removeSpanProcessor(processor: ISpanProcessor): void;
92
+ forceFlush(timeout?: number): Promise<void>;
93
+ shutdown(): Promise<void>;
94
+ }
95
+ /**
96
+ * Configuration for the SDK initialization.
97
+ */
98
+ export interface SDKConfig {
99
+ apiKey?: string;
100
+ endpoint?: string;
101
+ sampleRate?: number;
102
+ maxQueueSize?: number;
103
+ maxExportBatchSize?: number;
104
+ scheduleDelayMs?: number;
105
+ enableConsoleExporter?: boolean;
106
+ enableTokenCounting?: boolean;
107
+ enableCostTracking?: boolean;
108
+ enableSpanLogging?: boolean;
109
+ pricingOverride?: Record<string, Record<string, number>>;
110
+ sessionId?: string;
111
+ userId?: string;
112
+ tenantId?: string;
113
+ projectId?: string;
114
+ debug?: boolean;
115
+ attrTruncationLimit?: number;
116
+ loadEnv?: boolean;
117
+ autoInstrument?: boolean;
118
+ toolInclude?: string[];
119
+ maxToolSpans?: number;
120
+ maxSpanDepth?: number;
121
+ resource?: Resource;
122
+ }
123
+ /**
124
+ * Sampling result for span sampling decisions.
125
+ */
126
+ export interface SamplingResult {
127
+ sampled: boolean;
128
+ }
129
+ /**
130
+ * Sampler interface for making sampling decisions.
131
+ */
132
+ export interface ISampler {
133
+ shouldSample(): SamplingResult;
134
+ }
135
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,oBAAY,UAAU;IACpB,KAAK,IAAI;IACT,EAAE,IAAI;IACN,KAAK,IAAI;CACV;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC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,16 @@
1
+ "use strict";
2
+ /**
3
+ * Core types and interfaces for the tracing SDK.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SpanStatus = void 0;
7
+ /**
8
+ * Span status enumeration.
9
+ */
10
+ var SpanStatus;
11
+ (function (SpanStatus) {
12
+ SpanStatus[SpanStatus["UNSET"] = 0] = "UNSET";
13
+ SpanStatus[SpanStatus["OK"] = 1] = "OK";
14
+ SpanStatus[SpanStatus["ERROR"] = 2] = "ERROR";
15
+ })(SpanStatus || (exports.SpanStatus = SpanStatus = {}));
16
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH;;GAEG;AACH,IAAY,UAIX;AAJD,WAAY,UAAU;IACpB,6CAAS,CAAA;IACT,uCAAM,CAAA;IACN,6CAAS,CAAA;AACX,CAAC,EAJW,UAAU,0BAAV,UAAU,QAIrB"}
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@traccia2/sdk",
3
+ "version": "0.0.1",
4
+ "description": "Tracing SDK for agent applications",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./auto": {
14
+ "import": "./dist/auto.js",
15
+ "require": "./dist/auto.js",
16
+ "types": "./dist/auto.d.ts"
17
+ },
18
+ "./tracer": {
19
+ "import": "./dist/tracer/index.js",
20
+ "require": "./dist/tracer/index.js",
21
+ "types": "./dist/tracer/index.d.ts"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "src",
27
+ "LICENSE",
28
+ "README.md"
29
+ ],
30
+ "keywords": [
31
+ "tracing",
32
+ "observability",
33
+ "agent",
34
+ "instrumentation",
35
+ "opentelemetry"
36
+ ],
37
+ "author": "Traccia",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/traccia/sdk-ts"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/traccia/sdk-ts/issues"
45
+ },
46
+ "homepage": "https://github.com/traccia/sdk-ts#readme",
47
+ "engines": {
48
+ "node": ">=16.0.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc",
52
+ "build:watch": "tsc --watch",
53
+ "clean": "rm -rf dist",
54
+ "prebuild": "npm run clean",
55
+ "lint": "eslint src --ext .ts",
56
+ "lint:fix": "eslint src --ext .ts --fix",
57
+ "format": "prettier --write \"src/**/*.ts\"",
58
+ "test": "jest",
59
+ "test:watch": "jest --watch",
60
+ "test:cov": "jest --coverage",
61
+ "prepublishOnly": "npm run build && npm run test",
62
+ "prepare": "npm run build"
63
+ },
64
+ "devDependencies": {
65
+ "@types/jest": "^29.5.11",
66
+ "@types/node": "^20.10.6",
67
+ "@typescript-eslint/eslint-plugin": "^6.17.0",
68
+ "@typescript-eslint/parser": "^6.17.0",
69
+ "eslint": "^8.56.0",
70
+ "jest": "^29.7.0",
71
+ "prettier": "^3.1.1",
72
+ "ts-jest": "^29.1.1",
73
+ "typescript": "^5.3.3"
74
+ },
75
+ "publishConfig": {
76
+ "access": "public"
77
+ },
78
+ "dependencies": {}
79
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Tests for exporters.
3
+ */
4
+
5
+ import { HttpExporter } from '../exporter/http-exporter';
6
+ import { ConsoleExporter } from '../exporter/console-exporter';
7
+ import { TracerProvider } from '../tracer/provider';
8
+
9
+ describe('HttpExporter', () => {
10
+ it('should create with options', () => {
11
+ const exporter = new HttpExporter({
12
+ endpoint: 'https://example.com/traces',
13
+ apiKey: 'test-key',
14
+ });
15
+
16
+ expect(exporter).toBeDefined();
17
+ });
18
+
19
+ it.skip('should serialize spans', async () => {
20
+ const exporter = new HttpExporter({
21
+ endpoint: 'http://localhost:9999', // Non-existent endpoint
22
+ });
23
+ const provider = new TracerProvider();
24
+ const tracer = provider.getTracer('test');
25
+ const span = tracer.startSpan('test-span', {
26
+ attributes: { key: 'value' },
27
+ });
28
+
29
+ span.end();
30
+
31
+ const result = await exporter.export([span]);
32
+ // Result depends on endpoint availability
33
+ expect(typeof result).toBe('boolean');
34
+ });
35
+
36
+ it('should handle shutdown', async () => {
37
+ const exporter = new HttpExporter();
38
+ await expect(exporter.shutdown()).resolves.toBeUndefined();
39
+ });
40
+ });
41
+
42
+ describe('ConsoleExporter', () => {
43
+ it('should export spans to console', async () => {
44
+ const exporter = new ConsoleExporter();
45
+ const provider = new TracerProvider();
46
+ const tracer = provider.getTracer('test');
47
+ const span = tracer.startSpan('test-span');
48
+
49
+ span.end();
50
+
51
+ const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
52
+ await exporter.export([span]);
53
+
54
+ expect(consoleSpy).toHaveBeenCalled();
55
+ consoleSpy.mockRestore();
56
+ });
57
+
58
+ it('should handle shutdown', async () => {
59
+ const exporter = new ConsoleExporter();
60
+ await expect(exporter.shutdown()).resolves.toBeUndefined();
61
+ });
62
+ });