@indra211/exios 3.2.0 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -1
- package/dist/index.cjs +178 -6
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +60 -2
- package/dist/index.js +179 -6
- package/dist/index.node.cjs +178 -6
- package/dist/index.node.js +179 -6
- package/dist/otel.cjs +199 -0
- package/dist/otel.d.cts +79 -0
- package/dist/otel.d.ts +79 -0
- package/dist/otel.js +168 -0
- package/package.json +29 -2
package/dist/otel.d.cts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { InternalConfig, ExiosPlugin } from '@indra211/exios';
|
|
2
|
+
|
|
3
|
+
/** Same shape as `AttributeValue` in @opentelemetry/api (homogeneous arrays). */
|
|
4
|
+
type AttributeValue = string | number | boolean | Array<null | undefined | string> | Array<null | undefined | number> | Array<null | undefined | boolean>;
|
|
5
|
+
type OtelAttributes = Record<string, AttributeValue | undefined>;
|
|
6
|
+
interface OtelSpan {
|
|
7
|
+
setAttribute(key: string, value: AttributeValue): unknown;
|
|
8
|
+
setAttributes(attributes: OtelAttributes): unknown;
|
|
9
|
+
addEvent(name: string, attributes?: OtelAttributes): unknown;
|
|
10
|
+
setStatus(status: {
|
|
11
|
+
code: number;
|
|
12
|
+
message?: string;
|
|
13
|
+
}): unknown;
|
|
14
|
+
recordException(exception: Error | string): void;
|
|
15
|
+
end(): void;
|
|
16
|
+
}
|
|
17
|
+
/** The parts of `@opentelemetry/api` the plugin uses (pass the module itself). */
|
|
18
|
+
interface OtelApi {
|
|
19
|
+
trace: {
|
|
20
|
+
getTracer(name: string, version?: string): {
|
|
21
|
+
startSpan(name: string, options?: {
|
|
22
|
+
kind?: number;
|
|
23
|
+
attributes?: OtelAttributes;
|
|
24
|
+
}, context?: unknown): OtelSpan;
|
|
25
|
+
};
|
|
26
|
+
setSpan(context: unknown, span: OtelSpan): unknown;
|
|
27
|
+
};
|
|
28
|
+
context: {
|
|
29
|
+
active(): unknown;
|
|
30
|
+
};
|
|
31
|
+
propagation: {
|
|
32
|
+
inject(context: unknown, carrier: unknown): void;
|
|
33
|
+
};
|
|
34
|
+
metrics?: {
|
|
35
|
+
getMeter(name: string, version?: string): {
|
|
36
|
+
createHistogram(name: string, options?: {
|
|
37
|
+
description?: string;
|
|
38
|
+
unit?: string;
|
|
39
|
+
advice?: {
|
|
40
|
+
explicitBucketBoundaries?: number[];
|
|
41
|
+
};
|
|
42
|
+
}): {
|
|
43
|
+
record(value: number, attributes?: OtelAttributes): void;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
interface OtelPluginOptions {
|
|
49
|
+
/** The `@opentelemetry/api` module: `import * as api from '@opentelemetry/api'`. */
|
|
50
|
+
api: OtelApi;
|
|
51
|
+
/** Tracer and meter name. Default `'@indra211/exios'`. */
|
|
52
|
+
name?: string;
|
|
53
|
+
/** Tracer and meter version. */
|
|
54
|
+
version?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Which requests get trace-context headers (`traceparent`, ... from the registered propagator).
|
|
57
|
+
* Default: every request in Node and other servers; same-origin requests only in browsers (a new header
|
|
58
|
+
* on a cross-origin request triggers a CORS preflight and must be allowed by the server). A RegExp is
|
|
59
|
+
* tested against the full URL.
|
|
60
|
+
*/
|
|
61
|
+
propagate?: boolean | RegExp | ((url: string) => boolean);
|
|
62
|
+
/** Record the `http.client.request.duration` histogram. Default true (when the API has metrics). */
|
|
63
|
+
metrics?: boolean;
|
|
64
|
+
/** Span name. Default: the HTTP method, as the semantic conventions prescribe (e.g. `GET`). */
|
|
65
|
+
spanName?: (config: InternalConfig) => string;
|
|
66
|
+
/** Extra span attributes, e.g. from `config.metadata`. Called once, when the span starts. */
|
|
67
|
+
attributes?: (config: InternalConfig) => OtelAttributes;
|
|
68
|
+
/** Plugin name. Default `'otel'`. */
|
|
69
|
+
pluginName?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* One CLIENT span per logical request (retries are span events on it), following the OpenTelemetry HTTP
|
|
73
|
+
* client semantic conventions, plus trace-context propagation and the `http.client.request.duration`
|
|
74
|
+
* histogram. Register it first, so it sees what every other plugin does.
|
|
75
|
+
*/
|
|
76
|
+
declare function createOtelPlugin(options: OtelPluginOptions): ExiosPlugin;
|
|
77
|
+
|
|
78
|
+
export { createOtelPlugin };
|
|
79
|
+
export type { OtelApi, OtelAttributes, OtelPluginOptions, OtelSpan };
|
package/dist/otel.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { InternalConfig, ExiosPlugin } from '@indra211/exios';
|
|
2
|
+
|
|
3
|
+
/** Same shape as `AttributeValue` in @opentelemetry/api (homogeneous arrays). */
|
|
4
|
+
type AttributeValue = string | number | boolean | Array<null | undefined | string> | Array<null | undefined | number> | Array<null | undefined | boolean>;
|
|
5
|
+
type OtelAttributes = Record<string, AttributeValue | undefined>;
|
|
6
|
+
interface OtelSpan {
|
|
7
|
+
setAttribute(key: string, value: AttributeValue): unknown;
|
|
8
|
+
setAttributes(attributes: OtelAttributes): unknown;
|
|
9
|
+
addEvent(name: string, attributes?: OtelAttributes): unknown;
|
|
10
|
+
setStatus(status: {
|
|
11
|
+
code: number;
|
|
12
|
+
message?: string;
|
|
13
|
+
}): unknown;
|
|
14
|
+
recordException(exception: Error | string): void;
|
|
15
|
+
end(): void;
|
|
16
|
+
}
|
|
17
|
+
/** The parts of `@opentelemetry/api` the plugin uses (pass the module itself). */
|
|
18
|
+
interface OtelApi {
|
|
19
|
+
trace: {
|
|
20
|
+
getTracer(name: string, version?: string): {
|
|
21
|
+
startSpan(name: string, options?: {
|
|
22
|
+
kind?: number;
|
|
23
|
+
attributes?: OtelAttributes;
|
|
24
|
+
}, context?: unknown): OtelSpan;
|
|
25
|
+
};
|
|
26
|
+
setSpan(context: unknown, span: OtelSpan): unknown;
|
|
27
|
+
};
|
|
28
|
+
context: {
|
|
29
|
+
active(): unknown;
|
|
30
|
+
};
|
|
31
|
+
propagation: {
|
|
32
|
+
inject(context: unknown, carrier: unknown): void;
|
|
33
|
+
};
|
|
34
|
+
metrics?: {
|
|
35
|
+
getMeter(name: string, version?: string): {
|
|
36
|
+
createHistogram(name: string, options?: {
|
|
37
|
+
description?: string;
|
|
38
|
+
unit?: string;
|
|
39
|
+
advice?: {
|
|
40
|
+
explicitBucketBoundaries?: number[];
|
|
41
|
+
};
|
|
42
|
+
}): {
|
|
43
|
+
record(value: number, attributes?: OtelAttributes): void;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
interface OtelPluginOptions {
|
|
49
|
+
/** The `@opentelemetry/api` module: `import * as api from '@opentelemetry/api'`. */
|
|
50
|
+
api: OtelApi;
|
|
51
|
+
/** Tracer and meter name. Default `'@indra211/exios'`. */
|
|
52
|
+
name?: string;
|
|
53
|
+
/** Tracer and meter version. */
|
|
54
|
+
version?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Which requests get trace-context headers (`traceparent`, ... from the registered propagator).
|
|
57
|
+
* Default: every request in Node and other servers; same-origin requests only in browsers (a new header
|
|
58
|
+
* on a cross-origin request triggers a CORS preflight and must be allowed by the server). A RegExp is
|
|
59
|
+
* tested against the full URL.
|
|
60
|
+
*/
|
|
61
|
+
propagate?: boolean | RegExp | ((url: string) => boolean);
|
|
62
|
+
/** Record the `http.client.request.duration` histogram. Default true (when the API has metrics). */
|
|
63
|
+
metrics?: boolean;
|
|
64
|
+
/** Span name. Default: the HTTP method, as the semantic conventions prescribe (e.g. `GET`). */
|
|
65
|
+
spanName?: (config: InternalConfig) => string;
|
|
66
|
+
/** Extra span attributes, e.g. from `config.metadata`. Called once, when the span starts. */
|
|
67
|
+
attributes?: (config: InternalConfig) => OtelAttributes;
|
|
68
|
+
/** Plugin name. Default `'otel'`. */
|
|
69
|
+
pluginName?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* One CLIENT span per logical request (retries are span events on it), following the OpenTelemetry HTTP
|
|
73
|
+
* client semantic conventions, plus trace-context propagation and the `http.client.request.duration`
|
|
74
|
+
* histogram. Register it first, so it sees what every other plugin does.
|
|
75
|
+
*/
|
|
76
|
+
declare function createOtelPlugin(options: OtelPluginOptions): ExiosPlugin;
|
|
77
|
+
|
|
78
|
+
export { createOtelPlugin };
|
|
79
|
+
export type { OtelApi, OtelAttributes, OtelPluginOptions, OtelSpan };
|
package/dist/otel.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// src/otel/index.ts
|
|
2
|
+
import exios from "@indra211/exios";
|
|
3
|
+
|
|
4
|
+
// src/core/internals.ts
|
|
5
|
+
var INTERNALS = /* @__PURE__ */ Symbol.for("@indra211/exios/internals");
|
|
6
|
+
|
|
7
|
+
// src/otel/index.ts
|
|
8
|
+
var core = exios[INTERNALS];
|
|
9
|
+
if (!core)
|
|
10
|
+
throw new Error("@indra211/exios/otel: the installed @indra211/exios is too old (needs 3.3.0 or newer)");
|
|
11
|
+
var { buildFullPath, buildURL, createRequestId, redactURL } = core;
|
|
12
|
+
var SPAN_KIND_CLIENT = 2;
|
|
13
|
+
var STATUS_ERROR = 2;
|
|
14
|
+
var KNOWN_METHODS = /* @__PURE__ */ new Set(["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"]);
|
|
15
|
+
var DURATION_BUCKETS = [5e-3, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10];
|
|
16
|
+
var now = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
17
|
+
function createOtelPlugin(options) {
|
|
18
|
+
const api = options?.api;
|
|
19
|
+
if (!api?.trace || !api.context || !api.propagation)
|
|
20
|
+
throw new TypeError("createOtelPlugin: pass the @opentelemetry/api module as `api`");
|
|
21
|
+
const name = options.name ?? "@indra211/exios";
|
|
22
|
+
const tracer = api.trace.getTracer(name, options.version);
|
|
23
|
+
const histogram = options.metrics !== false && api.metrics ? api.metrics.getMeter(name, options.version).createHistogram("http.client.request.duration", {
|
|
24
|
+
description: "Duration of HTTP client requests.",
|
|
25
|
+
unit: "s",
|
|
26
|
+
advice: { explicitBucketBoundaries: DURATION_BUCKETS }
|
|
27
|
+
}) : void 0;
|
|
28
|
+
const shouldPropagate = propagationRule(options.propagate);
|
|
29
|
+
const byConfig = /* @__PURE__ */ new WeakMap();
|
|
30
|
+
const byId = /* @__PURE__ */ new Map();
|
|
31
|
+
const finish = (config, outcome) => {
|
|
32
|
+
const state = byConfig.get(config);
|
|
33
|
+
if (!state) return;
|
|
34
|
+
byConfig.delete(config);
|
|
35
|
+
if (config.requestId !== void 0 && byId.get(config.requestId) === state) byId.delete(config.requestId);
|
|
36
|
+
const { span } = state;
|
|
37
|
+
const status = outcome.response?.status ?? outcome.status;
|
|
38
|
+
let errorType = outcome.errorType;
|
|
39
|
+
if (errorType === void 0 && status !== void 0 && status >= 400) errorType = String(status);
|
|
40
|
+
const end = {};
|
|
41
|
+
if (status !== void 0) end["http.response.status_code"] = status;
|
|
42
|
+
if (errorType !== void 0) end["error.type"] = errorType;
|
|
43
|
+
const meta = outcome.response?.meta;
|
|
44
|
+
const attempts = meta?.attempts ?? outcome.attempts;
|
|
45
|
+
if (attempts !== void 0) {
|
|
46
|
+
end["exios.attempts"] = attempts;
|
|
47
|
+
if (attempts > 1) end["http.request.resend_count"] = attempts - 1;
|
|
48
|
+
}
|
|
49
|
+
if (meta?.cached) end["exios.cached"] = true;
|
|
50
|
+
if (meta?.deduped) end["exios.deduped"] = true;
|
|
51
|
+
try {
|
|
52
|
+
span.setAttributes(end);
|
|
53
|
+
if (outcome.error !== void 0) {
|
|
54
|
+
const err = outcome.error;
|
|
55
|
+
span.recordException(err instanceof Error ? err : String(err));
|
|
56
|
+
}
|
|
57
|
+
if (errorType !== void 0 && errorType !== "ERR_CANCELED") {
|
|
58
|
+
const message = outcome.error instanceof Error ? outcome.error.message : void 0;
|
|
59
|
+
span.setStatus(message === void 0 ? { code: STATUS_ERROR } : { code: STATUS_ERROR, message });
|
|
60
|
+
}
|
|
61
|
+
span.end();
|
|
62
|
+
histogram?.record((now() - state.start) / 1e3, {
|
|
63
|
+
...state.common,
|
|
64
|
+
...status !== void 0 ? { "http.response.status_code": status } : {},
|
|
65
|
+
...errorType !== void 0 ? { "error.type": errorType } : {}
|
|
66
|
+
});
|
|
67
|
+
} catch (e) {
|
|
68
|
+
report(e);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
name: options.pluginName ?? "otel",
|
|
73
|
+
setup(instance) {
|
|
74
|
+
return instance.on("retry", (e) => {
|
|
75
|
+
const state = byId.get(e.requestId);
|
|
76
|
+
if (!state) return;
|
|
77
|
+
const attrs = { "exios.retry.attempt": e.attempt, "exios.retry.delay_ms": e.delayMs };
|
|
78
|
+
if (e.status !== void 0) attrs["http.response.status_code"] = e.status;
|
|
79
|
+
if (e.error?.code !== void 0) attrs["error.type"] = e.error.code;
|
|
80
|
+
state.span.addEvent("exios.retry", attrs);
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
beforeRequest(config) {
|
|
84
|
+
try {
|
|
85
|
+
config.requestId ??= createRequestId();
|
|
86
|
+
const url = buildURL(
|
|
87
|
+
buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls),
|
|
88
|
+
config.params,
|
|
89
|
+
config.paramsSerializer
|
|
90
|
+
);
|
|
91
|
+
const method = config.method.toUpperCase();
|
|
92
|
+
const common = { "http.request.method": KNOWN_METHODS.has(method) ? method : "_OTHER" };
|
|
93
|
+
const target = parseURL(url);
|
|
94
|
+
if (target) {
|
|
95
|
+
common["server.address"] = target.hostname.replace(/^\[|\]$/g, "");
|
|
96
|
+
common["server.port"] = Number(target.port) || (target.protocol === "https:" ? 443 : 80);
|
|
97
|
+
common["url.scheme"] = target.protocol.replace(/:$/, "");
|
|
98
|
+
}
|
|
99
|
+
const attributes = {
|
|
100
|
+
...common,
|
|
101
|
+
"url.full": redactURL(target ? target.href : url),
|
|
102
|
+
"exios.request_id": config.requestId,
|
|
103
|
+
...KNOWN_METHODS.has(method) ? {} : { "http.request.method_original": method },
|
|
104
|
+
...options.attributes?.(config)
|
|
105
|
+
};
|
|
106
|
+
const parent = api.context.active();
|
|
107
|
+
const span = tracer.startSpan(options.spanName?.(config) ?? (KNOWN_METHODS.has(method) ? method : "HTTP"), { kind: SPAN_KIND_CLIENT, attributes }, parent);
|
|
108
|
+
const state = { span, start: now(), common };
|
|
109
|
+
byConfig.set(config, state);
|
|
110
|
+
byId.set(config.requestId, state);
|
|
111
|
+
if (shouldPropagate(target ? target.href : url, target)) {
|
|
112
|
+
const carrier = {};
|
|
113
|
+
api.propagation.inject(api.trace.setSpan(parent, span), carrier);
|
|
114
|
+
for (const [k, v] of Object.entries(carrier))
|
|
115
|
+
if (config.headers[k] === void 0) config.headers[k] = v;
|
|
116
|
+
}
|
|
117
|
+
} catch (e) {
|
|
118
|
+
report(e);
|
|
119
|
+
}
|
|
120
|
+
return config;
|
|
121
|
+
},
|
|
122
|
+
afterResponse(response, config) {
|
|
123
|
+
finish(config, { response });
|
|
124
|
+
},
|
|
125
|
+
onError(error, config) {
|
|
126
|
+
const e = error;
|
|
127
|
+
const status = e?.response?.status ?? e?.status;
|
|
128
|
+
const outcome = {
|
|
129
|
+
error,
|
|
130
|
+
// Semantic conventions: the status code when there was an error response, else the error kind.
|
|
131
|
+
errorType: status !== void 0 ? String(status) : e?.code ?? e?.name ?? "_OTHER"
|
|
132
|
+
};
|
|
133
|
+
if (status !== void 0) outcome.status = status;
|
|
134
|
+
if (e?.meta?.attempts !== void 0) outcome.attempts = e.meta.attempts;
|
|
135
|
+
finish(config, outcome);
|
|
136
|
+
},
|
|
137
|
+
onCancel(error, config) {
|
|
138
|
+
finish(config, { errorType: "ERR_CANCELED", attempts: error.meta.attempts });
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function parseURL(url) {
|
|
143
|
+
try {
|
|
144
|
+
const base = typeof location !== "undefined" ? location.href : void 0;
|
|
145
|
+
return base === void 0 ? new URL(url) : new URL(url, base);
|
|
146
|
+
} catch {
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function propagationRule(rule) {
|
|
151
|
+
if (rule === false) return () => false;
|
|
152
|
+
if (rule === true) return () => true;
|
|
153
|
+
if (typeof rule === "function") return rule;
|
|
154
|
+
if (rule instanceof RegExp)
|
|
155
|
+
return (url) => {
|
|
156
|
+
rule.lastIndex = 0;
|
|
157
|
+
return rule.test(url);
|
|
158
|
+
};
|
|
159
|
+
if (rule !== void 0) throw new TypeError("createOtelPlugin: propagate must be a boolean, a RegExp or a function");
|
|
160
|
+
if (typeof location === "undefined" || !location.origin || location.origin === "null") return () => true;
|
|
161
|
+
return (_url, target) => target?.origin === location.origin;
|
|
162
|
+
}
|
|
163
|
+
function report(err) {
|
|
164
|
+
if (typeof console !== "undefined") console.error("[exios] otel plugin failed:", err);
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
createOtelPlugin
|
|
168
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@indra211/exios",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "Zero-dependency axios-compatible HTTP client (XHR in browsers, Node http in Node, fetch fallback) with retry and a circuit breaker.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Indra211/exios.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/Indra211/exios/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/Indra211/exios#readme",
|
|
6
14
|
"author": {
|
|
7
|
-
"email": "work.indrasena@outlook.com"
|
|
15
|
+
"email": "work.indrasena@outlook.com",
|
|
16
|
+
"name": "Indrasena Reddy"
|
|
8
17
|
},
|
|
9
18
|
"type": "module",
|
|
10
19
|
"sideEffects": false,
|
|
@@ -15,6 +24,9 @@
|
|
|
15
24
|
"*": {
|
|
16
25
|
"testing": [
|
|
17
26
|
"./dist/testing.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"otel": [
|
|
29
|
+
"./dist/otel.d.ts"
|
|
18
30
|
]
|
|
19
31
|
}
|
|
20
32
|
},
|
|
@@ -89,6 +101,16 @@
|
|
|
89
101
|
"default": "./dist/testing.cjs"
|
|
90
102
|
}
|
|
91
103
|
},
|
|
104
|
+
"./otel": {
|
|
105
|
+
"import": {
|
|
106
|
+
"types": "./dist/otel.d.ts",
|
|
107
|
+
"default": "./dist/otel.js"
|
|
108
|
+
},
|
|
109
|
+
"require": {
|
|
110
|
+
"types": "./dist/otel.d.cts",
|
|
111
|
+
"default": "./dist/otel.cjs"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
92
114
|
"./package.json": "./package.json"
|
|
93
115
|
},
|
|
94
116
|
"files": [
|
|
@@ -115,6 +137,11 @@
|
|
|
115
137
|
},
|
|
116
138
|
"devDependencies": {
|
|
117
139
|
"@arethetypeswrong/cli": "^0.18.5",
|
|
140
|
+
"@opentelemetry/api": "~1.9.0",
|
|
141
|
+
"@opentelemetry/context-async-hooks": "^2.12.1",
|
|
142
|
+
"@opentelemetry/core": "^2.12.1",
|
|
143
|
+
"@opentelemetry/sdk-metrics": "^2.12.1",
|
|
144
|
+
"@opentelemetry/sdk-trace-base": "^2.12.1",
|
|
118
145
|
"@types/node": "^26.6.6",
|
|
119
146
|
"@vitest/browser-playwright": "^5.1.0",
|
|
120
147
|
"@vitest/browser-webdriverio": "^5.0.0",
|