vident-sdk 0.11.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/README.md +216 -0
- package/dist/client.d.ts +100 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +249 -0
- package/dist/client.js.map +1 -0
- package/dist/context.d.ts +15 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +41 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +35 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +4 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +60 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/instrument/aws-sdk.d.ts +9 -0
- package/dist/instrument/aws-sdk.d.ts.map +1 -0
- package/dist/instrument/aws-sdk.js +57 -0
- package/dist/instrument/aws-sdk.js.map +1 -0
- package/dist/instrument/fetch.d.ts +15 -0
- package/dist/instrument/fetch.d.ts.map +1 -0
- package/dist/instrument/fetch.js +124 -0
- package/dist/instrument/fetch.js.map +1 -0
- package/dist/instrument/handler.d.ts +40 -0
- package/dist/instrument/handler.d.ts.map +1 -0
- package/dist/instrument/handler.js +90 -0
- package/dist/instrument/handler.js.map +1 -0
- package/dist/instrument/hono.d.ts +4 -0
- package/dist/instrument/hono.d.ts.map +1 -0
- package/dist/instrument/hono.js +130 -0
- package/dist/instrument/hono.js.map +1 -0
- package/dist/instrument/index.d.ts +7 -0
- package/dist/instrument/index.d.ts.map +1 -0
- package/dist/instrument/index.js +6 -0
- package/dist/instrument/index.js.map +1 -0
- package/dist/instrument/prisma.d.ts +25 -0
- package/dist/instrument/prisma.d.ts.map +1 -0
- package/dist/instrument/prisma.js +73 -0
- package/dist/instrument/prisma.js.map +1 -0
- package/dist/instrument/types.d.ts +7 -0
- package/dist/instrument/types.d.ts.map +1 -0
- package/dist/instrument/types.js +2 -0
- package/dist/instrument/types.js.map +1 -0
- package/dist/resources/api-keys.d.ts +10 -0
- package/dist/resources/api-keys.d.ts.map +1 -0
- package/dist/resources/api-keys.js +17 -0
- package/dist/resources/api-keys.js.map +1 -0
- package/dist/resources/channels.d.ts +16 -0
- package/dist/resources/channels.d.ts.map +1 -0
- package/dist/resources/channels.js +38 -0
- package/dist/resources/channels.js.map +1 -0
- package/dist/resources/checks.d.ts +15 -0
- package/dist/resources/checks.d.ts.map +1 -0
- package/dist/resources/checks.js +44 -0
- package/dist/resources/checks.js.map +1 -0
- package/dist/resources/events.d.ts +25 -0
- package/dist/resources/events.d.ts.map +1 -0
- package/dist/resources/events.js +122 -0
- package/dist/resources/events.js.map +1 -0
- package/dist/resources/metrics.d.ts +24 -0
- package/dist/resources/metrics.d.ts.map +1 -0
- package/dist/resources/metrics.js +121 -0
- package/dist/resources/metrics.js.map +1 -0
- package/dist/resources/organizations.d.ts +12 -0
- package/dist/resources/organizations.d.ts.map +1 -0
- package/dist/resources/organizations.js +23 -0
- package/dist/resources/organizations.js.map +1 -0
- package/dist/resources/projects.d.ts +12 -0
- package/dist/resources/projects.d.ts.map +1 -0
- package/dist/resources/projects.js +35 -0
- package/dist/resources/projects.js.map +1 -0
- package/dist/resources/tracing.d.ts +26 -0
- package/dist/resources/tracing.d.ts.map +1 -0
- package/dist/resources/tracing.js +320 -0
- package/dist/resources/tracing.js.map +1 -0
- package/dist/sampler.d.ts +61 -0
- package/dist/sampler.d.ts.map +1 -0
- package/dist/sampler.js +73 -0
- package/dist/sampler.js.map +1 -0
- package/dist/trace-context.d.ts +31 -0
- package/dist/trace-context.d.ts.map +1 -0
- package/dist/trace-context.js +60 -0
- package/dist/trace-context.js.map +1 -0
- package/dist/types.d.ts +256 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +33 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# @spanwise/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Spanwise observability - monitoring, tracing, and error tracking.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @spanwise/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Spanwise } from "@spanwise/sdk"
|
|
15
|
+
|
|
16
|
+
const client = new Spanwise({ apiKey: "sw_..." })
|
|
17
|
+
|
|
18
|
+
// Wrap your cron job - automatically tracks start/success/fail
|
|
19
|
+
await client.wrap("check-id", async () => {
|
|
20
|
+
await myDatabaseBackup()
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Wrapping Jobs (Recommended)
|
|
27
|
+
|
|
28
|
+
The `wrap()` method automatically sends start/success/fail signals:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
await client.wrap("check-id", async () => {
|
|
32
|
+
// Your job logic here
|
|
33
|
+
await syncUsers()
|
|
34
|
+
await sendReports()
|
|
35
|
+
})
|
|
36
|
+
// Sends "start" → runs function → sends "success" (or "fail" if error thrown)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Manual Pinging
|
|
40
|
+
|
|
41
|
+
For more control, use `ping()` directly:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
await client.ping("check-id") // Success
|
|
45
|
+
await client.ping("check-id", { type: "start" }) // Job started
|
|
46
|
+
await client.ping("check-id", { type: "fail" }) // Job failed
|
|
47
|
+
await client.ping("check-id", { type: "fail", body: "Error details" })
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Configuration
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const client = new Spanwise({
|
|
54
|
+
apiKey: "sw_...",
|
|
55
|
+
timeout: 30000, // optional, default 30s
|
|
56
|
+
retries: 2, // optional, default 2 retries with exponential backoff
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Development Mode (No-op)
|
|
61
|
+
|
|
62
|
+
When `apiKey` is undefined, `ping()` and `wrap()` silently no-op. This allows running in development without setting up monitoring:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
const client = new Spanwise({ apiKey: process.env.SPANWISE_API_KEY })
|
|
66
|
+
|
|
67
|
+
// Works in dev (no-op) and prod (sends pings)
|
|
68
|
+
await client.wrap("check-id", async () => {
|
|
69
|
+
await myJob()
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Management APIs (projects, checks, etc.) still require an API key and will throw if accessed without one.
|
|
74
|
+
|
|
75
|
+
## API Reference
|
|
76
|
+
|
|
77
|
+
### Projects
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const projects = await client.projects.list()
|
|
81
|
+
const project = await client.projects.get("project-id")
|
|
82
|
+
const project = await client.projects.create({ name: "My Project", slug: "my-project", orgId: "org-id" })
|
|
83
|
+
await client.projects.update("project-id", { name: "New Name" })
|
|
84
|
+
await client.projects.delete("project-id")
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Checks
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const checks = await client.checks.list("project-id")
|
|
91
|
+
const check = await client.checks.get("check-id")
|
|
92
|
+
const check = await client.checks.create("project-id", {
|
|
93
|
+
name: "DB Backup",
|
|
94
|
+
scheduleType: "PERIOD",
|
|
95
|
+
scheduleValue: "3600", // every hour
|
|
96
|
+
})
|
|
97
|
+
await client.checks.update("check-id", { name: "New Name" })
|
|
98
|
+
await client.checks.pause("check-id")
|
|
99
|
+
await client.checks.resume("check-id")
|
|
100
|
+
await client.checks.delete("check-id")
|
|
101
|
+
|
|
102
|
+
// Uptime stats
|
|
103
|
+
const stats = await client.checks.stats("check-id", 90) // last 90 days
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Channels (Notifications)
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const channels = await client.channels.list("project-id")
|
|
110
|
+
const channel = await client.channels.create("project-id", {
|
|
111
|
+
type: "SLACK_WEBHOOK",
|
|
112
|
+
name: "Slack Alerts",
|
|
113
|
+
config: { webhookUrl: "https://hooks.slack.com/..." },
|
|
114
|
+
})
|
|
115
|
+
await client.channels.update("project-id", "channel-id", { name: "New Name" })
|
|
116
|
+
await client.channels.test("project-id", "channel-id") // Test notification
|
|
117
|
+
await client.channels.delete("project-id", "channel-id")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Incidents
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const incidents = await client.incidents.list("project-id")
|
|
124
|
+
const incidents = await client.incidents.list("project-id", { status: "INVESTIGATING" })
|
|
125
|
+
|
|
126
|
+
const incident = await client.incidents.create("project-id", {
|
|
127
|
+
title: "API Degradation",
|
|
128
|
+
impact: "MINOR",
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
await client.incidents.update("project-id", "incident-id", { status: "IDENTIFIED" })
|
|
132
|
+
|
|
133
|
+
// Add status update
|
|
134
|
+
await client.incidents.addUpdate("project-id", "incident-id", {
|
|
135
|
+
status: "MONITORING",
|
|
136
|
+
message: "Fix deployed, monitoring for stability",
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
await client.incidents.delete("project-id", "incident-id")
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Maintenance Windows
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const windows = await client.maintenance.list("project-id")
|
|
146
|
+
const windows = await client.maintenance.list("project-id", { upcoming: true })
|
|
147
|
+
|
|
148
|
+
const maintenance = await client.maintenance.create("project-id", {
|
|
149
|
+
title: "Database Migration",
|
|
150
|
+
startsAt: "2024-01-15T03:00:00Z",
|
|
151
|
+
endsAt: "2024-01-15T05:00:00Z",
|
|
152
|
+
checkIds: ["check-1", "check-2"],
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
await client.maintenance.update("project-id", "maintenance-id", { title: "Updated Title" })
|
|
156
|
+
await client.maintenance.delete("project-id", "maintenance-id")
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Organizations
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const orgs = await client.organizations.list()
|
|
163
|
+
const org = await client.organizations.get("org-id")
|
|
164
|
+
const org = await client.organizations.create({ name: "My Org", slug: "my-org" })
|
|
165
|
+
await client.organizations.update("org-id", { autoCreateIncidents: true })
|
|
166
|
+
await client.organizations.delete("org-id")
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### API Keys
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
const keys = await client.apiKeys.list("project-id")
|
|
173
|
+
const newKey = await client.apiKeys.create("project-id", { name: "CI/CD" })
|
|
174
|
+
console.log(newKey.key) // Full key shown only once
|
|
175
|
+
await client.apiKeys.delete("project-id", "key-id")
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Error Handling
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { SpanwiseError, NotFoundError, UnauthorizedError } from "@spanwise/sdk"
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
await client.checks.get("invalid-id")
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (error instanceof NotFoundError) {
|
|
187
|
+
console.log("Check not found")
|
|
188
|
+
} else if (error instanceof UnauthorizedError) {
|
|
189
|
+
console.log("Invalid API key")
|
|
190
|
+
} else if (error instanceof SpanwiseError) {
|
|
191
|
+
console.log(`Error: ${error.message} (${error.code})`)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Types
|
|
197
|
+
|
|
198
|
+
All types are exported for TypeScript users:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import type {
|
|
202
|
+
Check,
|
|
203
|
+
CheckStatus,
|
|
204
|
+
Channel,
|
|
205
|
+
ChannelType,
|
|
206
|
+
Incident,
|
|
207
|
+
IncidentStatus,
|
|
208
|
+
Maintenance,
|
|
209
|
+
Organization,
|
|
210
|
+
Project,
|
|
211
|
+
} from "@spanwise/sdk"
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ApiKeysClient } from "./resources/api-keys.js";
|
|
2
|
+
import { ChannelsClient } from "./resources/channels.js";
|
|
3
|
+
import { ChecksClient } from "./resources/checks.js";
|
|
4
|
+
import { OrganizationsClient } from "./resources/organizations.js";
|
|
5
|
+
import { ProjectsClient } from "./resources/projects.js";
|
|
6
|
+
import type { PingOptions, SpanInterface, SpanOptions, TraceOptions, VidentConfig, WrapOptions } from "./types.js";
|
|
7
|
+
export declare class Vident {
|
|
8
|
+
private readonly baseUrl;
|
|
9
|
+
private readonly apiKey;
|
|
10
|
+
private readonly timeout;
|
|
11
|
+
private readonly retries;
|
|
12
|
+
private readonly enabled;
|
|
13
|
+
private readonly tracingClient;
|
|
14
|
+
private readonly metricsClient;
|
|
15
|
+
private readonly eventsClient;
|
|
16
|
+
private readonly instrumentations;
|
|
17
|
+
readonly projects: ProjectsClient;
|
|
18
|
+
readonly checks: ChecksClient;
|
|
19
|
+
readonly channels: ChannelsClient;
|
|
20
|
+
readonly organizations: OrganizationsClient;
|
|
21
|
+
readonly apiKeys: ApiKeysClient;
|
|
22
|
+
constructor(config?: VidentConfig);
|
|
23
|
+
private request;
|
|
24
|
+
ping(slug: string, options?: PingOptions): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Wraps an async function with success/fail pings.
|
|
27
|
+
* Optionally sends a start ping before execution for duration tracking.
|
|
28
|
+
* Rethrows the original error after sending fail ping.
|
|
29
|
+
* If no apiKey configured, just runs the function without pings.
|
|
30
|
+
*/
|
|
31
|
+
wrap<T>(slug: string, fn: () => Promise<T>, options?: WrapOptions): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Wrap an async operation with tracing.
|
|
34
|
+
* Automatically handles span creation and error recording.
|
|
35
|
+
*/
|
|
36
|
+
trace<T>(name: string, fn: (span: SpanInterface) => Promise<T>, options?: TraceOptions): Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Manually start a span. Call span.end() when done.
|
|
39
|
+
*/
|
|
40
|
+
startSpan(name: string, options?: SpanOptions): SpanInterface;
|
|
41
|
+
/**
|
|
42
|
+
* Capture an error without an associated span.
|
|
43
|
+
*/
|
|
44
|
+
captureError(error: Error, context?: Record<string, string>): void;
|
|
45
|
+
/**
|
|
46
|
+
* Flush pending spans immediately.
|
|
47
|
+
*/
|
|
48
|
+
flushSpans(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Record a counter metric (increments by value, default 1).
|
|
51
|
+
*/
|
|
52
|
+
counter(name: string, value?: number, attributes?: Record<string, string>): void;
|
|
53
|
+
/**
|
|
54
|
+
* Record a gauge metric (point-in-time value).
|
|
55
|
+
*/
|
|
56
|
+
gauge(name: string, value: number, attributes?: Record<string, string>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Record a histogram metric (for distributions).
|
|
59
|
+
*/
|
|
60
|
+
histogram(name: string, value: number, attributes?: Record<string, string>): void;
|
|
61
|
+
/**
|
|
62
|
+
* Simple metric helper (defaults to gauge).
|
|
63
|
+
*/
|
|
64
|
+
metric(name: string, value: number, attributes?: Record<string, string>): void;
|
|
65
|
+
/**
|
|
66
|
+
* Flush pending metrics immediately.
|
|
67
|
+
*/
|
|
68
|
+
flushMetrics(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Record a structured event with info level.
|
|
71
|
+
* Auto-links to active trace if available.
|
|
72
|
+
*/
|
|
73
|
+
event(name: string, message: string, attributes?: Record<string, string>): void;
|
|
74
|
+
/**
|
|
75
|
+
* Record a debug-level event.
|
|
76
|
+
*/
|
|
77
|
+
debug(name: string, message: string, attributes?: Record<string, string>): void;
|
|
78
|
+
/**
|
|
79
|
+
* Record an info-level event.
|
|
80
|
+
*/
|
|
81
|
+
info(name: string, message: string, attributes?: Record<string, string>): void;
|
|
82
|
+
/**
|
|
83
|
+
* Record a warn-level event.
|
|
84
|
+
*/
|
|
85
|
+
warn(name: string, message: string, attributes?: Record<string, string>): void;
|
|
86
|
+
/**
|
|
87
|
+
* Record an error-level event.
|
|
88
|
+
*/
|
|
89
|
+
errorEvent(name: string, message: string, attributes?: Record<string, string>): void;
|
|
90
|
+
/**
|
|
91
|
+
* Flush pending events immediately.
|
|
92
|
+
*/
|
|
93
|
+
flushEvents(): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Destroy the client and clean up resources.
|
|
96
|
+
* Stops any pending flush timers and discards pending data.
|
|
97
|
+
*/
|
|
98
|
+
destroy(): void;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAGpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAA;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAGxD,OAAO,KAAK,EACX,WAAW,EACX,aAAa,EACb,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,MAAM,YAAY,CAAA;AAanB,qBAAa,MAAM;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,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AI;IAIP;;OAEG;IACH,UAAU,CACT,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,IAAI;IAIP;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAIlC;;;OAGG;IACH,OAAO,IAAI,IAAI;CAQf"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { BadRequestError } from "./errors.js";
|
|
2
|
+
import { fetchWithRetry, handleResponse } from "./http.js";
|
|
3
|
+
import { ApiKeysClient } from "./resources/api-keys.js";
|
|
4
|
+
import { ChannelsClient } from "./resources/channels.js";
|
|
5
|
+
import { ChecksClient } from "./resources/checks.js";
|
|
6
|
+
import { EventsClient } from "./resources/events.js";
|
|
7
|
+
import { MetricsClient } from "./resources/metrics.js";
|
|
8
|
+
import { OrganizationsClient } from "./resources/organizations.js";
|
|
9
|
+
import { ProjectsClient } from "./resources/projects.js";
|
|
10
|
+
import { TracingClient } from "./resources/tracing.js";
|
|
11
|
+
import { alwaysOnSampler, parentBasedSampler } from "./sampler.js";
|
|
12
|
+
// No-op span for disabled mode - zero overhead
|
|
13
|
+
const noopSpan = {
|
|
14
|
+
spanId: "",
|
|
15
|
+
traceId: "",
|
|
16
|
+
name: "",
|
|
17
|
+
end: () => { },
|
|
18
|
+
setAttributes: () => { },
|
|
19
|
+
updateName: () => { },
|
|
20
|
+
context: () => ({ traceId: "", spanId: "" }),
|
|
21
|
+
};
|
|
22
|
+
export class Vident {
|
|
23
|
+
baseUrl;
|
|
24
|
+
apiKey;
|
|
25
|
+
timeout;
|
|
26
|
+
retries;
|
|
27
|
+
enabled;
|
|
28
|
+
tracingClient;
|
|
29
|
+
metricsClient;
|
|
30
|
+
eventsClient;
|
|
31
|
+
instrumentations;
|
|
32
|
+
projects;
|
|
33
|
+
checks;
|
|
34
|
+
channels;
|
|
35
|
+
organizations;
|
|
36
|
+
apiKeys;
|
|
37
|
+
constructor(config = {}) {
|
|
38
|
+
// Determine if SDK should be enabled
|
|
39
|
+
// Disabled when: explicit enabled:false, or no apiKey
|
|
40
|
+
this.enabled = config.enabled !== false && !!config.apiKey;
|
|
41
|
+
if (config.apiKey && !config.apiKey.startsWith("sw_")) {
|
|
42
|
+
throw new BadRequestError("Invalid API key format. Expected key starting with 'sw_'");
|
|
43
|
+
}
|
|
44
|
+
this.apiKey = config.apiKey;
|
|
45
|
+
this.baseUrl = config.baseUrl ?? "https://api.vident.dev";
|
|
46
|
+
this.timeout = config.timeout ?? 30000;
|
|
47
|
+
this.retries = config.retries ?? 2;
|
|
48
|
+
const request = this.request.bind(this);
|
|
49
|
+
this.projects = new ProjectsClient(request);
|
|
50
|
+
this.checks = new ChecksClient(request);
|
|
51
|
+
this.channels = new ChannelsClient(request);
|
|
52
|
+
this.organizations = new OrganizationsClient(request);
|
|
53
|
+
this.apiKeys = new ApiKeysClient(request);
|
|
54
|
+
// When disabled: no clients, no timers, no async hooks
|
|
55
|
+
if (!this.enabled) {
|
|
56
|
+
this.tracingClient = null;
|
|
57
|
+
this.metricsClient = null;
|
|
58
|
+
this.eventsClient = null;
|
|
59
|
+
this.instrumentations = [];
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// Default: parent-based with 100% root sampling
|
|
63
|
+
const sampler = config.sampler ?? parentBasedSampler({ root: alwaysOnSampler() });
|
|
64
|
+
this.tracingClient = new TracingClient(request, config.serviceName ?? "unknown", this.enabled, sampler);
|
|
65
|
+
this.metricsClient = new MetricsClient(request, config.serviceName ?? "unknown", this.enabled);
|
|
66
|
+
this.eventsClient = new EventsClient(request, config.serviceName ?? "unknown", this.enabled);
|
|
67
|
+
this.instrumentations = config.instrumentations ?? [];
|
|
68
|
+
for (const inst of this.instrumentations) {
|
|
69
|
+
inst.init(this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async request(method, path, body) {
|
|
73
|
+
if (!this.enabled) {
|
|
74
|
+
throw new BadRequestError("API key is required for management APIs. Set apiKey in Vident config.");
|
|
75
|
+
}
|
|
76
|
+
const response = await fetchWithRetry(`${this.baseUrl}${path}`, {
|
|
77
|
+
method,
|
|
78
|
+
headers: {
|
|
79
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
},
|
|
82
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
83
|
+
}, this.timeout, this.retries);
|
|
84
|
+
return handleResponse(response);
|
|
85
|
+
}
|
|
86
|
+
async ping(slug, options = {}) {
|
|
87
|
+
if (!this.enabled)
|
|
88
|
+
return;
|
|
89
|
+
const { type = "success", body } = options;
|
|
90
|
+
let path = `/ping/${slug}`;
|
|
91
|
+
if (type === "start")
|
|
92
|
+
path += "/start";
|
|
93
|
+
else if (type === "fail")
|
|
94
|
+
path += "/fail";
|
|
95
|
+
try {
|
|
96
|
+
const response = await fetchWithRetry(`${this.baseUrl}${path}`, {
|
|
97
|
+
method: body ? "POST" : "GET",
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
100
|
+
},
|
|
101
|
+
body: body ?? undefined,
|
|
102
|
+
}, this.timeout, this.retries);
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
console.warn(`[vident] ping failed for "${slug}": ${response.status}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
console.warn(`[vident] ping failed for "${slug}": ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Wraps an async function with success/fail pings.
|
|
113
|
+
* Optionally sends a start ping before execution for duration tracking.
|
|
114
|
+
* Rethrows the original error after sending fail ping.
|
|
115
|
+
* If no apiKey configured, just runs the function without pings.
|
|
116
|
+
*/
|
|
117
|
+
async wrap(slug, fn, options = {}) {
|
|
118
|
+
if (!this.enabled)
|
|
119
|
+
return fn();
|
|
120
|
+
const { trackDuration = false } = options;
|
|
121
|
+
if (trackDuration) {
|
|
122
|
+
await this.ping(slug, { type: "start" });
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
// Wrap in trace to create span context for child operations
|
|
126
|
+
const result = await this.trace(`cron:${slug}`, () => fn(), {
|
|
127
|
+
kind: "internal",
|
|
128
|
+
});
|
|
129
|
+
await this.ping(slug, { type: "success" });
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
const errorMessage = error instanceof Error
|
|
134
|
+
? `${error.name}: ${error.message}`
|
|
135
|
+
: String(error);
|
|
136
|
+
await this.ping(slug, { type: "fail", body: errorMessage });
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Wrap an async operation with tracing.
|
|
142
|
+
* Automatically handles span creation and error recording.
|
|
143
|
+
*/
|
|
144
|
+
async trace(name, fn, options) {
|
|
145
|
+
if (!this.tracingClient)
|
|
146
|
+
return fn(noopSpan);
|
|
147
|
+
return this.tracingClient.trace(name, fn, options);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Manually start a span. Call span.end() when done.
|
|
151
|
+
*/
|
|
152
|
+
startSpan(name, options) {
|
|
153
|
+
if (!this.tracingClient)
|
|
154
|
+
return noopSpan;
|
|
155
|
+
return this.tracingClient.startSpan(name, options);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Capture an error without an associated span.
|
|
159
|
+
*/
|
|
160
|
+
captureError(error, context) {
|
|
161
|
+
this.tracingClient?.captureError(error, context);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Flush pending spans immediately.
|
|
165
|
+
*/
|
|
166
|
+
async flushSpans() {
|
|
167
|
+
return this.tracingClient?.flush();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Record a counter metric (increments by value, default 1).
|
|
171
|
+
*/
|
|
172
|
+
counter(name, value, attributes) {
|
|
173
|
+
this.metricsClient?.counter(name, value, attributes);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Record a gauge metric (point-in-time value).
|
|
177
|
+
*/
|
|
178
|
+
gauge(name, value, attributes) {
|
|
179
|
+
this.metricsClient?.gauge(name, value, attributes);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Record a histogram metric (for distributions).
|
|
183
|
+
*/
|
|
184
|
+
histogram(name, value, attributes) {
|
|
185
|
+
this.metricsClient?.histogram(name, value, attributes);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Simple metric helper (defaults to gauge).
|
|
189
|
+
*/
|
|
190
|
+
metric(name, value, attributes) {
|
|
191
|
+
this.metricsClient?.metric(name, value, attributes);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Flush pending metrics immediately.
|
|
195
|
+
*/
|
|
196
|
+
async flushMetrics() {
|
|
197
|
+
return this.metricsClient?.flush();
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Record a structured event with info level.
|
|
201
|
+
* Auto-links to active trace if available.
|
|
202
|
+
*/
|
|
203
|
+
event(name, message, attributes) {
|
|
204
|
+
this.eventsClient?.event(name, message, attributes);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Record a debug-level event.
|
|
208
|
+
*/
|
|
209
|
+
debug(name, message, attributes) {
|
|
210
|
+
this.eventsClient?.debug(name, message, attributes);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Record an info-level event.
|
|
214
|
+
*/
|
|
215
|
+
info(name, message, attributes) {
|
|
216
|
+
this.eventsClient?.info(name, message, attributes);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Record a warn-level event.
|
|
220
|
+
*/
|
|
221
|
+
warn(name, message, attributes) {
|
|
222
|
+
this.eventsClient?.warn(name, message, attributes);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Record an error-level event.
|
|
226
|
+
*/
|
|
227
|
+
errorEvent(name, message, attributes) {
|
|
228
|
+
this.eventsClient?.error(name, message, attributes);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Flush pending events immediately.
|
|
232
|
+
*/
|
|
233
|
+
async flushEvents() {
|
|
234
|
+
return this.eventsClient?.flush();
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Destroy the client and clean up resources.
|
|
238
|
+
* Stops any pending flush timers and discards pending data.
|
|
239
|
+
*/
|
|
240
|
+
destroy() {
|
|
241
|
+
for (const inst of this.instrumentations) {
|
|
242
|
+
inst.destroy?.();
|
|
243
|
+
}
|
|
244
|
+
this.tracingClient?.destroy();
|
|
245
|
+
this.metricsClient?.destroy();
|
|
246
|
+
this.eventsClient?.destroy();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAA;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;A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AAI,CAAC,YAAY,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC,CAAA;IACpD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,WAAW;QAChB,OAAO,IAAI,CAAC,YAAY,EAAE,KAAK,EAAE,CAAA;IAClC,CAAC;IAED;;;OAGG;IACH,OAAO;QACN,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC1C,IAAI,CAAC,OAAO,EAAE,EAAE,CAAA;QACjB,CAAC;QACD,IAAI,CAAC,aAAa,EAAE,OAAO,EAAE,CAAA;QAC7B,IAAI,CAAC,aAAa,EAAE,OAAO,EAAE,CAAA;QAC7B,IAAI,CAAC,YAAY,EAAE,OAAO,EAAE,CAAA;IAC7B,CAAC;CACD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SpanInterface } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Enable debug mode to trace context propagation issues.
|
|
4
|
+
* When enabled, logs a stack trace whenever a span is created without parent context.
|
|
5
|
+
*/
|
|
6
|
+
export declare function enableContextDebug(): void;
|
|
7
|
+
/**
|
|
8
|
+
* Check if debug mode is enabled.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isContextDebugEnabled(): boolean;
|
|
11
|
+
export declare function getActiveSpan(): SpanInterface | undefined;
|
|
12
|
+
export declare function isTracingSuppressed(): boolean;
|
|
13
|
+
export declare function runWithSpan<T>(span: SpanInterface, fn: () => T): T;
|
|
14
|
+
export declare function suppressTracing<T>(fn: () => T): T;
|
|
15
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAoB/C;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,OAAO,CAE/C;AAED,wBAAgB,aAAa,IAAI,aAAa,GAAG,SAAS,CAEzD;AAED,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAIlE;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAIjD"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
// Lazy-initialize to avoid conflicts with test runners (Vitest, Jest)
|
|
3
|
+
// that also use async_hooks for module mocking
|
|
4
|
+
let store = null;
|
|
5
|
+
function getStore() {
|
|
6
|
+
if (!store) {
|
|
7
|
+
store = new AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
return store;
|
|
10
|
+
}
|
|
11
|
+
let debugMode = false;
|
|
12
|
+
/**
|
|
13
|
+
* Enable debug mode to trace context propagation issues.
|
|
14
|
+
* When enabled, logs a stack trace whenever a span is created without parent context.
|
|
15
|
+
*/
|
|
16
|
+
export function enableContextDebug() {
|
|
17
|
+
debugMode = true;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if debug mode is enabled.
|
|
21
|
+
*/
|
|
22
|
+
export function isContextDebugEnabled() {
|
|
23
|
+
return debugMode;
|
|
24
|
+
}
|
|
25
|
+
export function getActiveSpan() {
|
|
26
|
+
return getStore().getStore()?.span;
|
|
27
|
+
}
|
|
28
|
+
export function isTracingSuppressed() {
|
|
29
|
+
return getStore().getStore()?.suppressed === true;
|
|
30
|
+
}
|
|
31
|
+
export function runWithSpan(span, fn) {
|
|
32
|
+
const s = getStore();
|
|
33
|
+
const current = s.getStore();
|
|
34
|
+
return s.run({ ...current, span }, fn);
|
|
35
|
+
}
|
|
36
|
+
export function suppressTracing(fn) {
|
|
37
|
+
const s = getStore();
|
|
38
|
+
const current = s.getStore();
|
|
39
|
+
return s.run({ ...current, suppressed: true }, fn);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAQpD,sEAAsE;AACtE,+CAA+C;AAC/C,IAAI,KAAK,GAA6C,IAAI,CAAA;AAE1D,SAAS,QAAQ;IAChB,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,KAAK,GAAG,IAAI,iBAAiB,EAAkB,CAAA;IAChD,CAAC;IACD,OAAO,KAAK,CAAA;AACb,CAAC;AAED,IAAI,SAAS,GAAG,KAAK,CAAA;AAErB;;;GAGG;AACH,MAAM,UAAU,kBAAkB;IACjC,SAAS,GAAG,IAAI,CAAA;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACpC,OAAO,SAAS,CAAA;AACjB,CAAC;AAED,MAAM,UAAU,aAAa;IAC5B,OAAO,QAAQ,EAAE,CAAC,QAAQ,EAAE,EAAE,IAAI,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,mBAAmB;IAClC,OAAO,QAAQ,EAAE,CAAC,QAAQ,EAAE,EAAE,UAAU,KAAK,IAAI,CAAA;AAClD,CAAC;AAED,MAAM,UAAU,WAAW,CAAI,IAAmB,EAAE,EAAW;IAC9D,MAAM,CAAC,GAAG,QAAQ,EAAE,CAAA;IACpB,MAAM,OAAO,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;IAC5B,OAAO,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;AACvC,CAAC;AAED,MAAM,UAAU,eAAe,CAAI,EAAW;IAC7C,MAAM,CAAC,GAAG,QAAQ,EAAE,CAAA;IACpB,MAAM,OAAO,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;IAC5B,OAAO,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;AACnD,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class VidentError extends Error {
|
|
2
|
+
code: string;
|
|
3
|
+
status: number;
|
|
4
|
+
constructor(message: string, code: string, status: number);
|
|
5
|
+
}
|
|
6
|
+
export declare class NotFoundError extends VidentError {
|
|
7
|
+
constructor(message?: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class UnauthorizedError extends VidentError {
|
|
10
|
+
constructor(message?: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class ForbiddenError extends VidentError {
|
|
13
|
+
constructor(message?: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class BadRequestError extends VidentError {
|
|
16
|
+
constructor(message?: string);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;IAG7B,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,MAAM;gBAFrB,OAAO,EAAE,MAAM,EACR,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM;CAKtB;AAED,qBAAa,aAAc,SAAQ,WAAW;gBACjC,OAAO,SAAuB;CAI1C;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBACrC,OAAO,SAAiB;CAIpC;AAED,qBAAa,cAAe,SAAQ,WAAW;gBAClC,OAAO,SAAc;CAIjC;AAED,qBAAa,eAAgB,SAAQ,WAAW;gBACnC,OAAO,SAAgB;CAInC"}
|