@fallom/trace 0.2.26 → 0.2.28

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.mjs CHANGED
@@ -23,7 +23,7 @@ import {
23
23
  isCustomMetric,
24
24
  runGEval,
25
25
  uploadResultsPublic
26
- } from "./chunk-FTZVXPQN.mjs";
26
+ } from "./chunk-MSI4HGK6.mjs";
27
27
  import {
28
28
  __export
29
29
  } from "./chunk-7P6ASYW6.mjs";
@@ -32,9 +32,11 @@ import {
32
32
  var trace_exports = {};
33
33
  __export(trace_exports, {
34
34
  FallomSession: () => FallomSession,
35
+ FallomSpan: () => FallomSpan,
35
36
  init: () => init3,
36
37
  session: () => session,
37
- shutdown: () => shutdown
38
+ shutdown: () => shutdown,
39
+ wrapTraced: () => wrapTraced
38
40
  });
39
41
 
40
42
  // src/trace/core.ts
@@ -820,6 +822,109 @@ function generateHexId(length) {
820
822
  return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
821
823
  }
822
824
 
825
+ // src/trace/span.ts
826
+ var FallomSpan = class {
827
+ constructor(name, ctx, options = {}) {
828
+ this.name = name;
829
+ this.ctx = ctx;
830
+ this.attrs = {};
831
+ this.ended = false;
832
+ this._status = "OK";
833
+ this.spanId = generateHexId(16);
834
+ this.traceId = options.traceId || generateHexId(32);
835
+ this.parentSpanId = options.parentSpanId;
836
+ this.kind = options.kind || "custom";
837
+ this.startTime = Date.now();
838
+ }
839
+ /**
840
+ * Set attributes on the span.
841
+ * Can be called multiple times - attributes are merged.
842
+ */
843
+ set(attributes) {
844
+ if (this.ended) {
845
+ console.warn("[Fallom] Cannot set attributes on ended span");
846
+ return this;
847
+ }
848
+ Object.assign(this.attrs, attributes);
849
+ return this;
850
+ }
851
+ /**
852
+ * Mark the span as errored.
853
+ */
854
+ setError(error) {
855
+ this._status = "ERROR";
856
+ this._errorMessage = error instanceof Error ? error.message : error;
857
+ return this;
858
+ }
859
+ /**
860
+ * Get span context for creating child spans.
861
+ */
862
+ context() {
863
+ return {
864
+ traceId: this.traceId,
865
+ spanId: this.spanId
866
+ };
867
+ }
868
+ /**
869
+ * End the span and send it.
870
+ * Must be called for the span to be recorded.
871
+ */
872
+ end() {
873
+ if (this.ended) {
874
+ console.warn("[Fallom] Span already ended");
875
+ return;
876
+ }
877
+ this.ended = true;
878
+ if (!isInitialized()) {
879
+ return;
880
+ }
881
+ const endTime = Date.now();
882
+ sendTrace({
883
+ config_key: this.ctx.configKey,
884
+ session_id: this.ctx.sessionId,
885
+ customer_id: this.ctx.customerId,
886
+ metadata: this.ctx.metadata,
887
+ tags: this.ctx.tags,
888
+ trace_id: this.traceId,
889
+ span_id: this.spanId,
890
+ parent_span_id: this.parentSpanId,
891
+ name: this.name,
892
+ kind: this.kind,
893
+ start_time: new Date(this.startTime).toISOString(),
894
+ end_time: new Date(endTime).toISOString(),
895
+ duration_ms: endTime - this.startTime,
896
+ status: this._status,
897
+ error_message: this._errorMessage,
898
+ attributes: {
899
+ "fallom.sdk_version": "2",
900
+ "fallom.span_type": "manual",
901
+ ...this.attrs
902
+ }
903
+ }).catch(() => {
904
+ });
905
+ }
906
+ };
907
+ function wrapTraced(session2, name, fn, options = {}) {
908
+ return (async (...args) => {
909
+ const span = session2.span(name, options);
910
+ if (args.length === 1) {
911
+ span.set({ input: args[0] });
912
+ } else if (args.length > 1) {
913
+ span.set({ input: args });
914
+ }
915
+ try {
916
+ const result = await fn(...args);
917
+ span.set({ output: result });
918
+ span.end();
919
+ return result;
920
+ } catch (error) {
921
+ span.setError(error instanceof Error ? error : String(error));
922
+ span.end();
923
+ throw error;
924
+ }
925
+ });
926
+ }
927
+
823
928
  // src/prompts.ts
824
929
  var prompts_exports = {};
825
930
  __export(prompts_exports, {
@@ -2689,6 +2794,26 @@ var FallomSession = class {
2689
2794
  getContext() {
2690
2795
  return { ...this.ctx };
2691
2796
  }
2797
+ /**
2798
+ * Create a manual span for custom operations.
2799
+ *
2800
+ * Use for non-LLM operations like RAG retrieval, preprocessing, tool execution, etc.
2801
+ * The span uses the session's context (configKey, sessionId, etc.).
2802
+ *
2803
+ * @example
2804
+ * ```typescript
2805
+ * const span = session.span("rag.retrieve");
2806
+ * span.set({ "rag.query": userQuery, "rag.topK": 5 });
2807
+ *
2808
+ * const docs = await retrieveDocuments(userQuery);
2809
+ * span.set({ "rag.documents.count": docs.length });
2810
+ *
2811
+ * span.end(); // Must call to send the span
2812
+ * ```
2813
+ */
2814
+ span(name, options) {
2815
+ return new FallomSpan(name, this.ctx, options);
2816
+ }
2692
2817
  /**
2693
2818
  * Get model assignment for this session (A/B testing).
2694
2819
  */
@@ -3168,6 +3293,7 @@ var index_default = {
3168
3293
  export {
3169
3294
  FallomExporter,
3170
3295
  FallomSession,
3296
+ FallomSpan,
3171
3297
  buildGEvalPrompt,
3172
3298
  calculateAggregateScores,
3173
3299
  clearMastraPrompt,
@@ -3181,5 +3307,6 @@ export {
3181
3307
  session,
3182
3308
  setMastraPrompt,
3183
3309
  setMastraPromptAB,
3184
- trace_exports as trace
3310
+ trace_exports as trace,
3311
+ wrapTraced
3185
3312
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fallom/trace",
3
- "version": "0.2.26",
3
+ "version": "0.2.28",
4
4
  "description": "Model A/B testing and tracing for LLM applications. Zero latency, production-ready.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -40,7 +40,7 @@
40
40
  "license": "MIT",
41
41
  "repository": {
42
42
  "type": "git",
43
- "url": "https://github.com/fallom/fallom-js"
43
+ "url": "https://github.com/Fallomai/fallom-typescript-sdk"
44
44
  },
45
45
  "dependencies": {
46
46
  "@opentelemetry/api": "^1.7.0",