@trafficwar/node 2.2.1 → 2.2.3

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 CHANGED
@@ -26,53 +26,25 @@ const trafficwar = new TrafficWar({
26
26
 
27
27
  trafficwar.capture({
28
28
  event: "http",
29
- http_method: "GET",
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
37
  distinct_id: "usr_7f3a91c2",
36
- trace_id: "tr_1",
37
38
  properties: {
38
39
  device_id: "dev_0198e743-a2c4-7c21",
39
40
  },
40
41
  });
41
-
42
- trafficwar.capture([
43
- {
44
- event: "database",
45
- label: "Checkout",
46
- path: "/v1/checkout",
47
- source: "db-primary",
48
- operation_type: "postgres.select",
49
- latency_ms: 48.2,
50
- distinct_id: "usr_7f3a91c2",
51
- trace_id: "tr_1",
52
- },
53
- {
54
- event: "redis",
55
- label: "Checkout",
56
- path: "/v1/checkout",
57
- source: "redis-1",
58
- operation_type: "redis.get",
59
- latency_ms: 1.4,
60
- distinct_id: "usr_7f3a91c2",
61
- trace_id: "tr_1",
62
- },
63
- {
64
- event: "s3",
65
- label: "Checkout",
66
- path: "/v1/checkout",
67
- source: "receipts.ovh-s3",
68
- operation_type: "s3.put_object",
69
- latency_ms: 22.0,
70
- distinct_id: "usr_7f3a91c2",
71
- trace_id: "tr_1",
72
- },
73
- ]);
74
42
  ```
75
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
+
76
48
  `capture` accepts one event or a non-empty array. It validates, snapshots, and
77
49
  enqueues the input synchronously, then returns `void`; it does not return an
78
50
  `IngestResult` or wait for the network.
@@ -80,7 +52,8 @@ enqueues the input synchronously, then returns `void`; it does not return an
80
52
  `Date` timestamps are serialized as RFC3339 without changing the caller's
81
53
  object. String timestamps must be RFC3339; numeric timestamps are integer epoch
82
54
  milliseconds. If `timestamp` is omitted, the SDK sets it to the current time at
83
- capture.
55
+ capture, which is right for a standalone event and wrong for a trace captured
56
+ after the request has already finished.
84
57
 
85
58
  ## Automatic batching
86
59
 
@@ -120,16 +93,136 @@ The SDK generates a process-monotonic RFC 9562 UUIDv7 `event_id` for each event
120
93
  that omits one. A caller may override it with any valid UUID. Caller-owned
121
94
  objects are never modified.
122
95
 
123
- Canonical `event` categories are `http`, `database`, `redis`, and `s3`. Use
96
+ Canonical `event` categories are `http`, `database`, `redis`, `s3`, and
97
+ `external`. Use `external` for an outbound HTTP service that belongs on the
98
+ infrastructure tier. Use
124
99
  `source` for the emitting host or dependency (`backend-a`, `db-primary`,
125
- `redis-1`, `ovh-s3`, `https://app.example.com`), `label` for the human route
126
- name (`Checkout`), `path` for the route URL (`/v1/checkout`), and
127
- `operation_type` for the concrete work (`route.handler`, `postgres.select`,
128
- `redis.get`, `s3.get_object`). For HTTP events, `http_method` is trimmed and
129
- normalized to uppercase and must be a 1–64-character RFC HTTP token. Spans of
130
- one request share `trace_id`. `span_kind` is separate, optional tracing
131
- semantics (`server`, `client`, `producer`, `consumer`, or `internal`); it does
132
- not replace `source`.
100
+ `redis-1`, `ovh-s3`, `google-routes`, `https://app.example.com`), `label` for
101
+ the human route name (`Checkout`), `path` for the route URL
102
+ (`/v1/checkout`), and `operation_type` for the concrete work
103
+ (`route.handler`, `postgres.select`, `redis.get`, `s3.get_object`,
104
+ `google.routes.compute`). For HTTP events, `http_method` is trimmed and
105
+ normalized to uppercase and must be a 1–64-character RFC HTTP token. Spans
106
+ of one request share `trace_id`. `span_kind` is one of `server`, `client`,
107
+ `producer`, `consumer`, or `internal`, lowercase; any other value is
108
+ discarded. It does not replace `source`, and it is not decorative: `event`,
109
+ `source`, and `span_kind` are the three fields that place a span on a tier.
110
+
111
+ ## Traces and tiers
112
+
113
+ Spans of one request share `trace_id` and `label`. There is no `span_id` or
114
+ `parent_span_id` on the wire, so TrafficWar places each span on a tier from its
115
+ own fields, in this order:
116
+
117
+ 1. `event` is `database`, `redis`, `s3`, or `external` —
118
+ **infrastructure**.
119
+ 2. `event` is `http` and `source` is an absolute `http(s)://` URL, or starts
120
+ with `web`, `browser`, `client`, `edge`, or `frontend` followed by a
121
+ separator or nothing (`web`, `web-eu`, `frontend.eu`, but not `webhooks`)
122
+ — **edge**.
123
+ 3. `event` is `http` and `source` contains `api`, `backend`, or `server` as a
124
+ token delimited by `-`, `_`, or `.` — **backend**.
125
+ 4. `event` is `http` and `span_kind` is `server` — **backend**; `client` —
126
+ **edge**.
127
+ 5. Anything else — **edge**.
128
+
129
+ `source` is compared case-insensitively and is tested before `span_kind`, so a
130
+ `source` such as `api.example.com` or `backend-a` pins a span to the backend
131
+ tier regardless of its `span_kind`. Sending your own API hostname as the
132
+ `source` of a browser-facing span is the most common way to get a span on the
133
+ wrong tier. `operation_type` never affects tier placement; it names the
134
+ concrete activity. Only `server` and `client` select an HTTP tier:
135
+ `producer`, `consumer`, and `internal` fall through to rule 5, so an `http`
136
+ span with one of those kinds lands on the edge tier.
137
+
138
+ On the edge and backend tiers the station is named by `label`, and `source`
139
+ becomes an instance dot inside that station. Infrastructure stations are named
140
+ from `event` and `source`, so `db-primary`, `redis-1`, `receipts.ovh-s3`, and
141
+ `google-routes` each get their own station.
142
+
143
+ ### Send a complete trace
144
+
145
+ Emit dependency spans first and the edge span last, and keep inclusive
146
+ durations nested: the sum of the dependency spans must not exceed the server
147
+ span, which must not exceed the client span. Set `timestamp` on every span of a
148
+ trace. A server normally captures all of its spans after the request has
149
+ finished, and the default capture-time timestamp would collapse them onto one
150
+ instant, leaving the waterfall with no chronological axis.
151
+
152
+ ```ts
153
+ const trace_id = crypto.randomUUID();
154
+ const startedAt = Date.now();
155
+ const at = (offsetMs: number) => new Date(startedAt + offsetMs);
156
+ const label = "Checkout";
157
+ const path = "/v1/checkout";
158
+ const distinct_id = "usr_7f3a91c2";
159
+
160
+ trafficwar.capture([
161
+ { event: "database", label, path, distinct_id, trace_id,
162
+ source: "db-primary", operation_type: "postgres.select",
163
+ span_kind: "client", timestamp: at(20), latency_ms: 48.2 },
164
+ { event: "redis", label, path, distinct_id, trace_id,
165
+ source: "redis-1", operation_type: "redis.get",
166
+ span_kind: "client", timestamp: at(70), latency_ms: 1.4 },
167
+ { event: "s3", label, path, distinct_id, trace_id,
168
+ source: "receipts.ovh-s3", operation_type: "s3.put_object",
169
+ span_kind: "client", timestamp: at(75), latency_ms: 22.0 },
170
+ { event: "external", label, path, distinct_id, trace_id,
171
+ source: "payment-gateway", operation_type: "payment.authorize",
172
+ span_kind: "client", timestamp: at(100), latency_ms: 20.0 },
173
+ { event: "http", label, path, distinct_id, trace_id,
174
+ source: "backend-a", operation_type: "route.handler",
175
+ span_kind: "server", timestamp: at(8), latency_ms: 160,
176
+ http_method: "POST", status_code: 200 },
177
+ { event: "http", label, path, distinct_id, trace_id,
178
+ source: "https://app.example.com", operation_type: "http.request",
179
+ span_kind: "client", timestamp: at(0), latency_ms: 184.2,
180
+ http_method: "POST", status_code: 200 },
181
+ ]);
182
+ ```
183
+
184
+ That trace renders as one journey: an edge station for `Checkout` with
185
+ `https://app.example.com` as its caller, a backend station for `Checkout`
186
+ served by `backend-a`, and four infrastructure stations beneath it.
187
+
188
+ A single-span request needs none of this. One `http` span with
189
+ `span_kind: "server"` is a complete, valid event.
190
+
191
+ ### Edge spans emitted by a server
192
+
193
+ A server cannot measure a browser's round trip. If you emit the edge span from
194
+ your backend, make it the widest interval you actually measured — the full
195
+ server-side hop including routing and middleware — and let the server span
196
+ cover only the handler. Do not invent a client round trip, and do not drop
197
+ `latency_ms` to sidestep the problem: an absent value is stored as `0` and
198
+ enters the latency histogram. If you have nothing wider than the handler to
199
+ report, send the server span alone. A trace does not require an edge span.
200
+
201
+ ### Two cautions
202
+
203
+ `source` is part of the metric aggregation grain. When you derive it from
204
+ `Origin` or `Referer`, those are caller-controlled values: match them against
205
+ an allowlist of hostnames you expect and collapse the rest to a single value
206
+ such as `web-other`, rather than forwarding arbitrary header content.
207
+
208
+ For an outbound third-party HTTP call, send `event: "external"` with
209
+ `span_kind: "client"`. Keep the provider in `source` and the API action in
210
+ `operation_type`:
211
+
212
+ ```ts
213
+ trafficwar.capture({
214
+ event: "external",
215
+ source: "google-routes",
216
+ label: "Route planner",
217
+ span_kind: "client",
218
+ operation_type: "google.routes.compute",
219
+ latency_ms: 13,
220
+ });
221
+ ```
222
+
223
+ This creates a source-keyed infrastructure station. The incoming caller remains
224
+ an `http` client span. If a request has no trusted `Origin` or `Referer`, use a
225
+ stable value such as `web-direct`; never substitute your own API `Host` header.
133
226
 
134
227
  ## Actor identity and application errors
135
228
 
@@ -150,7 +243,10 @@ try {
150
243
 
151
244
  trafficwar.capture({
152
245
  event: "http",
153
- label: "/checkout",
246
+ label: "Checkout",
247
+ path: "/v1/checkout",
248
+ source: "backend-a",
249
+ span_kind: "server",
154
250
  http_method: "POST",
155
251
  status_code: 500,
156
252
  error: error.message,
@@ -252,7 +348,7 @@ import {
252
348
  TrafficWarValidationError,
253
349
  } from "@trafficwar/node";
254
350
 
255
- trafficwar.capture({ event: "task.failed", status_code: 500 });
351
+ trafficwar.capture({ event: "http", label: "Checkout", status_code: 500 });
256
352
 
257
353
  try {
258
354
  await trafficwar.close();
package/dist/index.cjs CHANGED
@@ -480,7 +480,7 @@ function uuidv7() {
480
480
 
481
481
  // package.json
482
482
  var package_default = {
483
- version: "2.2.1"};
483
+ version: "2.2.3"};
484
484
 
485
485
  // src/client.ts
486
486
  var TRAFFICWAR_DEFAULT_BASE_URL = "https://ingest.trafficwar.tech";