@encore.dev/sdk 0.0.0 → 2.0.0-beta.5
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 +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +10 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +22 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +14 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +12 -0
- package/dist/services/index.js +22 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +53 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +91 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/index.ts +20 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -0
- package/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +21 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
package/metrics/mod.ts
ADDED
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics: counters, up-down counters, histograms and gauges of what an app
|
|
3
|
+
* does.
|
|
4
|
+
*
|
|
5
|
+
* A metric is declared at module level and used wherever the thing it
|
|
6
|
+
* measures happens. Its JSDoc comment, else its `description` option, is
|
|
7
|
+
* its description:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { Counter, Histogram } from "@encore.dev/sdk/metrics";
|
|
11
|
+
*
|
|
12
|
+
* export const ordersPlaced = new Counter("orders_placed", { unit: "{order}" });
|
|
13
|
+
* export const uploadSize = new Histogram("upload_size", {
|
|
14
|
+
* unit: "By",
|
|
15
|
+
* buckets: [1024, 65536, 1048576]
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* ordersPlaced.increment();
|
|
19
|
+
* uploadSize.record(2048);
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Each is an instrument of the OpenTelemetry Metrics API
|
|
23
|
+
* (`@opentelemetry/api`), of the meter `encore`, named as declared, a
|
|
24
|
+
* service's own prefixed with its service's name (`orders_placed` of the
|
|
25
|
+
* service `shop` is exported as `shop.orders_placed`), created when it is
|
|
26
|
+
* first recorded to, recording through Encore's meter provider whatever the
|
|
27
|
+
* global one is. Instruments of the API itself made with it
|
|
28
|
+
* (`meterProvider().getMeter(…)`) are exported alongside: observable
|
|
29
|
+
* instruments are made with it.
|
|
30
|
+
*
|
|
31
|
+
* Which class declares a metric says who records to it: a `Counter`
|
|
32
|
+
* belongs to the declaring service, which alone records to it; a
|
|
33
|
+
* `SharedCounter` is shared between services, each that records to it
|
|
34
|
+
* tracked as using it; a `GlobalCounter` is the whole app's, recorded to
|
|
35
|
+
* from any code, untracked. `SharedCounter` and `GlobalCounter` extend
|
|
36
|
+
* `Counter`, so code taking a `Counter` takes one of any scope. The
|
|
37
|
+
* `…Group` classes take a label type, whose fields are the attributes of
|
|
38
|
+
* what is recorded:
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { CounterGroup } from "@encore.dev/sdk/metrics";
|
|
42
|
+
*
|
|
43
|
+
* interface Outcome {
|
|
44
|
+
* method: string;
|
|
45
|
+
* status: number;
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* export const requests = new CounterGroup<Outcome>("requests_handled");
|
|
49
|
+
*
|
|
50
|
+
* requests.with({ method: "GET", status: 200 }).increment();
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* A shared or global metric is declared once in the app, as a shared
|
|
54
|
+
* secret is. Other code records to it by importing the declaration, or,
|
|
55
|
+
* from another service in another language, binds it by name with
|
|
56
|
+
* `named`, which states the declaration's kind and labels and takes its
|
|
57
|
+
* scope, unit, description and buckets. Every scope's class has it and
|
|
58
|
+
* binds alike: `SharedCounter.named` is `Counter.named`. A bound metric is
|
|
59
|
+
* exported as declared, never prefixed.
|
|
60
|
+
*
|
|
61
|
+
* ```typescript
|
|
62
|
+
* import { CounterGroup } from "@encore.dev/sdk/metrics";
|
|
63
|
+
*
|
|
64
|
+
* const served = CounterGroup.named<Served>("greetings.served");
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* To hand a metric to other code, pass its `ref()`: it counts as recording
|
|
68
|
+
* to it.
|
|
69
|
+
*
|
|
70
|
+
* @module
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
import type {Recorder} from '../internal/otel/meter';
|
|
74
|
+
import {hostedService, meter, meterProvider} from '../internal/runtime/meter';
|
|
75
|
+
import {StringLiteral} from '../internal/utils/constraints';
|
|
76
|
+
|
|
77
|
+
export {meterProvider};
|
|
78
|
+
export type {MeterProvider} from '../internal/otel/meter';
|
|
79
|
+
|
|
80
|
+
/** The options every metric is declared with. */
|
|
81
|
+
export interface MetricOptions {
|
|
82
|
+
/** What the metric measures in, as exported: `"s"`, `"By"`,
|
|
83
|
+
* `"{request}"` ([UCUM](https://ucum.org/ucum)). */
|
|
84
|
+
unit?: string;
|
|
85
|
+
/** What the metric measures, for one without a JSDoc comment. */
|
|
86
|
+
description?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The options a histogram is declared with. */
|
|
90
|
+
export interface HistogramOptions extends MetricOptions {
|
|
91
|
+
/** The upper bounds of the buckets, increasing: what is recorded is
|
|
92
|
+
* counted in the first it is no greater than, or one past the last.
|
|
93
|
+
* Without them, the OpenTelemetry defaults. */
|
|
94
|
+
buckets?: number[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A metric group's labels: an interface or type whose fields are strings,
|
|
98
|
+
* numbers or booleans. Each field is a label, named as the field in
|
|
99
|
+
* snake_case. A number is recorded as an integer, its value floored
|
|
100
|
+
* (`Math.floor`): `200.7` is `200`. */
|
|
101
|
+
export type Labels<L> = {[K in keyof L]: string | number | boolean};
|
|
102
|
+
|
|
103
|
+
type Kind = 'counter' | 'upDownCounter' | 'histogram' | 'gauge';
|
|
104
|
+
|
|
105
|
+
/** A metric's instrument, made when first recorded to. */
|
|
106
|
+
class Lazy {
|
|
107
|
+
private inst?: Recorder;
|
|
108
|
+
|
|
109
|
+
/** `own`: the declaring service's alone, exported as
|
|
110
|
+
* `<service>.<name>`. */
|
|
111
|
+
constructor(
|
|
112
|
+
private readonly kind: Kind,
|
|
113
|
+
private readonly name: string,
|
|
114
|
+
private readonly opts: HistogramOptions | undefined,
|
|
115
|
+
private readonly own: boolean,
|
|
116
|
+
) {}
|
|
117
|
+
|
|
118
|
+
get(): Recorder {
|
|
119
|
+
this.inst ??= this.make();
|
|
120
|
+
return this.inst;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private make(): Recorder {
|
|
124
|
+
const m = meter();
|
|
125
|
+
const svc = this.own ? hostedService() : undefined;
|
|
126
|
+
const name = svc ? `${svc}.${this.name}` : this.name;
|
|
127
|
+
const opts = {unit: this.opts?.unit, description: this.opts?.description};
|
|
128
|
+
switch (this.kind) {
|
|
129
|
+
case 'counter':
|
|
130
|
+
return m.createCounter(name, opts) as Recorder;
|
|
131
|
+
case 'upDownCounter':
|
|
132
|
+
return m.createUpDownCounter(name, opts) as Recorder;
|
|
133
|
+
case 'gauge':
|
|
134
|
+
return m.createGauge(name, opts) as Recorder;
|
|
135
|
+
case 'histogram':
|
|
136
|
+
return m.createHistogram(name, {
|
|
137
|
+
...opts,
|
|
138
|
+
advice: this.opts?.buckets && {
|
|
139
|
+
explicitBucketBoundaries: this.opts.buckets,
|
|
140
|
+
},
|
|
141
|
+
}) as Recorder;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A `cls` recording to `inst` with `attrs`, attributes as a JSON object's
|
|
147
|
+
* text, made without its constructor, which declares a metric. */
|
|
148
|
+
function bind<T>(cls: {prototype: T}, inst: Lazy, attrs?: string): T {
|
|
149
|
+
return Object.assign(Object.create(cls.prototype as object), {
|
|
150
|
+
inst,
|
|
151
|
+
attrs,
|
|
152
|
+
}) as T;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A group's instrument, for a subclass's `with`. */
|
|
156
|
+
function lazy(group: object): Lazy {
|
|
157
|
+
return (group as {inst: Lazy}).inst;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `labels` as attributes, a JSON object's text: keys in snake_case,
|
|
161
|
+
* numbers floored. */
|
|
162
|
+
function attributes(labels: object): string {
|
|
163
|
+
const out: Record<string, string | number | boolean> = {};
|
|
164
|
+
for (const [key, value] of Object.entries(labels)) {
|
|
165
|
+
out[snakeCase(key)] = typeof value === 'number' ? Math.floor(value) : value;
|
|
166
|
+
}
|
|
167
|
+
return JSON.stringify(out);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** `statusCode` is `status_code`, `HTTPStatus` `http_status`. */
|
|
171
|
+
function snakeCase(s: string): string {
|
|
172
|
+
return s
|
|
173
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
174
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
|
|
175
|
+
.toLowerCase();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Encore reads a metric's scope from the class that declares it. A name is a
|
|
179
|
+
// string literal: a letter, then letters, digits, `_`, `.`, `-` and `/`.
|
|
180
|
+
// `named` binds a shared or global metric declared elsewhere, by another
|
|
181
|
+
// service, possibly in another language: its kind and labels are the
|
|
182
|
+
// declaration's, and so are its scope, unit, description and buckets.
|
|
183
|
+
|
|
184
|
+
/** A number that only goes up: how many times something happened, or how
|
|
185
|
+
* much of something there has been. Belongs to the declaring service: only it records to it. */
|
|
186
|
+
export class Counter {
|
|
187
|
+
private readonly inst: Lazy;
|
|
188
|
+
private readonly attrs?: string;
|
|
189
|
+
|
|
190
|
+
/** Declares the counter `name`. */
|
|
191
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
192
|
+
this.inst = new Lazy('counter', name, opts, new.target === Counter);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Binds the shared or global counter `name`, declared elsewhere. */
|
|
196
|
+
static named<N extends string>(name: StringLiteral<N>): Counter {
|
|
197
|
+
return bind(Counter, new Lazy('counter', name, undefined, false));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Adds one. */
|
|
201
|
+
increment(): void {
|
|
202
|
+
this.inst.get().recordEncoded(1, this.attrs);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Adds `n`. A counter does not go down: an amount below zero is
|
|
206
|
+
* ignored. */
|
|
207
|
+
add(n: number): void {
|
|
208
|
+
this.inst.get().recordEncoded(n, this.attrs);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The counter itself, to hand to other code. */
|
|
212
|
+
ref(): this {
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A counter shared between services: each that records to it is tracked as
|
|
218
|
+
* using it. */
|
|
219
|
+
export class SharedCounter extends Counter {
|
|
220
|
+
/** Declares the shared counter `name`. */
|
|
221
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
222
|
+
super(name, opts);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** A counter of the whole app: any code may record to it. */
|
|
227
|
+
export class GlobalCounter extends Counter {
|
|
228
|
+
/** Declares the global counter `name`. */
|
|
229
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
230
|
+
super(name, opts);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** A number that goes up and down by what is added to it: how many
|
|
235
|
+
* requests are in flight, how many jobs are queued. Belongs to the declaring service: only it records to it. */
|
|
236
|
+
export class UpDownCounter {
|
|
237
|
+
private readonly inst: Lazy;
|
|
238
|
+
private readonly attrs?: string;
|
|
239
|
+
|
|
240
|
+
/** Declares the up-down counter `name`. */
|
|
241
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
242
|
+
this.inst = new Lazy(
|
|
243
|
+
'upDownCounter',
|
|
244
|
+
name,
|
|
245
|
+
opts,
|
|
246
|
+
new.target === UpDownCounter,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Binds the shared or global up-down counter `name`, declared elsewhere. */
|
|
251
|
+
static named<N extends string>(name: StringLiteral<N>): UpDownCounter {
|
|
252
|
+
return bind(
|
|
253
|
+
UpDownCounter,
|
|
254
|
+
new Lazy('upDownCounter', name, undefined, false),
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Adds `n`, which may be below zero. */
|
|
259
|
+
add(n: number): void {
|
|
260
|
+
this.inst.get().recordEncoded(n, this.attrs);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Adds one. */
|
|
264
|
+
increment(): void {
|
|
265
|
+
this.inst.get().recordEncoded(1, this.attrs);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Takes away one. */
|
|
269
|
+
decrement(): void {
|
|
270
|
+
this.inst.get().recordEncoded(-1, this.attrs);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** The up-down counter itself, to hand to other code. */
|
|
274
|
+
ref(): this {
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** An up-down counter shared between services: each that records to it is tracked as
|
|
280
|
+
* using it. */
|
|
281
|
+
export class SharedUpDownCounter extends UpDownCounter {
|
|
282
|
+
/** Declares the shared up-down counter `name`. */
|
|
283
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
284
|
+
super(name, opts);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** An up-down counter of the whole app: any code may record to it. */
|
|
289
|
+
export class GlobalUpDownCounter extends UpDownCounter {
|
|
290
|
+
/** Declares the global up-down counter `name`. */
|
|
291
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
292
|
+
super(name, opts);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** A number that is what it was last set to: what something is right now. Belongs to the declaring service: only it records to it. */
|
|
297
|
+
export class Gauge {
|
|
298
|
+
private readonly inst: Lazy;
|
|
299
|
+
private readonly attrs?: string;
|
|
300
|
+
|
|
301
|
+
/** Declares the gauge `name`. */
|
|
302
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
303
|
+
this.inst = new Lazy('gauge', name, opts, new.target === Gauge);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Binds the shared or global gauge `name`, declared elsewhere. */
|
|
307
|
+
static named<N extends string>(name: StringLiteral<N>): Gauge {
|
|
308
|
+
return bind(Gauge, new Lazy('gauge', name, undefined, false));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Sets the gauge to `value`. */
|
|
312
|
+
set(value: number): void {
|
|
313
|
+
this.inst.get().recordEncoded(value, this.attrs);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** The gauge itself, to hand to other code. */
|
|
317
|
+
ref(): this {
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** A gauge shared between services: each that records to it is tracked as
|
|
323
|
+
* using it. */
|
|
324
|
+
export class SharedGauge extends Gauge {
|
|
325
|
+
/** Declares the shared gauge `name`. */
|
|
326
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
327
|
+
super(name, opts);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** A gauge of the whole app: any code may record to it. */
|
|
332
|
+
export class GlobalGauge extends Gauge {
|
|
333
|
+
/** Declares the global gauge `name`. */
|
|
334
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
335
|
+
super(name, opts);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** How what is recorded is distributed: how long requests take, how large
|
|
340
|
+
* uploads are. Belongs to the declaring service: only it records to it. */
|
|
341
|
+
export class Histogram {
|
|
342
|
+
private readonly inst: Lazy;
|
|
343
|
+
private readonly attrs?: string;
|
|
344
|
+
|
|
345
|
+
/** Declares the histogram `name`. */
|
|
346
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
347
|
+
this.inst = new Lazy('histogram', name, opts, new.target === Histogram);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** Binds the shared or global histogram `name`, declared elsewhere. */
|
|
351
|
+
static named<N extends string>(name: StringLiteral<N>): Histogram {
|
|
352
|
+
return bind(Histogram, new Lazy('histogram', name, undefined, false));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Records `value`. */
|
|
356
|
+
record(value: number): void {
|
|
357
|
+
this.inst.get().recordEncoded(value, this.attrs);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** The histogram itself, to hand to other code. */
|
|
361
|
+
ref(): this {
|
|
362
|
+
return this;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** A histogram shared between services: each that records to it is tracked as
|
|
367
|
+
* using it. */
|
|
368
|
+
export class SharedHistogram extends Histogram {
|
|
369
|
+
/** Declares the shared histogram `name`. */
|
|
370
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
371
|
+
super(name, opts);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** A histogram of the whole app: any code may record to it. */
|
|
376
|
+
export class GlobalHistogram extends Histogram {
|
|
377
|
+
/** Declares the global histogram `name`. */
|
|
378
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
379
|
+
super(name, opts);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// A group's `named` takes its label type as its type argument. TypeScript
|
|
384
|
+
// cannot infer the name's literal type beside an explicit type argument, so
|
|
385
|
+
// the name is a plain `string`, and the parser's check is the only one it
|
|
386
|
+
// gets.
|
|
387
|
+
|
|
388
|
+
/** A counter for each combination of the labels `L`. Belongs to the
|
|
389
|
+
* declaring service: only it records to it. */
|
|
390
|
+
export class CounterGroup<L extends Labels<L>> {
|
|
391
|
+
private readonly inst: Lazy;
|
|
392
|
+
|
|
393
|
+
/** Declares the counter group `name`. */
|
|
394
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
395
|
+
this.inst = new Lazy('counter', name, opts, new.target === CounterGroup);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** Binds the shared or global counter group `name`, declared elsewhere,
|
|
399
|
+
* whose labels are `L`. */
|
|
400
|
+
static named<L extends Labels<L>>(name: string): CounterGroup<L> {
|
|
401
|
+
return bind(CounterGroup, new Lazy('counter', name, undefined, false));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** The counter for `labels`. Worth keeping where it is used often:
|
|
405
|
+
* making it encodes the labels' attributes. */
|
|
406
|
+
with(labels: L): Counter {
|
|
407
|
+
return bind(Counter, this.inst, attributes(labels));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** The group itself, to hand to other code. */
|
|
411
|
+
ref(): this {
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** A counter group shared between services: each that records to it is tracked
|
|
417
|
+
* as using it. */
|
|
418
|
+
export class SharedCounterGroup<L extends Labels<L>> extends CounterGroup<L> {
|
|
419
|
+
/** Declares the shared counter group `name`. */
|
|
420
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
421
|
+
super(name, opts);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
override with(labels: L): SharedCounter {
|
|
425
|
+
return bind(SharedCounter, lazy(this), attributes(labels));
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A counter group of the whole app: any code may record to it. */
|
|
430
|
+
export class GlobalCounterGroup<L extends Labels<L>> extends CounterGroup<L> {
|
|
431
|
+
/** Declares the global counter group `name`. */
|
|
432
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
433
|
+
super(name, opts);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
override with(labels: L): GlobalCounter {
|
|
437
|
+
return bind(GlobalCounter, lazy(this), attributes(labels));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** An up-down counter for each combination of the labels `L`. Belongs to the
|
|
442
|
+
* declaring service: only it records to it. */
|
|
443
|
+
export class UpDownCounterGroup<L extends Labels<L>> {
|
|
444
|
+
private readonly inst: Lazy;
|
|
445
|
+
|
|
446
|
+
/** Declares the up-down counter group `name`. */
|
|
447
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
448
|
+
this.inst = new Lazy(
|
|
449
|
+
'upDownCounter',
|
|
450
|
+
name,
|
|
451
|
+
opts,
|
|
452
|
+
new.target === UpDownCounterGroup,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Binds the shared or global up-down counter group `name`, declared elsewhere,
|
|
457
|
+
* whose labels are `L`. */
|
|
458
|
+
static named<L extends Labels<L>>(name: string): UpDownCounterGroup<L> {
|
|
459
|
+
return bind(
|
|
460
|
+
UpDownCounterGroup,
|
|
461
|
+
new Lazy('upDownCounter', name, undefined, false),
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** The up-down counter for `labels`. Worth keeping where it is used often:
|
|
466
|
+
* making it encodes the labels' attributes. */
|
|
467
|
+
with(labels: L): UpDownCounter {
|
|
468
|
+
return bind(UpDownCounter, this.inst, attributes(labels));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** The group itself, to hand to other code. */
|
|
472
|
+
ref(): this {
|
|
473
|
+
return this;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** An up-down counter group shared between services: each that records to it is tracked
|
|
478
|
+
* as using it. */
|
|
479
|
+
export class SharedUpDownCounterGroup<
|
|
480
|
+
L extends Labels<L>,
|
|
481
|
+
> extends UpDownCounterGroup<L> {
|
|
482
|
+
/** Declares the shared up-down counter group `name`. */
|
|
483
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
484
|
+
super(name, opts);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
override with(labels: L): SharedUpDownCounter {
|
|
488
|
+
return bind(SharedUpDownCounter, lazy(this), attributes(labels));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** An up-down counter group of the whole app: any code may record to it. */
|
|
493
|
+
export class GlobalUpDownCounterGroup<
|
|
494
|
+
L extends Labels<L>,
|
|
495
|
+
> extends UpDownCounterGroup<L> {
|
|
496
|
+
/** Declares the global up-down counter group `name`. */
|
|
497
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
498
|
+
super(name, opts);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
override with(labels: L): GlobalUpDownCounter {
|
|
502
|
+
return bind(GlobalUpDownCounter, lazy(this), attributes(labels));
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** A gauge for each combination of the labels `L`. Belongs to the
|
|
507
|
+
* declaring service: only it records to it. */
|
|
508
|
+
export class GaugeGroup<L extends Labels<L>> {
|
|
509
|
+
private readonly inst: Lazy;
|
|
510
|
+
|
|
511
|
+
/** Declares the gauge group `name`. */
|
|
512
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
513
|
+
this.inst = new Lazy('gauge', name, opts, new.target === GaugeGroup);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** Binds the shared or global gauge group `name`, declared elsewhere,
|
|
517
|
+
* whose labels are `L`. */
|
|
518
|
+
static named<L extends Labels<L>>(name: string): GaugeGroup<L> {
|
|
519
|
+
return bind(GaugeGroup, new Lazy('gauge', name, undefined, false));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** The gauge for `labels`. Worth keeping where it is used often:
|
|
523
|
+
* making it encodes the labels' attributes. */
|
|
524
|
+
with(labels: L): Gauge {
|
|
525
|
+
return bind(Gauge, this.inst, attributes(labels));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/** The group itself, to hand to other code. */
|
|
529
|
+
ref(): this {
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** A gauge group shared between services: each that records to it is tracked
|
|
535
|
+
* as using it. */
|
|
536
|
+
export class SharedGaugeGroup<L extends Labels<L>> extends GaugeGroup<L> {
|
|
537
|
+
/** Declares the shared gauge group `name`. */
|
|
538
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
539
|
+
super(name, opts);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
override with(labels: L): SharedGauge {
|
|
543
|
+
return bind(SharedGauge, lazy(this), attributes(labels));
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** A gauge group of the whole app: any code may record to it. */
|
|
548
|
+
export class GlobalGaugeGroup<L extends Labels<L>> extends GaugeGroup<L> {
|
|
549
|
+
/** Declares the global gauge group `name`. */
|
|
550
|
+
constructor(name: string, opts?: MetricOptions) {
|
|
551
|
+
super(name, opts);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
override with(labels: L): GlobalGauge {
|
|
555
|
+
return bind(GlobalGauge, lazy(this), attributes(labels));
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** A histogram for each combination of the labels `L`. Belongs to the
|
|
560
|
+
* declaring service: only it records to it. */
|
|
561
|
+
export class HistogramGroup<L extends Labels<L>> {
|
|
562
|
+
private readonly inst: Lazy;
|
|
563
|
+
|
|
564
|
+
/** Declares the histogram group `name`. */
|
|
565
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
566
|
+
this.inst = new Lazy(
|
|
567
|
+
'histogram',
|
|
568
|
+
name,
|
|
569
|
+
opts,
|
|
570
|
+
new.target === HistogramGroup,
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** Binds the shared or global histogram group `name`, declared elsewhere,
|
|
575
|
+
* whose labels are `L`. */
|
|
576
|
+
static named<L extends Labels<L>>(name: string): HistogramGroup<L> {
|
|
577
|
+
return bind(HistogramGroup, new Lazy('histogram', name, undefined, false));
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** The histogram for `labels`. Worth keeping where it is used often:
|
|
581
|
+
* making it encodes the labels' attributes. */
|
|
582
|
+
with(labels: L): Histogram {
|
|
583
|
+
return bind(Histogram, this.inst, attributes(labels));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** The group itself, to hand to other code. */
|
|
587
|
+
ref(): this {
|
|
588
|
+
return this;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** A histogram group shared between services: each that records to it is tracked
|
|
593
|
+
* as using it. */
|
|
594
|
+
export class SharedHistogramGroup<
|
|
595
|
+
L extends Labels<L>,
|
|
596
|
+
> extends HistogramGroup<L> {
|
|
597
|
+
/** Declares the shared histogram group `name`. */
|
|
598
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
599
|
+
super(name, opts);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
override with(labels: L): SharedHistogram {
|
|
603
|
+
return bind(SharedHistogram, lazy(this), attributes(labels));
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** A histogram group of the whole app: any code may record to it. */
|
|
608
|
+
export class GlobalHistogramGroup<
|
|
609
|
+
L extends Labels<L>,
|
|
610
|
+
> extends HistogramGroup<L> {
|
|
611
|
+
/** Declares the global histogram group `name`. */
|
|
612
|
+
constructor(name: string, opts?: HistogramOptions) {
|
|
613
|
+
super(name, opts);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
override with(labels: L): GlobalHistogram {
|
|
617
|
+
return bind(GlobalHistogram, lazy(this), attributes(labels));
|
|
618
|
+
}
|
|
619
|
+
}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export {appMeta} from './app_meta';
|
|
2
|
+
export type {
|
|
3
|
+
AppMeta,
|
|
4
|
+
BuildMeta,
|
|
5
|
+
CloudProvider,
|
|
6
|
+
DeployMeta,
|
|
7
|
+
EnvironmentMeta,
|
|
8
|
+
EnvironmentType,
|
|
9
|
+
GatewayMeta,
|
|
10
|
+
HostedService,
|
|
11
|
+
} from './app_meta';
|
|
12
|
+
|
|
13
|
+
export {currentRequest} from './req_meta';
|
|
14
|
+
export type {
|
|
15
|
+
BaseRequestMeta,
|
|
16
|
+
PubSubMessageMeta,
|
|
17
|
+
RequestMeta,
|
|
18
|
+
TraceData,
|
|
19
|
+
} from './req_meta';
|
|
20
|
+
|
|
21
|
+
export {meterProvider, tracerProvider} from './otel';
|
|
22
|
+
export type {MeterProvider, TracerProvider} from './otel';
|