@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.
- package/LICENSE +23 -0
- package/README.md +503 -0
- package/dist/auto.d.ts +27 -0
- package/dist/auto.d.ts.map +1 -0
- package/dist/auto.js +171 -0
- package/dist/auto.js.map +1 -0
- package/dist/config/env-config.d.ts +21 -0
- package/dist/config/env-config.d.ts.map +1 -0
- package/dist/config/env-config.js +111 -0
- package/dist/config/env-config.js.map +1 -0
- package/dist/config/pricing-config.d.ts +27 -0
- package/dist/config/pricing-config.d.ts.map +1 -0
- package/dist/config/pricing-config.js +74 -0
- package/dist/config/pricing-config.js.map +1 -0
- package/dist/config/runtime-config.d.ts +65 -0
- package/dist/config/runtime-config.d.ts.map +1 -0
- package/dist/config/runtime-config.js +97 -0
- package/dist/config/runtime-config.js.map +1 -0
- package/dist/context/context.d.ts +29 -0
- package/dist/context/context.d.ts.map +1 -0
- package/dist/context/context.js +48 -0
- package/dist/context/context.js.map +1 -0
- package/dist/exporter/console-exporter.d.ts +18 -0
- package/dist/exporter/console-exporter.d.ts.map +1 -0
- package/dist/exporter/console-exporter.js +39 -0
- package/dist/exporter/console-exporter.js.map +1 -0
- package/dist/exporter/http-exporter.d.ts +57 -0
- package/dist/exporter/http-exporter.d.ts.map +1 -0
- package/dist/exporter/http-exporter.js +181 -0
- package/dist/exporter/http-exporter.js.map +1 -0
- package/dist/exporter/index.d.ts +7 -0
- package/dist/exporter/index.d.ts.map +1 -0
- package/dist/exporter/index.js +12 -0
- package/dist/exporter/index.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/index.d.ts +9 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +16 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/langchain-callback.d.ts +72 -0
- package/dist/integrations/langchain-callback.d.ts.map +1 -0
- package/dist/integrations/langchain-callback.js +201 -0
- package/dist/integrations/langchain-callback.js.map +1 -0
- package/dist/integrations/langgraph-instrumentation.d.ts +57 -0
- package/dist/integrations/langgraph-instrumentation.d.ts.map +1 -0
- package/dist/integrations/langgraph-instrumentation.js +162 -0
- package/dist/integrations/langgraph-instrumentation.js.map +1 -0
- package/dist/processor/batch-processor.d.ts +68 -0
- package/dist/processor/batch-processor.d.ts.map +1 -0
- package/dist/processor/batch-processor.js +150 -0
- package/dist/processor/batch-processor.js.map +1 -0
- package/dist/processor/cost-processor.d.ts +16 -0
- package/dist/processor/cost-processor.d.ts.map +1 -0
- package/dist/processor/cost-processor.js +50 -0
- package/dist/processor/cost-processor.js.map +1 -0
- package/dist/processor/index.d.ts +9 -0
- package/dist/processor/index.d.ts.map +1 -0
- package/dist/processor/index.js +18 -0
- package/dist/processor/index.js.map +1 -0
- package/dist/processor/logging-processor.d.ts +13 -0
- package/dist/processor/logging-processor.d.ts.map +1 -0
- package/dist/processor/logging-processor.js +26 -0
- package/dist/processor/logging-processor.js.map +1 -0
- package/dist/processor/sampler.d.ts +20 -0
- package/dist/processor/sampler.d.ts.map +1 -0
- package/dist/processor/sampler.js +33 -0
- package/dist/processor/sampler.js.map +1 -0
- package/dist/processor/token-counter.d.ts +13 -0
- package/dist/processor/token-counter.d.ts.map +1 -0
- package/dist/processor/token-counter.js +40 -0
- package/dist/processor/token-counter.js.map +1 -0
- package/dist/tracer/index.d.ts +8 -0
- package/dist/tracer/index.d.ts.map +1 -0
- package/dist/tracer/index.js +15 -0
- package/dist/tracer/index.js.map +1 -0
- package/dist/tracer/provider.d.ts +59 -0
- package/dist/tracer/provider.d.ts.map +1 -0
- package/dist/tracer/provider.js +114 -0
- package/dist/tracer/provider.js.map +1 -0
- package/dist/tracer/span-context.d.ts +23 -0
- package/dist/tracer/span-context.d.ts.map +1 -0
- package/dist/tracer/span-context.js +34 -0
- package/dist/tracer/span-context.js.map +1 -0
- package/dist/tracer/span.d.ts +49 -0
- package/dist/tracer/span.d.ts.map +1 -0
- package/dist/tracer/span.js +118 -0
- package/dist/tracer/span.js.map +1 -0
- package/dist/tracer/tracer.d.ts +28 -0
- package/dist/tracer/tracer.d.ts.map +1 -0
- package/dist/tracer/tracer.js +75 -0
- package/dist/tracer/tracer.js.map +1 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/package.json +79 -0
- package/src/__tests__/exporter.test.ts +62 -0
- package/src/__tests__/integrations-langchain.test.ts +384 -0
- package/src/__tests__/integrations-langgraph.test.ts +479 -0
- package/src/__tests__/processor.test.ts +89 -0
- package/src/__tests__/span.test.ts +103 -0
- package/src/__tests__/tracer.test.ts +89 -0
- package/src/auto.ts +198 -0
- package/src/config/env-config.ts +93 -0
- package/src/config/pricing-config.ts +84 -0
- package/src/config/runtime-config.ts +108 -0
- package/src/context/context.ts +52 -0
- package/src/exporter/console-exporter.ts +38 -0
- package/src/exporter/http-exporter.ts +188 -0
- package/src/exporter/index.ts +7 -0
- package/src/index.ts +51 -0
- package/src/integrations/README.md +287 -0
- package/src/integrations/index.ts +13 -0
- package/src/integrations/langchain-callback.ts +229 -0
- package/src/integrations/langgraph-instrumentation.ts +174 -0
- package/src/processor/batch-processor.ts +180 -0
- package/src/processor/cost-processor.ts +57 -0
- package/src/processor/index.ts +9 -0
- package/src/processor/logging-processor.ts +26 -0
- package/src/processor/sampler.ts +35 -0
- package/src/processor/token-counter.ts +42 -0
- package/src/tracer/index.ts +8 -0
- package/src/tracer/provider.ts +130 -0
- package/src/tracer/span-context.ts +46 -0
- package/src/tracer/span.ts +145 -0
- package/src/tracer/tracer.ts +100 -0
- package/src/types.ts +155 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# License
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024 Traccia
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
# Traccia SDK for TypeScript
|
|
2
|
+
|
|
3
|
+
Production-grade distributed tracing SDK for agent applications. Built with TypeScript and designed for high performance, reliability, and ease of integration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Distributed Tracing**: Create, propagate, and manage spans across service boundaries
|
|
8
|
+
- **Automatic Instrumentation**: Optional auto-instrumentation for popular LLM libraries (OpenAI, Anthropic)
|
|
9
|
+
- **Span Processors**: Built-in processors for sampling, batching, token counting, and cost analysis
|
|
10
|
+
- **Token & Cost Tracking**: Automatic token counting and cost calculation for LLM calls
|
|
11
|
+
- **Flexible Exporters**: Export spans via HTTP, console, or custom exporters
|
|
12
|
+
- **Context Management**: Automatic context propagation using AsyncLocalStorage
|
|
13
|
+
- **Production Ready**: Thread-safe, performant, and battle-tested patterns
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @traccia/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### Basic Setup
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { startTracing, getTracer } from '@traccia/sdk';
|
|
27
|
+
|
|
28
|
+
// Initialize tracing
|
|
29
|
+
await startTracing({
|
|
30
|
+
apiKey: 'your-api-key',
|
|
31
|
+
endpoint: 'https://api.example.com/traces',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Get a tracer
|
|
35
|
+
const tracer = getTracer('my-app');
|
|
36
|
+
|
|
37
|
+
// Create a span
|
|
38
|
+
const span = tracer.startSpan('process-request', {
|
|
39
|
+
attributes: {
|
|
40
|
+
userId: '123',
|
|
41
|
+
action: 'process',
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
// Do work
|
|
47
|
+
span.setAttribute('status', 'success');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
span.recordException(error as Error);
|
|
50
|
+
} finally {
|
|
51
|
+
span.end();
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Using Active Spans
|
|
56
|
+
|
|
57
|
+
For better context management, use active spans which automatically handle parent-child relationships:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const tracer = getTracer('my-app');
|
|
61
|
+
|
|
62
|
+
await tracer.startActiveSpan('api-call', async (span) => {
|
|
63
|
+
span.setAttribute('method', 'POST');
|
|
64
|
+
|
|
65
|
+
// Child spans automatically use this span as parent
|
|
66
|
+
const childSpan = tracer.startSpan('database-query');
|
|
67
|
+
// ... do work ...
|
|
68
|
+
childSpan.end();
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Configuration
|
|
73
|
+
|
|
74
|
+
Pass a `SDKConfig` object to `startTracing()`:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
interface SDKConfig {
|
|
78
|
+
// API Configuration
|
|
79
|
+
apiKey?: string; // API key for authentication
|
|
80
|
+
endpoint?: string; // Backend endpoint URL
|
|
81
|
+
|
|
82
|
+
// Sampling
|
|
83
|
+
sampleRate?: number; // Span sampling rate (0.0 to 1.0)
|
|
84
|
+
|
|
85
|
+
// Queue Management
|
|
86
|
+
maxQueueSize?: number; // Max pending spans (default: 5000)
|
|
87
|
+
maxExportBatchSize?: number; // Max spans per export (default: 512)
|
|
88
|
+
scheduleDelayMs?: number; // Flush interval (default: 5000)
|
|
89
|
+
|
|
90
|
+
// Features
|
|
91
|
+
enableConsoleExporter?: boolean; // Also log to console
|
|
92
|
+
enableTokenCounting?: boolean; // Auto-count tokens (default: true)
|
|
93
|
+
enableCostTracking?: boolean; // Calculate costs (default: true)
|
|
94
|
+
enableSpanLogging?: boolean; // Log spans to console
|
|
95
|
+
|
|
96
|
+
// Context
|
|
97
|
+
sessionId?: string; // Session identifier
|
|
98
|
+
userId?: string; // User identifier
|
|
99
|
+
tenantId?: string; // Tenant identifier
|
|
100
|
+
projectId?: string; // Project identifier
|
|
101
|
+
|
|
102
|
+
// Instrumentation
|
|
103
|
+
autoInstrument?: boolean; // Enable auto-instrumentation
|
|
104
|
+
toolInclude?: string[]; // Which tools to instrument
|
|
105
|
+
maxToolSpans?: number; // Max tool spans (default: 100)
|
|
106
|
+
maxSpanDepth?: number; // Max depth (default: 10)
|
|
107
|
+
|
|
108
|
+
// Debugging
|
|
109
|
+
debug?: boolean; // Force sampling all traces
|
|
110
|
+
attrTruncationLimit?: number; // Max attribute size
|
|
111
|
+
|
|
112
|
+
// Environment
|
|
113
|
+
loadEnv?: boolean; // Load .env file
|
|
114
|
+
pricingOverride?: PricingTable; // Custom pricing
|
|
115
|
+
|
|
116
|
+
resource?: Resource; // Service resource info
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Environment Variables
|
|
121
|
+
|
|
122
|
+
Configure via environment variables (useful for deployment):
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
# Authentication
|
|
126
|
+
AGENT_DASHBOARD_API_KEY=your-key
|
|
127
|
+
|
|
128
|
+
# Backend
|
|
129
|
+
AGENT_DASHBOARD_ENDPOINT=https://api.example.com/traces
|
|
130
|
+
|
|
131
|
+
# Sampling
|
|
132
|
+
AGENT_DASHBOARD_SAMPLE_RATE=0.1
|
|
133
|
+
|
|
134
|
+
# Pricing (JSON string)
|
|
135
|
+
AGENT_DASHBOARD_PRICING_JSON='{"gpt-4":{"inputCost":0.03,"outputCost":0.06}}'
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### .env File
|
|
139
|
+
|
|
140
|
+
Create a `.env` file in your project root:
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
AGENT_DASHBOARD_API_KEY=your-api-key
|
|
144
|
+
AGENT_DASHBOARD_ENDPOINT=https://api.example.com/traces
|
|
145
|
+
AGENT_DASHBOARD_SAMPLE_RATE=1.0
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## API Reference
|
|
149
|
+
|
|
150
|
+
### Tracer
|
|
151
|
+
|
|
152
|
+
#### `startSpan(name, options?): ISpan`
|
|
153
|
+
|
|
154
|
+
Create a new span.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const span = tracer.startSpan('operation', {
|
|
158
|
+
attributes: {
|
|
159
|
+
userId: '123',
|
|
160
|
+
action: 'create',
|
|
161
|
+
},
|
|
162
|
+
parent: parentSpan, // Optional: explicit parent
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### `startActiveSpan(name, fn, options?): Promise<any>`
|
|
167
|
+
|
|
168
|
+
Create an active span and run a function within it. Automatically handles parent-child relationships.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const result = await tracer.startActiveSpan(
|
|
172
|
+
'api-call',
|
|
173
|
+
async (span) => {
|
|
174
|
+
span.setAttribute('endpoint', '/users');
|
|
175
|
+
// Nested spans automatically use this as parent
|
|
176
|
+
return await fetchUser('123');
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
attributes: { timeout: 5000 },
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Span
|
|
185
|
+
|
|
186
|
+
#### `setAttribute(key, value): void`
|
|
187
|
+
|
|
188
|
+
Set an attribute on the span.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
span.setAttribute('userId', '123');
|
|
192
|
+
span.setAttribute('items', 5);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### `addEvent(name, attributes?): void`
|
|
196
|
+
|
|
197
|
+
Add an event to the span.
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
span.addEvent('cache-hit', {
|
|
201
|
+
cacheKey: 'user:123',
|
|
202
|
+
ttl: 3600,
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### `recordException(error, attributes?): void`
|
|
207
|
+
|
|
208
|
+
Record an exception on the span.
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
try {
|
|
212
|
+
// do work
|
|
213
|
+
} catch (error) {
|
|
214
|
+
span.recordException(error as Error, {
|
|
215
|
+
errorType: 'NetworkError',
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
#### `end(): void`
|
|
221
|
+
|
|
222
|
+
End the span and mark it for export.
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
span.end();
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
#### Properties
|
|
229
|
+
|
|
230
|
+
- `context: ISpanContext` - Span context (trace ID, span ID, flags)
|
|
231
|
+
- `name: string` - Span name
|
|
232
|
+
- `attributes: Record<string, unknown>` - Span attributes
|
|
233
|
+
- `events: SpanEvent[]` - Recorded events
|
|
234
|
+
- `status: SpanStatus` - Span status (UNSET, OK, ERROR)
|
|
235
|
+
- `durationNs: number | undefined` - Duration in nanoseconds
|
|
236
|
+
- `isRecording(): boolean` - Whether span is still open
|
|
237
|
+
|
|
238
|
+
### Processors
|
|
239
|
+
|
|
240
|
+
The SDK includes several built-in processors:
|
|
241
|
+
|
|
242
|
+
#### TokenCountingProcessor
|
|
243
|
+
|
|
244
|
+
Automatically estimates token counts from text attributes:
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
const tracer = getTracer('app');
|
|
248
|
+
const span = tracer.startSpan('llm-call', {
|
|
249
|
+
attributes: {
|
|
250
|
+
prompt: 'Hello, how are you?',
|
|
251
|
+
completion: 'I am doing well, thank you!',
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// After processing:
|
|
256
|
+
span.attributes.input_tokens; // Auto-populated
|
|
257
|
+
span.attributes.output_tokens; // Auto-populated
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
#### CostAnnotatingProcessor
|
|
261
|
+
|
|
262
|
+
Calculates costs based on token counts and pricing:
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
span.setAttribute('model', 'gpt-4');
|
|
266
|
+
span.setAttribute('input_tokens', 100);
|
|
267
|
+
span.setAttribute('output_tokens', 50);
|
|
268
|
+
|
|
269
|
+
// After processing:
|
|
270
|
+
span.attributes.cost_usd; // 0.003 (example)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### LoggingSpanProcessor
|
|
274
|
+
|
|
275
|
+
Logs completed spans to console (for debugging).
|
|
276
|
+
|
|
277
|
+
#### BatchSpanProcessor
|
|
278
|
+
|
|
279
|
+
Batches spans and exports them efficiently (enabled by default).
|
|
280
|
+
|
|
281
|
+
### Global Functions
|
|
282
|
+
|
|
283
|
+
#### `getTracer(name, version?): ITracer`
|
|
284
|
+
|
|
285
|
+
Get a tracer instance.
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
const tracer = getTracer('my-service', '1.0.0');
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
#### `getTracerProvider(): ITracerProvider`
|
|
292
|
+
|
|
293
|
+
Get the global tracer provider.
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
const provider = getTracerProvider();
|
|
297
|
+
provider.forceFlush();
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### `setTracerProvider(provider): void`
|
|
301
|
+
|
|
302
|
+
Override the global tracer provider (useful for testing).
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
const testProvider = new TracerProvider();
|
|
306
|
+
setTracerProvider(testProvider);
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### `startTracing(config): Promise<ITracerProvider>`
|
|
310
|
+
|
|
311
|
+
Initialize the SDK with configuration.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
await startTracing({
|
|
315
|
+
apiKey: 'key',
|
|
316
|
+
sampleRate: 0.1,
|
|
317
|
+
});
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
#### `stopTracing(): Promise<void>`
|
|
321
|
+
|
|
322
|
+
Gracefully shutdown the SDK.
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
await stopTracing();
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
## Advanced Usage
|
|
329
|
+
|
|
330
|
+
### Custom Exporters
|
|
331
|
+
|
|
332
|
+
Implement the `ISpanExporter` interface:
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
import { ISpanExporter, ISpan } from '@traccia/sdk';
|
|
336
|
+
|
|
337
|
+
class MyExporter implements ISpanExporter {
|
|
338
|
+
async export(spans: ISpan[]): Promise<boolean> {
|
|
339
|
+
// Send spans somewhere
|
|
340
|
+
console.log(`Exporting ${spans.length} spans`);
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async shutdown(): Promise<void> {
|
|
345
|
+
// Cleanup
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Use it
|
|
350
|
+
const provider = getTracerProvider();
|
|
351
|
+
provider.addSpanProcessor(new BatchSpanProcessor({
|
|
352
|
+
exporter: new MyExporter(),
|
|
353
|
+
}));
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Custom Processors
|
|
357
|
+
|
|
358
|
+
Implement the `ISpanProcessor` interface:
|
|
359
|
+
|
|
360
|
+
```typescript
|
|
361
|
+
import { ISpanProcessor, ISpan } from '@traccia/sdk';
|
|
362
|
+
|
|
363
|
+
class CustomProcessor implements ISpanProcessor {
|
|
364
|
+
onEnd(span: ISpan): void {
|
|
365
|
+
// Process span
|
|
366
|
+
if (span.name.startsWith('error')) {
|
|
367
|
+
// Handle errors
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async shutdown(): Promise<void> {}
|
|
372
|
+
async forceFlush(): Promise<void> {}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Use it
|
|
376
|
+
const provider = getTracerProvider();
|
|
377
|
+
provider.addSpanProcessor(new CustomProcessor());
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Span Context Propagation
|
|
381
|
+
|
|
382
|
+
Spans are automatically propagated through `AsyncLocalStorage`:
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
const tracer = getTracer('app');
|
|
386
|
+
|
|
387
|
+
await tracer.startActiveSpan('parent', async () => {
|
|
388
|
+
// Nested span automatically gets parent as context
|
|
389
|
+
const childSpan = tracer.startSpan('child');
|
|
390
|
+
// childSpan.parentSpanId === parent.spanId
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Force Flush
|
|
395
|
+
|
|
396
|
+
Flush pending spans before shutdown:
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
const provider = getTracerProvider();
|
|
400
|
+
await provider.forceFlush(5000); // 5 second timeout
|
|
401
|
+
await provider.shutdown();
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## Performance Considerations
|
|
405
|
+
|
|
406
|
+
### Sampling
|
|
407
|
+
|
|
408
|
+
Use sampling to reduce span volume:
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
await startTracing({
|
|
412
|
+
sampleRate: 0.1, // Keep 10% of traces
|
|
413
|
+
});
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Batch Export
|
|
417
|
+
|
|
418
|
+
Configure batching to optimize network usage:
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
await startTracing({
|
|
422
|
+
maxExportBatchSize: 1024, // Export up to 1024 spans
|
|
423
|
+
scheduleDelayMs: 10000, // Flush every 10 seconds
|
|
424
|
+
maxQueueSize: 10000, // Queue up to 10k spans
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Attribute Management
|
|
429
|
+
|
|
430
|
+
Keep attributes minimal and use truncation:
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
await startTracing({
|
|
434
|
+
attrTruncationLimit: 256, // Max 256 chars per attribute
|
|
435
|
+
});
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
## Testing
|
|
439
|
+
|
|
440
|
+
The SDK includes comprehensive examples and tests:
|
|
441
|
+
|
|
442
|
+
```typescript
|
|
443
|
+
import { TracerProvider } from '@traccia/sdk';
|
|
444
|
+
|
|
445
|
+
describe('My Integration', () => {
|
|
446
|
+
let provider: TracerProvider;
|
|
447
|
+
let tracer: ITracer;
|
|
448
|
+
|
|
449
|
+
beforeEach(() => {
|
|
450
|
+
provider = new TracerProvider();
|
|
451
|
+
tracer = provider.getTracer('test');
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it('should create spans', () => {
|
|
455
|
+
const span = tracer.startSpan('test');
|
|
456
|
+
span.setAttribute('test', true);
|
|
457
|
+
span.end();
|
|
458
|
+
|
|
459
|
+
expect(span.endTimeNs).toBeDefined();
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
## Error Handling
|
|
465
|
+
|
|
466
|
+
The SDK is designed to be resilient:
|
|
467
|
+
|
|
468
|
+
- **Processor errors** are silently caught to prevent cascading failures
|
|
469
|
+
- **Export errors** trigger automatic retry with exponential backoff
|
|
470
|
+
- **Configuration errors** are logged but don't prevent tracing
|
|
471
|
+
|
|
472
|
+
For critical errors, enable debug mode:
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
await startTracing({
|
|
476
|
+
debug: true,
|
|
477
|
+
enableSpanLogging: true,
|
|
478
|
+
});
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
## Monitoring
|
|
482
|
+
|
|
483
|
+
Monitor SDK health:
|
|
484
|
+
|
|
485
|
+
```typescript
|
|
486
|
+
const provider = getTracerProvider();
|
|
487
|
+
|
|
488
|
+
// Force flush to ensure all spans are sent
|
|
489
|
+
await provider.forceFlush(30000); // 30 second timeout
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
## Contributing
|
|
493
|
+
|
|
494
|
+
Contributions are welcome! Please ensure:
|
|
495
|
+
|
|
496
|
+
- Code passes linting: `npm run lint:fix`
|
|
497
|
+
- Code is formatted: `npm run format`
|
|
498
|
+
- Tests pass: `npm test`
|
|
499
|
+
- Coverage is maintained
|
|
500
|
+
|
|
501
|
+
## License
|
|
502
|
+
|
|
503
|
+
MIT
|
package/dist/auto.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK initialization and global tracer management.
|
|
3
|
+
*/
|
|
4
|
+
import { TracerProvider } from './tracer/provider';
|
|
5
|
+
import { SDKConfig, ITracer } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Get the global tracer provider.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getTracerProvider(): TracerProvider;
|
|
10
|
+
/**
|
|
11
|
+
* Set the global tracer provider.
|
|
12
|
+
*/
|
|
13
|
+
export declare function setTracerProvider(provider: TracerProvider): void;
|
|
14
|
+
/**
|
|
15
|
+
* Get a tracer from the global provider.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getTracer(name: string, version?: string): ITracer;
|
|
18
|
+
/**
|
|
19
|
+
* Initialize tracing with automatic setup.
|
|
20
|
+
*/
|
|
21
|
+
export declare function startTracing(config?: SDKConfig): Promise<TracerProvider>;
|
|
22
|
+
/**
|
|
23
|
+
* Stop tracing and shutdown all processors.
|
|
24
|
+
*/
|
|
25
|
+
export declare function stopTracing(): Promise<void>;
|
|
26
|
+
export { getTracerProvider as initSDK };
|
|
27
|
+
//# sourceMappingURL=auto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto.d.ts","sourceRoot":"","sources":["../src/auto.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAiBnD,OAAO,EAAE,SAAS,EAAiB,OAAO,EAAE,MAAM,SAAS,CAAC;AAK5D;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,CAKlD;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAEhE;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAEjE;AAED;;GAEG;AACH,wBAAsB,YAAY,CAAC,MAAM,GAAE,SAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CA2FlF;AAED;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAQjD;AAwCD,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,CAAC"}
|