@spfn/monitor 0.1.0-beta.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 INFLIKE Inc.
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 ADDED
@@ -0,0 +1,232 @@
1
+ # @spfn/monitor
2
+
3
+ Error tracking, log management, and monitoring dashboard for SPFN applications.
4
+
5
+ ## Features
6
+
7
+ - **DB-backed error tracking**: Fingerprint-based deduplication with automatic grouping
8
+ - **State-based notifications**: Slack alerts only on new errors and reopened errors (no duplicates)
9
+ - **Developer logging**: Pluggable log storage with DB default
10
+ - **Admin API**: Superadmin-only routes for managing errors and viewing logs
11
+ - **React dashboard**: Ready-to-use monitoring UI components
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add @spfn/monitor
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ### Server Configuration
22
+
23
+ ```typescript
24
+ import { defineServerConfig } from '@spfn/core/server';
25
+ import { createMonitorErrorHandler, createMonitorLifecycle, monitorRouter } from '@spfn/monitor/server';
26
+
27
+ export default defineServerConfig()
28
+ .middleware({ onError: createMonitorErrorHandler() })
29
+ .routes(appRouter)
30
+ .lifecycle(createMonitorLifecycle())
31
+ .build();
32
+ ```
33
+
34
+ Register the monitor router as a package router:
35
+
36
+ ```typescript
37
+ import { monitorRouter } from '@spfn/monitor/server';
38
+
39
+ export const appRouter = defineRouter({ ... })
40
+ .packages([authRouter, monitorRouter]);
41
+ ```
42
+
43
+ ### Database Migration
44
+
45
+ ```bash
46
+ spfn db migrate
47
+ ```
48
+
49
+ This creates the `spfn_monitor` schema with `error_groups`, `error_events`, and `logs` tables.
50
+
51
+ ## Configuration
52
+
53
+ ### Environment Variables
54
+
55
+ ```bash
56
+ # Slack webhook for error notifications (optional)
57
+ SPFN_MONITOR_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...
58
+
59
+ # Error retention in days (default: 90)
60
+ SPFN_MONITOR_ERROR_RETENTION_DAYS=90
61
+
62
+ # Log retention in days (default: 30)
63
+ SPFN_MONITOR_LOG_RETENTION_DAYS=30
64
+
65
+ # Minimum HTTP status code to track (default: 500)
66
+ SPFN_MONITOR_MIN_STATUS_CODE=500
67
+ ```
68
+
69
+ ### Code Configuration
70
+
71
+ ```typescript
72
+ import { configureMonitor } from '@spfn/monitor/config';
73
+
74
+ configureMonitor({
75
+ slackWebhookUrl: 'https://hooks.slack.com/services/...',
76
+ errorRetentionDays: 90,
77
+ logRetentionDays: 30,
78
+ minStatusCode: 500,
79
+ });
80
+ ```
81
+
82
+ ## Error Tracking
83
+
84
+ Errors are automatically tracked when using `createMonitorErrorHandler()`:
85
+
86
+ 1. **New error** - Creates error group + event, sends Slack notification
87
+ 2. **Repeated error** (active/ignored) - Increments count, records event, no notification
88
+ 3. **Reopened error** (was resolved) - Changes status to active, sends Slack notification
89
+
90
+ ### Fingerprinting
91
+
92
+ Errors are grouped by a SHA-256 fingerprint of `name:message:path`, producing a 16-character hex ID.
93
+
94
+ ### Manual Error Tracking
95
+
96
+ ```typescript
97
+ import { trackError } from '@spfn/monitor/server';
98
+
99
+ await trackError(error, {
100
+ statusCode: 500,
101
+ path: '/api/example',
102
+ method: 'POST',
103
+ requestId: 'req_123',
104
+ });
105
+ ```
106
+
107
+ ## Developer Logging
108
+
109
+ ```typescript
110
+ import { writeLog, queryLogs } from '@spfn/monitor/server';
111
+
112
+ // Write a log entry
113
+ await writeLog({
114
+ level: 'info',
115
+ message: 'User signed in',
116
+ source: 'auth',
117
+ userId: 'user_123',
118
+ metadata: { provider: 'google' },
119
+ });
120
+
121
+ // Query logs
122
+ const logs = await queryLogs({
123
+ level: 'error',
124
+ source: 'payment',
125
+ limit: 50,
126
+ });
127
+ ```
128
+
129
+ ### Custom Log Store
130
+
131
+ Replace the default DB storage with a custom implementation:
132
+
133
+ ```typescript
134
+ import { setLogStore } from '@spfn/monitor/server';
135
+
136
+ setLogStore({
137
+ async write(entry) { /* S3, ClickHouse, etc. */ },
138
+ async query(filters) { /* ... */ },
139
+ async purge(olderThan) { /* ... */ },
140
+ });
141
+ ```
142
+
143
+ ## Admin API Routes
144
+
145
+ All routes require `superadmin` role authentication.
146
+
147
+ | Method | Path | Description |
148
+ |--------|------|-------------|
149
+ | GET | `/_monitor/admin/errors` | List error groups (filter by status, path, search) |
150
+ | GET | `/_monitor/admin/errors/:id` | Error group detail + recent events |
151
+ | PATCH | `/_monitor/admin/errors/:id` | Update error status (resolve/ignore/reopen) |
152
+ | GET | `/_monitor/admin/errors/:id/events` | List events for an error group |
153
+ | GET | `/_monitor/admin/logs` | Query logs (filter by level, source, search) |
154
+ | GET | `/_monitor/admin/stats` | Dashboard statistics |
155
+
156
+ ## Dashboard Components
157
+
158
+ ```typescript
159
+ // In your Next.js page
160
+ import { MonitorDashboard } from '@spfn/monitor/nextjs/client';
161
+
162
+ export default function MonitorPage() {
163
+ return <MonitorDashboard />;
164
+ }
165
+ ```
166
+
167
+ Available components:
168
+
169
+ - `MonitorDashboard` - Full dashboard with tabs (errors, logs) and stats
170
+ - `StatsOverview` - Error/log count cards with trends
171
+ - `ErrorListView` - Filterable error group table
172
+ - `ErrorDetailView` - Error detail with event timeline and status actions
173
+ - `LogViewer` - Searchable log list with expandable metadata
174
+
175
+ ## API Client
176
+
177
+ ```typescript
178
+ import { monitorApi } from '@spfn/monitor';
179
+
180
+ // Get dashboard stats
181
+ const stats = await monitorApi.getStats.call({});
182
+
183
+ // List active errors
184
+ const errors = await monitorApi.listErrors.call({
185
+ query: { status: 'active', limit: 20 },
186
+ });
187
+
188
+ // Resolve an error
189
+ await monitorApi.updateErrorStatus.call({
190
+ params: { id: 1 },
191
+ body: { status: 'resolved' },
192
+ });
193
+ ```
194
+
195
+ ## Exports
196
+
197
+ ```typescript
198
+ // From '@spfn/monitor'
199
+ export { monitorApi };
200
+ export type { MonitorRouter, MonitorStats, ErrorGroupStatus, LogLevel };
201
+
202
+ // From '@spfn/monitor/server'
203
+ export {
204
+ // Integration
205
+ monitorRouter,
206
+ createMonitorErrorHandler,
207
+ createMonitorLifecycle,
208
+
209
+ // Services
210
+ trackError, updateErrorGroupStatus,
211
+ writeLog, queryLogs,
212
+ getMonitorStats,
213
+ setLogStore,
214
+
215
+ // Entities & Repositories
216
+ errorGroups, errorEvents, logs,
217
+ errorGroupsRepository, errorEventsRepository, logsRepository,
218
+ };
219
+
220
+ // From '@spfn/monitor/config'
221
+ export { configureMonitor };
222
+
223
+ // From '@spfn/monitor/nextjs/client'
224
+ export {
225
+ MonitorDashboard, StatsOverview,
226
+ ErrorListView, ErrorDetailView, LogViewer,
227
+ };
228
+ ```
229
+
230
+ ## License
231
+
232
+ MIT
@@ -0,0 +1,136 @@
1
+ import * as _spfn_core_env from '@spfn/core/env';
2
+
3
+ /**
4
+ * @spfn/monitor - Environment Schema
5
+ */
6
+ declare const monitorEnvSchema: {
7
+ SPFN_MONITOR_SLACK_WEBHOOK_URL: {
8
+ description: string;
9
+ required: boolean;
10
+ examples: string[];
11
+ type: "string";
12
+ } & {
13
+ key: "SPFN_MONITOR_SLACK_WEBHOOK_URL";
14
+ };
15
+ SPFN_MONITOR_ERROR_RETENTION_DAYS: {
16
+ description: string;
17
+ default: number;
18
+ required: boolean;
19
+ examples: number[];
20
+ type: "number";
21
+ validator: (value: string) => number;
22
+ } & {
23
+ key: "SPFN_MONITOR_ERROR_RETENTION_DAYS";
24
+ };
25
+ SPFN_MONITOR_LOG_RETENTION_DAYS: {
26
+ description: string;
27
+ default: number;
28
+ required: boolean;
29
+ examples: number[];
30
+ type: "number";
31
+ validator: (value: string) => number;
32
+ } & {
33
+ key: "SPFN_MONITOR_LOG_RETENTION_DAYS";
34
+ };
35
+ SPFN_MONITOR_MIN_STATUS_CODE: {
36
+ description: string;
37
+ default: number;
38
+ required: boolean;
39
+ examples: number[];
40
+ type: "number";
41
+ validator: (value: string) => number;
42
+ } & {
43
+ key: "SPFN_MONITOR_MIN_STATUS_CODE";
44
+ };
45
+ };
46
+
47
+ declare const env: _spfn_core_env.InferEnvType<{
48
+ SPFN_MONITOR_SLACK_WEBHOOK_URL: {
49
+ description: string;
50
+ required: boolean;
51
+ examples: string[];
52
+ type: "string";
53
+ } & {
54
+ key: "SPFN_MONITOR_SLACK_WEBHOOK_URL";
55
+ };
56
+ SPFN_MONITOR_ERROR_RETENTION_DAYS: {
57
+ description: string;
58
+ default: number;
59
+ required: boolean;
60
+ examples: number[];
61
+ type: "number";
62
+ validator: (value: string) => number;
63
+ } & {
64
+ key: "SPFN_MONITOR_ERROR_RETENTION_DAYS";
65
+ };
66
+ SPFN_MONITOR_LOG_RETENTION_DAYS: {
67
+ description: string;
68
+ default: number;
69
+ required: boolean;
70
+ examples: number[];
71
+ type: "number";
72
+ validator: (value: string) => number;
73
+ } & {
74
+ key: "SPFN_MONITOR_LOG_RETENTION_DAYS";
75
+ };
76
+ SPFN_MONITOR_MIN_STATUS_CODE: {
77
+ description: string;
78
+ default: number;
79
+ required: boolean;
80
+ examples: number[];
81
+ type: "number";
82
+ validator: (value: string) => number;
83
+ } & {
84
+ key: "SPFN_MONITOR_MIN_STATUS_CODE";
85
+ };
86
+ }>;
87
+ /**
88
+ * Monitor configuration
89
+ */
90
+ interface MonitorConfig {
91
+ /**
92
+ * Slack webhook URL override
93
+ */
94
+ slackWebhookUrl?: string;
95
+ /**
96
+ * Error retention days
97
+ * @default 90
98
+ */
99
+ errorRetentionDays?: number;
100
+ /**
101
+ * Log retention days
102
+ * @default 30
103
+ */
104
+ logRetentionDays?: number;
105
+ /**
106
+ * Minimum HTTP status code to track
107
+ * @default 500
108
+ */
109
+ minStatusCode?: number;
110
+ }
111
+ /**
112
+ * Configure monitor settings
113
+ */
114
+ declare function configureMonitor(config: MonitorConfig): void;
115
+ /**
116
+ * Get current monitor configuration
117
+ */
118
+ declare function getMonitorConfig(): MonitorConfig;
119
+ /**
120
+ * Get Slack webhook URL (config > env)
121
+ */
122
+ declare function getSlackWebhookUrl(): string | undefined;
123
+ /**
124
+ * Get error retention days
125
+ */
126
+ declare function getErrorRetentionDays(): number;
127
+ /**
128
+ * Get log retention days
129
+ */
130
+ declare function getLogRetentionDays(): number;
131
+ /**
132
+ * Get minimum status code for error tracking
133
+ */
134
+ declare function getMinStatusCode(): number;
135
+
136
+ export { type MonitorConfig, configureMonitor, env, getErrorRetentionDays, getLogRetentionDays, getMinStatusCode, getMonitorConfig, getSlackWebhookUrl, monitorEnvSchema };
@@ -0,0 +1,72 @@
1
+ // src/config/index.ts
2
+ import { createEnvRegistry } from "@spfn/core/env";
3
+
4
+ // src/config/schema.ts
5
+ import { defineEnvSchema, envString, envNumber } from "@spfn/core/env";
6
+ var monitorEnvSchema = defineEnvSchema({
7
+ SPFN_MONITOR_SLACK_WEBHOOK_URL: {
8
+ ...envString({
9
+ description: "Slack webhook URL for error notifications (falls back to notification default)",
10
+ required: false,
11
+ examples: ["https://hooks.slack.com/services/xxx/xxx/xxx"]
12
+ })
13
+ },
14
+ SPFN_MONITOR_ERROR_RETENTION_DAYS: {
15
+ ...envNumber({
16
+ description: "Number of days to retain error events before purging",
17
+ default: 90,
18
+ required: false,
19
+ examples: [30, 60, 90]
20
+ })
21
+ },
22
+ SPFN_MONITOR_LOG_RETENTION_DAYS: {
23
+ ...envNumber({
24
+ description: "Number of days to retain logs before purging",
25
+ default: 30,
26
+ required: false,
27
+ examples: [7, 14, 30]
28
+ })
29
+ },
30
+ SPFN_MONITOR_MIN_STATUS_CODE: {
31
+ ...envNumber({
32
+ description: "Minimum HTTP status code to track as error",
33
+ default: 500,
34
+ required: false,
35
+ examples: [400, 500]
36
+ })
37
+ }
38
+ });
39
+
40
+ // src/config/index.ts
41
+ var registry = createEnvRegistry(monitorEnvSchema);
42
+ var env = registry.validate();
43
+ var globalConfig = {};
44
+ function configureMonitor(config) {
45
+ globalConfig = { ...globalConfig, ...config };
46
+ }
47
+ function getMonitorConfig() {
48
+ return { ...globalConfig };
49
+ }
50
+ function getSlackWebhookUrl() {
51
+ return globalConfig.slackWebhookUrl || env.SPFN_MONITOR_SLACK_WEBHOOK_URL;
52
+ }
53
+ function getErrorRetentionDays() {
54
+ return globalConfig.errorRetentionDays ?? env.SPFN_MONITOR_ERROR_RETENTION_DAYS ?? 90;
55
+ }
56
+ function getLogRetentionDays() {
57
+ return globalConfig.logRetentionDays ?? env.SPFN_MONITOR_LOG_RETENTION_DAYS ?? 30;
58
+ }
59
+ function getMinStatusCode() {
60
+ return globalConfig.minStatusCode ?? env.SPFN_MONITOR_MIN_STATUS_CODE ?? 500;
61
+ }
62
+ export {
63
+ configureMonitor,
64
+ env,
65
+ getErrorRetentionDays,
66
+ getLogRetentionDays,
67
+ getMinStatusCode,
68
+ getMonitorConfig,
69
+ getSlackWebhookUrl,
70
+ monitorEnvSchema
71
+ };
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/config/index.ts","../../src/config/schema.ts"],"sourcesContent":["/**\n * @spfn/monitor - Configuration\n */\n\nimport { createEnvRegistry } from '@spfn/core/env';\nimport { monitorEnvSchema } from './schema';\n\nexport { monitorEnvSchema };\n\n/**\n * Environment registry\n */\nconst registry = createEnvRegistry(monitorEnvSchema);\nexport const env = registry.validate();\n\n/**\n * Monitor configuration\n */\nexport interface MonitorConfig\n{\n /**\n * Slack webhook URL override\n */\n slackWebhookUrl?: string;\n\n /**\n * Error retention days\n * @default 90\n */\n errorRetentionDays?: number;\n\n /**\n * Log retention days\n * @default 30\n */\n logRetentionDays?: number;\n\n /**\n * Minimum HTTP status code to track\n * @default 500\n */\n minStatusCode?: number;\n}\n\nlet globalConfig: MonitorConfig = {};\n\n/**\n * Configure monitor settings\n */\nexport function configureMonitor(config: MonitorConfig): void\n{\n globalConfig = { ...globalConfig, ...config };\n}\n\n/**\n * Get current monitor configuration\n */\nexport function getMonitorConfig(): MonitorConfig\n{\n return { ...globalConfig };\n}\n\n/**\n * Get Slack webhook URL (config > env)\n */\nexport function getSlackWebhookUrl(): string | undefined\n{\n return globalConfig.slackWebhookUrl || env.SPFN_MONITOR_SLACK_WEBHOOK_URL;\n}\n\n/**\n * Get error retention days\n */\nexport function getErrorRetentionDays(): number\n{\n return globalConfig.errorRetentionDays ?? env.SPFN_MONITOR_ERROR_RETENTION_DAYS ?? 90;\n}\n\n/**\n * Get log retention days\n */\nexport function getLogRetentionDays(): number\n{\n return globalConfig.logRetentionDays ?? env.SPFN_MONITOR_LOG_RETENTION_DAYS ?? 30;\n}\n\n/**\n * Get minimum status code for error tracking\n */\nexport function getMinStatusCode(): number\n{\n return globalConfig.minStatusCode ?? env.SPFN_MONITOR_MIN_STATUS_CODE ?? 500;\n}\n","/**\n * @spfn/monitor - Environment Schema\n */\n\nimport { defineEnvSchema, envString, envNumber } from '@spfn/core/env';\n\nexport const monitorEnvSchema = defineEnvSchema({\n SPFN_MONITOR_SLACK_WEBHOOK_URL: {\n ...envString({\n description: 'Slack webhook URL for error notifications (falls back to notification default)',\n required: false,\n examples: ['https://hooks.slack.com/services/xxx/xxx/xxx'],\n }),\n },\n\n SPFN_MONITOR_ERROR_RETENTION_DAYS: {\n ...envNumber({\n description: 'Number of days to retain error events before purging',\n default: 90,\n required: false,\n examples: [30, 60, 90],\n }),\n },\n\n SPFN_MONITOR_LOG_RETENTION_DAYS: {\n ...envNumber({\n description: 'Number of days to retain logs before purging',\n default: 30,\n required: false,\n examples: [7, 14, 30],\n }),\n },\n\n SPFN_MONITOR_MIN_STATUS_CODE: {\n ...envNumber({\n description: 'Minimum HTTP status code to track as error',\n default: 500,\n required: false,\n examples: [400, 500],\n }),\n },\n});\n"],"mappings":";AAIA,SAAS,yBAAyB;;;ACAlC,SAAS,iBAAiB,WAAW,iBAAiB;AAE/C,IAAM,mBAAmB,gBAAgB;AAAA,EAC5C,gCAAgC;AAAA,IAC5B,GAAG,UAAU;AAAA,MACT,aAAa;AAAA,MACb,UAAU;AAAA,MACV,UAAU,CAAC,8CAA8C;AAAA,IAC7D,CAAC;AAAA,EACL;AAAA,EAEA,mCAAmC;AAAA,IAC/B,GAAG,UAAU;AAAA,MACT,aAAa;AAAA,MACb,SAAS;AAAA,MACT,UAAU;AAAA,MACV,UAAU,CAAC,IAAI,IAAI,EAAE;AAAA,IACzB,CAAC;AAAA,EACL;AAAA,EAEA,iCAAiC;AAAA,IAC7B,GAAG,UAAU;AAAA,MACT,aAAa;AAAA,MACb,SAAS;AAAA,MACT,UAAU;AAAA,MACV,UAAU,CAAC,GAAG,IAAI,EAAE;AAAA,IACxB,CAAC;AAAA,EACL;AAAA,EAEA,8BAA8B;AAAA,IAC1B,GAAG,UAAU;AAAA,MACT,aAAa;AAAA,MACb,SAAS;AAAA,MACT,UAAU;AAAA,MACV,UAAU,CAAC,KAAK,GAAG;AAAA,IACvB,CAAC;AAAA,EACL;AACJ,CAAC;;;AD7BD,IAAM,WAAW,kBAAkB,gBAAgB;AAC5C,IAAM,MAAM,SAAS,SAAS;AA+BrC,IAAI,eAA8B,CAAC;AAK5B,SAAS,iBAAiB,QACjC;AACI,iBAAe,EAAE,GAAG,cAAc,GAAG,OAAO;AAChD;AAKO,SAAS,mBAChB;AACI,SAAO,EAAE,GAAG,aAAa;AAC7B;AAKO,SAAS,qBAChB;AACI,SAAO,aAAa,mBAAmB,IAAI;AAC/C;AAKO,SAAS,wBAChB;AACI,SAAO,aAAa,sBAAsB,IAAI,qCAAqC;AACvF;AAKO,SAAS,sBAChB;AACI,SAAO,aAAa,oBAAoB,IAAI,mCAAmC;AACnF;AAKO,SAAS,mBAChB;AACI,SAAO,aAAa,iBAAiB,IAAI,gCAAgC;AAC7E;","names":[]}