svoose 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -75,8 +75,15 @@ const cleanup = observe({
75
75
  batchSize: 10,
76
76
  flushInterval: 5000,
77
77
 
78
- // Sampling
79
- sampleRate: 0.1, // 10% of users
78
+ // Sampling (v0.1.3+)
79
+ sampling: 0.1, // 10% of all events
80
+ // or per-event-type (recommended)
81
+ sampling: {
82
+ vitals: 0.1, // 10% — sufficient for statistics
83
+ errors: 1.0, // 100% — all errors matter
84
+ custom: 0.5, // 50% of custom metrics
85
+ transitions: 0.0, // disabled
86
+ },
80
87
 
81
88
  // Debug
82
89
  debug: false,
@@ -86,6 +93,32 @@ const cleanup = observe({
86
93
  cleanup();
87
94
  ```
88
95
 
96
+ #### Sampling (v0.1.3+)
97
+
98
+ Control what percentage of events are sent to your backend:
99
+
100
+ ```typescript
101
+ // Simple: same rate for all events
102
+ observe({
103
+ endpoint: '/api/metrics',
104
+ sampling: 0.1, // 10% of all events
105
+ });
106
+
107
+ // Per-event-type: recommended for production
108
+ observe({
109
+ endpoint: '/api/metrics',
110
+ sampling: {
111
+ vitals: 0.1, // 10% — sufficient for accurate statistics
112
+ errors: 1.0, // 100% — capture all errors
113
+ custom: 0.5, // 50% of custom metrics
114
+ transitions: 0.0, // disabled — no state machine events
115
+ identify: 1.0, // 100% — always track user identification
116
+ },
117
+ });
118
+ ```
119
+
120
+ > **Note**: `sampleRate` is deprecated. Use `sampling` instead.
121
+
89
122
  ### `createMachine(config)`
90
123
 
91
124
  Create a state machine.
@@ -369,9 +402,15 @@ const machine = createMachine({
369
402
 
370
403
  ## Roadmap
371
404
 
372
- - **v0.2** — `invoke()`, `spawn()`, `after()` (delayed transitions)
373
- - **v0.3** — Parallel states, history states, devtools
374
- - **v0.4** — svelte-reactor integration, SvelteKit hooks
405
+ - **v0.1.3-v0.1.9** — Incremental features (sampling, sessions, custom metrics, retry, privacy)
406
+ - **v0.2.0** — Production-Ready Observability (network awareness, offline queue, user identification)
407
+ - **v0.3.0** — SvelteKit Integration (Vite plugin, hooks, route tracking)
408
+ - **v0.4.0** — Developer Experience (CLI, dashboard template)
409
+ - **v1.0.0** — Stable Release (Q1 2027)
410
+
411
+ > **Note**: FSM is a lightweight bonus feature, not an XState competitor. For complex state machines, use XState.
412
+
413
+ See [ROADMAP.md](./ROADMAP.md) for detailed plans.
375
414
 
376
415
  ## License
377
416
 
package/dist/index.d.ts CHANGED
@@ -5,6 +5,8 @@
5
5
  */
6
6
  export { observe } from './observe/index.js';
7
7
  export type { ObserveOptions } from './observe/index.js';
8
+ export { createSampler, eventTypeToSamplingType } from './observe/index.js';
9
+ export type { SamplingConfig, SamplingOption, SamplingEventType, Sampler } from './observe/index.js';
8
10
  export { observeErrors, registerMachineContext, unregisterMachineContext, } from './observe/index.js';
9
11
  export type { ObserveErrorEvent, ErrorEvent, UnhandledRejectionEvent, } from './observe/index.js';
10
12
  export { observeCLS, observeLCP, observeFID, observeINP, observeFCP, observeTTFB, vitalObservers, } from './observe/index.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAKzD,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,wBAAwB,GACzB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,iBAAiB,EACjB,UAAU,EACV,uBAAuB,GACxB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,EACL,UAAU,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,WAAW,EACX,cAAc,GACf,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA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E,aAAa,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAChE,YAAY,EACV,aAAa,EACb,OAAO,EACP,WAAW,EACX,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,YAAY,GACb,MAAM,oBAAoB,CAAC;AAK5B,YAAY,EACV,UAAU,EACV,eAAe,EACf,YAAY,GACb,MAAM,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{observe as t}from"./observe/index.js";import{observeErrors as n,registerMachineContext as s,unregisterMachineContext as i}from"./observe/index.js";import{observeCLS as v,observeLCP as a,observeFID as x,observeINP as b,observeFCP as c,observeTTFB as f,vitalObservers as m}from"./observe/index.js";import{createFetchTransport as d,createConsoleTransport as C}from"./transport/index.js";import{createMachine as j,createEvent as M}from"./machine/index.js";export{C as createConsoleTransport,M as createEvent,d as createFetchTransport,j as createMachine,t as observe,v as observeCLS,n as observeErrors,c as observeFCP,x as observeFID,b as observeINP,a as observeLCP,f as observeTTFB,s as registerMachineContext,i as unregisterMachineContext,m as vitalObservers};
1
+ import{observe as t}from"./observe/index.js";import{createSampler as n,eventTypeToSamplingType as s}from"./observe/index.js";import{observeErrors as i,registerMachineContext as a,unregisterMachineContext as v}from"./observe/index.js";import{observeCLS as m,observeLCP as f,observeFID as b,observeINP as c,observeFCP as E,observeTTFB as T,vitalObservers as y}from"./observe/index.js";import{createFetchTransport as g,createConsoleTransport as l}from"./transport/index.js";import{createMachine as j,createEvent as S}from"./machine/index.js";export{l as createConsoleTransport,S as createEvent,g as createFetchTransport,j as createMachine,n as createSampler,s as eventTypeToSamplingType,t as observe,m as observeCLS,i as observeErrors,E as observeFCP,b as observeFID,c as observeINP,f as observeLCP,T as observeTTFB,a as registerMachineContext,v as unregisterMachineContext,y as vitalObservers};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["/**\n * \uD83E\uDEBF svoose - Observability + State Machines for Svelte 5\n *\n * @packageDocumentation\n */\n\n// ============================================\n// Core Observability\n// ============================================\nexport { observe } from './observe/index.js';\nexport type { ObserveOptions } from './observe/index.js';\n\n// ============================================\n// Error Tracking\n// ============================================\nexport {\n observeErrors,\n registerMachineContext,\n unregisterMachineContext,\n} from './observe/index.js';\nexport type {\n ObserveErrorEvent,\n ErrorEvent,\n UnhandledRejectionEvent,\n} from './observe/index.js';\n\n// ============================================\n// Web Vitals\n// ============================================\nexport {\n observeCLS,\n observeLCP,\n observeFID,\n observeINP,\n observeFCP,\n observeTTFB,\n vitalObservers,\n} from './observe/index.js';\nexport type { Metric, MetricName, MetricRating } from './observe/index.js';\n\n// ============================================\n// Transport\n// ============================================\nexport { createFetchTransport, createConsoleTransport } from './transport/index.js';\nexport type { Transport, TransportOptions } from './transport/index.js';\n\n// ============================================\n// State Machines\n// ============================================\nexport { createMachine, createEvent } from './machine/index.js';\nexport type {\n MachineConfig,\n Machine,\n EventObject,\n StateNode,\n TransitionConfig,\n InferStates,\n InferEvents,\n InferContext,\n} from './machine/index.js';\n\n// ============================================\n// Shared Types\n// ============================================\nexport type {\n VitalEvent,\n TransitionEvent,\n ObserveEvent,\n} from './types/index.js';\n"],
5
- "mappings": "AASA,OAAS,WAAAA,MAAe,qBAMxB,OACE,iBAAAC,EACA,0BAAAC,EACA,4BAAAC,MACK,qBAUP,OACE,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,eAAAC,EACA,kBAAAC,MACK,qBAMP,OAAS,wBAAAC,EAAsB,0BAAAC,MAA8B,uBAM7D,OAAS,iBAAAC,EAAe,eAAAC,MAAmB",
6
- "names": ["observe", "observeErrors", "registerMachineContext", "unregisterMachineContext", "observeCLS", "observeLCP", "observeFID", "observeINP", "observeFCP", "observeTTFB", "vitalObservers", "createFetchTransport", "createConsoleTransport", "createMachine", "createEvent"]
4
+ "sourcesContent": ["/**\n * \uD83E\uDEBF svoose - Observability + State Machines for Svelte 5\n *\n * @packageDocumentation\n */\n\n// ============================================\n// Core Observability\n// ============================================\nexport { observe } from './observe/index.js';\nexport type { ObserveOptions } from './observe/index.js';\n\n// ============================================\n// Sampling\n// ============================================\nexport { createSampler, eventTypeToSamplingType } from './observe/index.js';\nexport type { SamplingConfig, SamplingOption, SamplingEventType, Sampler } from './observe/index.js';\n\n// ============================================\n// Error Tracking\n// ============================================\nexport {\n observeErrors,\n registerMachineContext,\n unregisterMachineContext,\n} from './observe/index.js';\nexport type {\n ObserveErrorEvent,\n ErrorEvent,\n UnhandledRejectionEvent,\n} from './observe/index.js';\n\n// ============================================\n// Web Vitals\n// ============================================\nexport {\n observeCLS,\n observeLCP,\n observeFID,\n observeINP,\n observeFCP,\n observeTTFB,\n vitalObservers,\n} from './observe/index.js';\nexport type { Metric, MetricName, MetricRating } from './observe/index.js';\n\n// ============================================\n// Transport\n// ============================================\nexport { createFetchTransport, createConsoleTransport } from './transport/index.js';\nexport type { Transport, TransportOptions } from './transport/index.js';\n\n// ============================================\n// State Machines\n// ============================================\nexport { createMachine, createEvent } from './machine/index.js';\nexport type {\n MachineConfig,\n Machine,\n EventObject,\n StateNode,\n TransitionConfig,\n InferStates,\n InferEvents,\n InferContext,\n} from './machine/index.js';\n\n// ============================================\n// Shared Types\n// ============================================\nexport type {\n VitalEvent,\n TransitionEvent,\n ObserveEvent,\n} from './types/index.js';\n"],
5
+ "mappings": "AASA,OAAS,WAAAA,MAAe,qBAMxB,OAAS,iBAAAC,EAAe,2BAAAC,MAA+B,qBAMvD,OACE,iBAAAC,EACA,0BAAAC,EACA,4BAAAC,MACK,qBAUP,OACE,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,cAAAC,EACA,eAAAC,EACA,kBAAAC,MACK,qBAMP,OAAS,wBAAAC,EAAsB,0BAAAC,MAA8B,uBAM7D,OAAS,iBAAAC,EAAe,eAAAC,MAAmB",
6
+ "names": ["observe", "createSampler", "eventTypeToSamplingType", "observeErrors", "registerMachineContext", "unregisterMachineContext", "observeCLS", "observeLCP", "observeFID", "observeINP", "observeFCP", "observeTTFB", "vitalObservers", "createFetchTransport", "createConsoleTransport", "createMachine", "createEvent"]
7
7
  }
@@ -7,4 +7,6 @@ export { observeErrors, registerMachineContext, unregisterMachineContext } from
7
7
  export type { ObserveErrorEvent, ErrorEvent, UnhandledRejectionEvent } from './errors.js';
8
8
  export { vitalObservers, observeCLS, observeLCP, observeFID, observeINP, observeFCP, observeTTFB } from './vitals.js';
9
9
  export type { Metric, MetricName, MetricRating } from './vitals.js';
10
+ export { createSampler, eventTypeToSamplingType } from './sampling.js';
11
+ export type { SamplingConfig, SamplingOption, SamplingEventType, Sampler } from './sampling.js';
10
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/observe/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACpF,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtH,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/observe/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACpF,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtH,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACvE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC"}
@@ -1,2 +1,2 @@
1
- import{observe as o,setGlobalObserver as t,getGlobalObserver as s}from"./observe.svelte.js";import{observeErrors as b,registerMachineContext as n,unregisterMachineContext as i}from"./errors.js";import{vitalObservers as a,observeCLS as l,observeLCP as x,observeFID as m,observeINP as c,observeFCP as f,observeTTFB as E}from"./vitals.js";export{s as getGlobalObserver,o as observe,l as observeCLS,b as observeErrors,f as observeFCP,m as observeFID,c as observeINP,x as observeLCP,E as observeTTFB,n as registerMachineContext,t as setGlobalObserver,i as unregisterMachineContext,a as vitalObservers};
1
+ import{observe as o,setGlobalObserver as t,getGlobalObserver as s}from"./observe.svelte.js";import{observeErrors as n,registerMachineContext as v,unregisterMachineContext as i}from"./errors.js";import{vitalObservers as b,observeCLS as m,observeLCP as l,observeFID as g,observeINP as x,observeFCP as f,observeTTFB as c}from"./vitals.js";import{createSampler as E,eventTypeToSamplingType as O}from"./sampling.js";export{E as createSampler,O as eventTypeToSamplingType,s as getGlobalObserver,o as observe,m as observeCLS,n as observeErrors,f as observeFCP,g as observeFID,x as observeINP,l as observeLCP,c as observeTTFB,v as registerMachineContext,t as setGlobalObserver,i as unregisterMachineContext,b as vitalObservers};
2
2
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/observe/index.ts"],
4
- "sourcesContent": ["/**\n * Observe module exports\n */\n\nexport { observe, setGlobalObserver, getGlobalObserver } from './observe.svelte.js';\nexport type { ObserveOptions } from './observe.svelte.js';\nexport { observeErrors, registerMachineContext, unregisterMachineContext } from './errors.js';\nexport type { ObserveErrorEvent, ErrorEvent, UnhandledRejectionEvent } from './errors.js';\nexport { vitalObservers, observeCLS, observeLCP, observeFID, observeINP, observeFCP, observeTTFB } from './vitals.js';\nexport type { Metric, MetricName, MetricRating } from './vitals.js';\n"],
5
- "mappings": "AAIA,OAAS,WAAAA,EAAS,qBAAAC,EAAmB,qBAAAC,MAAyB,sBAE9D,OAAS,iBAAAC,EAAe,0BAAAC,EAAwB,4BAAAC,MAAgC,cAEhF,OAAS,kBAAAC,EAAgB,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,eAAAC,MAAmB",
6
- "names": ["observe", "setGlobalObserver", "getGlobalObserver", "observeErrors", "registerMachineContext", "unregisterMachineContext", "vitalObservers", "observeCLS", "observeLCP", "observeFID", "observeINP", "observeFCP", "observeTTFB"]
4
+ "sourcesContent": ["/**\n * Observe module exports\n */\n\nexport { observe, setGlobalObserver, getGlobalObserver } from './observe.svelte.js';\nexport type { ObserveOptions } from './observe.svelte.js';\nexport { observeErrors, registerMachineContext, unregisterMachineContext } from './errors.js';\nexport type { ObserveErrorEvent, ErrorEvent, UnhandledRejectionEvent } from './errors.js';\nexport { vitalObservers, observeCLS, observeLCP, observeFID, observeINP, observeFCP, observeTTFB } from './vitals.js';\nexport type { Metric, MetricName, MetricRating } from './vitals.js';\nexport { createSampler, eventTypeToSamplingType } from './sampling.js';\nexport type { SamplingConfig, SamplingOption, SamplingEventType, Sampler } from './sampling.js';\n"],
5
+ "mappings": "AAIA,OAAS,WAAAA,EAAS,qBAAAC,EAAmB,qBAAAC,MAAyB,sBAE9D,OAAS,iBAAAC,EAAe,0BAAAC,EAAwB,4BAAAC,MAAgC,cAEhF,OAAS,kBAAAC,EAAgB,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,cAAAC,EAAY,eAAAC,MAAmB,cAExG,OAAS,iBAAAC,EAAe,2BAAAC,MAA+B",
6
+ "names": ["observe", "setGlobalObserver", "getGlobalObserver", "observeErrors", "registerMachineContext", "unregisterMachineContext", "vitalObservers", "observeCLS", "observeLCP", "observeFID", "observeINP", "observeFCP", "observeTTFB", "createSampler", "eventTypeToSamplingType"]
7
7
  }
@@ -1 +1 @@
1
- {"version":3,"file":"observe.svelte.d.ts","sourceRoot":"","sources":["../../src/observe/observe.svelte.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,KAAK,EAAE,cAAc,EAAc,YAAY,EAAa,MAAM,mBAAmB,CAAC;AAc7F,QAAA,IAAI,sBAAsB,EAAE,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,GAAG,IAAW,CAAC;AAE1E;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,sBAAsB,GAAG,IAAI,CAE/E;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,sBAAsB,CAEjE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,MAAM,IAAI,CAqIhE;AAED,YAAY,EAAE,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"observe.svelte.d.ts","sourceRoot":"","sources":["../../src/observe/observe.svelte.ts"],"names":[],"mappings":"AAAA;;GAEG;AAUH,OAAO,KAAK,EAAE,cAAc,EAAc,YAAY,EAAa,MAAM,mBAAmB,CAAC;AAc7F,QAAA,IAAI,sBAAsB,EAAE,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,GAAG,IAAW,CAAC;AAE1E;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,sBAAsB,GAAG,IAAI,CAE/E;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,sBAAsB,CAEjE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,MAAM,IAAI,CAkJhE;AAED,YAAY,EAAE,cAAc,EAAE,CAAC"}
@@ -1,2 +1,2 @@
1
- import{vitalObservers as h}from"./vitals.js";import{observeErrors as m}from"./errors.js";import{createFetchTransport as O}from"../transport/fetch.js";const f={endpoint:"/api/observe",vitals:!0,errors:!0,batchSize:10,flushInterval:5e3,sampleRate:1,debug:!1};let c=null;function u(i){c=i}function T(){return c}function I(i={}){if(Math.random()>(i.sampleRate??f.sampleRate))return()=>{};const t={...f,...i},p=t.transport??O(t.endpoint),r=[],o=[];let a=null;const d=()=>{try{return typeof location<"u"?location.href:""}catch{return""}},v=e=>{t.filter&&!t.filter(e)||(t.debug&&console.log("[svoose]",e),o.push(e),o.length>=t.batchSize&&s())},s=()=>{if(o.length===0)return;const e=o.splice(0,o.length),n=p.send(e);n&&typeof n.catch=="function"&&n.catch(l=>{t.debug&&console.error("[svoose] transport error:",l)})},b=e=>{const n={type:"vital",name:e.name,value:e.value,rating:e.rating,delta:e.delta,timestamp:e.timestamp,url:d()};v(n)};if(t.vitals){const e=t.vitals===!0?["CLS","LCP","FID","INP","FCP","TTFB"]:t.vitals;for(const n of e){const l=h[n];l&&r.push(l(b))}}if(t.errors&&r.push(m(e=>{v(e)})),u(v),r.push(()=>u(null)),a=setInterval(s,t.flushInterval),r.push(()=>{a&&clearInterval(a)}),typeof document<"u"){const e=()=>{document.visibilityState==="hidden"&&s()};document.addEventListener("visibilitychange",e),r.push(()=>{document.removeEventListener("visibilitychange",e)})}if(typeof window<"u"){const e=()=>{s()};window.addEventListener("beforeunload",e),r.push(()=>{window.removeEventListener("beforeunload",e)})}return()=>{s(),r.forEach(e=>e())}}export{T as getGlobalObserver,I as observe,u as setGlobalObserver};
1
+ import{vitalObservers as h}from"./vitals.js";import{observeErrors as y}from"./errors.js";import{createFetchTransport as g}from"../transport/fetch.js";import{createSampler as O,eventTypeToSamplingType as E}from"./sampling.js";const u={endpoint:"/api/observe",vitals:!0,errors:!0,batchSize:10,flushInterval:5e3,sampleRate:1,debug:!1};let c=null;function f(i){c=i}function C(){return c}function L(i={}){if(Math.random()>(i.sampleRate??u.sampleRate))return()=>{};const t={...u,...i},d=t.transport??g(t.endpoint),p=t.sampling!=null?O(t.sampling):null,n=[],o=[];let a=null;const b=()=>{try{return typeof location<"u"?location.href:""}catch{return""}},v=e=>{if(!(t.filter&&!t.filter(e))){if(p){const r=E(e.type);if(r&&!p.shouldSample(r))return}t.debug&&console.log("[svoose]",e),o.push(e),o.length>=t.batchSize&&s()}},s=()=>{if(o.length===0)return;const e=o.splice(0,o.length),r=d.send(e);r&&typeof r.catch=="function"&&r.catch(l=>{t.debug&&console.error("[svoose] transport error:",l)})},m=e=>{const r={type:"vital",name:e.name,value:e.value,rating:e.rating,delta:e.delta,timestamp:e.timestamp,url:b()};v(r)};if(t.vitals){const e=t.vitals===!0?["CLS","LCP","FID","INP","FCP","TTFB"]:t.vitals;for(const r of e){const l=h[r];l&&n.push(l(m))}}if(t.errors&&n.push(y(e=>{v(e)})),f(v),n.push(()=>f(null)),a=setInterval(s,t.flushInterval),n.push(()=>{a&&clearInterval(a)}),typeof document<"u"){const e=()=>{document.visibilityState==="hidden"&&s()};document.addEventListener("visibilitychange",e),n.push(()=>{document.removeEventListener("visibilitychange",e)})}if(typeof window<"u"){const e=()=>{s()};window.addEventListener("beforeunload",e),n.push(()=>{window.removeEventListener("beforeunload",e)})}return()=>{s(),n.forEach(e=>e())}}export{C as getGlobalObserver,L as observe,f as setGlobalObserver};
2
2
  //# sourceMappingURL=observe.svelte.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/observe/observe.svelte.ts"],
4
- "sourcesContent": ["/**\n * Main observe() function - combines vitals, errors, and transport\n */\n\nimport { vitalObservers, type Metric, type MetricName } from './vitals.js';\nimport { observeErrors, type ObserveErrorEvent } from './errors.js';\nimport { createFetchTransport } from '../transport/fetch.js';\nimport type { ObserveOptions, VitalEvent, ObserveEvent, Transport } from '../types/index.js';\n\n// Default configuration\nconst defaults = {\n endpoint: '/api/observe',\n vitals: true as const,\n errors: true,\n batchSize: 10,\n flushInterval: 5000,\n sampleRate: 1,\n debug: false,\n} satisfies Required<Omit<ObserveOptions, 'transport' | 'filter'>>;\n\n// Global observer callback for state machines\nlet globalObserverCallback: ((event: ObserveEvent) => void) | null = null;\n\n/**\n * Set global observer callback for state machines\n * Called internally to connect machines to observe()\n */\nexport function setGlobalObserver(callback: typeof globalObserverCallback): void {\n globalObserverCallback = callback;\n}\n\n/**\n * Get global observer callback\n * Used by createMachine to send transition events\n */\nexport function getGlobalObserver(): typeof globalObserverCallback {\n return globalObserverCallback;\n}\n\n/**\n * Main observe function - starts collecting metrics and errors\n *\n * @param options - Configuration options\n * @returns Cleanup function to stop observing\n *\n * @example\n * // Basic usage\n * observe();\n *\n * @example\n * // With options\n * observe({\n * endpoint: '/api/metrics',\n * vitals: ['CLS', 'LCP', 'INP'],\n * errors: true,\n * debug: true,\n * });\n */\nexport function observe(options: ObserveOptions = {}): () => void {\n // Check sampling rate\n if (Math.random() > (options.sampleRate ?? defaults.sampleRate)) {\n return () => {};\n }\n\n const config = { ...defaults, ...options };\n const transport: Transport = config.transport ?? createFetchTransport(config.endpoint);\n\n const cleanups: (() => void)[] = [];\n const buffer: ObserveEvent[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n\n // Get current URL\n const getUrl = (): string => {\n try {\n return typeof location !== 'undefined' ? location.href : '';\n } catch {\n return '';\n }\n };\n\n // Buffer an event and potentially flush\n const bufferEvent = (event: ObserveEvent): void => {\n // Apply filter if provided\n if (config.filter && !config.filter(event)) {\n return;\n }\n\n if (config.debug) {\n console.log('[svoose]', event);\n }\n\n buffer.push(event);\n\n if (buffer.length >= config.batchSize) {\n flush();\n }\n };\n\n // Send buffered events to transport\n const flush = (): void => {\n if (buffer.length === 0) return;\n\n const events = buffer.splice(0, buffer.length);\n // Handle both Promise and non-Promise returns from transport.send()\n const result = transport.send(events);\n if (result && typeof result.catch === 'function') {\n result.catch((err) => {\n if (config.debug) {\n console.error('[svoose] transport error:', err);\n }\n });\n }\n };\n\n // Convert metric to vital event\n const handleMetric = (metric: Metric): void => {\n const vitalEvent: VitalEvent = {\n type: 'vital',\n name: metric.name,\n value: metric.value,\n rating: metric.rating,\n delta: metric.delta,\n timestamp: metric.timestamp,\n url: getUrl(),\n };\n bufferEvent(vitalEvent);\n };\n\n // Setup vitals observers\n if (config.vitals) {\n const vitalsToObserve: MetricName[] =\n config.vitals === true\n ? ['CLS', 'LCP', 'FID', 'INP', 'FCP', 'TTFB']\n : config.vitals;\n\n for (const name of vitalsToObserve) {\n const observer = vitalObservers[name];\n if (observer) {\n cleanups.push(observer(handleMetric));\n }\n }\n }\n\n // Setup error observer\n if (config.errors) {\n cleanups.push(\n observeErrors((event: ObserveErrorEvent) => {\n bufferEvent(event);\n })\n );\n }\n\n // Setup global observer for state machines\n setGlobalObserver(bufferEvent);\n cleanups.push(() => setGlobalObserver(null));\n\n // Setup flush interval\n flushTimer = setInterval(flush, config.flushInterval);\n cleanups.push(() => {\n if (flushTimer) clearInterval(flushTimer);\n });\n\n // Flush on page visibility change (user navigating away)\n if (typeof document !== 'undefined') {\n const visibilityHandler = (): void => {\n if (document.visibilityState === 'hidden') {\n flush();\n }\n };\n document.addEventListener('visibilitychange', visibilityHandler);\n cleanups.push(() => {\n document.removeEventListener('visibilitychange', visibilityHandler);\n });\n }\n\n // Flush on beforeunload\n if (typeof window !== 'undefined') {\n const unloadHandler = (): void => {\n flush();\n };\n window.addEventListener('beforeunload', unloadHandler);\n cleanups.push(() => {\n window.removeEventListener('beforeunload', unloadHandler);\n });\n }\n\n // Return cleanup function\n return () => {\n flush();\n cleanups.forEach((fn) => fn());\n };\n}\n\nexport type { ObserveOptions };\n"],
5
- "mappings": "AAIA,OAAS,kBAAAA,MAAoD,cAC7D,OAAS,iBAAAC,MAA6C,cACtD,OAAS,wBAAAC,MAA4B,wBAIrC,MAAMC,EAAW,CACf,SAAU,eACV,OAAQ,GACR,OAAQ,GACR,UAAW,GACX,cAAe,IACf,WAAY,EACZ,MAAO,EACT,EAGA,IAAIC,EAAiE,KAM9D,SAASC,EAAkBC,EAA+C,CAC/EF,EAAyBE,CAC3B,CAMO,SAASC,GAAmD,CACjE,OAAOH,CACT,CAqBO,SAASI,EAAQC,EAA0B,CAAC,EAAe,CAEhE,GAAI,KAAK,OAAO,GAAKA,EAAQ,YAAcN,EAAS,YAClD,MAAO,IAAM,CAAC,EAGhB,MAAMO,EAAS,CAAE,GAAGP,EAAU,GAAGM,CAAQ,EACnCE,EAAuBD,EAAO,WAAaR,EAAqBQ,EAAO,QAAQ,EAE/EE,EAA2B,CAAC,EAC5BC,EAAyB,CAAC,EAChC,IAAIC,EAAoD,KAGxD,MAAMC,EAAS,IAAc,CAC3B,GAAI,CACF,OAAO,OAAO,SAAa,IAAc,SAAS,KAAO,EAC3D,MAAQ,CACN,MAAO,EACT,CACF,EAGMC,EAAeC,GAA8B,CAE7CP,EAAO,QAAU,CAACA,EAAO,OAAOO,CAAK,IAIrCP,EAAO,OACT,QAAQ,IAAI,WAAYO,CAAK,EAG/BJ,EAAO,KAAKI,CAAK,EAEbJ,EAAO,QAAUH,EAAO,WAC1BQ,EAAM,EAEV,EAGMA,EAAQ,IAAY,CACxB,GAAIL,EAAO,SAAW,EAAG,OAEzB,MAAMM,EAASN,EAAO,OAAO,EAAGA,EAAO,MAAM,EAEvCO,EAAST,EAAU,KAAKQ,CAAM,EAChCC,GAAU,OAA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ACXX,EAAM,EACNN,EAAS,QAASkB,GAAOA,EAAG,CAAC,CAC/B,CACF",
6
- "names": ["vitalObservers", "observeErrors", "createFetchTransport", "defaults", "globalObserverCallback", "setGlobalObserver", "callback", "getGlobalObserver", "observe", "options", "config", "transport", "cleanups", "buffer", "flushTimer", "getUrl", "bufferEvent", "event", "flush", "events", "result", "err", "handleMetric", "metric", "vitalEvent", "vitalsToObserve", "name", "observer", "visibilityHandler", "unloadHandler", "fn"]
4
+ "sourcesContent": ["/**\n * Main observe() function - combines vitals, errors, and transport\n */\n\nimport { vitalObservers, type Metric, type MetricName } from './vitals.js';\nimport { observeErrors, type ObserveErrorEvent } from './errors.js';\nimport { createFetchTransport } from '../transport/fetch.js';\nimport {\n createSampler,\n eventTypeToSamplingType,\n type Sampler,\n} from './sampling.js';\nimport type { ObserveOptions, VitalEvent, ObserveEvent, Transport } from '../types/index.js';\n\n// Default configuration\nconst defaults = {\n endpoint: '/api/observe',\n vitals: true as const,\n errors: true,\n batchSize: 10,\n flushInterval: 5000,\n sampleRate: 1,\n debug: false,\n} satisfies Required<Omit<ObserveOptions, 'transport' | 'filter' | 'sampling'>>;\n\n// Global observer callback for state machines\nlet globalObserverCallback: ((event: ObserveEvent) => void) | null = null;\n\n/**\n * Set global observer callback for state machines\n * Called internally to connect machines to observe()\n */\nexport function setGlobalObserver(callback: typeof globalObserverCallback): void {\n globalObserverCallback = callback;\n}\n\n/**\n * Get global observer callback\n * Used by createMachine to send transition events\n */\nexport function getGlobalObserver(): typeof globalObserverCallback {\n return globalObserverCallback;\n}\n\n/**\n * Main observe function - starts collecting metrics and errors\n *\n * @param options - Configuration options\n * @returns Cleanup function to stop observing\n *\n * @example\n * // Basic usage\n * observe();\n *\n * @example\n * // With options\n * observe({\n * endpoint: '/api/metrics',\n * vitals: ['CLS', 'LCP', 'INP'],\n * errors: true,\n * debug: true,\n * });\n */\nexport function observe(options: ObserveOptions = {}): () => void {\n // Legacy sampleRate support (deprecated) - skip entire observer\n if (Math.random() > (options.sampleRate ?? defaults.sampleRate)) {\n return () => {};\n }\n\n const config = { ...defaults, ...options };\n const transport: Transport = config.transport ?? createFetchTransport(config.endpoint);\n\n // Create sampler if sampling option is provided\n const sampler: Sampler | null = config.sampling != null\n ? createSampler(config.sampling)\n : null;\n\n const cleanups: (() => void)[] = [];\n const buffer: ObserveEvent[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n\n // Get current URL\n const getUrl = (): string => {\n try {\n return typeof location !== 'undefined' ? location.href : '';\n } catch {\n return '';\n }\n };\n\n // Buffer an event and potentially flush\n const bufferEvent = (event: ObserveEvent): void => {\n // Apply filter if provided\n if (config.filter && !config.filter(event)) {\n return;\n }\n\n // Apply per-event-type sampling\n if (sampler) {\n const samplingType = eventTypeToSamplingType(event.type);\n if (samplingType && !sampler.shouldSample(samplingType)) {\n return;\n }\n }\n\n if (config.debug) {\n console.log('[svoose]', event);\n }\n\n buffer.push(event);\n\n if (buffer.length >= config.batchSize) {\n flush();\n }\n };\n\n // Send buffered events to transport\n const flush = (): void => {\n if (buffer.length === 0) return;\n\n const events = buffer.splice(0, buffer.length);\n // Handle both Promise and non-Promise returns from transport.send()\n const result = transport.send(events);\n if (result && typeof result.catch === 'function') {\n result.catch((err) => {\n if (config.debug) {\n console.error('[svoose] transport error:', err);\n }\n });\n }\n };\n\n // Convert metric to vital event\n const handleMetric = (metric: Metric): void => {\n const vitalEvent: VitalEvent = {\n type: 'vital',\n name: metric.name,\n value: metric.value,\n rating: metric.rating,\n delta: metric.delta,\n timestamp: metric.timestamp,\n url: getUrl(),\n };\n bufferEvent(vitalEvent);\n };\n\n // Setup vitals observers\n if (config.vitals) {\n const vitalsToObserve: MetricName[] =\n config.vitals === true\n ? ['CLS', 'LCP', 'FID', 'INP', 'FCP', 'TTFB']\n : config.vitals;\n\n for (const name of vitalsToObserve) {\n const observer = vitalObservers[name];\n if (observer) {\n cleanups.push(observer(handleMetric));\n }\n }\n }\n\n // Setup error observer\n if (config.errors) {\n cleanups.push(\n observeErrors((event: ObserveErrorEvent) => {\n bufferEvent(event);\n })\n );\n }\n\n // Setup global observer for state machines\n setGlobalObserver(bufferEvent);\n cleanups.push(() => setGlobalObserver(null));\n\n // Setup flush interval\n flushTimer = setInterval(flush, config.flushInterval);\n cleanups.push(() => {\n if (flushTimer) clearInterval(flushTimer);\n });\n\n // Flush on page visibility change (user navigating away)\n if (typeof document !== 'undefined') {\n const visibilityHandler = (): void => {\n if (document.visibilityState === 'hidden') {\n flush();\n }\n };\n document.addEventListener('visibilitychange', visibilityHandler);\n cleanups.push(() => {\n document.removeEventListener('visibilitychange', visibilityHandler);\n });\n }\n\n // Flush on beforeunload\n if (typeof window !== 'undefined') {\n const unloadHandler = (): void => {\n flush();\n };\n window.addEventListener('beforeunload', unloadHandler);\n cleanups.push(() => {\n window.removeEventListener('beforeunload', unloadHandler);\n });\n }\n\n // Return cleanup function\n return () => {\n flush();\n cleanups.forEach((fn) => fn());\n };\n}\n\nexport type { ObserveOptions };\n"],
5
+ "mappings": "AAIA,OAAS,kBAAAA,MAAoD,cAC7D,OAAS,iBAAAC,MAA6C,cACtD,OAAS,wBAAAC,MAA4B,wBACrC,OACE,iBAAAC,EACA,2BAAAC,MAEK,gBAIP,MAAMC,EAAW,CACf,SAAU,eACV,OAAQ,GACR,OAAQ,GACR,UAAW,GACX,cAAe,IACf,WAAY,EACZ,MAAO,EACT,EAGA,IAAIC,EAAiE,KAM9D,SAASC,EAAkBC,EAA+C,CAC/EF,EAAyBE,CAC3B,CAMO,SAASC,GAAmD,CACjE,OAAOH,CACT,CAqBO,SAASI,EAAQC,EAA0B,CAAC,EAAe,CAEhE,GAAI,KAAK,OAAO,GAAKA,EAAQ,YAAcN,EAAS,YAClD,MAAO,IAAM,CAAC,EAGhB,MAAMO,EAAS,CAAE,GAAGP,EAAU,GAAGM,CAAQ,EACnCE,EAAuBD,EAAO,WAAaV,EAAqBU,EAAO,QAAQ,EAG/EE,EAA0BF,EAAO,UAAY,KAC/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AExC,CACF,CAsBA,GAnBId,EAAO,QACTG,EAAS,KACPd,EAAemB,GAA6B,CAC1CD,EAAYC,CAAK,CACnB,CAAC,CACH,EAIFb,EAAkBY,CAAW,EAC7BJ,EAAS,KAAK,IAAMR,EAAkB,IAAI,CAAC,EAG3CU,EAAa,YAAYK,EAAOV,EAAO,aAAa,EACpDG,EAAS,KAAK,IAAM,CACdE,GAAY,cAAcA,CAAU,CAC1C,CAAC,EAGG,OAAO,SAAa,IAAa,CACnC,MAAMe,EAAoB,IAAY,CAChC,SAAS,kBAAoB,UAC/BV,EAAM,CAEV,EACA,SAAS,iBAAiB,mBAAoBU,CAAiB,EAC/DjB,EAAS,KAAK,IAAM,CAClB,SAAS,oBAAoB,mBAAoBiB,CAAiB,CACpE,CAAC,CACH,CAGA,GAAI,OAAO,OAAW,IAAa,CACjC,MAAMC,EAAgB,IAAY,CAChCX,EAAM,CACR,EACA,OAAO,iBAAiB,eAAgBW,CAAa,EACrDlB,EAAS,KAAK,IAAM,CAClB,OAAO,oBAAoB,eAAgBkB,CAAa,CAC1D,CAAC,CACH,CAGA,MAAO,IAAM,CACXX,EAAM,EACNP,EAAS,QAASmB,GAAOA,EAAG,CAAC,CAC/B,CACF",
6
+ "names": ["vitalObservers", "observeErrors", "createFetchTransport", "createSampler", "eventTypeToSamplingType", "defaults", "globalObserverCallback", "setGlobalObserver", "callback", "getGlobalObserver", "observe", "options", "config", "transport", "sampler", "cleanups", "buffer", "flushTimer", "getUrl", "bufferEvent", "event", "samplingType", "flush", "events", "result", "err", "handleMetric", "metric", "vitalEvent", "vitalsToObserve", "name", "observer", "visibilityHandler", "unloadHandler", "fn"]
7
7
  }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Sampling utilities for rate limiting events by type
3
+ */
4
+ /**
5
+ * Per-event-type sampling rates
6
+ * Each rate is a number between 0 and 1 (0 = disabled, 1 = all)
7
+ */
8
+ export interface SamplingConfig {
9
+ /** Sampling rate for Web Vitals events (default: 1) */
10
+ vitals?: number;
11
+ /** Sampling rate for error events (default: 1) */
12
+ errors?: number;
13
+ /** Sampling rate for custom metric events (default: 1) */
14
+ custom?: number;
15
+ /** Sampling rate for state machine transition events (default: 1) */
16
+ transitions?: number;
17
+ /** Sampling rate for identify events (default: 1) */
18
+ identify?: number;
19
+ }
20
+ /**
21
+ * Sampling option - either a single rate for all events or per-type config
22
+ */
23
+ export type SamplingOption = number | SamplingConfig;
24
+ /**
25
+ * Event types that can be sampled
26
+ */
27
+ export type SamplingEventType = keyof SamplingConfig;
28
+ /**
29
+ * Sampler interface returned by createSampler
30
+ */
31
+ export interface Sampler {
32
+ /**
33
+ * Check if an event should be sampled (included)
34
+ * @param eventType - The type of event
35
+ * @returns true if the event should be included, false if dropped
36
+ */
37
+ shouldSample(eventType: SamplingEventType): boolean;
38
+ /**
39
+ * Get the sampling rate for an event type
40
+ * @param eventType - The type of event
41
+ * @returns The rate (0-1)
42
+ */
43
+ getRate(eventType: SamplingEventType): number;
44
+ }
45
+ /**
46
+ * Create a sampler instance for filtering events by sampling rate
47
+ *
48
+ * @param config - Sampling configuration (number or per-type config)
49
+ * @returns Sampler instance
50
+ *
51
+ * @example
52
+ * // Simple - same rate for all events
53
+ * const sampler = createSampler(0.1); // 10% of all events
54
+ *
55
+ * @example
56
+ * // Per-event-type rates
57
+ * const sampler = createSampler({
58
+ * vitals: 0.1, // 10% of vitals
59
+ * errors: 1.0, // 100% of errors
60
+ * custom: 0.5, // 50% of custom metrics
61
+ * transitions: 0.0, // disabled
62
+ * });
63
+ */
64
+ export declare function createSampler(config: SamplingOption): Sampler;
65
+ /**
66
+ * Map ObserveEvent.type to SamplingEventType
67
+ */
68
+ export declare function eventTypeToSamplingType(eventType: string): SamplingEventType | null;
69
+ //# sourceMappingURL=sampling.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sampling.d.ts","sourceRoot":"","sources":["../../src/observe/sampling.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0DAA0D;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qEAAqE;IACrE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,cAAc,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,cAAc,CAAC;AAMrD;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB;;;;OAIG;IACH,YAAY,CAAC,SAAS,EAAE,iBAAiB,GAAG,OAAO,CAAC;IAEpD;;;;OAIG;IACH,OAAO,CAAC,SAAS,EAAE,iBAAiB,GAAG,MAAM,CAAC;CAC/C;AAWD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAmC7D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,SAAS,EAAE,MAAM,GAChB,iBAAiB,GAAG,IAAI,CAgB1B"}
@@ -55,6 +55,26 @@ export interface TransportOptions {
55
55
  headers?: Record<string, string>;
56
56
  onError?: (error: Error) => void;
57
57
  }
58
+ /**
59
+ * Per-event-type sampling rates
60
+ * Each rate is a number between 0 and 1 (0 = disabled, 1 = all)
61
+ */
62
+ export interface SamplingConfig {
63
+ /** Sampling rate for Web Vitals events (default: 1) */
64
+ vitals?: number;
65
+ /** Sampling rate for error events (default: 1) */
66
+ errors?: number;
67
+ /** Sampling rate for custom metric events (default: 1) */
68
+ custom?: number;
69
+ /** Sampling rate for state machine transition events (default: 1) */
70
+ transitions?: number;
71
+ /** Sampling rate for identify events (default: 1) */
72
+ identify?: number;
73
+ }
74
+ /**
75
+ * Sampling option - either a single rate for all events or per-type config
76
+ */
77
+ export type SamplingOption = number | SamplingConfig;
58
78
  export interface ObserveOptions {
59
79
  /** URL endpoint for sending data */
60
80
  endpoint?: string;
@@ -70,7 +90,27 @@ export interface ObserveOptions {
70
90
  flushInterval?: number;
71
91
  /** Filter function for events */
72
92
  filter?: (event: ObserveEvent) => boolean;
73
- /** Sampling rate (0-1) */
93
+ /**
94
+ * Per-event-type sampling rates
95
+ *
96
+ * @example
97
+ * // Simple - same rate for all events
98
+ * sampling: 0.1 // 10% of all events
99
+ *
100
+ * @example
101
+ * // Per-event-type rates (recommended)
102
+ * sampling: {
103
+ * vitals: 0.1, // 10% - sufficient for statistics
104
+ * errors: 1.0, // 100% - all errors matter
105
+ * custom: 0.5, // 50%
106
+ * transitions: 0.0, // disabled
107
+ * }
108
+ */
109
+ sampling?: SamplingOption;
110
+ /**
111
+ * @deprecated Use `sampling` instead. Will be removed in v0.3.0.
112
+ * Global sampling rate (0-1) - applies to entire observer
113
+ */
74
114
  sampleRate?: number;
75
115
  /** Log to console */
76
116
  debug?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;AAExE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,mBAAmB,GAAG,MAAM,CAAC;AAEjE,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,YAAY,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAMD,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,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
+ "version": "0.1.3",
4
4
  "description": "Observability + State Machines for Svelte 5 — the goose that sees everything",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",