@trafficwar/node 2.2.0 → 2.2.2
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 +178 -47
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -3
- package/dist/index.d.ts +19 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,50 +26,25 @@ const trafficwar = new TrafficWar({
|
|
|
26
26
|
|
|
27
27
|
trafficwar.capture({
|
|
28
28
|
event: "http",
|
|
29
|
-
http_method: "
|
|
29
|
+
http_method: "POST",
|
|
30
30
|
label: "Checkout",
|
|
31
31
|
path: "/v1/checkout",
|
|
32
32
|
source: "backend-a",
|
|
33
|
+
span_kind: "server",
|
|
33
34
|
operation_type: "route.handler",
|
|
35
|
+
status_code: 200,
|
|
34
36
|
latency_ms: 184.2,
|
|
35
|
-
distinct_id: "
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
trafficwar.capture([
|
|
40
|
-
{
|
|
41
|
-
event: "database",
|
|
42
|
-
label: "Checkout",
|
|
43
|
-
path: "/v1/checkout",
|
|
44
|
-
source: "db-primary",
|
|
45
|
-
operation_type: "postgres.select",
|
|
46
|
-
latency_ms: 48.2,
|
|
47
|
-
distinct_id: "u_123",
|
|
48
|
-
trace_id: "tr_1",
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
event: "redis",
|
|
52
|
-
label: "Checkout",
|
|
53
|
-
path: "/v1/checkout",
|
|
54
|
-
source: "redis-1",
|
|
55
|
-
operation_type: "redis.get",
|
|
56
|
-
latency_ms: 1.4,
|
|
57
|
-
distinct_id: "u_123",
|
|
58
|
-
trace_id: "tr_1",
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
event: "s3",
|
|
62
|
-
label: "Checkout",
|
|
63
|
-
path: "/v1/checkout",
|
|
64
|
-
source: "s3",
|
|
65
|
-
operation_type: "s3.get_object",
|
|
66
|
-
latency_ms: 22.0,
|
|
67
|
-
distinct_id: "u_123",
|
|
68
|
-
trace_id: "tr_1",
|
|
37
|
+
distinct_id: "usr_7f3a91c2",
|
|
38
|
+
properties: {
|
|
39
|
+
device_id: "dev_0198e743-a2c4-7c21",
|
|
69
40
|
},
|
|
70
|
-
|
|
41
|
+
});
|
|
71
42
|
```
|
|
72
43
|
|
|
44
|
+
`event`, `source`, and `span_kind` together decide which tier this span appears
|
|
45
|
+
on. Read [Traces and tiers](#traces-and-tiers) before sending several spans per
|
|
46
|
+
request.
|
|
47
|
+
|
|
73
48
|
`capture` accepts one event or a non-empty array. It validates, snapshots, and
|
|
74
49
|
enqueues the input synchronously, then returns `void`; it does not return an
|
|
75
50
|
`IngestResult` or wait for the network.
|
|
@@ -77,7 +52,8 @@ enqueues the input synchronously, then returns `void`; it does not return an
|
|
|
77
52
|
`Date` timestamps are serialized as RFC3339 without changing the caller's
|
|
78
53
|
object. String timestamps must be RFC3339; numeric timestamps are integer epoch
|
|
79
54
|
milliseconds. If `timestamp` is omitted, the SDK sets it to the current time at
|
|
80
|
-
capture
|
|
55
|
+
capture, which is right for a standalone event and wrong for a trace captured
|
|
56
|
+
after the request has already finished.
|
|
81
57
|
|
|
82
58
|
## Automatic batching
|
|
83
59
|
|
|
@@ -118,15 +94,170 @@ that omits one. A caller may override it with any valid UUID. Caller-owned
|
|
|
118
94
|
objects are never modified.
|
|
119
95
|
|
|
120
96
|
Canonical `event` categories are `http`, `database`, `redis`, and `s3`. Use
|
|
121
|
-
`source` for the emitting host (`backend-a`, `db-primary`,
|
|
122
|
-
`https://app.example.com`), `label` for the human route
|
|
123
|
-
`path` for the route URL (`/v1/checkout`), and
|
|
124
|
-
concrete work (`route.handler`, `postgres.select`,
|
|
125
|
-
`s3.get_object`). For HTTP events, `http_method` is trimmed and
|
|
126
|
-
uppercase and must be a 1–64-character RFC HTTP token. Spans of
|
|
127
|
-
share `trace_id`. `span_kind` is
|
|
128
|
-
|
|
129
|
-
replace `source
|
|
97
|
+
`source` for the emitting host or dependency (`backend-a`, `db-primary`,
|
|
98
|
+
`redis-1`, `ovh-s3`, `https://app.example.com`), `label` for the human route
|
|
99
|
+
name (`Checkout`), `path` for the route URL (`/v1/checkout`), and
|
|
100
|
+
`operation_type` for the concrete work (`route.handler`, `postgres.select`,
|
|
101
|
+
`redis.get`, `s3.get_object`). For HTTP events, `http_method` is trimmed and
|
|
102
|
+
normalized to uppercase and must be a 1–64-character RFC HTTP token. Spans of
|
|
103
|
+
one request share `trace_id`. `span_kind` is one of `server`, `client`,
|
|
104
|
+
`producer`, `consumer`, or `internal`, lowercase; any other value is discarded.
|
|
105
|
+
It does not replace `source`, and it is not decorative: `event`, `source`, and
|
|
106
|
+
`span_kind` are the three fields that place a span on a tier.
|
|
107
|
+
|
|
108
|
+
## Traces and tiers
|
|
109
|
+
|
|
110
|
+
Spans of one request share `trace_id` and `label`. There is no `span_id` or
|
|
111
|
+
`parent_span_id` on the wire, so TrafficWar places each span on a tier from its
|
|
112
|
+
own fields, in this order:
|
|
113
|
+
|
|
114
|
+
1. `event` is `database`, `redis`, or `s3` — **infrastructure**.
|
|
115
|
+
2. `event` is `http` and `source` is an absolute `http(s)://` URL, or starts
|
|
116
|
+
with `web`, `browser`, `client`, `edge`, or `frontend` followed by a
|
|
117
|
+
separator or nothing (`web`, `web-eu`, `frontend.eu`, but not `webhooks`)
|
|
118
|
+
— **edge**.
|
|
119
|
+
3. `event` is `http` and `source` contains `api`, `backend`, or `server` as a
|
|
120
|
+
token delimited by `-`, `_`, or `.` — **backend**.
|
|
121
|
+
4. `event` is `http` and `span_kind` is `server` — **backend**; `client` —
|
|
122
|
+
**edge**.
|
|
123
|
+
5. Anything else — **edge**.
|
|
124
|
+
|
|
125
|
+
`source` is compared case-insensitively and is tested before `span_kind`, so a
|
|
126
|
+
`source` such as `api.example.com` or `backend-a` pins a span to the backend
|
|
127
|
+
tier regardless of its `span_kind`. Sending your own API hostname as the
|
|
128
|
+
`source` of a browser-facing span is the most common way to get a span on the
|
|
129
|
+
wrong tier. `operation_type` never affects tier placement; it names the
|
|
130
|
+
operation dot on S3 stations. Only `server` and `client` select a tier:
|
|
131
|
+
`producer`, `consumer`, and `internal` fall through to rule 5, so an `http`
|
|
132
|
+
span with one of those kinds lands on the edge tier.
|
|
133
|
+
|
|
134
|
+
On the edge and backend tiers the station is named by `label`, and `source`
|
|
135
|
+
becomes an instance dot inside that station. Infrastructure stations are named
|
|
136
|
+
from `event` and `source`, so `db-primary`, `redis-1`, and `receipts.ovh-s3`
|
|
137
|
+
each get their own station.
|
|
138
|
+
|
|
139
|
+
### Send a complete trace
|
|
140
|
+
|
|
141
|
+
Emit dependency spans first and the edge span last, and keep inclusive
|
|
142
|
+
durations nested: the sum of the dependency spans must not exceed the server
|
|
143
|
+
span, which must not exceed the client span. Set `timestamp` on every span of a
|
|
144
|
+
trace. A server normally captures all of its spans after the request has
|
|
145
|
+
finished, and the default capture-time timestamp would collapse them onto one
|
|
146
|
+
instant, leaving the waterfall with no chronological axis.
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const trace_id = crypto.randomUUID();
|
|
150
|
+
const startedAt = Date.now();
|
|
151
|
+
const at = (offsetMs: number) => new Date(startedAt + offsetMs);
|
|
152
|
+
const label = "Checkout";
|
|
153
|
+
const path = "/v1/checkout";
|
|
154
|
+
const distinct_id = "usr_7f3a91c2";
|
|
155
|
+
|
|
156
|
+
trafficwar.capture([
|
|
157
|
+
{ event: "database", label, path, distinct_id, trace_id,
|
|
158
|
+
source: "db-primary", operation_type: "postgres.select",
|
|
159
|
+
span_kind: "client", timestamp: at(20), latency_ms: 48.2 },
|
|
160
|
+
{ event: "redis", label, path, distinct_id, trace_id,
|
|
161
|
+
source: "redis-1", operation_type: "redis.get",
|
|
162
|
+
span_kind: "client", timestamp: at(70), latency_ms: 1.4 },
|
|
163
|
+
{ event: "s3", label, path, distinct_id, trace_id,
|
|
164
|
+
source: "receipts.ovh-s3", operation_type: "s3.put_object",
|
|
165
|
+
span_kind: "client", timestamp: at(75), latency_ms: 22.0 },
|
|
166
|
+
{ event: "http", label, path, distinct_id, trace_id,
|
|
167
|
+
source: "backend-a", operation_type: "route.handler",
|
|
168
|
+
span_kind: "server", timestamp: at(8), latency_ms: 160,
|
|
169
|
+
http_method: "POST", status_code: 200 },
|
|
170
|
+
{ event: "http", label, path, distinct_id, trace_id,
|
|
171
|
+
source: "https://app.example.com", operation_type: "http.request",
|
|
172
|
+
span_kind: "client", timestamp: at(0), latency_ms: 184.2,
|
|
173
|
+
http_method: "POST", status_code: 200 },
|
|
174
|
+
]);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
That trace renders as one journey: an edge station for `Checkout` with
|
|
178
|
+
`https://app.example.com` as its caller, a backend station for `Checkout`
|
|
179
|
+
served by `backend-a`, and three infrastructure stations beneath it.
|
|
180
|
+
|
|
181
|
+
A single-span request needs none of this. One `http` span with
|
|
182
|
+
`span_kind: "server"` is a complete, valid event.
|
|
183
|
+
|
|
184
|
+
### Edge spans emitted by a server
|
|
185
|
+
|
|
186
|
+
A server cannot measure a browser's round trip. If you emit the edge span from
|
|
187
|
+
your backend, make it the widest interval you actually measured — the full
|
|
188
|
+
server-side hop including routing and middleware — and let the server span
|
|
189
|
+
cover only the handler. Do not invent a client round trip, and do not drop
|
|
190
|
+
`latency_ms` to sidestep the problem: an absent value is stored as `0` and
|
|
191
|
+
enters the latency histogram. If you have nothing wider than the handler to
|
|
192
|
+
report, send the server span alone. A trace does not require an edge span.
|
|
193
|
+
|
|
194
|
+
### Two cautions
|
|
195
|
+
|
|
196
|
+
`source` is part of the metric aggregation grain. When you derive it from
|
|
197
|
+
`Origin` or `Referer`, those are caller-controlled values: match them against
|
|
198
|
+
an allowlist of hostnames you expect and collapse the rest to a single value
|
|
199
|
+
such as `web-other`, rather than forwarding arbitrary header content.
|
|
200
|
+
|
|
201
|
+
There is no category for outbound third-party HTTP. A call your backend makes
|
|
202
|
+
to another vendor's API is `event: "http"` with `span_kind: "client"`, which is
|
|
203
|
+
the edge signature, so it renders on the edge tier beside your real callers.
|
|
204
|
+
|
|
205
|
+
## Actor identity and application errors
|
|
206
|
+
|
|
207
|
+
Use a stable pseudonymous `distinct_id` derived by your backend so one actor
|
|
208
|
+
keeps the same identity across devices. Prefer an opaque internal ID or a
|
|
209
|
+
backend-only HMAC; do not send a raw email address. Keep a separate
|
|
210
|
+
installation identifier such as `device_id` in `properties` when device-level
|
|
211
|
+
investigation matters.
|
|
212
|
+
|
|
213
|
+
Send application failures with first-class outcome fields and keep the full
|
|
214
|
+
stack trace in `properties`:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
try {
|
|
218
|
+
await submitOrder();
|
|
219
|
+
} catch (caught) {
|
|
220
|
+
const error = caught instanceof Error ? caught : new Error(String(caught));
|
|
221
|
+
|
|
222
|
+
trafficwar.capture({
|
|
223
|
+
event: "http",
|
|
224
|
+
label: "Checkout",
|
|
225
|
+
path: "/v1/checkout",
|
|
226
|
+
source: "backend-a",
|
|
227
|
+
span_kind: "server",
|
|
228
|
+
http_method: "POST",
|
|
229
|
+
status_code: 500,
|
|
230
|
+
error: error.message,
|
|
231
|
+
error_code: "CHECKOUT_FAILED",
|
|
232
|
+
distinct_id: "usr_7f3a91c2",
|
|
233
|
+
properties: {
|
|
234
|
+
exception_type: error.name,
|
|
235
|
+
stack_trace: error.stack ?? error.message,
|
|
236
|
+
cause: "connection deadline exceeded",
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`properties` is opaque and does not set `is_error`. Always send `error`,
|
|
243
|
+
`error_code`, or a `status_code` of 400 or greater with diagnostic properties.
|
|
244
|
+
|
|
245
|
+
For S3, a provider alias such as `ovh-s3`, `aws-s3`, or `minio` creates one
|
|
246
|
+
provider station. Prefix it as `<bucket>.<provider>` only when the map should
|
|
247
|
+
separate buckets: `assets.ovh-s3` and `archive.ovh-s3` become distinct
|
|
248
|
+
stations, while `operation_type` values such as `s3.get_object` and
|
|
249
|
+
`s3.put_object` remain operation dots. The final dot is the reserved
|
|
250
|
+
bucket/provider separator, so provider aliases must not contain dots.
|
|
251
|
+
|
|
252
|
+
The package exports `EventCategory`, `OperationType`, `KnownS3Provider`, and
|
|
253
|
+
`S3Source` for typed wrappers. These types suggest the canonical values in
|
|
254
|
+
editors while still accepting custom event, operation, and provider aliases:
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
import type { S3Source } from "@trafficwar/node";
|
|
258
|
+
|
|
259
|
+
const assets: S3Source = "assets.ovh-s3";
|
|
260
|
+
```
|
|
130
261
|
|
|
131
262
|
## Shutdown
|
|
132
263
|
|
|
@@ -195,7 +326,7 @@ import {
|
|
|
195
326
|
TrafficWarValidationError,
|
|
196
327
|
} from "@trafficwar/node";
|
|
197
328
|
|
|
198
|
-
trafficwar.capture({ event: "
|
|
329
|
+
trafficwar.capture({ event: "http", label: "Checkout", status_code: 500 });
|
|
199
330
|
|
|
200
331
|
try {
|
|
201
332
|
await trafficwar.close();
|