@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.21
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 +201 -0
- package/README.md +33 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +3 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +220 -51
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +19 -16
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +81 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +99 -74
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +420 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +11 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +111 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/max-message-bytes.d.ts +42 -0
- package/lib/max-message-bytes.d.ts.map +1 -0
- package/lib/max-message-bytes.js +2 -0
- package/lib/max-message-bytes.js.map +1 -0
- package/lib/ndjson.d.ts +3 -0
- package/lib/ndjson.d.ts.map +1 -0
- package/lib/ndjson.js +2 -0
- package/lib/ndjson.js.map +1 -0
- package/lib/pagination.d.ts +105 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +21 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +85 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +123 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +206 -25
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +258 -64
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/protocol.d.ts +113 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +0 -0
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +4 -4
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry-state.d.ts +26 -0
- package/lib/retry-state.d.ts.map +1 -0
- package/lib/retry-state.js +2 -0
- package/lib/retry-state.js.map +1 -0
- package/lib/retry.d.ts +141 -42
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +179 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +41 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +31 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/send.d.ts +37 -0
- package/lib/send.d.ts.map +1 -0
- package/lib/send.js +2 -0
- package/lib/send.js.map +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +317 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/token-cache.d.ts +31 -0
- package/lib/token-cache.d.ts.map +1 -0
- package/lib/token-cache.js +2 -0
- package/lib/token-cache.js.map +1 -0
- package/lib/trait.d.ts +94 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1597 -234
- package/src/category.ts +97 -134
- package/src/chunks.ts +30 -0
- package/src/config.ts +261 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +99 -77
- package/src/graphql.ts +1817 -0
- package/src/graphql.types.ts +120 -0
- package/src/grpc-transport-cloudflare-http2.ts +569 -0
- package/src/grpc-transport-cloudflare-sockets.ts +368 -0
- package/src/grpc-transport-node-http2.ts +307 -0
- package/src/grpc-transport.ts +287 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +379 -94
- package/src/plain-values.ts +70 -0
- package/src/protobuf.ts +1367 -0
- package/src/protocol-grpc.ts +811 -0
- package/src/protocol-http.ts +1045 -371
- package/src/protocol-rest.ts +908 -255
- package/src/protocol.ts +150 -0
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +333 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +8 -14
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +260 -145
- package/src/schema-lite-impl.ts +621 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +45 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +37 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +188 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +800 -0
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +139 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client call metrics (opt in): a program that never imports this module
|
|
3
|
+
* never bundles it.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { TelemetryMetrics } from "@distilled.cloud/core/telemetry-metrics";
|
|
7
|
+
* program.pipe(Effect.provide(TelemetryMetrics.layer));
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Records `rpc.client.call.duration` (semantic conventions v1.44:
|
|
11
|
+
* histogram, seconds) for every operation call, around all its retries,
|
|
12
|
+
* whether or not a span is recorded, with `rpc.system.name`, `rpc.method`
|
|
13
|
+
* and, on failure, `error.type`. Effect's metric exporters
|
|
14
|
+
* (`@effect/opentelemetry`, `effect/observability`) send it on.
|
|
15
|
+
*/
|
|
16
|
+
import * as Cause from "effect/Cause";
|
|
17
|
+
import * as Effect from "effect/Effect";
|
|
18
|
+
import * as Exit from "effect/Exit";
|
|
19
|
+
import * as Layer from "effect/Layer";
|
|
20
|
+
import * as Metric from "effect/Metric";
|
|
21
|
+
import { type Measure, Metrics, type OperationInfo, tagOf } from "./telemetry.ts";
|
|
22
|
+
|
|
23
|
+
/** The buckets the RPC and HTTP conventions recommend, in seconds. */
|
|
24
|
+
const BOUNDARIES = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10];
|
|
25
|
+
|
|
26
|
+
const duration = /*@__PURE__*/ Metric.histogram("rpc.client.call.duration", {
|
|
27
|
+
description: "Duration of outbound RPC calls.",
|
|
28
|
+
boundaries: BOUNDARIES,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** One histogram per operation and outcome, built once. */
|
|
32
|
+
const byOperation = new WeakMap<OperationInfo, Map<string, Metric.Metric<number, unknown>>>();
|
|
33
|
+
|
|
34
|
+
const metricOf = (info: OperationInfo, errorType: string | undefined) => {
|
|
35
|
+
let byOutcome = byOperation.get(info);
|
|
36
|
+
if (byOutcome === undefined) byOperation.set(info, (byOutcome = new Map()));
|
|
37
|
+
const key = errorType ?? "";
|
|
38
|
+
let metric = byOutcome.get(key);
|
|
39
|
+
if (metric === undefined) {
|
|
40
|
+
const attributes: Record<string, string> = {};
|
|
41
|
+
for (const name of ["rpc.system.name", "rpc.method"]) {
|
|
42
|
+
const value = info.attributes[name];
|
|
43
|
+
if (typeof value === "string") attributes[name] = value;
|
|
44
|
+
}
|
|
45
|
+
if (errorType !== undefined) attributes["error.type"] = errorType;
|
|
46
|
+
metric = Metric.withAttributes(duration, attributes) as Metric.Metric<number, unknown>;
|
|
47
|
+
byOutcome.set(key, metric);
|
|
48
|
+
}
|
|
49
|
+
return metric;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const errorTypeOf = (exit: Exit.Exit<unknown, unknown>): string | undefined =>
|
|
53
|
+
Exit.isSuccess(exit) ? undefined : tagOf(Cause.squash(exit.cause));
|
|
54
|
+
|
|
55
|
+
const measure: Measure = (info, effect) =>
|
|
56
|
+
Effect.clockWith((clock) => {
|
|
57
|
+
const start = clock.currentTimeNanosUnsafe();
|
|
58
|
+
return Effect.onExit(effect, (exit) =>
|
|
59
|
+
Metric.update(
|
|
60
|
+
metricOf(info, errorTypeOf(exit)),
|
|
61
|
+
Number(clock.currentTimeNanosUnsafe() - start) / 1e9,
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const TelemetryMetrics = {
|
|
67
|
+
/** Records `rpc.client.call.duration` for every distilled call it is provided to. */
|
|
68
|
+
layer: /*@__PURE__*/ Layer.succeed(Metrics, measure) as Layer.Layer<never>,
|
|
69
|
+
} as const;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `com.distilled.telemetry#operation` trait's `nameTemplate`, as a
|
|
3
|
+
* function: generated code imports this only where a trait has one
|
|
4
|
+
* (messaging operations). See `packages/dev/src/codegen/telemetry-traits.md`.
|
|
5
|
+
*/
|
|
6
|
+
import type { SpanRename } from "./telemetry.ts";
|
|
7
|
+
|
|
8
|
+
const PLACEHOLDER = /\{([^{}]+)\}/g;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Span name from `{attribute}` placeholders filled from the call's input
|
|
12
|
+
* attributes (`send {messaging.destination.name}`); undefined (the trait's
|
|
13
|
+
* `name`) when one is missing.
|
|
14
|
+
*/
|
|
15
|
+
export const nameTemplate =
|
|
16
|
+
(template: string): SpanRename =>
|
|
17
|
+
(attributes) => {
|
|
18
|
+
let complete = true;
|
|
19
|
+
const name = template.replace(PLACEHOLDER, (_, key: string) => {
|
|
20
|
+
const value = attributes[key];
|
|
21
|
+
if (typeof value === "string" || typeof value === "number") return String(value);
|
|
22
|
+
complete = false;
|
|
23
|
+
return "";
|
|
24
|
+
});
|
|
25
|
+
return complete ? name : undefined;
|
|
26
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operation spans that cover a streamed response (the
|
|
3
|
+
* `com.distilled.telemetry#operation` trait's `stream`). Generated modules
|
|
4
|
+
* import this only for the operations whose trait has it, so other programs
|
|
5
|
+
* never bundle it.
|
|
6
|
+
*
|
|
7
|
+
* The GenAI conventions say a GenAI span ends "when the response is fully
|
|
8
|
+
* received", and the OpenTelemetry AWS SDK instrumentations (JavaScript,
|
|
9
|
+
* Python) end Bedrock `ConverseStream` and `InvokeModelWithResponseStream`
|
|
10
|
+
* spans when the caller has read the stream. Every other operation span,
|
|
11
|
+
* S3 `GetObject` included, ends when the call returns, after the response
|
|
12
|
+
* headers, as those instrumentations and the HTTP client span conventions
|
|
13
|
+
* do: reading an object body is the app's work.
|
|
14
|
+
*
|
|
15
|
+
* With `stream: { member }`, the span ends when the caller has read that
|
|
16
|
+
* output member to its end. An error while reading fails it and sets
|
|
17
|
+
* `error.type`; a caller that stops early (`Stream.take`, an interruption)
|
|
18
|
+
* ends it then, with success. A stream the caller never reads never ends
|
|
19
|
+
* the span, as in those instrumentations: the conventions ask clients not
|
|
20
|
+
* to end a span later, in clean-up. `firstChunk` names the attribute that
|
|
21
|
+
* records the seconds from the span's start to the first item
|
|
22
|
+
* (`gen_ai.response.time_to_first_chunk`). `events` reads attributes from
|
|
23
|
+
* the items as they pass, by event member, with the same rules as the
|
|
24
|
+
* output's: Bedrock `ConverseStream`'s `metadata` carries the token usage
|
|
25
|
+
* and its `messageStop` the finish reason.
|
|
26
|
+
*/
|
|
27
|
+
import * as Cause from "effect/Cause";
|
|
28
|
+
import * as Clock from "effect/Clock";
|
|
29
|
+
import * as Effect from "effect/Effect";
|
|
30
|
+
import * as Exit from "effect/Exit";
|
|
31
|
+
import * as References from "effect/References";
|
|
32
|
+
import * as Stream from "effect/Stream";
|
|
33
|
+
import { type AttributeRule, flatten, readRules, type SpanHold, tagOf } from "./telemetry.ts";
|
|
34
|
+
|
|
35
|
+
export interface StreamRule {
|
|
36
|
+
/** The output member holding the stream (the generated property name). */
|
|
37
|
+
readonly member: string;
|
|
38
|
+
/** Attribute for the seconds from the span's start to the first item. */
|
|
39
|
+
readonly firstChunk?: string;
|
|
40
|
+
/** Attributes read from each item, by event member (the generated property name). */
|
|
41
|
+
readonly events?: Readonly<Record<string, ReadonlyArray<AttributeRule>>>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const holdForStream = (rule: StreamRule): SpanHold => {
|
|
45
|
+
const events = flatten(rule.events);
|
|
46
|
+
return (fiber, name, options, debug, inSpan) => {
|
|
47
|
+
const clock = fiber.getRef(Clock.Clock);
|
|
48
|
+
const timing = fiber.getRef(References.TracerTimingEnabled);
|
|
49
|
+
const now = () => (timing ? clock.currentTimeNanosUnsafe() : 0n);
|
|
50
|
+
return Effect.uninterruptibleMask((restore) =>
|
|
51
|
+
Effect.flatMap(Effect.makeSpan(name, options), (span) => {
|
|
52
|
+
const end = (exit: Exit.Exit<unknown, unknown>) => {
|
|
53
|
+
if (span.status._tag !== "Ended") span.end(now(), exit);
|
|
54
|
+
};
|
|
55
|
+
let held = false;
|
|
56
|
+
const body = Effect.map(inSpan(span), (output) => {
|
|
57
|
+
const fields = output as Record<string, unknown> | null;
|
|
58
|
+
const stream = fields?.[rule.member];
|
|
59
|
+
if (!Stream.isStream(stream)) return output;
|
|
60
|
+
held = true;
|
|
61
|
+
let done = false;
|
|
62
|
+
const read = Stream.suspend(() => {
|
|
63
|
+
let items = debug === undefined ? stream : debug.readStream(stream, span, rule.member);
|
|
64
|
+
const attribute = rule.firstChunk;
|
|
65
|
+
if (attribute !== undefined || events.length > 0) {
|
|
66
|
+
let first = true;
|
|
67
|
+
items = Stream.map(items, (item) => {
|
|
68
|
+
if (first) {
|
|
69
|
+
first = false;
|
|
70
|
+
if (attribute !== undefined) {
|
|
71
|
+
span.attribute(attribute, Number(now() - span.status.startTime) / 1e9);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
readRules(events, item, (key, value) => span.attribute(key, value));
|
|
75
|
+
return item;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return items;
|
|
79
|
+
});
|
|
80
|
+
const wrapped = Stream.onExit(read, (exit) =>
|
|
81
|
+
Effect.sync(() => {
|
|
82
|
+
if (done) return;
|
|
83
|
+
done = true;
|
|
84
|
+
if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) {
|
|
85
|
+
span.attribute("error.type", tagOf(Cause.squash(exit.cause)));
|
|
86
|
+
end(exit);
|
|
87
|
+
} else end(Exit.void);
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
return { ...fields, [rule.member]: wrapped } as typeof output;
|
|
91
|
+
});
|
|
92
|
+
return Effect.onExit(restore(body), (exit) =>
|
|
93
|
+
Effect.sync(() => {
|
|
94
|
+
if (!held || Exit.isFailure(exit)) end(exit);
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWS X-Ray trace context, which AWS services read and pass on (the OTel
|
|
3
|
+
* AWS SDK conventions recommend it for AWS calls). Generated code imports
|
|
4
|
+
* this only where the model's `com.distilled.telemetry` traits ask for it:
|
|
5
|
+
* a service's `propagation: "xray"`, an operation's `inject` with format
|
|
6
|
+
* `xrayTraceHeader`. See `packages/dev/src/codegen/telemetry-traits.md`.
|
|
7
|
+
*
|
|
8
|
+
* `Root=1-<8 hex>-<24 hex>;Parent=<16 hex>;Sampled=<0|1>`, from a W3C trace
|
|
9
|
+
* id split after 8 hex digits, as OTel's AWS X-Ray propagator writes it.
|
|
10
|
+
*/
|
|
11
|
+
import * as Tracer from "effect/Tracer";
|
|
12
|
+
|
|
13
|
+
export const HEADER = "X-Amzn-Trace-Id";
|
|
14
|
+
|
|
15
|
+
/** The X-Ray trace header of a span. */
|
|
16
|
+
export const traceHeader = (span: Tracer.AnySpan): string =>
|
|
17
|
+
`Root=1-${span.traceId.slice(0, 8)}-${span.traceId.slice(8)};Parent=${span.spanId};Sampled=${
|
|
18
|
+
span.sampled ? 1 : 0
|
|
19
|
+
}`;
|
|
20
|
+
|
|
21
|
+
const ROOT = /(?:^|;)\s*Root=1-([0-9a-f]{8})-([0-9a-f]{24})/;
|
|
22
|
+
const PARENT = /(?:^|;)\s*Parent=([0-9a-f]{16})/;
|
|
23
|
+
const SAMPLED = /(?:^|;)\s*Sampled=([01])/;
|
|
24
|
+
|
|
25
|
+
/** The span an X-Ray trace header names, if it has a root and a parent. */
|
|
26
|
+
export const parseTraceHeader = (value: unknown): Tracer.ExternalSpan | undefined => {
|
|
27
|
+
if (typeof value !== "string") return undefined;
|
|
28
|
+
const root = ROOT.exec(value);
|
|
29
|
+
const parent = PARENT.exec(value);
|
|
30
|
+
if (root === null || parent === null) return undefined;
|
|
31
|
+
return Tracer.externalSpan({
|
|
32
|
+
traceId: root[1]! + root[2]!,
|
|
33
|
+
spanId: parent[1]!,
|
|
34
|
+
sampled: SAMPLED.exec(value)?.[1] !== "0",
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** `propagation: "xray"`: the `X-Amzn-Trace-Id` request header. */
|
|
39
|
+
export const propagate = (span: Tracer.AnySpan): readonly [string, string] => [
|
|
40
|
+
HEADER,
|
|
41
|
+
traceHeader(span),
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Write the span's X-Ray trace header into a string `member` of the input,
|
|
46
|
+
* or of every element of its `list` member (EventBridge `PutEvents`
|
|
47
|
+
* entries' `TraceHeader`). A value the caller set is kept.
|
|
48
|
+
*/
|
|
49
|
+
export const injectTraceHeader =
|
|
50
|
+
(place: { readonly member: string; readonly list?: string }) =>
|
|
51
|
+
(span: Tracer.Span, input: unknown): unknown => {
|
|
52
|
+
if (input === null || typeof input !== "object") return input;
|
|
53
|
+
const record = input as Record<string, unknown>;
|
|
54
|
+
const value = traceHeader(span);
|
|
55
|
+
const set = (holder: Record<string, unknown>) =>
|
|
56
|
+
holder[place.member] === undefined ? { ...holder, [place.member]: value } : holder;
|
|
57
|
+
if (place.list === undefined) return set(record);
|
|
58
|
+
const entries = record[place.list];
|
|
59
|
+
if (!Array.isArray(entries)) return input;
|
|
60
|
+
return {
|
|
61
|
+
...record,
|
|
62
|
+
[place.list]: entries.map((entry) =>
|
|
63
|
+
entry !== null && typeof entry === "object" ? set(entry as Record<string, unknown>) : entry,
|
|
64
|
+
),
|
|
65
|
+
};
|
|
66
|
+
};
|