@softistx/telemetry-httpyz 0.0.0-stage → 0.3.4
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 +118 -2
- package/dist/attributes/http.d.ts +51 -0
- package/dist/attributes/http.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +140 -0
- package/dist/index.js.map +11 -0
- package/dist/middleware/tracing.d.ts +57 -0
- package/dist/middleware/tracing.d.ts.map +1 -0
- package/package.json +70 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Tsala
|
|
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,119 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @softistx/telemetry-httpyz
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One client span per `@softistx/httpyz`
|
|
4
|
+
request, with the current `traceparent` on the way out — which is the half that
|
|
5
|
+
makes a trace a trace.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @softistx/telemetry @softistx/telemetry-httpyz @softistx/httpyz
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createHttpClient } from '@softistx/httpyz';
|
|
13
|
+
import { tracing } from '@softistx/telemetry-httpyz';
|
|
14
|
+
|
|
15
|
+
const api = createHttpClient({
|
|
16
|
+
baseUrl: 'https://api.example',
|
|
17
|
+
use: [tracing()],
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
That is all of it. Inside a server span — from `@softistx/telemetry-hono`, or from
|
|
22
|
+
any `span()` — the outgoing request carries the header that lets the service on
|
|
23
|
+
the other end continue the same trace, and the client span becomes a child of
|
|
24
|
+
whatever is open **now**. Nothing has to be passed down to the call site.
|
|
25
|
+
|
|
26
|
+
## What it records
|
|
27
|
+
|
|
28
|
+
| attribute | |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| `http.request.method` | uppercased, whatever the caller wrote |
|
|
31
|
+
| `url.full` | the URL, **without its userinfo** |
|
|
32
|
+
| `server.address`, `server.port` | the port only when there is one |
|
|
33
|
+
| `url.template` | the path as the caller wrote it: `/employees/{id}` |
|
|
34
|
+
| `http.operation` | the `operationId`, when the call has one |
|
|
35
|
+
| `http.response.status_code` | after the reply |
|
|
36
|
+
|
|
37
|
+
The span is named for the `operationId` when there is one, and
|
|
38
|
+
`"<METHOD> <path>"` otherwise — where the path is the template the caller wrote,
|
|
39
|
+
`/employees/{id}`, not the one it filled in. There is no cardinality problem to
|
|
40
|
+
solve here: httpyz hands over the template already.
|
|
41
|
+
|
|
42
|
+
## A client span fails at 400, not at 500
|
|
43
|
+
|
|
44
|
+
This is where a client span and a server span disagree, and the asymmetry is the
|
|
45
|
+
point. A `404` answered by a server is that server working — `@softistx/telemetry-hono`
|
|
46
|
+
records it as `ok`. The same `404` received by a caller is a call that did not do
|
|
47
|
+
what it was for, and this records it as an error. Both readings are correct, and
|
|
48
|
+
each belongs to the span on its own side of the wire.
|
|
49
|
+
|
|
50
|
+
A timeout is neither: `cancelled`, because a dashboard that counts timeouts as
|
|
51
|
+
failures is a dashboard nobody trusts.
|
|
52
|
+
|
|
53
|
+
## Options
|
|
54
|
+
|
|
55
|
+
| | default | |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| `traced` | everything | whether a call gets a span at all |
|
|
58
|
+
| `spanName` | the `operationId`, else `"<METHOD> <path>"` | |
|
|
59
|
+
| `url` | the URL without its userinfo | return `undefined` to record none |
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
tracing({
|
|
63
|
+
traced: (call) => call.path !== '/health',
|
|
64
|
+
url: (request) => new URL(request.url).origin, // a query string that carries a key
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Nothing is skipped by default, and a hook that throws costs its own answer and
|
|
69
|
+
nothing else: `traced` falls back to tracing, `spanName` to the default name,
|
|
70
|
+
`url` to recording none. A predicate that raises must not turn observability
|
|
71
|
+
into a failed call.
|
|
72
|
+
|
|
73
|
+
## API
|
|
74
|
+
|
|
75
|
+
| | |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `tracing(options)` | the middleware |
|
|
78
|
+
| `TracingOptions` | the options above |
|
|
79
|
+
| `TRACEPARENT` | `'traceparent'` |
|
|
80
|
+
| `callAttributes(request, method)` | what is known before the call is sent |
|
|
81
|
+
| `callFailed(status)`, `CLIENT_ERROR_FROM` | the 400 rule |
|
|
82
|
+
| `safeUrl(url)` | a `URL` with its credentials removed, or `undefined` |
|
|
83
|
+
| `method(call)` | the call's method, uppercased |
|
|
84
|
+
| `HTTP_METHOD`, `URL_FULL`, `SERVER_ADDRESS`, `SERVER_PORT`, `HTTP_STATUS`, `OPERATION` | the attribute names |
|
|
85
|
+
|
|
86
|
+
## Traps
|
|
87
|
+
|
|
88
|
+
- **With no telemetry anywhere, no header is sent.** A detached scope's
|
|
89
|
+
`traceparent()` is all zeros, which this library's own parser rejects and
|
|
90
|
+
which W3C calls invalid; sending it is worse than sending nothing, because a
|
|
91
|
+
strict receiver refuses the request and a lenient one starts a fresh trace
|
|
92
|
+
exactly as an absent header would. The call still goes out.
|
|
93
|
+
- **An existing `traceparent` on the request is replaced.** The header describes
|
|
94
|
+
the call being made now. A stale one — copied from an inbound request, say —
|
|
95
|
+
would attach this call to a span that has already ended.
|
|
96
|
+
- **`@softistx/httpyz` is an optional peer, and only its types are used.** Nothing
|
|
97
|
+
here imports a value from it, so this package has no runtime dependency on the
|
|
98
|
+
client at all.
|
|
99
|
+
- **The span ends when the `Response` is returned, not when its body is read.**
|
|
100
|
+
A streamed reply is recorded as the time it took to *start*.
|
|
101
|
+
- **`url.full` keeps the query string.** It is usually what tells one call from
|
|
102
|
+
another. If yours carries a key, pass a `url` hook — the userinfo is stripped
|
|
103
|
+
for you, the query is not.
|
|
104
|
+
- **A retry is a span per attempt.** `retry` and `auth` sit *outside* the
|
|
105
|
+
middlewares in httpyz — `use:` is composed inside both — so a retried call,
|
|
106
|
+
and a call replayed after a token refresh, each open a fresh span. Three
|
|
107
|
+
attempts are three spans under one parent, which is what a trace should show;
|
|
108
|
+
a middleware cannot see that it is the second one, and there is no placement
|
|
109
|
+
that gets outside `retry`.
|
|
110
|
+
- **A failure that happens after the reply arrives is not on the span.**
|
|
111
|
+
httpyz reads and validates the body *outside* the middleware chain, so a
|
|
112
|
+
`ValidationError`, an `UndeclaredStatusError` or a `ReplyStatusError` leaves
|
|
113
|
+
the client span `ok` with the status the server sent. The caller saw a
|
|
114
|
+
failure and the trace says the call was fine. `currentSpan()` — or the
|
|
115
|
+
enclosing `scope.fail(…)` — is where to put it if you need it.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Attributes } from '@softistx/telemetry';
|
|
2
|
+
/**
|
|
3
|
+
* The semantic conventions a **client** span carries.
|
|
4
|
+
*
|
|
5
|
+
* `-hono` sets four names that look the same. The duplication is deliberate and
|
|
6
|
+
* recorded in `AGENTS.md`: a shared helper would make each package depend on
|
|
7
|
+
* the other's host, and the two disagree about the thing that matters most —
|
|
8
|
+
* which statuses are failures.
|
|
9
|
+
*/
|
|
10
|
+
export declare const HTTP_METHOD = "http.request.method";
|
|
11
|
+
export declare const URL_FULL = "url.full";
|
|
12
|
+
export declare const SERVER_ADDRESS = "server.address";
|
|
13
|
+
export declare const SERVER_PORT = "server.port";
|
|
14
|
+
export declare const HTTP_STATUS = "http.response.status_code";
|
|
15
|
+
/**
|
|
16
|
+
* The path as the caller wrote it: `/employees/{id}`. An OTel convention, and
|
|
17
|
+
* the one thing that lets a backend group calls that differ only by their
|
|
18
|
+
* parameters.
|
|
19
|
+
*/
|
|
20
|
+
export declare const URL_TEMPLATE = "url.template";
|
|
21
|
+
/**
|
|
22
|
+
* The call's name, when it had one: an OpenAPI `operationId`.
|
|
23
|
+
*
|
|
24
|
+
* This name is **not** in the OTel semantic conventions — there is none for it
|
|
25
|
+
* — and it is an addition to the vocabulary this estate shares with
|
|
26
|
+
* `stx-telemetry`. Its ktor module should use the same one.
|
|
27
|
+
*/
|
|
28
|
+
export declare const OPERATION = "http.operation";
|
|
29
|
+
/**
|
|
30
|
+
* The status at which a **client** span failed — and it is `400`, not `500`.
|
|
31
|
+
*
|
|
32
|
+
* This is where a client span and a server span disagree, and the asymmetry is
|
|
33
|
+
* the point. A `404` answered by a server is that server working; the same
|
|
34
|
+
* `404` received by a caller is a call that did not do what it was for. Both
|
|
35
|
+
* readings are correct, and each belongs to the span on its own side of the
|
|
36
|
+
* wire.
|
|
37
|
+
*/
|
|
38
|
+
export declare const CLIENT_ERROR_FROM = 400;
|
|
39
|
+
export declare function callFailed(status: number): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* What is known about a call **before** it is sent.
|
|
42
|
+
*
|
|
43
|
+
* `url.full` has its **userinfo removed**: `https://user:token@api.example` is
|
|
44
|
+
* a real way to carry a credential, and a span is read by everybody who reads
|
|
45
|
+
* the dashboard. The query string is kept — it is usually what distinguishes
|
|
46
|
+
* one call from another — so pass a `url` hook if yours carries a key.
|
|
47
|
+
*/
|
|
48
|
+
export declare function callAttributes(request: Request, method: string): Attributes;
|
|
49
|
+
/** The URL as a span may repeat it: everything but the credentials. */
|
|
50
|
+
export declare function safeUrl(url: string): URL | undefined;
|
|
51
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/attributes/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,wBAAwB,CAAC;AACjD,eAAO,MAAM,QAAQ,aAAa,CAAC;AACnC,eAAO,MAAM,cAAc,mBAAmB,CAAC;AAC/C,eAAO,MAAM,WAAW,gBAAgB,CAAC;AACzC,eAAO,MAAM,WAAW,8BAA8B,CAAC;AACvD;;;;GAIG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAC3C;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,mBAAmB,CAAC;AAE1C;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,MAAM,CAAC;AAErC,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAa3E;AAED,uEAAuE;AACvE,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAUpD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// src/attributes/http.ts
|
|
2
|
+
var HTTP_METHOD = "http.request.method";
|
|
3
|
+
var URL_FULL = "url.full";
|
|
4
|
+
var SERVER_ADDRESS = "server.address";
|
|
5
|
+
var SERVER_PORT = "server.port";
|
|
6
|
+
var HTTP_STATUS = "http.response.status_code";
|
|
7
|
+
var URL_TEMPLATE = "url.template";
|
|
8
|
+
var OPERATION = "http.operation";
|
|
9
|
+
var CLIENT_ERROR_FROM = 400;
|
|
10
|
+
function callFailed(status) {
|
|
11
|
+
return status >= CLIENT_ERROR_FROM;
|
|
12
|
+
}
|
|
13
|
+
function callAttributes(request, method) {
|
|
14
|
+
const url = safeUrl(request.url);
|
|
15
|
+
return {
|
|
16
|
+
[HTTP_METHOD]: method,
|
|
17
|
+
...url === undefined ? {} : {
|
|
18
|
+
[URL_FULL]: url.href,
|
|
19
|
+
[SERVER_ADDRESS]: url.hostname,
|
|
20
|
+
...url.port === "" ? {} : { [SERVER_PORT]: Number(url.port) }
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function safeUrl(url) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = new URL(url);
|
|
27
|
+
parsed.username = "";
|
|
28
|
+
parsed.password = "";
|
|
29
|
+
return parsed;
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// src/middleware/tracing.ts
|
|
35
|
+
import { isDetached, span } from "@softistx/telemetry";
|
|
36
|
+
var TRACEPARENT = "traceparent";
|
|
37
|
+
function tracing(options = {}) {
|
|
38
|
+
const traced = guarded(options.traced ?? always, always);
|
|
39
|
+
const byDefault = guarded(defaultName, fallbackName);
|
|
40
|
+
const named = options.spanName === undefined ? byDefault : guarded(options.spanName, byDefault);
|
|
41
|
+
const url = guarded(options.url ?? defaultUrl, nothing);
|
|
42
|
+
return async (request, next, call) => {
|
|
43
|
+
if (!traced(call))
|
|
44
|
+
return next(request);
|
|
45
|
+
return span(named(call, request), { kind: "client", attributes: attributesFor(call, request, url) }, async (scope) => {
|
|
46
|
+
const outgoing = carrying(request, scope);
|
|
47
|
+
const reply = await next(outgoing);
|
|
48
|
+
record(scope, reply);
|
|
49
|
+
return reply;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function carrying(request, scope) {
|
|
54
|
+
if (isDetached(scope.context))
|
|
55
|
+
return request;
|
|
56
|
+
const traceparent = scope.traceparent();
|
|
57
|
+
try {
|
|
58
|
+
request.headers.set(TRACEPARENT, traceparent);
|
|
59
|
+
return request;
|
|
60
|
+
} catch {
|
|
61
|
+
try {
|
|
62
|
+
const headers = new Headers(request.headers);
|
|
63
|
+
headers.set(TRACEPARENT, traceparent);
|
|
64
|
+
return new Request(request, { headers });
|
|
65
|
+
} catch {
|
|
66
|
+
return request;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function record(scope, reply) {
|
|
71
|
+
let status;
|
|
72
|
+
try {
|
|
73
|
+
status = reply.status;
|
|
74
|
+
} catch {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
scope.attribute(HTTP_STATUS, status);
|
|
78
|
+
if (callFailed(status) && scope.status === "ok")
|
|
79
|
+
scope.status = "error";
|
|
80
|
+
}
|
|
81
|
+
function attributesFor(call, request, url) {
|
|
82
|
+
try {
|
|
83
|
+
return {
|
|
84
|
+
...callAttributes(request, method(call)),
|
|
85
|
+
[URL_FULL]: url(request),
|
|
86
|
+
[URL_TEMPLATE]: call.path,
|
|
87
|
+
...call.operationId === undefined ? {} : { [OPERATION]: call.operationId }
|
|
88
|
+
};
|
|
89
|
+
} catch {
|
|
90
|
+
return {};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function defaultName(call) {
|
|
94
|
+
return call.operationId ?? `${method(call)} ${call.path}`;
|
|
95
|
+
}
|
|
96
|
+
function fallbackName() {
|
|
97
|
+
return UNNAMED;
|
|
98
|
+
}
|
|
99
|
+
var UNNAMED = "HTTP";
|
|
100
|
+
function method(call) {
|
|
101
|
+
return call.method.toUpperCase();
|
|
102
|
+
}
|
|
103
|
+
function defaultUrl(request) {
|
|
104
|
+
return safeUrl(request.url)?.href;
|
|
105
|
+
}
|
|
106
|
+
function guarded(hook, fallback) {
|
|
107
|
+
return (...args) => {
|
|
108
|
+
try {
|
|
109
|
+
return hook(...args);
|
|
110
|
+
} catch {
|
|
111
|
+
return fallback(...args);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function always() {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
function nothing() {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
CLIENT_ERROR_FROM,
|
|
123
|
+
HTTP_METHOD,
|
|
124
|
+
HTTP_STATUS,
|
|
125
|
+
OPERATION,
|
|
126
|
+
SERVER_ADDRESS,
|
|
127
|
+
SERVER_PORT,
|
|
128
|
+
TRACEPARENT,
|
|
129
|
+
UNNAMED,
|
|
130
|
+
URL_FULL,
|
|
131
|
+
URL_TEMPLATE,
|
|
132
|
+
callAttributes,
|
|
133
|
+
callFailed,
|
|
134
|
+
method,
|
|
135
|
+
safeUrl,
|
|
136
|
+
tracing
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
//# debugId=BF532C3A5B20154B64756E2164756E21
|
|
140
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/attributes/http.ts", "../src/middleware/tracing.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { Attributes } from '@softistx/telemetry';\n\n/**\n * The semantic conventions a **client** span carries.\n *\n * `-hono` sets four names that look the same. The duplication is deliberate and\n * recorded in `AGENTS.md`: a shared helper would make each package depend on\n * the other's host, and the two disagree about the thing that matters most —\n * which statuses are failures.\n */\nexport const HTTP_METHOD = 'http.request.method';\nexport const URL_FULL = 'url.full';\nexport const SERVER_ADDRESS = 'server.address';\nexport const SERVER_PORT = 'server.port';\nexport const HTTP_STATUS = 'http.response.status_code';\n/**\n * The path as the caller wrote it: `/employees/{id}`. An OTel convention, and\n * the one thing that lets a backend group calls that differ only by their\n * parameters.\n */\nexport const URL_TEMPLATE = 'url.template';\n/**\n * The call's name, when it had one: an OpenAPI `operationId`.\n *\n * This name is **not** in the OTel semantic conventions — there is none for it\n * — and it is an addition to the vocabulary this estate shares with\n * `stx-telemetry`. Its ktor module should use the same one.\n */\nexport const OPERATION = 'http.operation';\n\n/**\n * The status at which a **client** span failed — and it is `400`, not `500`.\n *\n * This is where a client span and a server span disagree, and the asymmetry is\n * the point. A `404` answered by a server is that server working; the same\n * `404` received by a caller is a call that did not do what it was for. Both\n * readings are correct, and each belongs to the span on its own side of the\n * wire.\n */\nexport const CLIENT_ERROR_FROM = 400;\n\nexport function callFailed(status: number): boolean {\n\treturn status >= CLIENT_ERROR_FROM;\n}\n\n/**\n * What is known about a call **before** it is sent.\n *\n * `url.full` has its **userinfo removed**: `https://user:token@api.example` is\n * a real way to carry a credential, and a span is read by everybody who reads\n * the dashboard. The query string is kept — it is usually what distinguishes\n * one call from another — so pass a `url` hook if yours carries a key.\n */\nexport function callAttributes(request: Request, method: string): Attributes {\n\tconst url = safeUrl(request.url);\n\n\treturn {\n\t\t[HTTP_METHOD]: method,\n\t\t...(url === undefined\n\t\t\t? {}\n\t\t\t: {\n\t\t\t\t\t[URL_FULL]: url.href,\n\t\t\t\t\t[SERVER_ADDRESS]: url.hostname,\n\t\t\t\t\t...(url.port === '' ? {} : { [SERVER_PORT]: Number(url.port) }),\n\t\t\t\t}),\n\t};\n}\n\n/** The URL as a span may repeat it: everything but the credentials. */\nexport function safeUrl(url: string): URL | undefined {\n\ttry {\n\t\tconst parsed = new URL(url);\n\t\tparsed.username = '';\n\t\tparsed.password = '';\n\t\treturn parsed;\n\t} catch {\n\t\t// A URL this runtime will not parse still gets a span.\n\t\treturn undefined;\n\t}\n}\n",
|
|
6
|
+
"import type { CallContext, Middleware, Next } from '@softistx/httpyz';\nimport { isDetached, type SpanScope, span } from '@softistx/telemetry';\nimport {\n\tcallAttributes,\n\tcallFailed,\n\tHTTP_STATUS,\n\tOPERATION,\n\tsafeUrl,\n\tURL_FULL,\n\tURL_TEMPLATE,\n} from '../attributes/http';\n\n/** The header that carries the trace across the wire. */\nexport const TRACEPARENT = 'traceparent';\n\nexport interface TracingOptions {\n\t/**\n\t * Whether this call gets a span at all. Default: every call does.\n\t *\n\t * Nothing is skipped by default. A library that decides for you which calls\n\t * do not matter is a library that hides the one that did.\n\t */\n\treadonly traced?: (call: CallContext) => boolean;\n\t/**\n\t * The span's name. Default: the `operationId` when the call has one, and\n\t * `\"<METHOD> <path>\"` otherwise — where the path is the template the caller\n\t * wrote, `/employees/{id}`, not the one it filled in.\n\t */\n\treadonly spanName?: (call: CallContext, request: Request) => string;\n\t/**\n\t * What goes in `url.full`. Default: the URL with its userinfo removed.\n\t * Return `undefined` to record no URL at all.\n\t */\n\treadonly url?: (request: Request) => string | undefined;\n}\n\n/**\n * One client span per request sent, with the current `traceparent` on the way\n * out.\n *\n * Per *request*, not per call: `retry` and `auth` sit **outside** the\n * middlewares in `@softistx/httpyz`, so a retried call and a call replayed after a\n * token refresh each open a fresh span, and a middleware cannot see that it is\n * the second one. Three attempts are three spans under one parent, which is\n * what a trace should show.\n *\n * ```ts\n * const api = createHttpClient({ baseUrl, use: [tracing()] });\n * ```\n *\n * Inside a server span, this is the half that makes a trace a trace: the\n * outgoing request carries the header that lets the service on the other end\n * continue it, and it is taken from whatever span is open **now** rather than\n * from anything the caller passed. Outside any span it is a no-op — the call\n * still goes out, and nothing is emitted.\n *\n * A client span **fails at `400`**, not at `500`. A `404` answered by a server\n * is that server working; the same `404` received by a caller is a call that\n * did not do what it was for.\n */\nexport function tracing(options: TracingOptions = {}): Middleware {\n\t// Every hook is application code on the path that decides whether a call is\n\t// observed, so one that throws costs its own answer and nothing else.\n\tconst traced = guarded(options.traced ?? always, always);\n\t// A hook the application wrote falls back to the default name; the default\n\t// falls back to a constant, because a span has to be called something.\n\tconst byDefault = guarded(defaultName, fallbackName);\n\tconst named =\n\t\toptions.spanName === undefined\n\t\t\t? byDefault\n\t\t\t: guarded(options.spanName, byDefault);\n\tconst url = guarded(options.url ?? defaultUrl, nothing);\n\n\treturn async (\n\t\trequest: Request,\n\t\tnext: Next,\n\t\tcall: CallContext,\n\t): Promise<Response> => {\n\t\tif (!traced(call)) return next(request);\n\n\t\treturn span(\n\t\t\tnamed(call, request),\n\t\t\t{ kind: 'client', attributes: attributesFor(call, request, url) },\n\t\t\tasync (scope) => {\n\t\t\t\tconst outgoing = carrying(request, scope);\n\t\t\t\tconst reply = await next(outgoing);\n\n\t\t\t\trecord(scope, reply);\n\t\t\t\treturn reply;\n\t\t\t},\n\t\t);\n\t};\n}\n\n/**\n * The request with this span's `traceparent` on it.\n *\n * **A detached scope is not sent.** With no telemetry installed, `traceparent()`\n * is `00-0…0-0…0-00` — all zeros, which this library's own parser rejects and\n * which the W3C specification says is invalid. Sending it is worse than sending\n * nothing: a strict receiver refuses the request, and a lenient one starts a\n * fresh trace exactly as an absent header would.\n *\n * The header is set on the request in place, and a copy is made if that throws.\n * No runtime tested here refuses it — `@softistx/httpyz` always hands over a\n * `Request` it built itself, whose headers are mutable — so the fallback is\n * belt and braces against a host that wraps one in something stricter. The copy\n * keeps the method, the URL, the body and the signal; a call must not fail\n * because it was being traced.\n */\nfunction carrying(request: Request, scope: SpanScope): Request {\n\tif (isDetached(scope.context)) return request;\n\n\tconst traceparent = scope.traceparent();\n\n\ttry {\n\t\trequest.headers.set(TRACEPARENT, traceparent);\n\t\treturn request;\n\t} catch {\n\t\ttry {\n\t\t\tconst headers = new Headers(request.headers);\n\t\t\theaders.set(TRACEPARENT, traceparent);\n\t\t\treturn new Request(request, { headers });\n\t\t} catch {\n\t\t\t// Nothing to be done: the call goes out untraced rather than not at\n\t\t\t// all. The span is still recorded on this side.\n\t\t\treturn request;\n\t\t}\n\t}\n}\n\nfunction record(scope: SpanScope, reply: Response): void {\n\tlet status: number;\n\ttry {\n\t\tstatus = reply.status;\n\t} catch {\n\t\treturn;\n\t}\n\n\tscope.attribute(HTTP_STATUS, status);\n\tif (callFailed(status) && scope.status === 'ok') scope.status = 'error';\n}\n\n/**\n * Everything known before the call is sent. Nothing in here may throw: it runs\n * between the caller and its request, and a span that raised would be the\n * tracing library causing the failure it was opened to record.\n */\nfunction attributesFor(\n\tcall: CallContext,\n\trequest: Request,\n\turl: (request: Request) => string | undefined,\n): Record<string, unknown> {\n\ttry {\n\t\treturn {\n\t\t\t...callAttributes(request, method(call)),\n\t\t\t// The hook owns the URL, including deciding there is not one.\n\t\t\t[URL_FULL]: url(request),\n\t\t\t[URL_TEMPLATE]: call.path,\n\t\t\t...(call.operationId === undefined\n\t\t\t\t? {}\n\t\t\t\t: { [OPERATION]: call.operationId }),\n\t\t};\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nfunction defaultName(call: CallContext): string {\n\treturn call.operationId ?? `${method(call)} ${call.path}`;\n}\n\n/**\n * When even the default name could not be built. A span has to be called\n * something, and failing the call over its name would be the tracing library\n * causing the outage.\n */\nfunction fallbackName(): string {\n\treturn UNNAMED;\n}\n\n/** The name of a span whose call context could not be read. */\nexport const UNNAMED = 'HTTP';\n\n/**\n * The call's method, uppercased. `@softistx/httpyz` passes it as the caller wrote\n * it — `api.get(…)` gives `'get'` — and the semantic conventions want `GET`, so\n * a dashboard that groups by method does not end up with two of each.\n */\nexport function method(call: CallContext): string {\n\treturn call.method.toUpperCase();\n}\n\nfunction defaultUrl(request: Request): string | undefined {\n\treturn safeUrl(request.url)?.href;\n}\n\n/** A hook, and what to ask instead when it throws. */\nfunction guarded<A extends unknown[], T>(\n\thook: (...args: A) => T,\n\tfallback: (...args: A) => T,\n): (...args: A) => T {\n\treturn (...args) => {\n\t\ttry {\n\t\t\treturn hook(...args);\n\t\t} catch {\n\t\t\treturn fallback(...args);\n\t\t}\n\t};\n}\n\nfunction always(): true {\n\treturn true;\n}\n\nfunction nothing(): undefined {\n\treturn undefined;\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": "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",
|
|
9
|
+
"debugId": "BF532C3A5B20154B64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { CallContext, Middleware } from '@softistx/httpyz';
|
|
2
|
+
/** The header that carries the trace across the wire. */
|
|
3
|
+
export declare const TRACEPARENT = "traceparent";
|
|
4
|
+
export interface TracingOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether this call gets a span at all. Default: every call does.
|
|
7
|
+
*
|
|
8
|
+
* Nothing is skipped by default. A library that decides for you which calls
|
|
9
|
+
* do not matter is a library that hides the one that did.
|
|
10
|
+
*/
|
|
11
|
+
readonly traced?: (call: CallContext) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The span's name. Default: the `operationId` when the call has one, and
|
|
14
|
+
* `"<METHOD> <path>"` otherwise — where the path is the template the caller
|
|
15
|
+
* wrote, `/employees/{id}`, not the one it filled in.
|
|
16
|
+
*/
|
|
17
|
+
readonly spanName?: (call: CallContext, request: Request) => string;
|
|
18
|
+
/**
|
|
19
|
+
* What goes in `url.full`. Default: the URL with its userinfo removed.
|
|
20
|
+
* Return `undefined` to record no URL at all.
|
|
21
|
+
*/
|
|
22
|
+
readonly url?: (request: Request) => string | undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One client span per request sent, with the current `traceparent` on the way
|
|
26
|
+
* out.
|
|
27
|
+
*
|
|
28
|
+
* Per *request*, not per call: `retry` and `auth` sit **outside** the
|
|
29
|
+
* middlewares in `@softistx/httpyz`, so a retried call and a call replayed after a
|
|
30
|
+
* token refresh each open a fresh span, and a middleware cannot see that it is
|
|
31
|
+
* the second one. Three attempts are three spans under one parent, which is
|
|
32
|
+
* what a trace should show.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const api = createHttpClient({ baseUrl, use: [tracing()] });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Inside a server span, this is the half that makes a trace a trace: the
|
|
39
|
+
* outgoing request carries the header that lets the service on the other end
|
|
40
|
+
* continue it, and it is taken from whatever span is open **now** rather than
|
|
41
|
+
* from anything the caller passed. Outside any span it is a no-op — the call
|
|
42
|
+
* still goes out, and nothing is emitted.
|
|
43
|
+
*
|
|
44
|
+
* A client span **fails at `400`**, not at `500`. A `404` answered by a server
|
|
45
|
+
* is that server working; the same `404` received by a caller is a call that
|
|
46
|
+
* did not do what it was for.
|
|
47
|
+
*/
|
|
48
|
+
export declare function tracing(options?: TracingOptions): Middleware;
|
|
49
|
+
/** The name of a span whose call context could not be read. */
|
|
50
|
+
export declare const UNNAMED = "HTTP";
|
|
51
|
+
/**
|
|
52
|
+
* The call's method, uppercased. `@softistx/httpyz` passes it as the caller wrote
|
|
53
|
+
* it — `api.get(…)` gives `'get'` — and the semantic conventions want `GET`, so
|
|
54
|
+
* a dashboard that groups by method does not end up with two of each.
|
|
55
|
+
*/
|
|
56
|
+
export declare function method(call: CallContext): string;
|
|
57
|
+
//# sourceMappingURL=tracing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracing.d.ts","sourceRoot":"","sources":["../../src/middleware/tracing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAQ,MAAM,kBAAkB,CAAC;AAYtE,yDAAyD;AACzD,eAAO,MAAM,WAAW,gBAAgB,CAAC;AAEzC,MAAM,WAAW,cAAc;IAC9B;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC;IACjD;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC;IACpE;;;OAGG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;CACxD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,UAAU,CAgChE;AAyFD,+DAA+D;AAC/D,eAAO,MAAM,OAAO,SAAS,CAAC;AAE9B;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAEhD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@softistx/telemetry-httpyz",
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"description": "An @softistx/httpyz middleware for @softistx/telemetry: one client span per call, with the current traceparent on the way out",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"package.json",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"httpyz",
|
|
25
|
+
"telemetry",
|
|
26
|
+
"observability",
|
|
27
|
+
"tracing",
|
|
28
|
+
"middleware",
|
|
29
|
+
"traceparent",
|
|
30
|
+
"opentelemetry",
|
|
31
|
+
"typescript"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/softistx/nxgt-telemetry.git",
|
|
36
|
+
"directory": "packages/telemetry-httpyz"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"registry": "https://registry.npmjs.org",
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "bun run ../../build.ts",
|
|
44
|
+
"test": "bun test src",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"nxgt": {
|
|
48
|
+
"entrypoints": [
|
|
49
|
+
"src/index.ts"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@softistx/telemetry": "^0.4.2"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@softistx/httpyz": "0.6.1",
|
|
57
|
+
"@types/bun": "^1.4.2"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"@softistx/httpyz": ">=0.6.0 <0.7.0",
|
|
61
|
+
"typescript": "^6.0.3 || ^7.0.0"
|
|
62
|
+
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"@softistx/httpyz": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"typescript": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|