@theholocron/observability 0.7.0-alpha.3 → 0.7.0-alpha.5

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
@@ -24,13 +24,18 @@ See the [documentation](https://docs.theholocron.dev/holocron/observability/) fo
24
24
 
25
25
  Add the peers for the subpaths you use — they are all optional:
26
26
 
27
- | Subpath | Peers |
28
- | -------------------------------------- | -------------------------------------- |
29
- | `@theholocron/observability/core` | none |
30
- | `@theholocron/observability/logger` | `pino`, `pino-pretty`, `@axiomhq/pino` |
31
- | `@theholocron/observability/errors` | `@sentry/node` |
32
- | `@theholocron/observability/analytics` | `posthog-node` |
33
- | `@theholocron/observability/testing` | `vitest` |
27
+ | Subpath | Peers |
28
+ | -------------------------------------------- | -------------------------------------------------------------------- |
29
+ | `@theholocron/observability/core` | none |
30
+ | `@theholocron/observability/logger` | `pino`, `pino-pretty`, `@axiomhq/pino` |
31
+ | `@theholocron/observability/errors` | `@sentry/node` |
32
+ | `@theholocron/observability/analytics` | `posthog-node` |
33
+ | `@theholocron/observability/browser/sentry` | `@sentry/browser` |
34
+ | `@theholocron/observability/browser/posthog` | `posthog-js` |
35
+ | `@theholocron/observability/edge/sentry` | `@sentry/vercel-edge` |
36
+ | `@theholocron/observability/edge/posthog` | none (`fetch`) |
37
+ | `@theholocron/observability/react-native` | `@sentry/react-native`, `posthog-react-native` (injected by the app) |
38
+ | `@theholocron/observability/testing` | `vitest` |
34
39
 
35
40
  ## Examples
36
41
 
@@ -66,9 +71,44 @@ errors.init({
66
71
  deploy({ logger, errors });
67
72
  ```
68
73
 
69
- For the browser, an edge runtime, or React Native, import
70
- `@theholocron/observability/core` (zero dependencies) and use `ConsoleLogger`
71
- from `/logger` in place of Pino.
74
+ ### In a browser, edge or React Native app
75
+
76
+ Import `@theholocron/observability/core` (zero dependencies) and use
77
+ `ConsoleLogger` from `/logger` in place of Pino. Then pick the adapters for the
78
+ runtime. None of these entry points imports a Node built-in.
79
+
80
+ ```typescript
81
+ // Browser or React app
82
+ import { PostHogBrowserSink } from "@theholocron/observability/browser/posthog";
83
+ import { SentryBrowserSink } from "@theholocron/observability/browser/sentry";
84
+
85
+ const errors = new SentryBrowserSink();
86
+ errors.init({ dsn, release: "web@1.0.0", environment: "production", tags: {} });
87
+ const analytics = new PostHogBrowserSink({ key, host: "https://us.i.posthog.com" });
88
+
89
+ // Edge runtime or worker: Sentry's edge SDK, and PostHog over fetch (no SDK)
90
+ import { PostHogFetchSink } from "@theholocron/observability/edge/posthog";
91
+ import { SentryEdgeSink } from "@theholocron/observability/edge/sentry";
92
+
93
+ const edgeAnalytics = new PostHogFetchSink({ key, host, waitUntil: (p) => ctx.waitUntil(p) });
94
+
95
+ // React Native: the app imports the SDKs (they need the react-native runtime) and passes them in
96
+ import * as Sentry from "@sentry/react-native";
97
+ import PostHog from "posthog-react-native";
98
+ import { PostHogReactNativeSink, SentryReactNativeSink } from "@theholocron/observability/react-native";
99
+
100
+ const rnErrors = new SentryReactNativeSink({ sdk: Sentry });
101
+ const rnAnalytics = new PostHogReactNativeSink({ client: new PostHog(key, { host }) });
102
+ ```
103
+
104
+ Each adapter is its own subpath, so importing one never resolves another's SDK.
105
+ Every adapter takes its SDK, client or `fetch` as an option, so tests pass a
106
+ fake, scrubs token-shaped strings from what it sends, and never throws into the
107
+ app when the SDK does (pass `onError` to see those errors). The browser PostHog
108
+ sink captures nothing by itself (autocapture and pageviews are off) and
109
+ identifies the person you name before it records for them. An empty DSN or key
110
+ is the caller's signal to install `NoopErrorSink` / `NoopAnalyticsSink`
111
+ instead. See the docs site for the full rules.
72
112
 
73
113
  Testing code that depends on `Logger` / `ErrorSink` / `AnalyticsSink`? Use the
74
114
  spy doubles from `/testing` instead of hand-rolling one per repo:
@@ -1,3 +1,12 @@
1
- import { t as PostHogSink } from "./posthog-sink-D4aRF3P2.mjs";
2
- import { i as NoopAnalyticsSink, t as AnalyticsSink } from "./sinks-v2TCQsCp.mjs";
1
+ /**
2
+ * `@theholocron/observability/analytics` — product analytics (usage, adoption,
3
+ * retention).
4
+ *
5
+ * `PostHogSink` is the Node adapter for the {@link AnalyticsSink} interface
6
+ * (from `/core`). `posthog-node` is an optional peer dependency; install it
7
+ * alongside this subpath. The caller resolves the project key + host and
8
+ * passes them to the constructor — this module reads no environment.
9
+ */
10
+ import { t as PostHogSink } from "./posthog-sink-CmQmma-o.mjs";
11
+ import { i as NoopAnalyticsSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
3
12
  export { type AnalyticsSink, NoopAnalyticsSink, PostHogSink };
@@ -1,3 +1,3 @@
1
1
  import { t as PostHogSink } from "./posthog-sink-BUvWHuIC.mjs";
2
- import { t as NoopAnalyticsSink } from "./sinks-Dz8Kxoha.mjs";
2
+ import { t as NoopAnalyticsSink } from "./sinks-DX4cyR-X.mjs";
3
3
  export { NoopAnalyticsSink, PostHogSink };
@@ -0,0 +1,69 @@
1
+ /**
2
+ * `@theholocron/observability/browser/posthog`: the `AnalyticsSink` for a web
3
+ * page or a React app, over `posthog-js`.
4
+ *
5
+ * Importing this entry loads `posthog-js` and nothing else; the Sentry adapter
6
+ * is its own entry (`/browser/sentry`). `posthog-js` is an optional peer
7
+ * dependency. The caller resolves the project key; this module reads no
8
+ * environment and imports no Node built-in.
9
+ */
10
+ import { t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
11
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
12
+ /** The slice of a `posthog-js` instance that {@link PostHogBrowserSink} calls. */
13
+ interface PostHogBrowserClient {
14
+ /** Starts the client with the project key and options. */
15
+ init(key: string, config: Record<string, unknown>): unknown;
16
+ /** The id of the person the client records for now. */
17
+ get_distinct_id(): string;
18
+ /** Sets the person and their properties. */
19
+ identify(distinctId: string, props?: Record<string, unknown>): unknown;
20
+ /** Records an event for the current person. */
21
+ capture(event: string, props?: Record<string, unknown>): unknown;
22
+ /** Flushes the queue and tears the client down. */
23
+ shutdown(): Promise<void>;
24
+ }
25
+ /** Options of {@link PostHogBrowserSink}. */
26
+ interface PostHogBrowserSinkConfig {
27
+ /** The project API key. */
28
+ key: string;
29
+ /** The PostHog host (`api_host`). */
30
+ host: string;
31
+ /** The client to call. Defaults to the `posthog-js` singleton; pass a fake in a test. */
32
+ client?: PostHogBrowserClient;
33
+ /**
34
+ * `posthog-js` options merged over the privacy-safe defaults (`autocapture`,
35
+ * `capture_pageview`, `capture_pageleave` and `disable_session_recording`
36
+ * switched off, `person_profiles: "identified_only"`). Set one here to opt in.
37
+ */
38
+ options?: Record<string, unknown>;
39
+ /** Called with each error the sink swallowed from the SDK. Defaults to ignoring them; it can never throw into the app. */
40
+ onError?: OnError;
41
+ }
42
+ /**
43
+ * `PostHogBrowserSink` is the `posthog-js` call site, an `AnalyticsSink` for a
44
+ * web page or a React app. The constructor starts the client with the
45
+ * resolved key and host and the privacy-safe defaults of
46
+ * {@link PostHogBrowserSinkConfig.options}: nothing is captured except what
47
+ * the app sends through `capture()`.
48
+ *
49
+ * A browser has one current person. The rule for `capture(distinctId, ...)`:
50
+ * when `distinctId` is not the client's current id (`get_distinct_id()`), the
51
+ * sink calls `identify(distinctId)` first, so the event is never attributed to
52
+ * another person. To record for an anonymous visitor, pass the client's own id.
53
+ *
54
+ * Event and person properties are scrubbed of token-shaped strings. Every
55
+ * call is guarded and never throws; errors go to `onError`. `posthog-js` is
56
+ * an optional peer dependency.
57
+ */
58
+ export declare class PostHogBrowserSink implements AnalyticsSink {
59
+ #private;
60
+ /** Starts the client with `config.key`, `config.host` and the defaults. */
61
+ constructor(config: PostHogBrowserSinkConfig);
62
+ /** Sets the current person to `distinctId` with `props` (scrubbed). */
63
+ identify(distinctId: string, props: Record<string, unknown>): void;
64
+ /** Records `event` with `props` (scrubbed) for `distinctId`, identifying that person first when the client is on another one. */
65
+ capture(distinctId: string, event: string, props: Record<string, unknown>): void;
66
+ /** Flushes queued events and shuts the client down; never rejects. */
67
+ shutdown(): Promise<void>;
68
+ }
69
+ export type { OnError, PostHogBrowserClient, PostHogBrowserSinkConfig };
@@ -0,0 +1,58 @@
1
+ import { i as redactObject } from "./redact-DCKZnDDa.mjs";
2
+ import { n as safely, r as safelyAsync } from "./safe-DIVuybiy.mjs";
3
+ import posthog from "posthog-js";
4
+ // src/browser/posthog-browser-sink.ts
5
+ /** What `posthog-js` records by itself unless asked: all off, and a person profile only once someone is identified. */
6
+ const PRIVACY_DEFAULTS = {
7
+ autocapture: false,
8
+ capture_pageview: false,
9
+ capture_pageleave: false,
10
+ disable_session_recording: true,
11
+ person_profiles: "identified_only"
12
+ };
13
+ /**
14
+ * `PostHogBrowserSink` is the `posthog-js` call site, an `AnalyticsSink` for a
15
+ * web page or a React app. The constructor starts the client with the
16
+ * resolved key and host and the privacy-safe defaults of
17
+ * {@link PostHogBrowserSinkConfig.options}: nothing is captured except what
18
+ * the app sends through `capture()`.
19
+ *
20
+ * A browser has one current person. The rule for `capture(distinctId, ...)`:
21
+ * when `distinctId` is not the client's current id (`get_distinct_id()`), the
22
+ * sink calls `identify(distinctId)` first, so the event is never attributed to
23
+ * another person. To record for an anonymous visitor, pass the client's own id.
24
+ *
25
+ * Event and person properties are scrubbed of token-shaped strings. Every
26
+ * call is guarded and never throws; errors go to `onError`. `posthog-js` is
27
+ * an optional peer dependency.
28
+ */
29
+ var PostHogBrowserSink = class {
30
+ #client;
31
+ #onError;
32
+ /** Starts the client with `config.key`, `config.host` and the defaults. */
33
+ constructor(config) {
34
+ this.#client = config.client ?? posthog;
35
+ this.#onError = config.onError;
36
+ safely(() => this.#client.init(config.key, {
37
+ ...PRIVACY_DEFAULTS,
38
+ ...config.options,
39
+ api_host: config.host
40
+ }), this.#onError, "posthog.init");
41
+ }
42
+ /** Sets the current person to `distinctId` with `props` (scrubbed). */
43
+ identify(distinctId, props) {
44
+ safely(() => this.#client.identify(distinctId, redactObject(props)), this.#onError, "posthog.identify");
45
+ }
46
+ /** Records `event` with `props` (scrubbed) for `distinctId`, identifying that person first when the client is on another one. */
47
+ capture(distinctId, event, props) {
48
+ safely(() => {
49
+ if (this.#client.get_distinct_id() !== distinctId) this.#client.identify(distinctId);
50
+ this.#client.capture(event, redactObject(props));
51
+ }, this.#onError, "posthog.capture");
52
+ }
53
+ /** Flushes queued events and shuts the client down; never rejects. */
54
+ async shutdown() {
55
+ await safelyAsync(() => this.#client.shutdown(), this.#onError, "posthog.shutdown");
56
+ }
57
+ };
58
+ export { PostHogBrowserSink };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * `@theholocron/observability/browser/sentry`: the `ErrorSink` for a web page
3
+ * or a React app, over `@sentry/browser`.
4
+ *
5
+ * Importing this entry loads `@sentry/browser` and nothing else; the PostHog
6
+ * adapter is its own entry (`/browser/posthog`), so an app that uses only one
7
+ * SDK never has to install the other. `@sentry/browser` is an optional peer
8
+ * dependency. The caller resolves the DSN; this module reads no environment and
9
+ * imports no Node built-in.
10
+ */
11
+ import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
12
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
13
+ /**
14
+ * `SentryBrowserSink` is the `@sentry/browser` call site, an `ErrorSink` for a
15
+ * web page or a React app (React Native has its own subpath). `@sentry/browser`
16
+ * is an optional peer dependency. Pass `options.sdk` to inject a fake.
17
+ */
18
+ export declare class SentryBrowserSink extends SentryCompatibleSink {
19
+ /** Builds the sink over `@sentry/browser`, or over `options.sdk` when given. */
20
+ constructor(options?: SentrySinkOptions);
21
+ }
22
+ export { type OnError, type SentrySdkLike, type SentrySinkOptions, scrubEvent };
@@ -0,0 +1,15 @@
1
+ import { n as scrubEvent, t as SentryCompatibleSink } from "./sentry-compatible-sink-DrpWebmE.mjs";
2
+ import * as Sentry from "@sentry/browser";
3
+ // src/browser/sentry-browser-sink.ts
4
+ /**
5
+ * `SentryBrowserSink` is the `@sentry/browser` call site, an `ErrorSink` for a
6
+ * web page or a React app (React Native has its own subpath). `@sentry/browser`
7
+ * is an optional peer dependency. Pass `options.sdk` to inject a fake.
8
+ */
9
+ var SentryBrowserSink = class extends SentryCompatibleSink {
10
+ /** Builds the sink over `@sentry/browser`, or over `options.sdk` when given. */
11
+ constructor(options = {}) {
12
+ super(options.sdk ?? Sentry, options);
13
+ }
14
+ };
15
+ export { SentryBrowserSink, scrubEvent };
package/dist/core.d.mts CHANGED
@@ -1,4 +1,13 @@
1
- import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-v2TCQsCp.mjs";
1
+ /**
2
+ * `@theholocron/observability/core` — the zero-dependency heart of the library.
3
+ *
4
+ * Interfaces ({@link Logger}, {@link ErrorSink}, {@link AnalyticsSink}), their
5
+ * no-op implementations, and the redaction helpers. Nothing here imports a
6
+ * vendor SDK or a Node built-in beyond what a browser / edge / React Native
7
+ * bundle tolerates, so application and library code can depend on `/core`
8
+ * everywhere; only the process entry point pulls a concrete adapter subpath.
9
+ */
10
+ import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
2
11
  import { a as NoopLogger, i as Logger, n as LogEnv, r as LogLevel, t as LOG_LEVELS } from "./logger-DC6M2rL_.mjs";
3
12
  import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./core-DDpFbotv.mjs";
4
13
  export { type AnalyticsSink, type CommandSpan, type ErrorSink, LOG_LEVELS, type LogEnv, type LogLevel, type Logger, NoopAnalyticsSink, NoopErrorSink, NoopLogger, REDACTED_PATHS, REDACT_CENSOR, redact, redactObject, redactOptions };
package/dist/core.mjs CHANGED
@@ -1,4 +1,4 @@
1
1
  import { n as NoopLogger, t as LOG_LEVELS } from "./logger-BqELCAzg.mjs";
2
2
  import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./redact-DCKZnDDa.mjs";
3
- import { n as NoopErrorSink, t as NoopAnalyticsSink } from "./sinks-Dz8Kxoha.mjs";
3
+ import { n as NoopErrorSink, t as NoopAnalyticsSink } from "./sinks-DX4cyR-X.mjs";
4
4
  export { LOG_LEVELS, NoopAnalyticsSink, NoopErrorSink, NoopLogger, REDACTED_PATHS, REDACT_CENSOR, redact, redactObject, redactOptions };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * `@theholocron/observability/edge/posthog`: the `AnalyticsSink` for an edge or
3
+ * worker runtime, over PostHog's HTTP capture endpoint with `fetch` alone.
4
+ *
5
+ * Importing this entry loads no SDK at all, so it has no peer dependency. The
6
+ * caller resolves the project key; this module reads no environment and
7
+ * imports no Node built-in.
8
+ */
9
+ import { t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
10
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
11
+ /** The part of a `fetch` response the sink reads. */
12
+ interface FetchResponseLike {
13
+ /** `true` for a 2xx status. */
14
+ ok?: boolean;
15
+ /** The HTTP status. */
16
+ status?: number;
17
+ }
18
+ /** The `fetch` shape the sink needs; the global `fetch` of every edge runtime fits. */
19
+ type FetchLike = (url: string, init: {
20
+ method: string;
21
+ headers: Record<string, string>;
22
+ body: string;
23
+ signal?: AbortSignal;
24
+ }) => Promise<FetchResponseLike | unknown>;
25
+ /** Options of {@link PostHogFetchSink}. */
26
+ interface PostHogFetchSinkConfig {
27
+ /** The project API key. */
28
+ key: string;
29
+ /** The PostHog host, such as `https://us.i.posthog.com`. */
30
+ host: string;
31
+ /** The `fetch` to call. Defaults to the global `fetch`; pass a fake in a test. */
32
+ fetch?: FetchLike;
33
+ /** Registers a pending request with the runtime (`ctx.waitUntil` in a Cloudflare Worker, `waitUntil` in a Vercel edge function). */
34
+ waitUntil?: (pending: Promise<unknown>) => void;
35
+ /** Milliseconds before a request is aborted. Defaults to `5000`. */
36
+ requestTimeoutMs?: number;
37
+ /** Called with each error the sink swallowed (a rejected request, an HTTP error status, a timeout, a dropped event). Defaults to ignoring them; it can never throw into the app. */
38
+ onError?: OnError;
39
+ }
40
+ /**
41
+ * `PostHogFetchSink` is an `AnalyticsSink` for edge and worker runtimes that
42
+ * talks to PostHog's HTTP capture endpoint with `fetch`. It imports no SDK and
43
+ * no Node built-in, so it has no peer dependency. Each call sends one request
44
+ * at once (no batching: an isolate may be frozen right after the response).
45
+ *
46
+ * - Properties, including person properties, are scrubbed of token-shaped strings.
47
+ * - A request is aborted after `requestTimeoutMs`. A non-2xx response or a
48
+ * rejected request goes to `onError`. There is no retry: the event is dropped.
49
+ * - `shutdown(timeoutMs)` stops accepting events (later calls go to `onError`
50
+ * and are dropped), then waits for every accepted request, at most `timeoutMs`.
51
+ * - Every call is guarded and never throws.
52
+ */
53
+ export declare class PostHogFetchSink implements AnalyticsSink {
54
+ #private;
55
+ /** Builds the sink for `config.key` and `config.host`. */
56
+ constructor(config: PostHogFetchSinkConfig);
57
+ /**
58
+ * Sends a `$identify` event that sets `props` on the person `distinctId`.
59
+ * Pass `previousId`, the anonymous id the visitor had before, to merge their
60
+ * earlier events into the person (sent as `$anon_distinct_id`).
61
+ */
62
+ identify(distinctId: string, props: Record<string, unknown>, previousId?: string): void;
63
+ /** Sends `event` for `distinctId` with `props`. */
64
+ capture(distinctId: string, event: string, props: Record<string, unknown>): void;
65
+ /** Stops accepting events and waits for the accepted requests, for at most `timeoutMs` (default 5000); never rejects. */
66
+ shutdown(timeoutMs?: number): Promise<void>;
67
+ }
68
+ export type { FetchLike, FetchResponseLike, OnError, PostHogFetchSinkConfig };
@@ -0,0 +1,99 @@
1
+ import { i as redactObject } from "./redact-DCKZnDDa.mjs";
2
+ import { n as safely, t as report } from "./safe-DIVuybiy.mjs";
3
+ // src/edge/posthog-fetch-sink.ts
4
+ /**
5
+ * `PostHogFetchSink` is an `AnalyticsSink` for edge and worker runtimes that
6
+ * talks to PostHog's HTTP capture endpoint with `fetch`. It imports no SDK and
7
+ * no Node built-in, so it has no peer dependency. Each call sends one request
8
+ * at once (no batching: an isolate may be frozen right after the response).
9
+ *
10
+ * - Properties, including person properties, are scrubbed of token-shaped strings.
11
+ * - A request is aborted after `requestTimeoutMs`. A non-2xx response or a
12
+ * rejected request goes to `onError`. There is no retry: the event is dropped.
13
+ * - `shutdown(timeoutMs)` stops accepting events (later calls go to `onError`
14
+ * and are dropped), then waits for every accepted request, at most `timeoutMs`.
15
+ * - Every call is guarded and never throws.
16
+ */
17
+ var PostHogFetchSink = class {
18
+ #key;
19
+ #url;
20
+ #fetch;
21
+ #waitUntil;
22
+ #timeoutMs;
23
+ #onError;
24
+ #pending = /* @__PURE__ */ new Set();
25
+ #closed = false;
26
+ /** Builds the sink for `config.key` and `config.host`. */
27
+ constructor(config) {
28
+ this.#key = config.key;
29
+ this.#url = `${config.host.replace(/\/+$/, "")}/capture/`;
30
+ this.#fetch = config.fetch ?? ((url, init) => fetch(url, init));
31
+ this.#waitUntil = config.waitUntil;
32
+ this.#timeoutMs = config.requestTimeoutMs ?? 5e3;
33
+ this.#onError = config.onError;
34
+ }
35
+ /**
36
+ * Sends a `$identify` event that sets `props` on the person `distinctId`.
37
+ * Pass `previousId`, the anonymous id the visitor had before, to merge their
38
+ * earlier events into the person (sent as `$anon_distinct_id`).
39
+ */
40
+ identify(distinctId, props, previousId) {
41
+ this.#send(distinctId, "$identify", {
42
+ $set: props,
43
+ ...previousId === void 0 ? {} : { $anon_distinct_id: previousId }
44
+ });
45
+ }
46
+ /** Sends `event` for `distinctId` with `props`. */
47
+ capture(distinctId, event, props) {
48
+ this.#send(distinctId, event, props);
49
+ }
50
+ /** Stops accepting events and waits for the accepted requests, for at most `timeoutMs` (default 5000); never rejects. */
51
+ async shutdown(timeoutMs = 5e3) {
52
+ this.#closed = true;
53
+ if (this.#pending.size === 0) return;
54
+ let timer;
55
+ const expired = new Promise((resolve) => {
56
+ timer = setTimeout(() => resolve("timeout"), timeoutMs);
57
+ });
58
+ const outcome = await Promise.race([Promise.allSettled([...this.#pending]), expired]);
59
+ clearTimeout(timer);
60
+ if (outcome === "timeout") report(this.#onError, /* @__PURE__ */ new Error("shutdown timed out"), "posthog.fetch.shutdown");
61
+ }
62
+ #send(distinctId, event, properties) {
63
+ if (this.#closed) {
64
+ report(this.#onError, /* @__PURE__ */ new Error(`dropped "${event}": the sink is shut down`), "posthog.fetch.closed");
65
+ return;
66
+ }
67
+ safely(() => {
68
+ const controller = new AbortController();
69
+ const timer = setTimeout(() => controller.abort(), this.#timeoutMs);
70
+ let started;
71
+ try {
72
+ started = this.#fetch(this.#url, {
73
+ method: "POST",
74
+ headers: { "content-type": "application/json" },
75
+ body: JSON.stringify({
76
+ api_key: this.#key,
77
+ event,
78
+ distinct_id: distinctId,
79
+ properties: redactObject(properties)
80
+ }),
81
+ signal: controller.signal
82
+ });
83
+ } catch (error) {
84
+ clearTimeout(timer);
85
+ throw error;
86
+ }
87
+ const request = Promise.resolve(started).then((response) => {
88
+ const res = response;
89
+ if (res && res.ok === false) report(this.#onError, /* @__PURE__ */ new Error(`PostHog responded ${String(res.status)}`), "posthog.fetch.response");
90
+ }).catch((error) => report(this.#onError, error, "posthog.fetch")).finally(() => {
91
+ clearTimeout(timer);
92
+ this.#pending.delete(request);
93
+ });
94
+ this.#pending.add(request);
95
+ safely(() => this.#waitUntil?.(request), this.#onError, "posthog.fetch.waitUntil");
96
+ }, this.#onError, "posthog.fetch.send");
97
+ }
98
+ };
99
+ export { PostHogFetchSink };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * `@theholocron/observability/edge/sentry`: the `ErrorSink` for an edge or
3
+ * worker runtime (Cloudflare Workers, Vercel Edge, Deno), over
4
+ * `@sentry/vercel-edge`.
5
+ *
6
+ * Importing this entry loads `@sentry/vercel-edge` and nothing else.
7
+ * `@sentry/vercel-edge` is an optional peer dependency. The caller resolves the
8
+ * DSN; this module reads no environment and imports no Node built-in.
9
+ */
10
+ import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
11
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
12
+ /**
13
+ * `SentryEdgeSink` is the `@sentry/vercel-edge` call site, an `ErrorSink` for an
14
+ * edge or worker runtime (WinterCG: no `window`, no Node built-ins). The edge
15
+ * SDK has no release-health sessions, so `endSession()` does nothing.
16
+ * `@sentry/vercel-edge` is an optional peer dependency. Pass `options.sdk` to
17
+ * inject a fake. In a Cloudflare Worker, call `flush()` inside
18
+ * `ctx.waitUntil()` so the events leave before the isolate is frozen.
19
+ */
20
+ export declare class SentryEdgeSink extends SentryCompatibleSink {
21
+ /** Builds the sink over `@sentry/vercel-edge`, or over `options.sdk` when given. */
22
+ constructor(options?: SentrySinkOptions);
23
+ }
24
+ export { type OnError, type SentrySdkLike, type SentrySinkOptions, scrubEvent };
@@ -0,0 +1,18 @@
1
+ import { n as scrubEvent, t as SentryCompatibleSink } from "./sentry-compatible-sink-DrpWebmE.mjs";
2
+ import * as Sentry from "@sentry/vercel-edge";
3
+ // src/edge/sentry-edge-sink.ts
4
+ /**
5
+ * `SentryEdgeSink` is the `@sentry/vercel-edge` call site, an `ErrorSink` for an
6
+ * edge or worker runtime (WinterCG: no `window`, no Node built-ins). The edge
7
+ * SDK has no release-health sessions, so `endSession()` does nothing.
8
+ * `@sentry/vercel-edge` is an optional peer dependency. Pass `options.sdk` to
9
+ * inject a fake. In a Cloudflare Worker, call `flush()` inside
10
+ * `ctx.waitUntil()` so the events leave before the isolate is frozen.
11
+ */
12
+ var SentryEdgeSink = class extends SentryCompatibleSink {
13
+ /** Builds the sink over `@sentry/vercel-edge`, or over `options.sdk` when given. */
14
+ constructor(options = {}) {
15
+ super(options.sdk ?? Sentry, options);
16
+ }
17
+ };
18
+ export { SentryEdgeSink, scrubEvent };
package/dist/errors.d.mts CHANGED
@@ -1,3 +1,11 @@
1
- import { a as NoopErrorSink, n as CommandSpan, r as ErrorSink } from "./sinks-v2TCQsCp.mjs";
2
- import { n as scrubError, t as SentrySink } from "./sentry-sink-C1E3tOl-.mjs";
1
+ /**
2
+ * `@theholocron/observability/errors` — error tracking + performance spans.
3
+ *
4
+ * `SentrySink` is the Node adapter for the {@link ErrorSink} interface (from
5
+ * `/core`). `@sentry/node` is an optional peer dependency; install it
6
+ * alongside this subpath. The caller resolves the DSN and passes it to
7
+ * `init()` — this module reads no environment.
8
+ */
9
+ import { a as NoopErrorSink, n as CommandSpan, r as ErrorSink } from "./sinks-D5CwDgDL.mjs";
10
+ import { n as scrubError, t as SentrySink } from "./sentry-sink-C0WWmUBS.mjs";
3
11
  export { type CommandSpan, type ErrorSink, NoopErrorSink, SentrySink, scrubError };
package/dist/errors.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as NoopErrorSink } from "./sinks-Dz8Kxoha.mjs";
1
+ import { n as NoopErrorSink } from "./sinks-DX4cyR-X.mjs";
2
2
  import { n as scrubError, t as SentrySink } from "./sentry-sink-QWpfJGg_.mjs";
3
3
  export { NoopErrorSink, SentrySink, scrubError };
package/dist/index.d.mts CHANGED
@@ -1,8 +1,16 @@
1
- import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-v2TCQsCp.mjs";
1
+ /**
2
+ * `@theholocron/observability` — the whole surface in one import.
3
+ *
4
+ * Prefer a subpath (`@theholocron/observability/core`, `/logger`, `/errors`,
5
+ * `/analytics`) so a browser or edge bundle only pulls what it uses and the
6
+ * optional peer dependencies stay optional. The root export is the convenience
7
+ * form for a Node process that wants all of it.
8
+ */
9
+ import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
2
10
  import { a as detectEnv, c as isTelemetryDisabled, d as resolveLevel, f as ConsoleLogger, i as createPinoInstance, l as parseLogLevel, n as PinoLogger, o as generateRunId, r as buildPinoOptions, s as isCI, t as CreatePinoInstanceInput, u as resolveAxiomFromEnv } from "./pino-DIfQGY7z.mjs";
3
11
  import { a as NoopLogger, i as Logger, n as LogEnv, r as LogLevel, t as LOG_LEVELS } from "./logger-DC6M2rL_.mjs";
4
12
  import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./core-DDpFbotv.mjs";
5
13
  import { a as BuildTransportInput, i as AxiomTransportConfig, n as LoggerConfig, o as buildTransport, r as createLogger, t as CreateLoggerResult } from "./create-X1v3LWjb.mjs";
6
- import { n as scrubError, t as SentrySink } from "./sentry-sink-C1E3tOl-.mjs";
7
- import { t as PostHogSink } from "./posthog-sink-D4aRF3P2.mjs";
14
+ import { n as scrubError, t as SentrySink } from "./sentry-sink-C0WWmUBS.mjs";
15
+ import { t as PostHogSink } from "./posthog-sink-CmQmma-o.mjs";
8
16
  export { type AnalyticsSink, type AxiomTransportConfig, type BuildTransportInput, type CommandSpan, ConsoleLogger, type CreateLoggerResult, type CreatePinoInstanceInput, type ErrorSink, LOG_LEVELS, type LogEnv, type LogLevel, type Logger, type LoggerConfig, NoopAnalyticsSink, NoopErrorSink, NoopLogger, PinoLogger, PostHogSink, REDACTED_PATHS, REDACT_CENSOR, SentrySink, buildPinoOptions, buildTransport, createLogger, createPinoInstance, detectEnv, generateRunId, isCI, isTelemetryDisabled, parseLogLevel, redact, redactObject, redactOptions, resolveAxiomFromEnv, resolveLevel, scrubError };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { n as NoopLogger, t as LOG_LEVELS } from "./logger-BqELCAzg.mjs";
2
2
  import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./redact-DCKZnDDa.mjs";
3
- import { n as NoopErrorSink, t as NoopAnalyticsSink } from "./sinks-Dz8Kxoha.mjs";
3
+ import { n as NoopErrorSink, t as NoopAnalyticsSink } from "./sinks-DX4cyR-X.mjs";
4
4
  import "./core.mjs";
5
5
  import { a as buildTransport, c as isCI, d as resolveAxiomFromEnv, f as resolveLevel, i as createPinoInstance, l as isTelemetryDisabled, n as PinoLogger, o as detectEnv, p as ConsoleLogger, r as buildPinoOptions, s as generateRunId, t as createLogger, u as parseLogLevel } from "./create-DpLJTs8A.mjs";
6
6
  import { n as scrubError, t as SentrySink } from "./sentry-sink-QWpfJGg_.mjs";
package/dist/logger.d.mts CHANGED
@@ -1,3 +1,15 @@
1
+ /**
2
+ * `@theholocron/observability/logger` — structured logging.
3
+ *
4
+ * `createLogger()` returns a Pino-backed {@link Logger} with pino-pretty
5
+ * locally, NDJSON in CI, and an Axiom transport when credentials resolve
6
+ * (ADR-0007). `ConsoleLogger` is the zero-dependency alternative for the
7
+ * browser / edge / React Native, behind the same interface.
8
+ *
9
+ * `pino`, `pino-pretty` and `@axiomhq/pino` are optional peer dependencies —
10
+ * install them alongside this subpath. The {@link Logger} interface itself
11
+ * lives in `/core` and needs none of them.
12
+ */
1
13
  import { i as Logger, n as LogEnv, r as LogLevel, t as LOG_LEVELS } from "./logger-DC6M2rL_.mjs";
2
14
  import { a as detectEnv, c as isTelemetryDisabled, d as resolveLevel, f as ConsoleLogger, i as createPinoInstance, l as parseLogLevel, n as PinoLogger, o as generateRunId, r as buildPinoOptions, s as isCI, t as CreatePinoInstanceInput, u as resolveAxiomFromEnv } from "./pino-DIfQGY7z.mjs";
3
15
  import { a as BuildTransportInput, i as AxiomTransportConfig, n as LoggerConfig, o as buildTransport, r as createLogger, t as CreateLoggerResult } from "./create-X1v3LWjb.mjs";
@@ -1,4 +1,4 @@
1
- import { t as AnalyticsSink } from "./sinks-v2TCQsCp.mjs";
1
+ import { t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
2
2
  declare class PostHogSink implements AnalyticsSink {
3
3
  #private;
4
4
  constructor(config: {
@@ -0,0 +1,79 @@
1
+ /**
2
+ * `@theholocron/observability/react-native`: error tracking and analytics for a
3
+ * React Native app.
4
+ *
5
+ * `SentryReactNativeSink` (over `@sentry/react-native`) implements
6
+ * {@link ErrorSink} and `PostHogReactNativeSink` (over `posthog-react-native`)
7
+ * implements {@link AnalyticsSink}, both from `/core`; for logging use
8
+ * `ConsoleLogger` from `/logger`. Both SDKs need the `react-native` runtime to
9
+ * load, so the app imports them and passes them in; this module imports neither
10
+ * and no Node built-in. The SDKs are optional peer dependencies. The caller
11
+ * resolves the DSN and the project key.
12
+ */
13
+ import { a as NoopErrorSink, i as NoopAnalyticsSink, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
14
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
15
+ import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
16
+ /** The slice of a `posthog-react-native` client that {@link PostHogReactNativeSink} calls. */
17
+ interface PostHogReactNativeClient {
18
+ /** The id of the person the client records for now. */
19
+ getDistinctId(): string;
20
+ /** Sets the person and their properties. */
21
+ identify(distinctId: string, props?: Record<string, unknown>): unknown;
22
+ /** Records an event for the current person. */
23
+ capture(event: string, props?: Record<string, unknown>): unknown;
24
+ /** Flushes the queue and stops the client. */
25
+ shutdown(): Promise<void>;
26
+ }
27
+ /** Options of {@link PostHogReactNativeSink}. */
28
+ interface PostHogReactNativeSinkConfig {
29
+ /** A `new PostHog(key, { host })` from `posthog-react-native`, built by the app. */
30
+ client: PostHogReactNativeClient;
31
+ /** Called with each error the sink swallowed from the SDK. Defaults to ignoring them; it can never throw into the app. */
32
+ onError?: OnError;
33
+ }
34
+ /**
35
+ * `PostHogReactNativeSink` is the `AnalyticsSink` for a React Native app over
36
+ * `posthog-react-native`. The app builds the client (`new PostHog(key, { host })`)
37
+ * and passes it in: the SDK needs the `react-native` runtime to load, so this
38
+ * module never imports it itself.
39
+ *
40
+ * The client has one current person. The rule for `capture(distinctId, ...)`:
41
+ * when `distinctId` is not the client's current id (`getDistinctId()`), the sink
42
+ * calls `identify(distinctId)` first, so the event is never attributed to
43
+ * another person. To record for an anonymous visitor, pass the client's own id.
44
+ *
45
+ * Event and person properties are scrubbed of token-shaped strings. Every call
46
+ * is guarded and never throws; errors go to `onError`. `posthog-react-native`
47
+ * is an optional peer dependency.
48
+ */
49
+ export declare class PostHogReactNativeSink implements AnalyticsSink {
50
+ #private;
51
+ /** Wraps `config.client`. */
52
+ constructor(config: PostHogReactNativeSinkConfig);
53
+ /** Sets the current person to `distinctId` with `props` (scrubbed). */
54
+ identify(distinctId: string, props: Record<string, unknown>): void;
55
+ /** Records `event` with `props` (scrubbed) for `distinctId`, identifying that person first when the client is on another one. */
56
+ capture(distinctId: string, event: string, props: Record<string, unknown>): void;
57
+ /** Flushes queued events and stops the client; never rejects. */
58
+ shutdown(): Promise<void>;
59
+ }
60
+ /** Options of {@link SentryReactNativeSink}. */
61
+ interface SentryReactNativeSinkOptions extends Omit<SentrySinkOptions, "sdk"> {
62
+ /** The `@sentry/react-native` namespace, as the app imports it (`import * as Sentry from "@sentry/react-native"`). */
63
+ sdk: SentrySdkLike;
64
+ }
65
+ /**
66
+ * `SentryReactNativeSink` is the `ErrorSink` for a React Native app over
67
+ * `@sentry/react-native`. The app imports the SDK and passes it in as
68
+ * `options.sdk`: the SDK needs the `react-native` runtime to load, so this
69
+ * module never imports it itself and stays loadable (and testable) anywhere.
70
+ * `sessions` defaults to `false` here, because the native layer tracks
71
+ * release-health sessions. `@sentry/react-native` is an optional peer
72
+ * dependency. Pair it with `Sentry.wrap(App)` for the native crash and
73
+ * navigation integrations, which sit outside this interface.
74
+ */
75
+ export declare class SentryReactNativeSink extends SentryCompatibleSink {
76
+ /** Builds the sink over `options.sdk`. */
77
+ constructor(options: SentryReactNativeSinkOptions);
78
+ }
79
+ export { type AnalyticsSink, type ErrorSink, NoopAnalyticsSink, NoopErrorSink, type OnError, type PostHogReactNativeClient, type PostHogReactNativeSinkConfig, type SentryReactNativeSinkOptions, type SentrySdkLike, scrubEvent };
@@ -0,0 +1,65 @@
1
+ import { n as NoopErrorSink, t as NoopAnalyticsSink } from "./sinks-DX4cyR-X.mjs";
2
+ import { i as redactObject } from "./redact-DCKZnDDa.mjs";
3
+ import { n as safely, r as safelyAsync } from "./safe-DIVuybiy.mjs";
4
+ import { n as scrubEvent, t as SentryCompatibleSink } from "./sentry-compatible-sink-DrpWebmE.mjs";
5
+ // src/react-native/posthog-react-native-sink.ts
6
+ /**
7
+ * `PostHogReactNativeSink` is the `AnalyticsSink` for a React Native app over
8
+ * `posthog-react-native`. The app builds the client (`new PostHog(key, { host })`)
9
+ * and passes it in: the SDK needs the `react-native` runtime to load, so this
10
+ * module never imports it itself.
11
+ *
12
+ * The client has one current person. The rule for `capture(distinctId, ...)`:
13
+ * when `distinctId` is not the client's current id (`getDistinctId()`), the sink
14
+ * calls `identify(distinctId)` first, so the event is never attributed to
15
+ * another person. To record for an anonymous visitor, pass the client's own id.
16
+ *
17
+ * Event and person properties are scrubbed of token-shaped strings. Every call
18
+ * is guarded and never throws; errors go to `onError`. `posthog-react-native`
19
+ * is an optional peer dependency.
20
+ */
21
+ var PostHogReactNativeSink = class {
22
+ #client;
23
+ #onError;
24
+ /** Wraps `config.client`. */
25
+ constructor(config) {
26
+ this.#client = config.client;
27
+ this.#onError = config.onError;
28
+ }
29
+ /** Sets the current person to `distinctId` with `props` (scrubbed). */
30
+ identify(distinctId, props) {
31
+ safely(() => this.#client.identify(distinctId, redactObject(props)), this.#onError, "posthog.identify");
32
+ }
33
+ /** Records `event` with `props` (scrubbed) for `distinctId`, identifying that person first when the client is on another one. */
34
+ capture(distinctId, event, props) {
35
+ safely(() => {
36
+ if (this.#client.getDistinctId() !== distinctId) this.#client.identify(distinctId);
37
+ this.#client.capture(event, redactObject(props));
38
+ }, this.#onError, "posthog.capture");
39
+ }
40
+ /** Flushes queued events and stops the client; never rejects. */
41
+ async shutdown() {
42
+ await safelyAsync(() => this.#client.shutdown(), this.#onError, "posthog.shutdown");
43
+ }
44
+ };
45
+ // src/react-native/sentry-react-native-sink.ts
46
+ /**
47
+ * `SentryReactNativeSink` is the `ErrorSink` for a React Native app over
48
+ * `@sentry/react-native`. The app imports the SDK and passes it in as
49
+ * `options.sdk`: the SDK needs the `react-native` runtime to load, so this
50
+ * module never imports it itself and stays loadable (and testable) anywhere.
51
+ * `sessions` defaults to `false` here, because the native layer tracks
52
+ * release-health sessions. `@sentry/react-native` is an optional peer
53
+ * dependency. Pair it with `Sentry.wrap(App)` for the native crash and
54
+ * navigation integrations, which sit outside this interface.
55
+ */
56
+ var SentryReactNativeSink = class extends SentryCompatibleSink {
57
+ /** Builds the sink over `options.sdk`. */
58
+ constructor(options) {
59
+ super(options.sdk, {
60
+ ...options,
61
+ sessions: options.sessions ?? false
62
+ });
63
+ }
64
+ };
65
+ export { NoopAnalyticsSink, NoopErrorSink, PostHogReactNativeSink, SentryReactNativeSink, scrubEvent };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Receives a vendor error an adapter swallowed. `context` names the call, such
3
+ * as `"sentry.captureException"` or `"posthog.fetch"`. Pass one as `onError`
4
+ * to log or count adapter failures; the default is to ignore them.
5
+ */
6
+ type OnError = (error: unknown, context: string) => void;
7
+ export { OnError as t };
@@ -0,0 +1,39 @@
1
+ // src/shared/safe.ts
2
+ /**
3
+ * Reports `error` to `onError`, guarded so the callback can never throw or
4
+ * start a loop: a throw from it is dropped and it is not called again for that
5
+ * failure.
6
+ */
7
+ function report(onError, error, context) {
8
+ if (!onError) return;
9
+ try {
10
+ onError(error, context);
11
+ } catch {}
12
+ }
13
+ /**
14
+ * Runs a vendor call so that nothing it throws reaches the application.
15
+ * Telemetry is best effort: a broken SDK, a blocked request or a bad
16
+ * configuration must never take the app down. A throw goes to `onError` (when
17
+ * given) with `context`; the result is then `undefined`.
18
+ */
19
+ function safely(call, onError, context = "call") {
20
+ try {
21
+ return call();
22
+ } catch (error) {
23
+ report(onError, error, context);
24
+ return;
25
+ }
26
+ }
27
+ /**
28
+ * Awaits a vendor promise and discards a rejection, like {@link safely} for
29
+ * asynchronous calls. Resolves `undefined` on a throw or a rejection.
30
+ */
31
+ async function safelyAsync(call, onError, context = "call") {
32
+ try {
33
+ return await call();
34
+ } catch (error) {
35
+ report(onError, error, context);
36
+ return;
37
+ }
38
+ }
39
+ export { safely as n, safelyAsync as r, report as t };
@@ -0,0 +1,74 @@
1
+ import { n as CommandSpan, r as ErrorSink } from "./sinks-D5CwDgDL.mjs";
2
+ import { t as OnError } from "./safe-6WuLqOVf.mjs";
3
+ /** The slice of a Sentry SDK namespace (`@sentry/browser`, `@sentry/vercel-edge`, ...) that {@link SentryCompatibleSink} calls. */
4
+ interface SentrySdkLike {
5
+ /** Initialises the SDK. */
6
+ init(options: Record<string, unknown>): unknown;
7
+ /** Sets a tag on every later event. */
8
+ setTag(key: string, value: string): unknown;
9
+ /** Reports an error. */
10
+ captureException(err: unknown): unknown;
11
+ /** Starts a span that is not made active. */
12
+ startInactiveSpan(options: {
13
+ name: string;
14
+ op: string;
15
+ forceTransaction: boolean;
16
+ }): {
17
+ setStatus(status: {
18
+ code: number;
19
+ }): unknown;
20
+ end(): unknown;
21
+ };
22
+ /** Sends queued events, resolving once done or after `timeout` milliseconds. */
23
+ flush(timeout?: number): PromiseLike<unknown>;
24
+ /** Starts a release-health session (absent on runtimes with no sessions). */
25
+ startSession?(): unknown;
26
+ /** Ends the release-health session (absent on runtimes with no sessions). */
27
+ endSession?(): unknown;
28
+ }
29
+ /** Options shared by the Sentry-compatible sinks. */
30
+ interface SentrySinkOptions {
31
+ /** The SDK to call. Defaults to the runtime's own package; pass a fake in a test. */
32
+ sdk?: SentrySdkLike;
33
+ /** Share of transactions to sample, 0 to 1. Defaults to `0.1`. */
34
+ tracesSampleRate?: number;
35
+ /** Milliseconds {@link ErrorSink.flush} waits for pending events. Defaults to `2000`. */
36
+ flushTimeoutMs?: number;
37
+ /** The span `op` for {@link ErrorSink.startSpan}. Defaults to `"holocron.command"`, as in the Node `SentrySink`. */
38
+ spanOp?: string;
39
+ /** Whether {@link ErrorSink.init} starts a JS-level release-health session. Defaults to `true`; React Native tracks sessions natively. */
40
+ sessions?: boolean;
41
+ /** Called with each error the sink swallowed from the SDK. Defaults to ignoring them; it can never throw into the app. */
42
+ onError?: OnError;
43
+ }
44
+ /** Redacts token-shaped strings from an event before it leaves the runtime (Sentry's `beforeSend` and `beforeSendTransaction`). */
45
+ declare function scrubEvent<T>(event: T): T;
46
+ /**
47
+ * An {@link ErrorSink} over any Sentry SDK that has the {@link SentrySdkLike}
48
+ * surface. The runtime classes only choose which package supplies the SDK.
49
+ * Every vendor call is guarded: an SDK that throws is reported to `onError`
50
+ * (when given), never propagated to the application. Errors, transactions,
51
+ * span names and tags are scrubbed of token-shaped strings. It reads no
52
+ * environment; the caller passes the resolved DSN.
53
+ */
54
+ declare class SentryCompatibleSink implements ErrorSink {
55
+ #private;
56
+ /** Builds the sink around `sdk`. */
57
+ constructor(sdk: SentrySdkLike, options?: Omit<SentrySinkOptions, "sdk">);
58
+ /** Initialises the SDK with the DSN, release and environment, starts a session when the runtime has one and applies `ctx.tags`. */
59
+ init(ctx: {
60
+ dsn: string;
61
+ release: string;
62
+ environment: string;
63
+ tags: Record<string, string>;
64
+ }): void;
65
+ /** Tags the scope with the command and starts a span named `name` (both scrubbed); its methods never throw, and do nothing when the SDK could not start one. */
66
+ startSpan(name: string): CommandSpan;
67
+ /** Reports `err`; a throw from the SDK goes to `onError`. */
68
+ captureException(err: unknown): void;
69
+ /** Ends the session, when the runtime has one. */
70
+ endSession(): void;
71
+ /** Waits for pending events for up to the flush timeout; never rejects. */
72
+ flush(): Promise<void>;
73
+ }
74
+ export { scrubEvent as i, SentrySdkLike as n, SentrySinkOptions as r, SentryCompatibleSink as t };
@@ -0,0 +1,72 @@
1
+ import { i as redactObject, r as redact } from "./redact-DCKZnDDa.mjs";
2
+ import { n as safely, r as safelyAsync } from "./safe-DIVuybiy.mjs";
3
+ // src/shared/sentry-compatible-sink.ts
4
+ /** Redacts token-shaped strings from an event before it leaves the runtime (Sentry's `beforeSend` and `beforeSendTransaction`). */
5
+ function scrubEvent(event) {
6
+ return redactObject(event);
7
+ }
8
+ /**
9
+ * An {@link ErrorSink} over any Sentry SDK that has the {@link SentrySdkLike}
10
+ * surface. The runtime classes only choose which package supplies the SDK.
11
+ * Every vendor call is guarded: an SDK that throws is reported to `onError`
12
+ * (when given), never propagated to the application. Errors, transactions,
13
+ * span names and tags are scrubbed of token-shaped strings. It reads no
14
+ * environment; the caller passes the resolved DSN.
15
+ */
16
+ var SentryCompatibleSink = class {
17
+ #sdk;
18
+ #tracesSampleRate;
19
+ #flushTimeoutMs;
20
+ #spanOp;
21
+ #sessions;
22
+ #onError;
23
+ /** Builds the sink around `sdk`. */
24
+ constructor(sdk, options = {}) {
25
+ this.#sdk = sdk;
26
+ this.#tracesSampleRate = options.tracesSampleRate ?? .1;
27
+ this.#flushTimeoutMs = options.flushTimeoutMs ?? 2e3;
28
+ this.#spanOp = options.spanOp ?? "holocron.command";
29
+ this.#sessions = options.sessions ?? true;
30
+ this.#onError = options.onError;
31
+ }
32
+ /** Initialises the SDK with the DSN, release and environment, starts a session when the runtime has one and applies `ctx.tags`. */
33
+ init(ctx) {
34
+ safely(() => this.#sdk.init({
35
+ dsn: ctx.dsn,
36
+ release: ctx.release,
37
+ environment: ctx.environment,
38
+ tracesSampleRate: this.#tracesSampleRate,
39
+ beforeSend: scrubEvent,
40
+ beforeSendTransaction: scrubEvent
41
+ }), this.#onError, "sentry.init");
42
+ if (this.#sessions) safely(() => this.#sdk.startSession?.(), this.#onError, "sentry.startSession");
43
+ for (const [key, value] of Object.entries(ctx.tags)) safely(() => this.#sdk.setTag(redact(key), redact(value)), this.#onError, "sentry.setTag");
44
+ }
45
+ /** Tags the scope with the command and starts a span named `name` (both scrubbed); its methods never throw, and do nothing when the SDK could not start one. */
46
+ startSpan(name) {
47
+ const safeName = redact(name);
48
+ safely(() => this.#sdk.setTag("command", safeName), this.#onError, "sentry.setTag");
49
+ const span = safely(() => this.#sdk.startInactiveSpan({
50
+ name: safeName,
51
+ op: this.#spanOp,
52
+ forceTransaction: true
53
+ }), this.#onError, "sentry.startInactiveSpan");
54
+ return {
55
+ setStatus: (ok) => void safely(() => span?.setStatus({ code: ok ? 1 : 2 }), this.#onError, "sentry.span.setStatus"),
56
+ end: () => void safely(() => span?.end(), this.#onError, "sentry.span.end")
57
+ };
58
+ }
59
+ /** Reports `err`; a throw from the SDK goes to `onError`. */
60
+ captureException(err) {
61
+ safely(() => this.#sdk.captureException(err), this.#onError, "sentry.captureException");
62
+ }
63
+ /** Ends the session, when the runtime has one. */
64
+ endSession() {
65
+ if (this.#sessions) safely(() => this.#sdk.endSession?.(), this.#onError, "sentry.endSession");
66
+ }
67
+ /** Waits for pending events for up to the flush timeout; never rejects. */
68
+ async flush() {
69
+ await safelyAsync(() => this.#sdk.flush(this.#flushTimeoutMs), this.#onError, "sentry.flush");
70
+ }
71
+ };
72
+ export { scrubEvent as n, SentryCompatibleSink as t };
@@ -1,4 +1,4 @@
1
- import { n as CommandSpan, r as ErrorSink } from "./sinks-v2TCQsCp.mjs";
1
+ import { n as CommandSpan, r as ErrorSink } from "./sinks-D5CwDgDL.mjs";
2
2
  import { ErrorEvent, EventHint } from "@sentry/node";
3
3
  /** `beforeSend` — token-shaped strings never leave the process. */
4
4
  declare function scrubError(event: ErrorEvent, _hint: EventHint): ErrorEvent;
@@ -7,7 +7,7 @@ declare class SentrySink implements ErrorSink {
7
7
  init(ctx: {
8
8
  dsn: string;
9
9
  release: string;
10
- environment: "ci" | "local";
10
+ environment: string;
11
11
  tags: Record<string, string>;
12
12
  }): void;
13
13
  startSpan(name: string): CommandSpan;
@@ -15,11 +15,14 @@ interface CommandSpan {
15
15
  }
16
16
  /** Error tracking + performance (Sentry). */
17
17
  interface ErrorSink {
18
- /** Initialises the tracker with the resolved DSN, release, environment and tags applied to every event. */
18
+ /**
19
+ * Initialises the tracker with the resolved DSN, release, environment and tags applied to every event.
20
+ * `environment` is a free name: `"ci"` and `"local"` for a CLI, `"development"`, `"staging"` or `"production"` for an app.
21
+ */
19
22
  init(ctx: {
20
23
  dsn: string;
21
24
  release: string;
22
- environment: "ci" | "local";
25
+ environment: string;
23
26
  tags: Record<string, string>;
24
27
  }): void;
25
28
  /** Start a command span. Returns `undefined` for a no-op sink. */
@@ -47,10 +50,16 @@ interface AnalyticsSink {
47
50
  */
48
51
  declare class NoopErrorSink implements ErrorSink {
49
52
  /** Does nothing. */
50
- init(): void;
51
- startSpan(): CommandSpan | undefined;
53
+ init(_ctx: {
54
+ dsn: string;
55
+ release: string;
56
+ environment: string;
57
+ tags: Record<string, string>;
58
+ }): void;
59
+ /** Returns `undefined`: there is no span. */
60
+ startSpan(_name: string): CommandSpan | undefined;
52
61
  /** Does nothing. */
53
- captureException(): void;
62
+ captureException(_err: unknown): void;
54
63
  /** Does nothing. */
55
64
  endSession(): void;
56
65
  /** Resolves immediately. */
@@ -59,9 +68,9 @@ declare class NoopErrorSink implements ErrorSink {
59
68
  /** @see NoopErrorSink */
60
69
  declare class NoopAnalyticsSink implements AnalyticsSink {
61
70
  /** Does nothing. */
62
- identify(): void;
71
+ identify(_distinctId: string, _props: Record<string, unknown>): void;
63
72
  /** Does nothing. */
64
- capture(): void;
73
+ capture(_distinctId: string, _event: string, _props: Record<string, unknown>): void;
65
74
  /** Resolves immediately. */
66
75
  shutdown(): Promise<void>;
67
76
  }
@@ -6,10 +6,11 @@
6
6
  */
7
7
  var NoopErrorSink = class {
8
8
  /** Does nothing. */
9
- init() {}
10
- startSpan() {}
9
+ init(_ctx) {}
10
+ /** Returns `undefined`: there is no span. */
11
+ startSpan(_name) {}
11
12
  /** Does nothing. */
12
- captureException() {}
13
+ captureException(_err) {}
13
14
  /** Does nothing. */
14
15
  endSession() {}
15
16
  /** Resolves immediately. */
@@ -18,9 +19,9 @@ var NoopErrorSink = class {
18
19
  /** @see NoopErrorSink */
19
20
  var NoopAnalyticsSink = class {
20
21
  /** Does nothing. */
21
- identify() {}
22
+ identify(_distinctId, _props) {}
22
23
  /** Does nothing. */
23
- capture() {}
24
+ capture(_distinctId, _event, _props) {}
24
25
  /** Resolves immediately. */
25
26
  async shutdown() {}
26
27
  };
@@ -1,4 +1,17 @@
1
- import { n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-v2TCQsCp.mjs";
1
+ /**
2
+ * `@theholocron/observability/testing` — vitest-based test doubles for the
3
+ * `/core` interfaces (`Logger`, `ErrorSink`, `AnalyticsSink`). Every consumer
4
+ * that tests code which logs or reports telemetry wants these — hand-rolling
5
+ * the same spy shape per repo is what this subpath replaces.
6
+ *
7
+ * `silenceLogger` swaps `createLogger` for a `NoopLogger` in a vitest setup file.
8
+ *
9
+ * `vitest` is an optional peer dependency: install it alongside this
10
+ * subpath (any repo with a vitest suite already has it as a devDependency).
11
+ * For a non-vitest context that needs a `Logger` but no output, use
12
+ * {@link NoopLogger} from `/core` instead — it records nothing to assert on.
13
+ */
14
+ import { n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
2
15
  import { i as Logger } from "./logger-DC6M2rL_.mjs";
3
16
  import { r as createLogger } from "./create-X1v3LWjb.mjs";
4
17
  import { Mock } from "vitest";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/observability",
3
- "version": "0.7.0-alpha.3",
3
+ "version": "0.7.0-alpha.5",
4
4
  "description": "Logging, error tracking, and analytics — one interface, swappable backends.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -49,6 +49,31 @@
49
49
  "import": "./dist/analytics.mjs",
50
50
  "default": "./dist/analytics.mjs"
51
51
  },
52
+ "./browser/sentry": {
53
+ "types": "./dist/browser-sentry.d.mts",
54
+ "import": "./dist/browser-sentry.mjs",
55
+ "default": "./dist/browser-sentry.mjs"
56
+ },
57
+ "./browser/posthog": {
58
+ "types": "./dist/browser-posthog.d.mts",
59
+ "import": "./dist/browser-posthog.mjs",
60
+ "default": "./dist/browser-posthog.mjs"
61
+ },
62
+ "./edge/sentry": {
63
+ "types": "./dist/edge-sentry.d.mts",
64
+ "import": "./dist/edge-sentry.mjs",
65
+ "default": "./dist/edge-sentry.mjs"
66
+ },
67
+ "./edge/posthog": {
68
+ "types": "./dist/edge-posthog.d.mts",
69
+ "import": "./dist/edge-posthog.mjs",
70
+ "default": "./dist/edge-posthog.mjs"
71
+ },
72
+ "./react-native": {
73
+ "types": "./dist/react-native.d.mts",
74
+ "import": "./dist/react-native.mjs",
75
+ "default": "./dist/react-native.mjs"
76
+ },
52
77
  "./testing": {
53
78
  "types": "./dist/testing.d.mts",
54
79
  "import": "./dist/testing.mjs",
@@ -59,11 +84,13 @@
59
84
  "dist"
60
85
  ],
61
86
  "dependencies": {
62
- "@theholocron/env-utils": "^1.17.0-alpha.3"
87
+ "@theholocron/env-utils": "^1.17.0-alpha.5"
63
88
  },
64
89
  "devDependencies": {
65
90
  "@axiomhq/pino": "^2.0.0",
66
- "@sentry/node": "^11.10.0",
91
+ "@sentry/browser": "^11.12.0",
92
+ "@sentry/node": "^11.12.0",
93
+ "@sentry/vercel-edge": "^11.12.0",
67
94
  "@types/node": "^26",
68
95
  "@vitest/coverage-v8": "^5.1.0",
69
96
  "@vitest/eslint-plugin": "^1.6.28",
@@ -72,39 +99,60 @@
72
99
  "globals": "^17.14.0",
73
100
  "pino": "^10.4.0",
74
101
  "pino-pretty": "^13.2.0",
75
- "posthog-node": "^5.61.2",
76
- "tsdown": "^0.23.5",
102
+ "posthog-js": "^1.447.15",
103
+ "posthog-node": "^5.71.0",
104
+ "tsdown": "^0.23.7",
77
105
  "typescript": "^5.9.3",
78
106
  "vitest": "^5.1.0",
79
- "@theholocron/eslint-config": "^8.9.0-alpha.13",
80
- "@theholocron/tsconfig": "^8.9.0-alpha.13",
81
- "@theholocron/vitest-config": "^8.9.0-alpha.13",
82
- "@theholocron/tsdown-config": "^8.9.0-alpha.13"
107
+ "@theholocron/tsconfig": "^8.9.0-alpha.18",
108
+ "@theholocron/eslint-config": "^8.9.0-alpha.18",
109
+ "@theholocron/tsdown-config": "^8.9.0-alpha.18",
110
+ "@theholocron/vitest-config": "^8.9.0-alpha.18"
83
111
  },
84
112
  "peerDependencies": {
85
113
  "@axiomhq/pino": ">=2",
114
+ "@sentry/browser": ">=10",
86
115
  "@sentry/node": ">=10",
116
+ "@sentry/react-native": ">=7",
117
+ "@sentry/vercel-edge": ">=10",
87
118
  "pino": ">=10",
88
119
  "pino-pretty": ">=13",
120
+ "posthog-js": ">=1",
89
121
  "posthog-node": ">=5",
122
+ "posthog-react-native": ">=4",
90
123
  "vitest": ">=4"
91
124
  },
92
125
  "peerDependenciesMeta": {
93
126
  "@axiomhq/pino": {
94
127
  "optional": true
95
128
  },
129
+ "@sentry/browser": {
130
+ "optional": true
131
+ },
96
132
  "@sentry/node": {
97
133
  "optional": true
98
134
  },
135
+ "@sentry/react-native": {
136
+ "optional": true
137
+ },
138
+ "@sentry/vercel-edge": {
139
+ "optional": true
140
+ },
99
141
  "pino": {
100
142
  "optional": true
101
143
  },
102
144
  "pino-pretty": {
103
145
  "optional": true
104
146
  },
147
+ "posthog-js": {
148
+ "optional": true
149
+ },
105
150
  "posthog-node": {
106
151
  "optional": true
107
152
  },
153
+ "posthog-react-native": {
154
+ "optional": true
155
+ },
108
156
  "vitest": {
109
157
  "optional": true
110
158
  }