anomalisa 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +138 -2
- package/esm/mod.d.ts +57 -0
- package/esm/mod.js +475 -0
- package/esm/package.json +3 -0
- package/esm/src/retry.d.ts +1 -0
- package/esm/src/retry.js +4 -0
- package/esm/src/types.d.ts +13 -0
- package/esm/src/types.js +1 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Uri Valevski
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,139 @@
|
|
|
1
|
-
#
|
|
1
|
+
# anomalisa
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
You're shipping fast. Multiple projects, lots of moving parts. Something breaks
|
|
4
|
+
or spikes at 2am and you find out the next morning from a user complaint.
|
|
5
|
+
|
|
6
|
+
Anomalisa watches your event streams and emails you when something weird
|
|
7
|
+
happens. A sudden spike in signups, a drop in purchases, one user hammering your
|
|
8
|
+
API 100x more than normal. You send events, it learns what's normal, and it
|
|
9
|
+
tells you when things aren't.
|
|
10
|
+
|
|
11
|
+
No dashboards to check. No thresholds to configure. Just email alerts when the
|
|
12
|
+
math says something is off.
|
|
13
|
+
|
|
14
|
+
**Try it now at [anomalisa.uriv.me](https://anomalisa.uriv.me)**
|
|
15
|
+
|
|
16
|
+
This is also fully open source. Run it yourself, fork it, rip it apart.
|
|
17
|
+
|
|
18
|
+
## how it works
|
|
19
|
+
|
|
20
|
+
You send events with a project token and user id. Anomalisa counts them in
|
|
21
|
+
hourly buckets (`YYYY-MM-DDTHH`) and maintains a decaying Welford's online
|
|
22
|
+
algorithm baseline (`statsDecay = 0.98`, `minDataPoints = 3`,
|
|
23
|
+
`minColdStartCount = 5`) per event.
|
|
24
|
+
|
|
25
|
+
It tracks four metrics independently:
|
|
26
|
+
|
|
27
|
+
- **`totalCount`**: Uses a two-sided Poisson test (`p < 1e-3`, encoding
|
|
28
|
+
`-log10(p)` in `zScore`) when variance is near-Poisson, falling back to `3σ`
|
|
29
|
+
(`zScoreThreshold = 3`) when overdispersed (`stdDev > max(1, sqrt(mean))`).
|
|
30
|
+
Includes cold-start burst detection (`count >= 5` when `n < 3`), live
|
|
31
|
+
mid-hour spike checks, and per-hour-of-day (`byHour:<HH>`) seasonality.
|
|
32
|
+
- **`percentageSpike`**: Fires when an hourly count is `>100%` above the
|
|
33
|
+
rolling mean (`diff >= 3`, `mean >= 10`, `z >= 2.5`) and is not explained by
|
|
34
|
+
`byHour:<HH>` seasonality.
|
|
35
|
+
- **`percentageDrop`**: Fires on a single-hour `>50%` drop (`mean >= 30`,
|
|
36
|
+
`diff >= 3`, `z >= 1.05`) or a rolling 2h/6h/24h drop against the prior 3-6
|
|
37
|
+
days (`z >= 2.5`; skipped for error/failure events).
|
|
38
|
+
- **`userSpike`**: Tracks `max_user_counts` (the highest single-user count per
|
|
39
|
+
hourly bucket) into a per-event `"perUser"` Welford baseline (`0.98` decay),
|
|
40
|
+
and alerts when any single user's current-hour count exceeds it by `> 3.5σ`
|
|
41
|
+
(`minDataPoints = 3`, `count >= 3`, and `count >= 5` if `0 < mean < 1`).
|
|
42
|
+
|
|
43
|
+
No configuration needed. It learns from the data, applies a 48-hour directional
|
|
44
|
+
cooldown to prevent alert spam unless an anomaly escalates by >2x, and stays
|
|
45
|
+
quiet during the initial cold start (`minDataPoints = 3`) unless a rare burst
|
|
46
|
+
(`count >= 5`) occurs.
|
|
47
|
+
|
|
48
|
+
## quickstart
|
|
49
|
+
|
|
50
|
+
Install with npm:
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
npm i anomalisa
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or with Deno:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
deno add npm:anomalisa
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Send events from your app:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { sendEvent } from "anomalisa";
|
|
66
|
+
|
|
67
|
+
await sendEvent({
|
|
68
|
+
token: "your-project-token",
|
|
69
|
+
userId: "user-123",
|
|
70
|
+
eventName: "purchase",
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Get your token by signing up at [anomalisa.uriv.me](https://anomalisa.uriv.me),
|
|
75
|
+
creating a project, and copying the token.
|
|
76
|
+
|
|
77
|
+
Check detected anomalies:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { getAnomalies } from "anomalisa";
|
|
81
|
+
|
|
82
|
+
const { anomalies } = await getAnomalies({ token: "your-project-token" });
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Each anomaly looks like:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"projectId": "abc-123",
|
|
90
|
+
"eventName": "purchase",
|
|
91
|
+
"bucket": "2026-02-23T14",
|
|
92
|
+
"expected": 48.5,
|
|
93
|
+
"actual": 3,
|
|
94
|
+
"zScore": 4.21,
|
|
95
|
+
"metric": "totalCount",
|
|
96
|
+
"detectedAt": "2026-02-23T15:00:01.234Z"
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`metric` can be `"totalCount"`, `"percentageSpike"`, `"percentageDrop"`, or
|
|
101
|
+
`"userSpike"`. For per-user spikes, `metric` is `"userSpike"` and `userId` is
|
|
102
|
+
included.
|
|
103
|
+
|
|
104
|
+
## self-hosting
|
|
105
|
+
|
|
106
|
+
Clone the repo and create a `.env`:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
TURSO_DATABASE_URL=your-turso-database-url
|
|
110
|
+
TURSO_AUTH_TOKEN=your-turso-auth-token
|
|
111
|
+
INSTANTDB_APP_ID=your-instantdb-app-id
|
|
112
|
+
INSTANTDB_ADMIN_TOKEN=your-instantdb-admin-token
|
|
113
|
+
AGENTMAIL_API_KEY=your-agentmail-api-key
|
|
114
|
+
# Optional: enables automatic GitHub issue triage via @uriv/chaff
|
|
115
|
+
GEMINI_API_KEY=your-gemini-api-key
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
You'll need a [Turso](https://turso.tech) database for event counts, Welford
|
|
119
|
+
baselines, and anomalies, an [InstantDB](https://instantdb.com) app for
|
|
120
|
+
user/project management, and an [AgentMail](https://theagentmail.net) account
|
|
121
|
+
for email alerts (`GEMINI_API_KEY` is optional for automatic GitHub issue
|
|
122
|
+
triage).
|
|
123
|
+
|
|
124
|
+
Push the schema and permissions:
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
npx instant-cli push schema --app $INSTANTDB_APP_ID --token $INSTANTDB_ADMIN_TOKEN
|
|
128
|
+
npx instant-cli push perms --app $INSTANTDB_APP_ID --token $INSTANTDB_ADMIN_TOKEN
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Run locally:
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
deno run --allow-net --allow-env --allow-read src/server.ts
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## license
|
|
138
|
+
|
|
139
|
+
MIT
|
package/esm/mod.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Anomaly, Metric } from "./src/types.js";
|
|
2
|
+
export type SendEventPayload = {
|
|
3
|
+
token: string;
|
|
4
|
+
userId?: string;
|
|
5
|
+
eventName: string;
|
|
6
|
+
};
|
|
7
|
+
export type { Anomaly, Metric };
|
|
8
|
+
export declare const sendEvent: (payload: SendEventPayload) => Promise<Record<string, never>>;
|
|
9
|
+
export declare const getAnomalies: (payload: {
|
|
10
|
+
token: string;
|
|
11
|
+
}) => Promise<{
|
|
12
|
+
anomalies: Anomaly[];
|
|
13
|
+
}>;
|
|
14
|
+
export declare const getEventCounts: (payload: {
|
|
15
|
+
token: string;
|
|
16
|
+
}) => Promise<{
|
|
17
|
+
events: Record<string, {
|
|
18
|
+
bucket: string;
|
|
19
|
+
count: number;
|
|
20
|
+
}[]>;
|
|
21
|
+
}>;
|
|
22
|
+
export declare const renderErrorEvent = "client_render_error";
|
|
23
|
+
export declare const uncaughtErrorEvent = "client_error";
|
|
24
|
+
export declare const unhandledRejectionEvent = "unhandled_rejection";
|
|
25
|
+
type EventTargetLike = {
|
|
26
|
+
addEventListener: (type: string, listener: (event: Event) => void, options?: boolean | AddEventListenerOptions) => void;
|
|
27
|
+
};
|
|
28
|
+
export declare const isBotUserAgent: (userAgent: string) => boolean;
|
|
29
|
+
export declare const messageOf: (reason: unknown) => string;
|
|
30
|
+
export declare const isBrowserExtensionError: (reason: unknown) => boolean;
|
|
31
|
+
export declare const isExtensionHostObjectRejection: (reason: unknown) => boolean;
|
|
32
|
+
export declare const isMediaOrNetworkAbortError: (reason: unknown) => boolean;
|
|
33
|
+
export declare const isResizeObserverError: (reason: unknown) => boolean;
|
|
34
|
+
export declare const isNullRejection: (reason: unknown) => boolean;
|
|
35
|
+
export declare const isNextSiblingNullError: (reason: unknown) => boolean;
|
|
36
|
+
export declare const isBrowserTranslationError: (reason: unknown) => boolean;
|
|
37
|
+
export declare const isEventRejection: (reason: unknown) => boolean;
|
|
38
|
+
export declare const isScriptError: (event: unknown) => boolean;
|
|
39
|
+
export declare const isNextSiblingNullErrorEvent: (event: unknown) => boolean;
|
|
40
|
+
export declare const isBrowserExtensionErrorEvent: (event: unknown) => boolean;
|
|
41
|
+
export declare const isResizeObserverErrorEvent: (event: unknown) => boolean;
|
|
42
|
+
export declare const isMediaOrNetworkAbortErrorEvent: (event: unknown) => boolean;
|
|
43
|
+
export declare const isBrowserTranslationErrorEvent: (event: unknown) => boolean;
|
|
44
|
+
export declare const isIgnorable: (reason: unknown) => boolean;
|
|
45
|
+
export declare const errorEventNames: (baseEvent: string) => (reason: unknown) => string[];
|
|
46
|
+
export declare const isIgnoredRejection: (reason: unknown) => boolean;
|
|
47
|
+
export declare const isIgnoredErrorEvent: (event: unknown) => boolean;
|
|
48
|
+
export declare const suppressClientErrorEvent: (event: unknown) => boolean;
|
|
49
|
+
export declare const suppressClientRejectionEvent: (event: unknown) => boolean;
|
|
50
|
+
export declare const suppressEventRejection: (event: unknown) => boolean;
|
|
51
|
+
export declare const suppressScriptError: (event: unknown) => boolean;
|
|
52
|
+
export declare const installClientErrorSuppression: (target: EventTargetLike) => void;
|
|
53
|
+
export declare const installEventRejectionSuppression: (target: EventTargetLike) => void;
|
|
54
|
+
export declare const captureClientErrors: ({ token, userId }: {
|
|
55
|
+
token: string;
|
|
56
|
+
userId?: string;
|
|
57
|
+
}) => (eventName: string) => void;
|
package/esm/mod.js
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { withRetry } from "./src/retry.js";
|
|
2
|
+
const serverUrl = "https://anomalisa.uriv.me";
|
|
3
|
+
const postJson = async (params) => {
|
|
4
|
+
const response = await fetch(serverUrl, {
|
|
5
|
+
method: "POST",
|
|
6
|
+
headers: { "Content-Type": "application/json" },
|
|
7
|
+
body: JSON.stringify(params),
|
|
8
|
+
});
|
|
9
|
+
return response.status === 200
|
|
10
|
+
? response.json()
|
|
11
|
+
: Promise.reject(new Error(`HTTP ${response.status}: ${await response.text()}`));
|
|
12
|
+
};
|
|
13
|
+
const client = (params) => withRetry(2, 500, (p) => postJson(p))(params);
|
|
14
|
+
export const sendEvent = (payload) => client({ endpoint: "sendEvent", payload });
|
|
15
|
+
export const getAnomalies = (payload) => client({ endpoint: "getAnomalies", payload });
|
|
16
|
+
export const getEventCounts = (payload) => client({ endpoint: "getEventCounts", payload });
|
|
17
|
+
export const renderErrorEvent = "client_render_error";
|
|
18
|
+
export const uncaughtErrorEvent = "client_error";
|
|
19
|
+
export const unhandledRejectionEvent = "unhandled_rejection";
|
|
20
|
+
const extensionPrefixes = [
|
|
21
|
+
"chrome-extension://",
|
|
22
|
+
"moz-extension://",
|
|
23
|
+
"safari-extension://",
|
|
24
|
+
"safari-web-extension://",
|
|
25
|
+
"ms-browser-extension://",
|
|
26
|
+
"extension://",
|
|
27
|
+
"webkit-masked-url://",
|
|
28
|
+
"user-script:",
|
|
29
|
+
];
|
|
30
|
+
const extensionMessages = [
|
|
31
|
+
"Object Not Found Matching Id",
|
|
32
|
+
"The message port closed before a response was received",
|
|
33
|
+
"message channel closed before a response was received",
|
|
34
|
+
"Could not establish connection. Receiving end does not exist",
|
|
35
|
+
"Extension context invalidated",
|
|
36
|
+
"Invalid call to runtime.sendMessage()",
|
|
37
|
+
"No Listener: tabs:",
|
|
38
|
+
"Can't find variable: EmptyRanges",
|
|
39
|
+
'Policy with name "default" already exists',
|
|
40
|
+
"The provider is disconnected from all chains",
|
|
41
|
+
"Failed to connect to MetaMask",
|
|
42
|
+
"MetaMask",
|
|
43
|
+
"Cannot redefine property: web3",
|
|
44
|
+
"xbrowser",
|
|
45
|
+
"reading 'M_ID'",
|
|
46
|
+
"window.webkit.messageHandlers",
|
|
47
|
+
"sendDataToNative",
|
|
48
|
+
"Java object is gone",
|
|
49
|
+
"Java exception was raised",
|
|
50
|
+
"Cannot read properties of undefined (reading 'buffer')",
|
|
51
|
+
"_META.IABInnerFrame",
|
|
52
|
+
"window.ethereum",
|
|
53
|
+
"User rejected the request",
|
|
54
|
+
];
|
|
55
|
+
const abortMessages = [
|
|
56
|
+
"The play() request was interrupted",
|
|
57
|
+
"play() failed because the user didn't interact",
|
|
58
|
+
"The operation was aborted",
|
|
59
|
+
"Fetch is aborted",
|
|
60
|
+
"The user aborted a request",
|
|
61
|
+
"AbortError",
|
|
62
|
+
"Failed to fetch",
|
|
63
|
+
"Load failed",
|
|
64
|
+
"NetworkError",
|
|
65
|
+
"Network error",
|
|
66
|
+
"signal is aborted",
|
|
67
|
+
"aborted without reason",
|
|
68
|
+
"Operation timed out",
|
|
69
|
+
"transaction timed out",
|
|
70
|
+
"Transaction timed out due to inactivity",
|
|
71
|
+
"Query timed out",
|
|
72
|
+
"The transaction was aborted",
|
|
73
|
+
"Database deleted by request of the user",
|
|
74
|
+
"Connection is closing because of",
|
|
75
|
+
"without an in-progress transaction",
|
|
76
|
+
"createObjectStore",
|
|
77
|
+
"objectStoreNames",
|
|
78
|
+
"Can't find variable: indexedDB",
|
|
79
|
+
"QuotaExceededError",
|
|
80
|
+
"JSON Parse error: Unterminated string",
|
|
81
|
+
"Unterminated string in JSON",
|
|
82
|
+
"Unexpected end of JSON input",
|
|
83
|
+
"JSON.parse: unterminated string",
|
|
84
|
+
"JSON.parse: end of data",
|
|
85
|
+
"Failed to read the 'localStorage' property",
|
|
86
|
+
"reading 'getItem'",
|
|
87
|
+
"reading 'setItem'",
|
|
88
|
+
"reading 'removeItem'",
|
|
89
|
+
"reading 'clear'",
|
|
90
|
+
"Access is denied for this document",
|
|
91
|
+
"SecurityError",
|
|
92
|
+
"Record not found: app-user-magic-code",
|
|
93
|
+
"ServiceWorkerContainer",
|
|
94
|
+
"Failed to register a ServiceWorker",
|
|
95
|
+
"service-worker.js",
|
|
96
|
+
"Please use the 'new' operator, this DOM object constructor cannot be called as a function",
|
|
97
|
+
];
|
|
98
|
+
const ignoredMessageFragments = [
|
|
99
|
+
"window.webkit.messageHandlers",
|
|
100
|
+
"sendDataToNative",
|
|
101
|
+
"Script error.",
|
|
102
|
+
"ResizeObserver loop",
|
|
103
|
+
"Database deleted by request of the user",
|
|
104
|
+
"Connection is closing because of",
|
|
105
|
+
"IO error",
|
|
106
|
+
"Unknown rejection",
|
|
107
|
+
"Unknown error",
|
|
108
|
+
"Not found",
|
|
109
|
+
"AbortError",
|
|
110
|
+
"The operation was aborted",
|
|
111
|
+
"Fetch is aborted",
|
|
112
|
+
"Java object is gone",
|
|
113
|
+
"Object Not Found Matching Id",
|
|
114
|
+
"Failed to read the 'localStorage' property",
|
|
115
|
+
"reading 'getItem'",
|
|
116
|
+
"reading 'setItem'",
|
|
117
|
+
"reading 'removeItem'",
|
|
118
|
+
"reading 'clear'",
|
|
119
|
+
"localStorage",
|
|
120
|
+
"Access is denied for this document",
|
|
121
|
+
"SecurityError",
|
|
122
|
+
"without an in-progress transaction",
|
|
123
|
+
"Failed to fetch",
|
|
124
|
+
"Load failed",
|
|
125
|
+
"NetworkError",
|
|
126
|
+
"Network error",
|
|
127
|
+
"Failed to connect to MetaMask",
|
|
128
|
+
"MetaMask",
|
|
129
|
+
"web3",
|
|
130
|
+
"The provider is disconnected from all chains",
|
|
131
|
+
"xbrowser",
|
|
132
|
+
"reading 'M_ID'",
|
|
133
|
+
"M_ID",
|
|
134
|
+
"Java exception was raised",
|
|
135
|
+
"The transaction was aborted",
|
|
136
|
+
"Transaction timed out due to inactivity",
|
|
137
|
+
"Operation timed out",
|
|
138
|
+
"transaction timed out",
|
|
139
|
+
"Query timed out",
|
|
140
|
+
"Cannot read properties of undefined (reading 'buffer')",
|
|
141
|
+
"Record not found: app-user-magic-code",
|
|
142
|
+
"createObjectStore",
|
|
143
|
+
"objectStoreNames",
|
|
144
|
+
"Can't find variable: indexedDB",
|
|
145
|
+
"QuotaExceededError",
|
|
146
|
+
"JSON Parse error: Unterminated string",
|
|
147
|
+
"Unterminated string in JSON",
|
|
148
|
+
"Unexpected end of JSON input",
|
|
149
|
+
"JSON.parse: unterminated string",
|
|
150
|
+
"JSON.parse: end of data",
|
|
151
|
+
"_META.IABInnerFrame",
|
|
152
|
+
"window.ethereum",
|
|
153
|
+
"User rejected the request",
|
|
154
|
+
"signal is aborted",
|
|
155
|
+
"aborted without reason",
|
|
156
|
+
"ServiceWorkerContainer",
|
|
157
|
+
"Failed to register a ServiceWorker",
|
|
158
|
+
"service-worker.js",
|
|
159
|
+
"Please use the 'new' operator, this DOM object constructor cannot be called as a function",
|
|
160
|
+
];
|
|
161
|
+
const translationMessages = [
|
|
162
|
+
"String contained an illegal UTF-16 sequence",
|
|
163
|
+
];
|
|
164
|
+
const translationKeywords = [
|
|
165
|
+
"translate",
|
|
166
|
+
];
|
|
167
|
+
const scriptExtensions = [
|
|
168
|
+
".js",
|
|
169
|
+
".mjs",
|
|
170
|
+
".ts",
|
|
171
|
+
".tsx",
|
|
172
|
+
];
|
|
173
|
+
const litDomMethods = [
|
|
174
|
+
"nextSibling",
|
|
175
|
+
"insertBefore",
|
|
176
|
+
"removeChild",
|
|
177
|
+
"parentNode",
|
|
178
|
+
"firstChild",
|
|
179
|
+
"setting 'data'",
|
|
180
|
+
];
|
|
181
|
+
const errorKeys = [
|
|
182
|
+
"message",
|
|
183
|
+
"error",
|
|
184
|
+
"reason",
|
|
185
|
+
"description",
|
|
186
|
+
"detail",
|
|
187
|
+
"details",
|
|
188
|
+
"title",
|
|
189
|
+
"code",
|
|
190
|
+
"type",
|
|
191
|
+
"name",
|
|
192
|
+
];
|
|
193
|
+
const nonActionableExactMessages = new Set([
|
|
194
|
+
"",
|
|
195
|
+
"null",
|
|
196
|
+
"undefined",
|
|
197
|
+
"error",
|
|
198
|
+
"rejected",
|
|
199
|
+
"{}",
|
|
200
|
+
"[object object]",
|
|
201
|
+
"unhandledrejection",
|
|
202
|
+
"empty error",
|
|
203
|
+
"empty error string",
|
|
204
|
+
"unknown rejection",
|
|
205
|
+
"unknown error",
|
|
206
|
+
]);
|
|
207
|
+
const botUserAgentFragments = [
|
|
208
|
+
"googlebot",
|
|
209
|
+
"adsbot-google",
|
|
210
|
+
"google-read-aloud",
|
|
211
|
+
"mediapartners-google",
|
|
212
|
+
"apis-google",
|
|
213
|
+
"feedfetcher-google",
|
|
214
|
+
"googleother",
|
|
215
|
+
"bingbot",
|
|
216
|
+
"adidxbot",
|
|
217
|
+
"msnbot",
|
|
218
|
+
"slurp",
|
|
219
|
+
"duckduckbot",
|
|
220
|
+
"baiduspider",
|
|
221
|
+
"yandexbot",
|
|
222
|
+
"sogou",
|
|
223
|
+
"exabot",
|
|
224
|
+
"facebot",
|
|
225
|
+
"facebookexternalhit",
|
|
226
|
+
"ia_archiver",
|
|
227
|
+
"applebot",
|
|
228
|
+
"twitterbot",
|
|
229
|
+
"linkedinbot",
|
|
230
|
+
"slackbot",
|
|
231
|
+
"discordbot",
|
|
232
|
+
"whatsapp",
|
|
233
|
+
"telegrambot",
|
|
234
|
+
"petalbot",
|
|
235
|
+
"bytespider",
|
|
236
|
+
"ahrefsbot",
|
|
237
|
+
"semrushbot",
|
|
238
|
+
"mj12bot",
|
|
239
|
+
"dotbot",
|
|
240
|
+
"headlesschrome",
|
|
241
|
+
];
|
|
242
|
+
export const isBotUserAgent = (userAgent) => {
|
|
243
|
+
const lower = userAgent.toLowerCase();
|
|
244
|
+
return botUserAgentFragments.some((fragment) => lower.includes(fragment));
|
|
245
|
+
};
|
|
246
|
+
const currentUserAgent = () => typeof globalThis.navigator === "object" &&
|
|
247
|
+
globalThis.navigator !== null &&
|
|
248
|
+
typeof globalThis.navigator.userAgent === "string"
|
|
249
|
+
? globalThis.navigator.userAgent
|
|
250
|
+
: "";
|
|
251
|
+
const hasExtensionPrefix = (text) => extensionPrefixes.some((prefix) => text.includes(prefix));
|
|
252
|
+
const hasScriptExtension = (text) => scriptExtensions.some((ext) => text.includes(ext));
|
|
253
|
+
const isRecord = (val) => typeof val === "object" && val !== null;
|
|
254
|
+
const safeJsonStringify = (obj) => {
|
|
255
|
+
try {
|
|
256
|
+
return JSON.stringify(obj);
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
return String(obj);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const extractPropertyMessage = (obj) => {
|
|
263
|
+
if (isRecord(obj.body) && typeof obj.body.message === "string") {
|
|
264
|
+
return obj.body.message;
|
|
265
|
+
}
|
|
266
|
+
const matchingKey = errorKeys.find((key) => key in obj && Boolean(obj[key]));
|
|
267
|
+
if (!matchingKey) {
|
|
268
|
+
if ("status" in obj && typeof obj.status === "number") {
|
|
269
|
+
const statusText = typeof obj.statusText === "string"
|
|
270
|
+
? obj.statusText
|
|
271
|
+
: "";
|
|
272
|
+
return statusText ? `${obj.status}: ${statusText}` : String(obj.status);
|
|
273
|
+
}
|
|
274
|
+
return safeJsonStringify(obj);
|
|
275
|
+
}
|
|
276
|
+
const val = obj[matchingKey];
|
|
277
|
+
if (typeof val === "string")
|
|
278
|
+
return val;
|
|
279
|
+
if (typeof val === "number" || typeof val === "boolean")
|
|
280
|
+
return String(val);
|
|
281
|
+
return messageOf(val);
|
|
282
|
+
};
|
|
283
|
+
export const messageOf = (reason) => reason instanceof Error
|
|
284
|
+
? reason.message || reason.name
|
|
285
|
+
: typeof reason === "string"
|
|
286
|
+
? reason || "Empty error string"
|
|
287
|
+
: typeof reason === "number" || typeof reason === "boolean"
|
|
288
|
+
? String(reason)
|
|
289
|
+
: reason === null
|
|
290
|
+
? "null"
|
|
291
|
+
: reason === undefined
|
|
292
|
+
? "undefined"
|
|
293
|
+
: isRecord(reason)
|
|
294
|
+
? extractPropertyMessage(reason)
|
|
295
|
+
: String(reason) || "Empty error";
|
|
296
|
+
const extractStrings = (input) => {
|
|
297
|
+
if (typeof input === "string")
|
|
298
|
+
return [input];
|
|
299
|
+
if (!isRecord(input))
|
|
300
|
+
return [];
|
|
301
|
+
const keys = [
|
|
302
|
+
"message",
|
|
303
|
+
"stack",
|
|
304
|
+
"filename",
|
|
305
|
+
"fileName",
|
|
306
|
+
"sourceURL",
|
|
307
|
+
"name",
|
|
308
|
+
];
|
|
309
|
+
return keys
|
|
310
|
+
.map((k) => input[k])
|
|
311
|
+
.filter((v) => typeof v === "string");
|
|
312
|
+
};
|
|
313
|
+
const extractEventStrings = (event) => isRecord(event)
|
|
314
|
+
? [...extractStrings(event.error), ...extractStrings(event)]
|
|
315
|
+
: extractStrings(event);
|
|
316
|
+
export const isBrowserExtensionError = (reason) => extractStrings(reason).some((str) => hasExtensionPrefix(str) ||
|
|
317
|
+
extensionMessages.some((msg) => str.includes(msg)));
|
|
318
|
+
export const isExtensionHostObjectRejection = (reason) => isBrowserExtensionError(reason);
|
|
319
|
+
export const isMediaOrNetworkAbortError = (reason) => extractStrings(reason).some((str) => abortMessages.some((msg) => str.includes(msg)));
|
|
320
|
+
export const isResizeObserverError = (reason) => extractStrings(reason).some((str) => str.includes("ResizeObserver"));
|
|
321
|
+
export const isNullRejection = (reason) => reason === null || reason === undefined;
|
|
322
|
+
const matchesNextSiblingNullStrings = (strings) => {
|
|
323
|
+
const combined = strings.join("\n");
|
|
324
|
+
return (litDomMethods.some((method) => combined.includes(method)) &&
|
|
325
|
+
(combined.includes("null") || combined.includes("undefined")));
|
|
326
|
+
};
|
|
327
|
+
export const isNextSiblingNullError = (reason) => matchesNextSiblingNullStrings(extractEventStrings(reason));
|
|
328
|
+
const matchesTranslationStrings = (reason, strings) => {
|
|
329
|
+
const combined = strings.join("\n");
|
|
330
|
+
const filename = isRecord(reason) && typeof reason.filename === "string"
|
|
331
|
+
? reason.filename
|
|
332
|
+
: "";
|
|
333
|
+
const hasDocumentFilename = (filename.startsWith("http://") || filename.startsWith("https://")) &&
|
|
334
|
+
!hasScriptExtension(filename);
|
|
335
|
+
const hasInlineStackFrame = (combined.includes("@http://") || combined.includes("@https://")) &&
|
|
336
|
+
!hasScriptExtension(combined);
|
|
337
|
+
const isInlineInjectedCallStackOverflow = combined.includes("Maximum call stack size exceeded") &&
|
|
338
|
+
(combined.includes("http://") || combined.includes("https://")) &&
|
|
339
|
+
!hasScriptExtension(combined);
|
|
340
|
+
return (hasDocumentFilename ||
|
|
341
|
+
hasInlineStackFrame ||
|
|
342
|
+
isInlineInjectedCallStackOverflow ||
|
|
343
|
+
strings.some((str) => translationMessages.some((msg) => str.includes(msg)) ||
|
|
344
|
+
(translationKeywords.some((kw) => str.includes(kw)) &&
|
|
345
|
+
(str.includes("encodeURIComponent") ||
|
|
346
|
+
str.includes("URI malformed") ||
|
|
347
|
+
str.includes("URIError")))));
|
|
348
|
+
};
|
|
349
|
+
export const isBrowserTranslationError = (reason) => matchesTranslationStrings(reason, extractEventStrings(reason));
|
|
350
|
+
const constructorName = (obj) => typeof obj.constructor === "function" ? obj.constructor.name : "";
|
|
351
|
+
export const isEventRejection = (reason) => {
|
|
352
|
+
if (!isRecord(reason))
|
|
353
|
+
return false;
|
|
354
|
+
if (typeof Event !== "undefined" && reason instanceof Event)
|
|
355
|
+
return true;
|
|
356
|
+
if (typeof DOMException !== "undefined" && reason instanceof DOMException) {
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
const tag = Object.prototype.toString.call(reason);
|
|
360
|
+
if (tag.endsWith("Event]") || tag === "[object DOMException]")
|
|
361
|
+
return true;
|
|
362
|
+
const name = constructorName(reason);
|
|
363
|
+
if (name === "Event" || name.endsWith("Event") || name === "DOMException") {
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
if (typeof reason.isTrusted === "boolean")
|
|
367
|
+
return true;
|
|
368
|
+
if (typeof reason.message === "string" && reason.message.includes("IDBDatabase")) {
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
};
|
|
373
|
+
export const isScriptError = (event) => isRecord(event) && event.message === "Script error." && !event.error;
|
|
374
|
+
export const isNextSiblingNullErrorEvent = (event) => isNextSiblingNullError(event);
|
|
375
|
+
export const isBrowserExtensionErrorEvent = (event) => isRecord(event) &&
|
|
376
|
+
(isBrowserExtensionError(event.error) ||
|
|
377
|
+
isBrowserExtensionError(event.filename) ||
|
|
378
|
+
isBrowserExtensionError(event.message));
|
|
379
|
+
export const isResizeObserverErrorEvent = (event) => isRecord(event) &&
|
|
380
|
+
(isResizeObserverError(event.error) || isResizeObserverError(event.message));
|
|
381
|
+
export const isMediaOrNetworkAbortErrorEvent = (event) => isRecord(event) &&
|
|
382
|
+
(isMediaOrNetworkAbortError(event.error) ||
|
|
383
|
+
isMediaOrNetworkAbortError(event.message));
|
|
384
|
+
export const isBrowserTranslationErrorEvent = (event) => isBrowserTranslationError(event);
|
|
385
|
+
export const isIgnorable = (reason) => {
|
|
386
|
+
if (isBotUserAgent(currentUserAgent()) ||
|
|
387
|
+
isNullRejection(reason) ||
|
|
388
|
+
isEventRejection(reason) ||
|
|
389
|
+
isBrowserExtensionError(reason) ||
|
|
390
|
+
isResizeObserverError(reason) ||
|
|
391
|
+
isNextSiblingNullError(reason) ||
|
|
392
|
+
isMediaOrNetworkAbortError(reason) ||
|
|
393
|
+
isBrowserTranslationError(reason)) {
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
const message = messageOf(reason);
|
|
397
|
+
const lowerMsg = message.toLowerCase().trim();
|
|
398
|
+
if (nonActionableExactMessages.has(lowerMsg))
|
|
399
|
+
return true;
|
|
400
|
+
return ignoredMessageFragments.some((fragment) => lowerMsg.includes(fragment.toLowerCase()));
|
|
401
|
+
};
|
|
402
|
+
export const errorEventNames = (baseEvent) => (reason) => isIgnorable(reason)
|
|
403
|
+
? []
|
|
404
|
+
: [baseEvent, `${baseEvent}: ${messageOf(reason)}`.slice(0, 120)];
|
|
405
|
+
export const isIgnoredRejection = isIgnorable;
|
|
406
|
+
export const isIgnoredErrorEvent = (event) => isScriptError(event) ||
|
|
407
|
+
isBrowserExtensionErrorEvent(event) ||
|
|
408
|
+
isResizeObserverErrorEvent(event) ||
|
|
409
|
+
isNextSiblingNullErrorEvent(event) ||
|
|
410
|
+
isMediaOrNetworkAbortErrorEvent(event) ||
|
|
411
|
+
isBrowserTranslationErrorEvent(event) ||
|
|
412
|
+
(isRecord(event) &&
|
|
413
|
+
(event.error !== undefined || event.message !== undefined) &&
|
|
414
|
+
isIgnorable(event.error ?? event.message));
|
|
415
|
+
const stopEvent = (event) => {
|
|
416
|
+
if (typeof event.preventDefault === "function")
|
|
417
|
+
event.preventDefault();
|
|
418
|
+
if (typeof event.stopImmediatePropagation === "function") {
|
|
419
|
+
event.stopImmediatePropagation();
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
export const suppressClientErrorEvent = (event) => {
|
|
423
|
+
if (!isRecord(event) || !isIgnoredErrorEvent(event))
|
|
424
|
+
return false;
|
|
425
|
+
stopEvent(event);
|
|
426
|
+
return true;
|
|
427
|
+
};
|
|
428
|
+
export const suppressClientRejectionEvent = (event) => {
|
|
429
|
+
if (!isRecord(event) || !isIgnoredRejection(event.reason))
|
|
430
|
+
return false;
|
|
431
|
+
stopEvent(event);
|
|
432
|
+
return true;
|
|
433
|
+
};
|
|
434
|
+
export const suppressEventRejection = suppressClientRejectionEvent;
|
|
435
|
+
export const suppressScriptError = suppressClientErrorEvent;
|
|
436
|
+
export const installClientErrorSuppression = (target) => {
|
|
437
|
+
target.addEventListener("error", (event) => {
|
|
438
|
+
suppressClientErrorEvent(event);
|
|
439
|
+
}, true);
|
|
440
|
+
target.addEventListener("unhandledrejection", (event) => {
|
|
441
|
+
suppressClientRejectionEvent(event);
|
|
442
|
+
}, true);
|
|
443
|
+
};
|
|
444
|
+
export const installEventRejectionSuppression = installClientErrorSuppression;
|
|
445
|
+
const report = (token, userId) => {
|
|
446
|
+
const state = { reporting: false };
|
|
447
|
+
return (eventName) => {
|
|
448
|
+
if (state.reporting)
|
|
449
|
+
return;
|
|
450
|
+
state.reporting = true;
|
|
451
|
+
sendEvent({ token, eventName, ...(userId ? { userId } : {}) })
|
|
452
|
+
.catch(() => { })
|
|
453
|
+
.finally(() => {
|
|
454
|
+
state.reporting = false;
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
};
|
|
458
|
+
export const captureClientErrors = ({ token, userId }) => {
|
|
459
|
+
const capture = report(token, userId);
|
|
460
|
+
if (typeof globalThis.addEventListener === "function") {
|
|
461
|
+
globalThis.addEventListener("error", (event) => {
|
|
462
|
+
if (suppressClientErrorEvent(event))
|
|
463
|
+
return;
|
|
464
|
+
if (!(event instanceof ErrorEvent))
|
|
465
|
+
return;
|
|
466
|
+
capture(uncaughtErrorEvent);
|
|
467
|
+
}, true);
|
|
468
|
+
globalThis.addEventListener("unhandledrejection", (event) => {
|
|
469
|
+
if (suppressClientRejectionEvent(event))
|
|
470
|
+
return;
|
|
471
|
+
capture(unhandledRejectionEvent);
|
|
472
|
+
}, true);
|
|
473
|
+
}
|
|
474
|
+
return capture;
|
|
475
|
+
};
|
package/esm/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const withRetry: <T, O>(retries: number, ms: number, fn: (params: T) => Promise<O>) => (params: T) => Promise<O>;
|
package/esm/src/retry.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Metric = "totalCount" | "userSpike" | "percentageSpike" | "percentageDrop";
|
|
2
|
+
export type Anomaly = {
|
|
3
|
+
projectId: string;
|
|
4
|
+
eventName: string;
|
|
5
|
+
bucket: string;
|
|
6
|
+
expected: number;
|
|
7
|
+
actual: number;
|
|
8
|
+
zScore: number;
|
|
9
|
+
detectedAt: string;
|
|
10
|
+
metric: Metric;
|
|
11
|
+
userId?: string;
|
|
12
|
+
trend?: string;
|
|
13
|
+
};
|
package/esm/src/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anomalisa",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-config statistical anomaly detection for your events. Send events, get emailed when something weird happens.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"anomaly-detection",
|
|
7
|
+
"monitoring",
|
|
8
|
+
"alerts",
|
|
9
|
+
"observability",
|
|
10
|
+
"error-tracking",
|
|
11
|
+
"statistics",
|
|
12
|
+
"welford",
|
|
13
|
+
"events"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://anomalisa.uriv.me",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/uriva/anomalisa.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/uriva/anomalisa/issues"
|
|
23
|
+
},
|
|
24
|
+
"module": "./esm/mod.js",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./esm/mod.d.ts",
|
|
28
|
+
"import": "./esm/mod.js",
|
|
29
|
+
"default": "./esm/mod.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18"
|
|
35
|
+
},
|
|
36
|
+
"_generatedBy": "dnt@0.44.0",
|
|
37
|
+
"type": "module",
|
|
38
|
+
"types": "./esm/mod.d.ts",
|
|
39
|
+
"main": "./esm/mod.js",
|
|
40
|
+
"files": [
|
|
41
|
+
"esm/",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
]
|
|
45
|
+
}
|