@glasstrace/sdk 0.1.0 → 0.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.
package/dist/index.d.cts CHANGED
@@ -217,6 +217,7 @@ declare class GlasstraceExporter implements SpanExporter {
217
217
  private readonly endpointUrl;
218
218
  private readonly createDelegateFn;
219
219
  private delegate;
220
+ private delegateKey;
220
221
  private pendingBatches;
221
222
  private pendingSpanCount;
222
223
  private overflowLogged;
@@ -232,22 +233,30 @@ declare class GlasstraceExporter implements SpanExporter {
232
233
  /**
233
234
  * Enriches a ReadableSpan with all glasstrace.* attributes.
234
235
  * Returns a new ReadableSpan wrapper; the original span is not mutated.
235
- * Each attribute derivation is wrapped in its own try-catch for partial
236
- * enrichment resilience.
236
+ *
237
+ * External function calls (getSessionId, deriveErrorCategory,
238
+ * deriveOrmProvider, classifyFetchTarget) are individually guarded so a
239
+ * failure in one does not prevent the remaining attributes from being set.
240
+ * On total failure, returns the original span unchanged.
237
241
  */
238
242
  private enrichSpan;
239
243
  /**
240
244
  * Lazily creates the delegate OTLP exporter once the API key is resolved.
245
+ * Recreates the delegate if the key has changed (e.g., after key rotation)
246
+ * so the Authorization header stays current.
241
247
  */
242
248
  private ensureDelegate;
243
249
  /**
244
- * Buffers enriched spans while the API key is pending.
250
+ * Buffers raw (unenriched) spans while the API key is pending.
245
251
  * Evicts oldest batches if the buffer exceeds MAX_PENDING_SPANS.
252
+ * Re-checks the key after buffering to close the race window where
253
+ * the key resolves between the caller's check and this buffer call.
246
254
  */
247
255
  private bufferSpans;
248
256
  /**
249
257
  * Flushes all buffered spans through the delegate exporter.
250
- * Called when the API key resolves.
258
+ * Enriches spans at flush time (not buffer time) so that session IDs
259
+ * are computed with the resolved API key instead of the "pending" sentinel.
251
260
  */
252
261
  private flushPending;
253
262
  }
@@ -325,6 +334,29 @@ declare function computeBuildHash(maps?: SourceMapEntry[]): Promise<string>;
325
334
  */
326
335
  declare function uploadSourceMaps(apiKey: string, endpoint: string, buildHash: string, maps: SourceMapEntry[]): Promise<SourceMapUploadResponse>;
327
336
 
337
+ /**
338
+ * Records an error as a span event on the currently active OTel span.
339
+ *
340
+ * Works regardless of the `consoleErrors` configuration — this is an
341
+ * explicit, opt-in API for manual error reporting. If no span is active
342
+ * or OTel is not available, the call is silently ignored.
343
+ *
344
+ * @param error - The error to capture. Accepts `Error` objects, strings, or any value.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * import { captureError } from "@glasstrace/sdk";
349
+ *
350
+ * try {
351
+ * await riskyOperation();
352
+ * } catch (err) {
353
+ * captureError(err);
354
+ * // handle error normally...
355
+ * }
356
+ * ```
357
+ */
358
+ declare function captureError(error: unknown): void;
359
+
328
360
  /**
329
361
  * Discovers test files by scanning the project directory for conventional
330
362
  * test file patterns. Also reads vitest/jest config files for custom include
@@ -354,4 +386,4 @@ declare function extractImports(fileContent: string): string[];
354
386
  */
355
387
  declare function buildImportGraph(projectRoot: string): Promise<ImportGraphPayload>;
356
388
 
357
- export { type FetchTarget, GlasstraceExporter, type GlasstraceExporterOptions, GlasstraceSpanProcessor, type ResolvedConfig, SdkError, SessionManager, type SourceMapEntry, buildImportGraph, classifyFetchTarget, collectSourceMaps, computeBuildHash, createDiscoveryHandler, deriveSessionId, discoverTestFiles, extractImports, getActiveConfig, getDateString, getDiscoveryHandler, getOrCreateAnonKey, getOrigin, isAnonymousMode, isProductionDisabled, loadCachedConfig, performInit, readAnonKey, readEnvVars, registerGlasstrace, resolveConfig, saveCachedConfig, sendInitRequest, uploadSourceMaps, withGlasstraceConfig };
389
+ export { type FetchTarget, GlasstraceExporter, type GlasstraceExporterOptions, GlasstraceSpanProcessor, type ResolvedConfig, SdkError, SessionManager, type SourceMapEntry, buildImportGraph, captureError, classifyFetchTarget, collectSourceMaps, computeBuildHash, createDiscoveryHandler, deriveSessionId, discoverTestFiles, extractImports, getActiveConfig, getDateString, getDiscoveryHandler, getOrCreateAnonKey, getOrigin, isAnonymousMode, isProductionDisabled, loadCachedConfig, performInit, readAnonKey, readEnvVars, registerGlasstrace, resolveConfig, saveCachedConfig, sendInitRequest, uploadSourceMaps, withGlasstraceConfig };
package/dist/index.d.ts CHANGED
@@ -217,6 +217,7 @@ declare class GlasstraceExporter implements SpanExporter {
217
217
  private readonly endpointUrl;
218
218
  private readonly createDelegateFn;
219
219
  private delegate;
220
+ private delegateKey;
220
221
  private pendingBatches;
221
222
  private pendingSpanCount;
222
223
  private overflowLogged;
@@ -232,22 +233,30 @@ declare class GlasstraceExporter implements SpanExporter {
232
233
  /**
233
234
  * Enriches a ReadableSpan with all glasstrace.* attributes.
234
235
  * Returns a new ReadableSpan wrapper; the original span is not mutated.
235
- * Each attribute derivation is wrapped in its own try-catch for partial
236
- * enrichment resilience.
236
+ *
237
+ * External function calls (getSessionId, deriveErrorCategory,
238
+ * deriveOrmProvider, classifyFetchTarget) are individually guarded so a
239
+ * failure in one does not prevent the remaining attributes from being set.
240
+ * On total failure, returns the original span unchanged.
237
241
  */
238
242
  private enrichSpan;
239
243
  /**
240
244
  * Lazily creates the delegate OTLP exporter once the API key is resolved.
245
+ * Recreates the delegate if the key has changed (e.g., after key rotation)
246
+ * so the Authorization header stays current.
241
247
  */
242
248
  private ensureDelegate;
243
249
  /**
244
- * Buffers enriched spans while the API key is pending.
250
+ * Buffers raw (unenriched) spans while the API key is pending.
245
251
  * Evicts oldest batches if the buffer exceeds MAX_PENDING_SPANS.
252
+ * Re-checks the key after buffering to close the race window where
253
+ * the key resolves between the caller's check and this buffer call.
246
254
  */
247
255
  private bufferSpans;
248
256
  /**
249
257
  * Flushes all buffered spans through the delegate exporter.
250
- * Called when the API key resolves.
258
+ * Enriches spans at flush time (not buffer time) so that session IDs
259
+ * are computed with the resolved API key instead of the "pending" sentinel.
251
260
  */
252
261
  private flushPending;
253
262
  }
@@ -325,6 +334,29 @@ declare function computeBuildHash(maps?: SourceMapEntry[]): Promise<string>;
325
334
  */
326
335
  declare function uploadSourceMaps(apiKey: string, endpoint: string, buildHash: string, maps: SourceMapEntry[]): Promise<SourceMapUploadResponse>;
327
336
 
337
+ /**
338
+ * Records an error as a span event on the currently active OTel span.
339
+ *
340
+ * Works regardless of the `consoleErrors` configuration — this is an
341
+ * explicit, opt-in API for manual error reporting. If no span is active
342
+ * or OTel is not available, the call is silently ignored.
343
+ *
344
+ * @param error - The error to capture. Accepts `Error` objects, strings, or any value.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * import { captureError } from "@glasstrace/sdk";
349
+ *
350
+ * try {
351
+ * await riskyOperation();
352
+ * } catch (err) {
353
+ * captureError(err);
354
+ * // handle error normally...
355
+ * }
356
+ * ```
357
+ */
358
+ declare function captureError(error: unknown): void;
359
+
328
360
  /**
329
361
  * Discovers test files by scanning the project directory for conventional
330
362
  * test file patterns. Also reads vitest/jest config files for custom include
@@ -354,4 +386,4 @@ declare function extractImports(fileContent: string): string[];
354
386
  */
355
387
  declare function buildImportGraph(projectRoot: string): Promise<ImportGraphPayload>;
356
388
 
357
- export { type FetchTarget, GlasstraceExporter, type GlasstraceExporterOptions, GlasstraceSpanProcessor, type ResolvedConfig, SdkError, SessionManager, type SourceMapEntry, buildImportGraph, classifyFetchTarget, collectSourceMaps, computeBuildHash, createDiscoveryHandler, deriveSessionId, discoverTestFiles, extractImports, getActiveConfig, getDateString, getDiscoveryHandler, getOrCreateAnonKey, getOrigin, isAnonymousMode, isProductionDisabled, loadCachedConfig, performInit, readAnonKey, readEnvVars, registerGlasstrace, resolveConfig, saveCachedConfig, sendInitRequest, uploadSourceMaps, withGlasstraceConfig };
389
+ export { type FetchTarget, GlasstraceExporter, type GlasstraceExporterOptions, GlasstraceSpanProcessor, type ResolvedConfig, SdkError, SessionManager, type SourceMapEntry, buildImportGraph, captureError, classifyFetchTarget, collectSourceMaps, computeBuildHash, createDiscoveryHandler, deriveSessionId, discoverTestFiles, extractImports, getActiveConfig, getDateString, getDiscoveryHandler, getOrCreateAnonKey, getOrigin, isAnonymousMode, isProductionDisabled, loadCachedConfig, performInit, readAnonKey, readEnvVars, registerGlasstrace, resolveConfig, saveCachedConfig, sendInitRequest, uploadSourceMaps, withGlasstraceConfig };