@theholocron/observability 0.7.0-alpha.3 → 0.7.0-alpha.4
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 +50 -10
- package/dist/analytics.d.mts +2 -2
- package/dist/analytics.mjs +1 -1
- package/dist/browser-posthog.d.mts +60 -0
- package/dist/browser-posthog.mjs +58 -0
- package/dist/browser-sentry.d.mts +12 -0
- package/dist/browser-sentry.mjs +15 -0
- package/dist/core.d.mts +1 -1
- package/dist/core.mjs +1 -1
- package/dist/edge-posthog.d.mts +60 -0
- package/dist/edge-posthog.mjs +99 -0
- package/dist/edge-sentry.d.mts +15 -0
- package/dist/edge-sentry.mjs +18 -0
- package/dist/errors.d.mts +2 -2
- package/dist/errors.mjs +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/{posthog-sink-D4aRF3P2.d.mts → posthog-sink-CmQmma-o.d.mts} +1 -1
- package/dist/react-native.d.mts +67 -0
- package/dist/react-native.mjs +65 -0
- package/dist/safe-6WuLqOVf.d.mts +7 -0
- package/dist/safe-DIVuybiy.mjs +39 -0
- package/dist/sentry-compatible-sink-D50iTP_8.d.mts +74 -0
- package/dist/sentry-compatible-sink-DrpWebmE.mjs +72 -0
- package/dist/{sentry-sink-C1E3tOl-.d.mts → sentry-sink-C0WWmUBS.d.mts} +2 -2
- package/dist/{sinks-v2TCQsCp.d.mts → sinks-D5CwDgDL.d.mts} +16 -7
- package/dist/{sinks-Dz8Kxoha.mjs → sinks-DX4cyR-X.mjs} +6 -5
- package/dist/testing.d.mts +1 -1
- package/package.json +53 -5
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
|
|
28
|
-
|
|
|
29
|
-
| `@theholocron/observability/core`
|
|
30
|
-
| `@theholocron/observability/logger`
|
|
31
|
-
| `@theholocron/observability/errors`
|
|
32
|
-
| `@theholocron/observability/analytics`
|
|
33
|
-
| `@theholocron/observability/
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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:
|
package/dist/analytics.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as PostHogSink } from "./posthog-sink-
|
|
2
|
-
import { i as NoopAnalyticsSink, t as AnalyticsSink } from "./sinks-
|
|
1
|
+
import { t as PostHogSink } from "./posthog-sink-CmQmma-o.mjs";
|
|
2
|
+
import { i as NoopAnalyticsSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
3
3
|
export { type AnalyticsSink, NoopAnalyticsSink, PostHogSink };
|
package/dist/analytics.mjs
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
|
+
import { t as OnError } from "./safe-6WuLqOVf.mjs";
|
|
3
|
+
/** The slice of a `posthog-js` instance that {@link PostHogBrowserSink} calls. */
|
|
4
|
+
interface PostHogBrowserClient {
|
|
5
|
+
/** Starts the client with the project key and options. */
|
|
6
|
+
init(key: string, config: Record<string, unknown>): unknown;
|
|
7
|
+
/** The id of the person the client records for now. */
|
|
8
|
+
get_distinct_id(): string;
|
|
9
|
+
/** Sets the person and their properties. */
|
|
10
|
+
identify(distinctId: string, props?: Record<string, unknown>): unknown;
|
|
11
|
+
/** Records an event for the current person. */
|
|
12
|
+
capture(event: string, props?: Record<string, unknown>): unknown;
|
|
13
|
+
/** Flushes the queue and tears the client down. */
|
|
14
|
+
shutdown(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link PostHogBrowserSink}. */
|
|
17
|
+
interface PostHogBrowserSinkConfig {
|
|
18
|
+
/** The project API key. */
|
|
19
|
+
key: string;
|
|
20
|
+
/** The PostHog host (`api_host`). */
|
|
21
|
+
host: string;
|
|
22
|
+
/** The client to call. Defaults to the `posthog-js` singleton; pass a fake in a test. */
|
|
23
|
+
client?: PostHogBrowserClient;
|
|
24
|
+
/**
|
|
25
|
+
* `posthog-js` options merged over the privacy-safe defaults (`autocapture`,
|
|
26
|
+
* `capture_pageview`, `capture_pageleave` and `disable_session_recording`
|
|
27
|
+
* switched off, `person_profiles: "identified_only"`). Set one here to opt in.
|
|
28
|
+
*/
|
|
29
|
+
options?: Record<string, unknown>;
|
|
30
|
+
/** Called with each error the sink swallowed from the SDK. Defaults to ignoring them; it can never throw into the app. */
|
|
31
|
+
onError?: OnError;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* `PostHogBrowserSink` is the `posthog-js` call site, an `AnalyticsSink` for a
|
|
35
|
+
* web page or a React app. The constructor starts the client with the
|
|
36
|
+
* resolved key and host and the privacy-safe defaults of
|
|
37
|
+
* {@link PostHogBrowserSinkConfig.options}: nothing is captured except what
|
|
38
|
+
* the app sends through `capture()`.
|
|
39
|
+
*
|
|
40
|
+
* A browser has one current person. The rule for `capture(distinctId, ...)`:
|
|
41
|
+
* when `distinctId` is not the client's current id (`get_distinct_id()`), the
|
|
42
|
+
* sink 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
|
|
46
|
+
* call is guarded and never throws; errors go to `onError`. `posthog-js` is
|
|
47
|
+
* an optional peer dependency.
|
|
48
|
+
*/
|
|
49
|
+
export declare class PostHogBrowserSink implements AnalyticsSink {
|
|
50
|
+
#private;
|
|
51
|
+
/** Starts the client with `config.key`, `config.host` and the defaults. */
|
|
52
|
+
constructor(config: PostHogBrowserSinkConfig);
|
|
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 shuts the client down; never rejects. */
|
|
58
|
+
shutdown(): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
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,12 @@
|
|
|
1
|
+
import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
|
|
2
|
+
import { t as OnError } from "./safe-6WuLqOVf.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* `SentryBrowserSink` is the `@sentry/browser` call site, an `ErrorSink` for a
|
|
5
|
+
* web page or a React app (React Native has its own subpath). `@sentry/browser`
|
|
6
|
+
* is an optional peer dependency. Pass `options.sdk` to inject a fake.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SentryBrowserSink extends SentryCompatibleSink {
|
|
9
|
+
/** Builds the sink over `@sentry/browser`, or over `options.sdk` when given. */
|
|
10
|
+
constructor(options?: SentrySinkOptions);
|
|
11
|
+
}
|
|
12
|
+
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,4 @@
|
|
|
1
|
-
import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-
|
|
1
|
+
import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
2
|
import { a as NoopLogger, i as Logger, n as LogEnv, r as LogLevel, t as LOG_LEVELS } from "./logger-DC6M2rL_.mjs";
|
|
3
3
|
import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./core-DDpFbotv.mjs";
|
|
4
4
|
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-
|
|
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,60 @@
|
|
|
1
|
+
import { t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
|
+
import { t as OnError } from "./safe-6WuLqOVf.mjs";
|
|
3
|
+
/** The part of a `fetch` response the sink reads. */
|
|
4
|
+
interface FetchResponseLike {
|
|
5
|
+
/** `true` for a 2xx status. */
|
|
6
|
+
ok?: boolean;
|
|
7
|
+
/** The HTTP status. */
|
|
8
|
+
status?: number;
|
|
9
|
+
}
|
|
10
|
+
/** The `fetch` shape the sink needs; the global `fetch` of every edge runtime fits. */
|
|
11
|
+
type FetchLike = (url: string, init: {
|
|
12
|
+
method: string;
|
|
13
|
+
headers: Record<string, string>;
|
|
14
|
+
body: string;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}) => Promise<FetchResponseLike | unknown>;
|
|
17
|
+
/** Options of {@link PostHogFetchSink}. */
|
|
18
|
+
interface PostHogFetchSinkConfig {
|
|
19
|
+
/** The project API key. */
|
|
20
|
+
key: string;
|
|
21
|
+
/** The PostHog host, such as `https://us.i.posthog.com`. */
|
|
22
|
+
host: string;
|
|
23
|
+
/** The `fetch` to call. Defaults to the global `fetch`; pass a fake in a test. */
|
|
24
|
+
fetch?: FetchLike;
|
|
25
|
+
/** Registers a pending request with the runtime (`ctx.waitUntil` in a Cloudflare Worker, `waitUntil` in a Vercel edge function). */
|
|
26
|
+
waitUntil?: (pending: Promise<unknown>) => void;
|
|
27
|
+
/** Milliseconds before a request is aborted. Defaults to `5000`. */
|
|
28
|
+
requestTimeoutMs?: number;
|
|
29
|
+
/** 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. */
|
|
30
|
+
onError?: OnError;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `PostHogFetchSink` is an `AnalyticsSink` for edge and worker runtimes that
|
|
34
|
+
* talks to PostHog's HTTP capture endpoint with `fetch`. It imports no SDK and
|
|
35
|
+
* no Node built-in, so it has no peer dependency. Each call sends one request
|
|
36
|
+
* at once (no batching: an isolate may be frozen right after the response).
|
|
37
|
+
*
|
|
38
|
+
* - Properties, including person properties, are scrubbed of token-shaped strings.
|
|
39
|
+
* - A request is aborted after `requestTimeoutMs`. A non-2xx response or a
|
|
40
|
+
* rejected request goes to `onError`. There is no retry: the event is dropped.
|
|
41
|
+
* - `shutdown(timeoutMs)` stops accepting events (later calls go to `onError`
|
|
42
|
+
* and are dropped), then waits for every accepted request, at most `timeoutMs`.
|
|
43
|
+
* - Every call is guarded and never throws.
|
|
44
|
+
*/
|
|
45
|
+
export declare class PostHogFetchSink implements AnalyticsSink {
|
|
46
|
+
#private;
|
|
47
|
+
/** Builds the sink for `config.key` and `config.host`. */
|
|
48
|
+
constructor(config: PostHogFetchSinkConfig);
|
|
49
|
+
/**
|
|
50
|
+
* Sends a `$identify` event that sets `props` on the person `distinctId`.
|
|
51
|
+
* Pass `previousId`, the anonymous id the visitor had before, to merge their
|
|
52
|
+
* earlier events into the person (sent as `$anon_distinct_id`).
|
|
53
|
+
*/
|
|
54
|
+
identify(distinctId: string, props: Record<string, unknown>, previousId?: string): void;
|
|
55
|
+
/** Sends `event` for `distinctId` with `props`. */
|
|
56
|
+
capture(distinctId: string, event: string, props: Record<string, unknown>): void;
|
|
57
|
+
/** Stops accepting events and waits for the accepted requests, for at most `timeoutMs` (default 5000); never rejects. */
|
|
58
|
+
shutdown(timeoutMs?: number): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
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,15 @@
|
|
|
1
|
+
import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
|
|
2
|
+
import { t as OnError } from "./safe-6WuLqOVf.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* `SentryEdgeSink` is the `@sentry/vercel-edge` call site, an `ErrorSink` for an
|
|
5
|
+
* edge or worker runtime (WinterCG: no `window`, no Node built-ins). The edge
|
|
6
|
+
* SDK has no release-health sessions, so `endSession()` does nothing.
|
|
7
|
+
* `@sentry/vercel-edge` is an optional peer dependency. Pass `options.sdk` to
|
|
8
|
+
* inject a fake. In a Cloudflare Worker, call `flush()` inside
|
|
9
|
+
* `ctx.waitUntil()` so the events leave before the isolate is frozen.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SentryEdgeSink extends SentryCompatibleSink {
|
|
12
|
+
/** Builds the sink over `@sentry/vercel-edge`, or over `options.sdk` when given. */
|
|
13
|
+
constructor(options?: SentrySinkOptions);
|
|
14
|
+
}
|
|
15
|
+
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,3 @@
|
|
|
1
|
-
import { a as NoopErrorSink, n as CommandSpan, r as ErrorSink } from "./sinks-
|
|
2
|
-
import { n as scrubError, t as SentrySink } from "./sentry-sink-
|
|
1
|
+
import { a as NoopErrorSink, n as CommandSpan, r as ErrorSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
|
+
import { n as scrubError, t as SentrySink } from "./sentry-sink-C0WWmUBS.mjs";
|
|
3
3
|
export { type CommandSpan, type ErrorSink, NoopErrorSink, SentrySink, scrubError };
|
package/dist/errors.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-
|
|
1
|
+
import { a as NoopErrorSink, i as NoopAnalyticsSink, n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
2
|
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
3
|
import { a as NoopLogger, i as Logger, n as LogEnv, r as LogLevel, t as LOG_LEVELS } from "./logger-DC6M2rL_.mjs";
|
|
4
4
|
import { a as redactOptions, i as redactObject, n as REDACT_CENSOR, r as redact, t as REDACTED_PATHS } from "./core-DDpFbotv.mjs";
|
|
5
5
|
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-
|
|
7
|
-
import { t as PostHogSink } from "./posthog-sink-
|
|
6
|
+
import { n as scrubError, t as SentrySink } from "./sentry-sink-C0WWmUBS.mjs";
|
|
7
|
+
import { t as PostHogSink } from "./posthog-sink-CmQmma-o.mjs";
|
|
8
8
|
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-
|
|
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";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { a as NoopErrorSink, i as NoopAnalyticsSink, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
|
+
import { t as OnError } from "./safe-6WuLqOVf.mjs";
|
|
3
|
+
import { i as scrubEvent, n as SentrySdkLike, r as SentrySinkOptions, t as SentryCompatibleSink } from "./sentry-compatible-sink-D50iTP_8.mjs";
|
|
4
|
+
/** The slice of a `posthog-react-native` client that {@link PostHogReactNativeSink} calls. */
|
|
5
|
+
interface PostHogReactNativeClient {
|
|
6
|
+
/** The id of the person the client records for now. */
|
|
7
|
+
getDistinctId(): string;
|
|
8
|
+
/** Sets the person and their properties. */
|
|
9
|
+
identify(distinctId: string, props?: Record<string, unknown>): unknown;
|
|
10
|
+
/** Records an event for the current person. */
|
|
11
|
+
capture(event: string, props?: Record<string, unknown>): unknown;
|
|
12
|
+
/** Flushes the queue and stops the client. */
|
|
13
|
+
shutdown(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/** Options of {@link PostHogReactNativeSink}. */
|
|
16
|
+
interface PostHogReactNativeSinkConfig {
|
|
17
|
+
/** A `new PostHog(key, { host })` from `posthog-react-native`, built by the app. */
|
|
18
|
+
client: PostHogReactNativeClient;
|
|
19
|
+
/** Called with each error the sink swallowed from the SDK. Defaults to ignoring them; it can never throw into the app. */
|
|
20
|
+
onError?: OnError;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `PostHogReactNativeSink` is the `AnalyticsSink` for a React Native app over
|
|
24
|
+
* `posthog-react-native`. The app builds the client (`new PostHog(key, { host })`)
|
|
25
|
+
* and passes it in: the SDK needs the `react-native` runtime to load, so this
|
|
26
|
+
* module never imports it itself.
|
|
27
|
+
*
|
|
28
|
+
* The client has one current person. The rule for `capture(distinctId, ...)`:
|
|
29
|
+
* when `distinctId` is not the client's current id (`getDistinctId()`), the sink
|
|
30
|
+
* calls `identify(distinctId)` first, so the event is never attributed to
|
|
31
|
+
* another person. To record for an anonymous visitor, pass the client's own id.
|
|
32
|
+
*
|
|
33
|
+
* Event and person properties are scrubbed of token-shaped strings. Every call
|
|
34
|
+
* is guarded and never throws; errors go to `onError`. `posthog-react-native`
|
|
35
|
+
* is an optional peer dependency.
|
|
36
|
+
*/
|
|
37
|
+
export declare class PostHogReactNativeSink implements AnalyticsSink {
|
|
38
|
+
#private;
|
|
39
|
+
/** Wraps `config.client`. */
|
|
40
|
+
constructor(config: PostHogReactNativeSinkConfig);
|
|
41
|
+
/** Sets the current person to `distinctId` with `props` (scrubbed). */
|
|
42
|
+
identify(distinctId: string, props: Record<string, unknown>): void;
|
|
43
|
+
/** Records `event` with `props` (scrubbed) for `distinctId`, identifying that person first when the client is on another one. */
|
|
44
|
+
capture(distinctId: string, event: string, props: Record<string, unknown>): void;
|
|
45
|
+
/** Flushes queued events and stops the client; never rejects. */
|
|
46
|
+
shutdown(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
/** Options of {@link SentryReactNativeSink}. */
|
|
49
|
+
interface SentryReactNativeSinkOptions extends Omit<SentrySinkOptions, "sdk"> {
|
|
50
|
+
/** The `@sentry/react-native` namespace, as the app imports it (`import * as Sentry from "@sentry/react-native"`). */
|
|
51
|
+
sdk: SentrySdkLike;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* `SentryReactNativeSink` is the `ErrorSink` for a React Native app over
|
|
55
|
+
* `@sentry/react-native`. The app imports the SDK and passes it in as
|
|
56
|
+
* `options.sdk`: the SDK needs the `react-native` runtime to load, so this
|
|
57
|
+
* module never imports it itself and stays loadable (and testable) anywhere.
|
|
58
|
+
* `sessions` defaults to `false` here, because the native layer tracks
|
|
59
|
+
* release-health sessions. `@sentry/react-native` is an optional peer
|
|
60
|
+
* dependency. Pair it with `Sentry.wrap(App)` for the native crash and
|
|
61
|
+
* navigation integrations, which sit outside this interface.
|
|
62
|
+
*/
|
|
63
|
+
export declare class SentryReactNativeSink extends SentryCompatibleSink {
|
|
64
|
+
/** Builds the sink over `options.sdk`. */
|
|
65
|
+
constructor(options: SentryReactNativeSinkOptions);
|
|
66
|
+
}
|
|
67
|
+
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-
|
|
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:
|
|
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
|
-
/**
|
|
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:
|
|
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(
|
|
51
|
-
|
|
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
|
-
|
|
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
|
};
|
package/dist/testing.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-
|
|
1
|
+
import { n as CommandSpan, r as ErrorSink, t as AnalyticsSink } from "./sinks-D5CwDgDL.mjs";
|
|
2
2
|
import { i as Logger } from "./logger-DC6M2rL_.mjs";
|
|
3
3
|
import { r as createLogger } from "./create-X1v3LWjb.mjs";
|
|
4
4
|
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
|
+
"version": "0.7.0-alpha.4",
|
|
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",
|
|
@@ -63,7 +88,9 @@
|
|
|
63
88
|
},
|
|
64
89
|
"devDependencies": {
|
|
65
90
|
"@axiomhq/pino": "^2.0.0",
|
|
91
|
+
"@sentry/browser": "^11.10.0",
|
|
66
92
|
"@sentry/node": "^11.10.0",
|
|
93
|
+
"@sentry/vercel-edge": "^11.10.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",
|
|
102
|
+
"posthog-js": "^1.447.7",
|
|
75
103
|
"posthog-node": "^5.61.2",
|
|
76
104
|
"tsdown": "^0.23.5",
|
|
77
105
|
"typescript": "^5.9.3",
|
|
78
106
|
"vitest": "^5.1.0",
|
|
79
|
-
"@theholocron/eslint-config": "^8.9.0-alpha.
|
|
80
|
-
"@theholocron/tsconfig": "^8.9.0-alpha.
|
|
81
|
-
"@theholocron/vitest-config": "^8.9.0-alpha.
|
|
82
|
-
"@theholocron/tsdown-config": "^8.9.0-alpha.
|
|
107
|
+
"@theholocron/eslint-config": "^8.9.0-alpha.16",
|
|
108
|
+
"@theholocron/tsconfig": "^8.9.0-alpha.16",
|
|
109
|
+
"@theholocron/vitest-config": "^8.9.0-alpha.16",
|
|
110
|
+
"@theholocron/tsdown-config": "^8.9.0-alpha.16"
|
|
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
|
}
|