@twin.org/telemetry-service 0.9.3-next.3 → 0.9.3-next.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/dist/es/metricsCollectorService.js +40 -13
- package/dist/es/metricsCollectorService.js.map +1 -1
- package/dist/es/models/IMetricsCollectorServiceConstructorOptions.js.map +1 -1
- package/dist/es/telemetryRoutes.js +36 -12
- package/dist/es/telemetryRoutes.js.map +1 -1
- package/dist/es/telemetryService.js +25 -4
- package/dist/es/telemetryService.js.map +1 -1
- package/dist/types/models/IMetricsCollectorServiceConstructorOptions.d.ts +5 -0
- package/dist/types/telemetryRoutes.d.ts +1 -1
- package/dist/types/telemetryService.d.ts +11 -5
- package/docs/changelog.md +28 -0
- package/docs/open-api/spec.json +34 -4
- package/docs/reference/classes/TelemetryService.md +30 -4
- package/docs/reference/functions/telemetryCreateMetric.md +1 -1
- package/docs/reference/interfaces/IMetricsCollectorServiceConstructorOptions.md +14 -0
- package/locales/en.json +3 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseError, ComponentFactory, Guards, Validation } from "@twin.org/core";
|
|
2
|
-
import { MetricsProducerFactory } from "@twin.org/telemetry-models";
|
|
2
|
+
import { MetricHelper, MetricsProducerFactory } from "@twin.org/telemetry-models";
|
|
3
3
|
/**
|
|
4
4
|
* Collects metrics from registered producers at a fixed interval.
|
|
5
5
|
*/
|
|
@@ -18,6 +18,11 @@ export class MetricsCollectorService {
|
|
|
18
18
|
* @internal
|
|
19
19
|
*/
|
|
20
20
|
_platformComponent;
|
|
21
|
+
/**
|
|
22
|
+
* Telemetry component the collected values are recorded with.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
_telemetryComponent;
|
|
21
26
|
/**
|
|
22
27
|
* Polling interval in milliseconds.
|
|
23
28
|
* @internal
|
|
@@ -41,6 +46,7 @@ export class MetricsCollectorService {
|
|
|
41
46
|
constructor(options) {
|
|
42
47
|
this._loggingComponent = ComponentFactory.getIfExists(options?.loggingComponentType);
|
|
43
48
|
this._platformComponent = ComponentFactory.get(options?.platformComponentType ?? "platform");
|
|
49
|
+
this._telemetryComponent = ComponentFactory.getIfExists(options?.telemetryComponentType ?? "telemetry");
|
|
44
50
|
const intervalMs = options?.config?.intervalMs ?? 60_000;
|
|
45
51
|
Guards.integer(MetricsCollectorService.CLASS_NAME, "intervalMs", intervalMs);
|
|
46
52
|
const failures = [];
|
|
@@ -95,21 +101,42 @@ export class MetricsCollectorService {
|
|
|
95
101
|
async tick() {
|
|
96
102
|
const names = MetricsProducerFactory.names();
|
|
97
103
|
const producers = names.map(name => MetricsProducerFactory.get(name));
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
try {
|
|
105
|
+
await this._platformComponent.execute(async () => {
|
|
106
|
+
const values = [];
|
|
107
|
+
for (const producer of producers) {
|
|
108
|
+
try {
|
|
109
|
+
values.push(...(await producer.collect()));
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
await this._loggingComponent?.log({
|
|
113
|
+
source: MetricsCollectorService.CLASS_NAME,
|
|
114
|
+
level: "error",
|
|
115
|
+
message: "producerCollectionFailed",
|
|
116
|
+
data: {
|
|
117
|
+
producer: producer.className()
|
|
118
|
+
},
|
|
119
|
+
error: BaseError.fromError(err)
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// The whole cycle is recorded in one call, so a connector which persists the values
|
|
124
|
+
// writes them together instead of once per producer.
|
|
125
|
+
await MetricHelper.metricValues(this._telemetryComponent, values, async (err) => this._loggingComponent?.log({
|
|
104
126
|
source: MetricsCollectorService.CLASS_NAME,
|
|
105
127
|
level: "error",
|
|
106
|
-
message: "
|
|
107
|
-
data: {
|
|
108
|
-
producer: producer.className()
|
|
109
|
-
},
|
|
128
|
+
message: "metricRecordFailed",
|
|
110
129
|
error: BaseError.fromError(err)
|
|
111
|
-
});
|
|
112
|
-
}
|
|
130
|
+
}));
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
await this._loggingComponent?.log({
|
|
135
|
+
source: MetricsCollectorService.CLASS_NAME,
|
|
136
|
+
level: "error",
|
|
137
|
+
message: "collectionCycleFailed",
|
|
138
|
+
error: BaseError.fromError(err)
|
|
139
|
+
});
|
|
113
140
|
}
|
|
114
141
|
this.startTimer();
|
|
115
142
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricsCollectorService.js","sourceRoot":"","sources":["../../src/metricsCollectorService.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,SAAS,EACT,gBAAgB,EAChB,MAAM,EAEN,UAAU,EACV,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAEN,sBAAsB,EACtB,MAAM,4BAA4B,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,uBAAuB;IACnC;;OAEG;IACI,MAAM,CAAU,UAAU,6BAA6C;IAE9E;;;OAGG;IACc,iBAAiB,CAAqB;IAEvD;;;OAGG;IACc,kBAAkB,CAAqB;IAExD;;;OAGG;IACc,WAAW,CAAS;IAErC;;;OAGG;IACK,MAAM,CAAuD;IAErE;;;OAGG;IACK,QAAQ,CAAU;IAE1B;;;;OAIG;IACH,YAAY,OAAoD;QAC/D,IAAI,CAAC,iBAAiB,GAAG,gBAAgB,CAAC,WAAW,CACpD,OAAO,EAAE,oBAAoB,CAC7B,CAAC;QACF,IAAI,CAAC,kBAAkB,GAAG,gBAAgB,CAAC,GAAG,CAC7C,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAC5C,CAAC;QAEF,MAAM,UAAU,GAAG,OAAO,EAAE,MAAM,EAAE,UAAU,IAAI,MAAM,CAAC;QACzD,MAAM,CAAC,OAAO,CAAC,uBAAuB,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACnF,MAAM,QAAQ,GAAyB,EAAE,CAAC;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,CAAC;YACrC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACzB,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IPlatformComponent } from \"@twin.org/api-models\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tGuards,\n\ttype IValidationFailure,\n\tValidation\n} from \"@twin.org/core\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\ttype IMetricsCollectorComponent,\n\tMetricsProducerFactory\n} from \"@twin.org/telemetry-models\";\nimport type { IMetricsCollectorServiceConstructorOptions } from \"./models/IMetricsCollectorServiceConstructorOptions.js\";\n\n/**\n * Collects metrics from registered producers at a fixed interval.\n */\nexport class MetricsCollectorService implements IMetricsCollectorComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MetricsCollectorService>();\n\n\t/**\n\t * Resolved logging component for internal use.\n\t * @internal\n\t */\n\tprivate readonly _loggingComponent?: ILoggingComponent;\n\n\t/**\n\t * Platform component.\n\t * @internal\n\t */\n\tprivate readonly _platformComponent: IPlatformComponent;\n\n\t/**\n\t * Polling interval in milliseconds.\n\t * @internal\n\t */\n\tprivate readonly _intervalMs: number;\n\n\t/**\n\t * Pending scheduled tick handle.\n\t * @internal\n\t */\n\tprivate _timer: ReturnType<typeof globalThis.setTimeout> | undefined;\n\n\t/**\n\t * Whether the service is currently running.\n\t * @internal\n\t */\n\tprivate _running: boolean;\n\n\t/**\n\t * Create a new instance of MetricsCollectorService.\n\t * @param options The options for the service.\n\t * @throws ValidationError If intervalMs is not a positive integer.\n\t */\n\tconstructor(options?: IMetricsCollectorServiceConstructorOptions) {\n\t\tthis._loggingComponent = ComponentFactory.getIfExists<ILoggingComponent>(\n\t\t\toptions?.loggingComponentType\n\t\t);\n\t\tthis._platformComponent = ComponentFactory.get<IPlatformComponent>(\n\t\t\toptions?.platformComponentType ?? \"platform\"\n\t\t);\n\n\t\tconst intervalMs = options?.config?.intervalMs ?? 60_000;\n\t\tGuards.integer(MetricsCollectorService.CLASS_NAME, nameof(intervalMs), intervalMs);\n\t\tconst failures: IValidationFailure[] = [];\n\t\tValidation.integer(nameof(intervalMs), intervalMs, failures, undefined, { minValue: 1 });\n\t\tValidation.asValidationError(\n\t\t\tMetricsCollectorService.CLASS_NAME,\n\t\t\tnameof<MetricsCollectorService>(),\n\t\t\tfailures\n\t\t);\n\t\tthis._intervalMs = intervalMs;\n\t\tthis._running = false;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MetricsCollectorService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Start the service: register all producers and begin the polling cycle.\n\t * The first collection tick fires immediately but does not block start() from returning.\n\t * @returns A promise that resolves once producers are registered and the first tick is in flight.\n\t */\n\tpublic async start(): Promise<void> {\n\t\tif (this._running) {\n\t\t\treturn;\n\t\t}\n\t\tthis._running = true;\n\n\t\tconst names = MetricsProducerFactory.names();\n\t\tconst producers = names.map(name => MetricsProducerFactory.get(name));\n\n\t\tfor (const producer of producers) {\n\t\t\tawait this._platformComponent.execute(async () => producer.register());\n\t\t}\n\n\t\t// Defer the first tick to the next event-loop turn so start() returns before any\n\t\t// collection work begins, keeping engine boot unblocked.\n\t\tthis._timer = globalThis.setTimeout(async () => {\n\t\t\tawait this.tick();\n\t\t}, 0);\n\t}\n\n\t/**\n\t * Stop the service and cancel the pending timer.\n\t * @returns A promise that resolves when the service has stopped.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._running) {\n\t\t\tthis._running = false;\n\t\t\tthis.stopTimer();\n\t\t}\n\t}\n\n\t/**\n\t * One collection cycle across all registered producers.\n\t * Public so tests can drive it deterministically.\n\t * @returns A promise that resolves when all producers have been polled and the next tick scheduled.\n\t */\n\tpublic async tick(): Promise<void> {\n\t\tconst names = MetricsProducerFactory.names();\n\t\tconst producers = names.map(name => MetricsProducerFactory.get(name));\n\n\t\tfor (const producer of producers) {\n\t\t\ttry {\n\t\t\t\tawait this._platformComponent.execute(async () => producer.collect());\n\t\t\t} catch (err) {\n\t\t\t\tawait this._loggingComponent?.log({\n\t\t\t\t\tsource: MetricsCollectorService.CLASS_NAME,\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tmessage: \"producerCollectionFailed\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tproducer: producer.className()\n\t\t\t\t\t},\n\t\t\t\t\terror: BaseError.fromError(err)\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tthis.startTimer();\n\t}\n\n\t/**\n\t * Schedule the next tick after the configured interval.\n\t * @internal\n\t */\n\tprivate startTimer(): void {\n\t\tif (this._running) {\n\t\t\tthis.stopTimer();\n\n\t\t\tthis._timer = globalThis.setTimeout(async () => {\n\t\t\t\tawait this.tick();\n\t\t\t}, this._intervalMs);\n\t\t}\n\t}\n\n\t/**\n\t * Stop the timer.\n\t * @internal\n\t */\n\tprivate stopTimer(): void {\n\t\tif (this._timer !== undefined) {\n\t\t\tglobalThis.clearTimeout(this._timer);\n\t\t\tthis._timer = undefined;\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"metricsCollectorService.js","sourceRoot":"","sources":["../../src/metricsCollectorService.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,SAAS,EACT,gBAAgB,EAChB,MAAM,EAEN,UAAU,EACV,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACN,YAAY,EACZ,sBAAsB,EAItB,MAAM,4BAA4B,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,uBAAuB;IACnC;;OAEG;IACI,MAAM,CAAU,UAAU,6BAA6C;IAE9E;;;OAGG;IACc,iBAAiB,CAAqB;IAEvD;;;OAGG;IACc,kBAAkB,CAAqB;IAExD;;;OAGG;IACc,mBAAmB,CAAuB;IAE3D;;;OAGG;IACc,WAAW,CAAS;IAErC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ACK,SAAS;QAChB,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC/B,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACrC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACzB,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IPlatformComponent } from \"@twin.org/api-models\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tGuards,\n\ttype IValidationFailure,\n\tValidation\n} from \"@twin.org/core\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tMetricHelper,\n\tMetricsProducerFactory,\n\ttype IMetricsCollectorComponent,\n\ttype ITelemetryComponent,\n\ttype ITelemetryMetricValueEntry\n} from \"@twin.org/telemetry-models\";\nimport type { IMetricsCollectorServiceConstructorOptions } from \"./models/IMetricsCollectorServiceConstructorOptions.js\";\n\n/**\n * Collects metrics from registered producers at a fixed interval.\n */\nexport class MetricsCollectorService implements IMetricsCollectorComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MetricsCollectorService>();\n\n\t/**\n\t * Resolved logging component for internal use.\n\t * @internal\n\t */\n\tprivate readonly _loggingComponent?: ILoggingComponent;\n\n\t/**\n\t * Platform component.\n\t * @internal\n\t */\n\tprivate readonly _platformComponent: IPlatformComponent;\n\n\t/**\n\t * Telemetry component the collected values are recorded with.\n\t * @internal\n\t */\n\tprivate readonly _telemetryComponent?: ITelemetryComponent;\n\n\t/**\n\t * Polling interval in milliseconds.\n\t * @internal\n\t */\n\tprivate readonly _intervalMs: number;\n\n\t/**\n\t * Pending scheduled tick handle.\n\t * @internal\n\t */\n\tprivate _timer: ReturnType<typeof globalThis.setTimeout> | undefined;\n\n\t/**\n\t * Whether the service is currently running.\n\t * @internal\n\t */\n\tprivate _running: boolean;\n\n\t/**\n\t * Create a new instance of MetricsCollectorService.\n\t * @param options The options for the service.\n\t * @throws ValidationError If intervalMs is not a positive integer.\n\t */\n\tconstructor(options?: IMetricsCollectorServiceConstructorOptions) {\n\t\tthis._loggingComponent = ComponentFactory.getIfExists<ILoggingComponent>(\n\t\t\toptions?.loggingComponentType\n\t\t);\n\t\tthis._platformComponent = ComponentFactory.get<IPlatformComponent>(\n\t\t\toptions?.platformComponentType ?? \"platform\"\n\t\t);\n\t\tthis._telemetryComponent = ComponentFactory.getIfExists<ITelemetryComponent>(\n\t\t\toptions?.telemetryComponentType ?? \"telemetry\"\n\t\t);\n\n\t\tconst intervalMs = options?.config?.intervalMs ?? 60_000;\n\t\tGuards.integer(MetricsCollectorService.CLASS_NAME, nameof(intervalMs), intervalMs);\n\t\tconst failures: IValidationFailure[] = [];\n\t\tValidation.integer(nameof(intervalMs), intervalMs, failures, undefined, { minValue: 1 });\n\t\tValidation.asValidationError(\n\t\t\tMetricsCollectorService.CLASS_NAME,\n\t\t\tnameof<MetricsCollectorService>(),\n\t\t\tfailures\n\t\t);\n\t\tthis._intervalMs = intervalMs;\n\t\tthis._running = false;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MetricsCollectorService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Start the service: register all producers and begin the polling cycle.\n\t * The first collection tick fires immediately but does not block start() from returning.\n\t * @returns A promise that resolves once producers are registered and the first tick is in flight.\n\t */\n\tpublic async start(): Promise<void> {\n\t\tif (this._running) {\n\t\t\treturn;\n\t\t}\n\t\tthis._running = true;\n\n\t\tconst names = MetricsProducerFactory.names();\n\t\tconst producers = names.map(name => MetricsProducerFactory.get(name));\n\n\t\tfor (const producer of producers) {\n\t\t\tawait this._platformComponent.execute(async () => producer.register());\n\t\t}\n\n\t\t// Defer the first tick to the next event-loop turn so start() returns before any\n\t\t// collection work begins, keeping engine boot unblocked.\n\t\tthis._timer = globalThis.setTimeout(async () => {\n\t\t\tawait this.tick();\n\t\t}, 0);\n\t}\n\n\t/**\n\t * Stop the service and cancel the pending timer.\n\t * @returns A promise that resolves when the service has stopped.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._running) {\n\t\t\tthis._running = false;\n\t\t\tthis.stopTimer();\n\t\t}\n\t}\n\n\t/**\n\t * One collection cycle across all registered producers.\n\t * Public so tests can drive it deterministically.\n\t * @returns A promise that resolves when all producers have been polled and the next tick scheduled.\n\t */\n\tpublic async tick(): Promise<void> {\n\t\tconst names = MetricsProducerFactory.names();\n\t\tconst producers = names.map(name => MetricsProducerFactory.get(name));\n\n\t\ttry {\n\t\t\tawait this._platformComponent.execute(async () => {\n\t\t\t\tconst values: ITelemetryMetricValueEntry[] = [];\n\n\t\t\t\tfor (const producer of producers) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tvalues.push(...(await producer.collect()));\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tawait this._loggingComponent?.log({\n\t\t\t\t\t\t\tsource: MetricsCollectorService.CLASS_NAME,\n\t\t\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\t\t\tmessage: \"producerCollectionFailed\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tproducer: producer.className()\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\terror: BaseError.fromError(err)\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// The whole cycle is recorded in one call, so a connector which persists the values\n\t\t\t\t// writes them together instead of once per producer.\n\t\t\t\tawait MetricHelper.metricValues(this._telemetryComponent, values, async err =>\n\t\t\t\t\tthis._loggingComponent?.log({\n\t\t\t\t\t\tsource: MetricsCollectorService.CLASS_NAME,\n\t\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\t\tmessage: \"metricRecordFailed\",\n\t\t\t\t\t\terror: BaseError.fromError(err)\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tawait this._loggingComponent?.log({\n\t\t\t\tsource: MetricsCollectorService.CLASS_NAME,\n\t\t\t\tlevel: \"error\",\n\t\t\t\tmessage: \"collectionCycleFailed\",\n\t\t\t\terror: BaseError.fromError(err)\n\t\t\t});\n\t\t}\n\n\t\tthis.startTimer();\n\t}\n\n\t/**\n\t * Schedule the next tick after the configured interval.\n\t * @internal\n\t */\n\tprivate startTimer(): void {\n\t\tif (this._running) {\n\t\t\tthis.stopTimer();\n\n\t\t\tthis._timer = globalThis.setTimeout(async () => {\n\t\t\t\tawait this.tick();\n\t\t\t}, this._intervalMs);\n\t\t}\n\t}\n\n\t/**\n\t * Stop the timer.\n\t * @internal\n\t */\n\tprivate stopTimer(): void {\n\t\tif (this._timer !== undefined) {\n\t\t\tglobalThis.clearTimeout(this._timer);\n\t\t\tthis._timer = undefined;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IMetricsCollectorServiceConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IMetricsCollectorServiceConstructorOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IMetricsCollectorServiceConfig } from \"./IMetricsCollectorServiceConfig.js\";\n\n/**\n * The options for the metrics collector service constructor.\n */\nexport interface IMetricsCollectorServiceConstructorOptions {\n\t/**\n\t * The type of the logging component to use, can be undefined for no logging.\n\t */\n\tloggingComponentType?: string;\n\n\t/**\n\t * The type of the platform component to use for partitioned producers.\n\t * @default platform\n\t */\n\tplatformComponentType?: string;\n\n\t/**\n\t * The configuration options for the metrics collector service.\n\t */\n\tconfig?: IMetricsCollectorServiceConfig;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"IMetricsCollectorServiceConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IMetricsCollectorServiceConstructorOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IMetricsCollectorServiceConfig } from \"./IMetricsCollectorServiceConfig.js\";\n\n/**\n * The options for the metrics collector service constructor.\n */\nexport interface IMetricsCollectorServiceConstructorOptions {\n\t/**\n\t * The type of the logging component to use, can be undefined for no logging.\n\t */\n\tloggingComponentType?: string;\n\n\t/**\n\t * The type of the platform component to use for partitioned producers.\n\t * @default platform\n\t */\n\tplatformComponentType?: string;\n\n\t/**\n\t * The type of the telemetry component the collected values are recorded with.\n\t * @default telemetry\n\t */\n\ttelemetryComponentType?: string;\n\n\t/**\n\t * The configuration options for the metrics collector service.\n\t */\n\tconfig?: IMetricsCollectorServiceConfig;\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
3
|
import { HttpContextIdKeys, HttpHeaderHelper, HttpUrlHelper } from "@twin.org/api-models";
|
|
4
4
|
import { ContextIdStore } from "@twin.org/context";
|
|
5
|
-
import { Coerce, ComponentFactory, Guards } from "@twin.org/core";
|
|
5
|
+
import { Coerce, ComponentFactory, Guards, Is } from "@twin.org/core";
|
|
6
6
|
import { MetricType } from "@twin.org/telemetry-models";
|
|
7
7
|
import { HeaderTypes, HttpStatusCode } from "@twin.org/web";
|
|
8
8
|
/**
|
|
@@ -27,7 +27,7 @@ export const tagsTelemetry = [
|
|
|
27
27
|
export function generateRestRoutesTelemetry(baseRouteName, componentName) {
|
|
28
28
|
const createMetricRoute = {
|
|
29
29
|
operationId: "telemetryCreateMetric",
|
|
30
|
-
summary: "Create
|
|
30
|
+
summary: "Create one or more telemetry metrics",
|
|
31
31
|
tag: tagsTelemetry[0].name,
|
|
32
32
|
method: "POST",
|
|
33
33
|
path: `${baseRouteName}/metric`,
|
|
@@ -43,9 +43,28 @@ export function generateRestRoutesTelemetry(baseRouteName, componentName) {
|
|
|
43
43
|
label: "My Counter",
|
|
44
44
|
description: "This is my counter",
|
|
45
45
|
type: MetricType.Counter,
|
|
46
|
-
unit: "KG"
|
|
46
|
+
unit: "KG",
|
|
47
|
+
maxHistory: 1000
|
|
47
48
|
}
|
|
48
49
|
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "telemetryCreateMetricsRequestExample",
|
|
53
|
+
request: {
|
|
54
|
+
body: [
|
|
55
|
+
{
|
|
56
|
+
id: "my-counter",
|
|
57
|
+
label: "My Counter",
|
|
58
|
+
type: MetricType.Counter,
|
|
59
|
+
maxHistory: 1000
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "my-gauge",
|
|
63
|
+
label: "My Gauge",
|
|
64
|
+
type: MetricType.Gauge
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
}
|
|
49
68
|
}
|
|
50
69
|
]
|
|
51
70
|
},
|
|
@@ -374,7 +393,7 @@ export function generateRestRoutesTelemetry(baseRouteName, componentName) {
|
|
|
374
393
|
];
|
|
375
394
|
}
|
|
376
395
|
/**
|
|
377
|
-
* Create
|
|
396
|
+
* Create one or more telemetry metrics.
|
|
378
397
|
* @param httpRequestContext The request context for the API.
|
|
379
398
|
* @param componentName The name of the component to use in the routes.
|
|
380
399
|
* @param request The request.
|
|
@@ -383,17 +402,22 @@ export function generateRestRoutesTelemetry(baseRouteName, componentName) {
|
|
|
383
402
|
*/
|
|
384
403
|
export async function telemetryCreateMetric(httpRequestContext, componentName, request, baseRouteName) {
|
|
385
404
|
Guards.object(ROUTES_SOURCE, "request", request);
|
|
386
|
-
Guards.
|
|
405
|
+
Guards.defined(ROUTES_SOURCE, "request.body", request.body);
|
|
387
406
|
const component = ComponentFactory.get(componentName);
|
|
388
|
-
await component.createMetric(
|
|
389
|
-
id: request.body.id,
|
|
390
|
-
label: request.body.label,
|
|
391
|
-
description: request.body.description,
|
|
392
|
-
type: request.body.type,
|
|
393
|
-
unit: request.body.unit
|
|
394
|
-
});
|
|
407
|
+
await component.createMetric(request.body);
|
|
395
408
|
const contextIds = await ContextIdStore.getContextIds();
|
|
396
409
|
const publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];
|
|
410
|
+
if (Is.array(request.body)) {
|
|
411
|
+
// A batch creates many metrics, so the location points at the collection they joined
|
|
412
|
+
// rather than at any one of them.
|
|
413
|
+
return {
|
|
414
|
+
statusCode: HttpStatusCode.created,
|
|
415
|
+
headers: {
|
|
416
|
+
[HeaderTypes.Location]: HttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/metric`) ??
|
|
417
|
+
`${baseRouteName}/metric`
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
}
|
|
397
421
|
const headers = {};
|
|
398
422
|
HttpHeaderHelper.buildId(headers, request.body.id, HttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`));
|
|
399
423
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetryRoutes.js","sourceRoot":"","sources":["../../src/telemetryRoutes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EAMb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAElE,OAAO,EACN,UAAU,EAcV,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,cAAc,EAAqB,MAAM,eAAe,CAAC;AAE/E;;GAEG;AACH,MAAM,aAAa,GAAG,iBAAiB,CAAC;AAExC;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAW;IACpC;QACC,IAAI,EAAE,WAAW;QACjB,WAAW,EAAE,8DAA8D;KAC3E;CACD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CAC1C,aAAqB,EACrB,aAAqB;IAErB,MAAM,iBAAiB,GAAgE;QACtF,WAAW,EAAE,uB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,mBAEb,OAAO,CAAC,KAAK,CACb,CAAC;IACF,MAAM,CAAC,WAAW,CAAC,aAAa,2BAAiC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAExF,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,CAAsB,aAAa,CAAC,CAAC;IAE3E,MAAM,cAAc,GAAG,MAAM,SAAS,CAAC,WAAW,CACjD,OAAO,EAAE,UAAU,CAAC,EAAE,EACtB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC,EACxC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,EACtC,OAAO,EAAE,KAAK,EAAE,MAAM,EACtB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,CACpC,CAAC;IAEF,OAAO;QACN,IAAI,EAAE,cAAc;KACpB,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpContextIdKeys,\n\tHttpHeaderHelper,\n\tHttpUrlHelper,\n\ttype ICreatedResponse,\n\ttype IHttpRequestContext,\n\ttype INoContentResponse,\n\ttype IRestRoute,\n\ttype ITag\n} from \"@twin.org/api-models\";\nimport { ContextIdStore } from \"@twin.org/context\";\nimport { Coerce, ComponentFactory, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tMetricType,\n\ttype ITelemetryAddMetricValueRequest,\n\ttype ITelemetryComponent,\n\ttype ITelemetryCreateMetricRequest,\n\ttype ITelemetryGetMetricRequest,\n\ttype ITelemetryGetMetricResponse,\n\ttype ITelemetryGetMetricValueRequest,\n\ttype ITelemetryGetMetricValueResponse,\n\ttype ITelemetryListRequest,\n\ttype ITelemetryListResponse,\n\ttype ITelemetryRemoveMetricRequest,\n\ttype ITelemetryUpdateMetricRequest,\n\ttype ITelemetryValuesListRequest,\n\ttype ITelemetryValuesListResponse\n} from \"@twin.org/telemetry-models\";\nimport { HeaderTypes, HttpStatusCode, type IHttpHeaders } from \"@twin.org/web\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"telemetryRoutes\";\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsTelemetry: ITag[] = [\n\t{\n\t\tname: \"Telemetry\",\n\t\tdescription: \"Endpoints which are modelled to access a telemetry contract.\"\n\t}\n];\n\n/**\n * The REST routes for telemetry.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @returns The generated routes.\n */\nexport function generateRestRoutesTelemetry(\n\tbaseRouteName: string,\n\tcomponentName: string\n): IRestRoute[] {\n\tconst createMetricRoute: IRestRoute<ITelemetryCreateMetricRequest, ICreatedResponse> = {\n\t\toperationId: \"telemetryCreateMetric\",\n\t\tsummary: \"Create a telemetry metric\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/metric`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryCreateMetric(httpRequestContext, componentName, request, baseRouteName),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryCreateMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryCreateMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\tdescription: \"This is my counter\",\n\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ICreatedResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryCreateMetricResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tstatusCode: HttpStatusCode.created,\n\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t[HeaderTypes.Location]: \"my-counter\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getMetricRoute: IRestRoute<ITelemetryGetMetricRequest, ITelemetryGetMetricResponse> = {\n\t\toperationId: \"telemetryGetMetric\",\n\t\tsummary: \"Get a telemetry metric and it's most recent value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryGetMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryGetMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryGetMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryGetMetricResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryGetMetricResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tmetric: {\n\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\tdescription: \"This is my counter\",\n\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tvalue: {\n\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst updateMetricRoute: IRestRoute<ITelemetryUpdateMetricRequest, INoContentResponse> = {\n\t\toperationId: \"telemetryUpdateMetric\",\n\t\tsummary: \"Update a telemetry metric\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"PUT\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryUpdateMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryUpdateMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryUpdateMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tlabel: \"My New Label\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst addMetricValueRoute: IRestRoute<ITelemetryAddMetricValueRequest, ICreatedResponse> = {\n\t\toperationId: \"telemetryAddMetricValue\",\n\t\tsummary: \"Add a telemetry metric value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/metric/:id/value`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryAddMetricValue(httpRequestContext, componentName, request, baseRouteName),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryAddMetricValueRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryAddMetricValueRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryAddMetricValueIncRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tvalue: \"inc\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ICreatedResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryAddMetricValueResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tstatusCode: HttpStatusCode.created,\n\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t[HeaderTypes.Location]: \"aabbccdd11223445566\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst removeMetricRoute: IRestRoute<ITelemetryRemoveMetricRequest, INoContentResponse> = {\n\t\toperationId: \"telemetryRemoveMetric\",\n\t\tsummary: \"Remove a telemetry metric and it's values.\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryRemoveMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryRemoveMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryRemoveMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listMetricsRoute: IRestRoute<ITelemetryListRequest, ITelemetryListResponse> = {\n\t\toperationId: \"telemetryListEntries\",\n\t\tsummary: \"Get a list of the telemetry metrics\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryMetricList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryListRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryListRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tquery: {\n\t\t\t\t\t\t\ttype: MetricType.Counter.toString()\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryListResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryListResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tentities: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"my-counter-2\",\n\t\t\t\t\t\t\t\t\t\tlabel: \"My Counter 2\",\n\t\t\t\t\t\t\t\t\t\ttype: MetricType.IncDecCounter,\n\t\t\t\t\t\t\t\t\t\tunit: \"M\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tcursor: \"1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getMetricValueRoute: IRestRoute<\n\t\tITelemetryGetMetricValueRequest,\n\t\tITelemetryGetMetricValueResponse\n\t> = {\n\t\toperationId: \"telemetryGetMetricValue\",\n\t\tsummary: \"Get a specific telemetry metric value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id/value/:valueId`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryGetMetricValue(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryGetMetricValueRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryGetMetricValueRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\tvalueId: \"aabbccdd11223445566\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryGetMetricValueResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryGetMetricValueResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listMetricsValuesRoute: IRestRoute<\n\t\tITelemetryValuesListRequest,\n\t\tITelemetryValuesListResponse\n\t> = {\n\t\toperationId: \"telemetryValuesListEntries\",\n\t\tsummary: \"Get a list of the values for a telemetry metric\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id/value`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryMetricValueList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryValuesListRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryValuesListRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryValuesListResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryValuesListResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tmetric: {\n\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tentities: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\t\tts: 1715252922274,\n\t\t\t\t\t\t\t\t\t\tvalue: 11\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tcursor: \"1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\treturn [\n\t\tcreateMetricRoute,\n\t\tgetMetricRoute,\n\t\tupdateMetricRoute,\n\t\taddMetricValueRoute,\n\t\tremoveMetricRoute,\n\t\tlistMetricsRoute,\n\t\tgetMetricValueRoute,\n\t\tlistMetricsValuesRoute\n\t];\n}\n\n/**\n * Create a new telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @param baseRouteName The base route name for the API.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryCreateMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryCreateMetricRequest,\n\tbaseRouteName: string\n): Promise<ICreatedResponse> {\n\tGuards.object<ITelemetryCreateMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryCreateMetricRequest[\"body\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.body),\n\t\trequest.body\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.createMetric({\n\t\tid: request.body.id,\n\t\tlabel: request.body.label,\n\t\tdescription: request.body.description,\n\t\ttype: request.body.type,\n\t\tunit: request.body.unit\n\t});\n\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tconst headers: IHttpHeaders = {};\n\tHttpHeaderHelper.buildId(\n\t\theaders,\n\t\trequest.body.id,\n\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`)\n\t);\n\n\treturn {\n\t\tstatusCode: HttpStatusCode.created,\n\t\theaders\n\t};\n}\n\n/**\n * Gets a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryGetMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryGetMetricRequest\n): Promise<ITelemetryGetMetricResponse> {\n\tGuards.object<ITelemetryGetMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryGetMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst result = await component.getMetric(request.pathParams.id);\n\treturn { body: result };\n}\n\n/**\n * Updates a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryUpdateMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryUpdateMetricRequest\n): Promise<INoContentResponse> {\n\tGuards.object<ITelemetryUpdateMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryUpdateMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.object<ITelemetryUpdateMetricRequest[\"body\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.body),\n\t\trequest.body\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.updateMetric({\n\t\tid: request.pathParams.id,\n\t\tlabel: request.body.label,\n\t\tdescription: request.body.description,\n\t\tunit: request.body.unit\n\t});\n\n\treturn { statusCode: HttpStatusCode.noContent };\n}\n\n/**\n * Add a telemetry metric value.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @param baseRouteName The base route name for the API.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryAddMetricValue(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryAddMetricValueRequest,\n\tbaseRouteName: string\n): Promise<ICreatedResponse> {\n\tGuards.object<ITelemetryAddMetricValueRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryAddMetricValueRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.object<ITelemetryAddMetricValueRequest[\"body\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.body),\n\t\trequest.body\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst id = await component.addMetricValue(\n\t\trequest.pathParams.id,\n\t\trequest.body.value,\n\t\trequest.body.customData\n\t);\n\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tconst headers: IHttpHeaders = {};\n\tHttpHeaderHelper.buildId(\n\t\theaders,\n\t\tid,\n\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`)\n\t);\n\n\treturn { statusCode: HttpStatusCode.created, headers };\n}\n\n/**\n * Removes a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryRemoveMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryRemoveMetricRequest\n): Promise<INoContentResponse> {\n\tGuards.object<ITelemetryRemoveMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryRemoveMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.removeMetric(request.pathParams.id);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Get a list of the telemetry metrics.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryMetricList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryListRequest\n): Promise<ITelemetryListResponse> {\n\tGuards.object<ITelemetryListRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryListRequest[\"query\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.query),\n\t\trequest.query\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\n\tconst itemsAndCursor = await component.query(\n\t\tCoerce.number(request?.query?.type) as MetricType,\n\t\trequest?.query?.cursor,\n\t\tCoerce.number(request?.query?.limit)\n\t);\n\n\treturn {\n\t\tbody: itemsAndCursor\n\t};\n}\n\n/**\n * Gets a specific telemetry metric value.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryGetMetricValue(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryGetMetricValueRequest\n): Promise<ITelemetryGetMetricValueResponse> {\n\tGuards.object<ITelemetryGetMetricValueRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryGetMetricValueRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.valueId), request.pathParams.valueId);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst result = await component.getMetricValue(request.pathParams.id, request.pathParams.valueId);\n\treturn { body: result };\n}\n\n/**\n * Get a list of the telemetry metric values.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryMetricValueList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryValuesListRequest\n): Promise<ITelemetryValuesListResponse> {\n\tGuards.object<ITelemetryValuesListRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryValuesListRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.query),\n\t\trequest.query\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\n\tconst itemsAndCursor = await component.queryValues(\n\t\trequest?.pathParams.id,\n\t\tCoerce.number(request?.query?.timeStart),\n\t\tCoerce.number(request?.query?.timeEnd),\n\t\trequest?.query?.cursor,\n\t\tCoerce.number(request?.query?.limit)\n\t);\n\n\treturn {\n\t\tbody: itemsAndCursor\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"telemetryRoutes.js","sourceRoot":"","sources":["../../src/telemetryRoutes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EAMb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CA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AAM,CAAC,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC,EACxC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,EACtC,OAAO,EAAE,KAAK,EAAE,MAAM,EACtB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,CACpC,CAAC;IAEF,OAAO;QACN,IAAI,EAAE,cAAc;KACpB,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpContextIdKeys,\n\tHttpHeaderHelper,\n\tHttpUrlHelper,\n\ttype ICreatedResponse,\n\ttype IHttpRequestContext,\n\ttype INoContentResponse,\n\ttype IRestRoute,\n\ttype ITag\n} from \"@twin.org/api-models\";\nimport { ContextIdStore } from \"@twin.org/context\";\nimport { Coerce, ComponentFactory, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tMetricType,\n\ttype ITelemetryAddMetricValueRequest,\n\ttype ITelemetryComponent,\n\ttype ITelemetryCreateMetricRequest,\n\ttype ITelemetryGetMetricRequest,\n\ttype ITelemetryGetMetricResponse,\n\ttype ITelemetryGetMetricValueRequest,\n\ttype ITelemetryGetMetricValueResponse,\n\ttype ITelemetryListRequest,\n\ttype ITelemetryListResponse,\n\ttype ITelemetryMetric,\n\ttype ITelemetryRemoveMetricRequest,\n\ttype ITelemetryUpdateMetricRequest,\n\ttype ITelemetryValuesListRequest,\n\ttype ITelemetryValuesListResponse\n} from \"@twin.org/telemetry-models\";\nimport { HeaderTypes, HttpStatusCode, type IHttpHeaders } from \"@twin.org/web\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"telemetryRoutes\";\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsTelemetry: ITag[] = [\n\t{\n\t\tname: \"Telemetry\",\n\t\tdescription: \"Endpoints which are modelled to access a telemetry contract.\"\n\t}\n];\n\n/**\n * The REST routes for telemetry.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @returns The generated routes.\n */\nexport function generateRestRoutesTelemetry(\n\tbaseRouteName: string,\n\tcomponentName: string\n): IRestRoute[] {\n\tconst createMetricRoute: IRestRoute<ITelemetryCreateMetricRequest, ICreatedResponse> = {\n\t\toperationId: \"telemetryCreateMetric\",\n\t\tsummary: \"Create one or more telemetry metrics\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/metric`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryCreateMetric(httpRequestContext, componentName, request, baseRouteName),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryCreateMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryCreateMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\tdescription: \"This is my counter\",\n\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\tunit: \"KG\",\n\t\t\t\t\t\t\tmaxHistory: 1000\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryCreateMetricsRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\tmaxHistory: 1000\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tid: \"my-gauge\",\n\t\t\t\t\t\t\t\tlabel: \"My Gauge\",\n\t\t\t\t\t\t\t\ttype: MetricType.Gauge\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ICreatedResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryCreateMetricResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tstatusCode: HttpStatusCode.created,\n\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t[HeaderTypes.Location]: \"my-counter\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getMetricRoute: IRestRoute<ITelemetryGetMetricRequest, ITelemetryGetMetricResponse> = {\n\t\toperationId: \"telemetryGetMetric\",\n\t\tsummary: \"Get a telemetry metric and it's most recent value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryGetMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryGetMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryGetMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryGetMetricResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryGetMetricResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tmetric: {\n\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\tdescription: \"This is my counter\",\n\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tvalue: {\n\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst updateMetricRoute: IRestRoute<ITelemetryUpdateMetricRequest, INoContentResponse> = {\n\t\toperationId: \"telemetryUpdateMetric\",\n\t\tsummary: \"Update a telemetry metric\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"PUT\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryUpdateMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryUpdateMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryUpdateMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tlabel: \"My New Label\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst addMetricValueRoute: IRestRoute<ITelemetryAddMetricValueRequest, ICreatedResponse> = {\n\t\toperationId: \"telemetryAddMetricValue\",\n\t\tsummary: \"Add a telemetry metric value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/metric/:id/value`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryAddMetricValue(httpRequestContext, componentName, request, baseRouteName),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryAddMetricValueRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryAddMetricValueRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryAddMetricValueIncRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tvalue: \"inc\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ICreatedResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryAddMetricValueResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tstatusCode: HttpStatusCode.created,\n\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t[HeaderTypes.Location]: \"aabbccdd11223445566\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst removeMetricRoute: IRestRoute<ITelemetryRemoveMetricRequest, INoContentResponse> = {\n\t\toperationId: \"telemetryRemoveMetric\",\n\t\tsummary: \"Remove a telemetry metric and it's values.\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/metric/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryRemoveMetric(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryRemoveMetricRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryRemoveMetricRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listMetricsRoute: IRestRoute<ITelemetryListRequest, ITelemetryListResponse> = {\n\t\toperationId: \"telemetryListEntries\",\n\t\tsummary: \"Get a list of the telemetry metrics\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryMetricList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryListRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryListRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tquery: {\n\t\t\t\t\t\t\ttype: MetricType.Counter.toString()\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryListResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryListResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tentities: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"my-counter-2\",\n\t\t\t\t\t\t\t\t\t\tlabel: \"My Counter 2\",\n\t\t\t\t\t\t\t\t\t\ttype: MetricType.IncDecCounter,\n\t\t\t\t\t\t\t\t\t\tunit: \"M\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tcursor: \"1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getMetricValueRoute: IRestRoute<\n\t\tITelemetryGetMetricValueRequest,\n\t\tITelemetryGetMetricValueResponse\n\t> = {\n\t\toperationId: \"telemetryGetMetricValue\",\n\t\tsummary: \"Get a specific telemetry metric value\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id/value/:valueId`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryGetMetricValue(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryGetMetricValueRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryGetMetricValueRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\tvalueId: \"aabbccdd11223445566\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryGetMetricValueResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryGetMetricValueResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listMetricsValuesRoute: IRestRoute<\n\t\tITelemetryValuesListRequest,\n\t\tITelemetryValuesListResponse\n\t> = {\n\t\toperationId: \"telemetryValuesListEntries\",\n\t\tsummary: \"Get a list of the values for a telemetry metric\",\n\t\ttag: tagsTelemetry[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/metric/:id/value`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\ttelemetryMetricValueList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<ITelemetryValuesListRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"telemetryValuesListRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"my-counter\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<ITelemetryValuesListResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"telemetryValuesListResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tmetric: {\n\t\t\t\t\t\t\t\t\tid: \"my-counter\",\n\t\t\t\t\t\t\t\t\tlabel: \"My Counter\",\n\t\t\t\t\t\t\t\t\ttype: MetricType.Counter,\n\t\t\t\t\t\t\t\t\tunit: \"KG\"\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tentities: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\t\tts: 1715252922273,\n\t\t\t\t\t\t\t\t\t\tvalue: 10\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"aabbccdd11223445566\",\n\t\t\t\t\t\t\t\t\t\tts: 1715252922274,\n\t\t\t\t\t\t\t\t\t\tvalue: 11\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tcursor: \"1\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\treturn [\n\t\tcreateMetricRoute,\n\t\tgetMetricRoute,\n\t\tupdateMetricRoute,\n\t\taddMetricValueRoute,\n\t\tremoveMetricRoute,\n\t\tlistMetricsRoute,\n\t\tgetMetricValueRoute,\n\t\tlistMetricsValuesRoute\n\t];\n}\n\n/**\n * Create one or more telemetry metrics.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @param baseRouteName The base route name for the API.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryCreateMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryCreateMetricRequest,\n\tbaseRouteName: string\n): Promise<ICreatedResponse> {\n\tGuards.object<ITelemetryCreateMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.defined(ROUTES_SOURCE, nameof(request.body), request.body);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.createMetric(request.body);\n\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tif (Is.array<ITelemetryMetric>(request.body)) {\n\t\t// A batch creates many metrics, so the location points at the collection they joined\n\t\t// rather than at any one of them.\n\t\treturn {\n\t\t\tstatusCode: HttpStatusCode.created,\n\t\t\theaders: {\n\t\t\t\t[HeaderTypes.Location]:\n\t\t\t\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/metric`) ??\n\t\t\t\t\t`${baseRouteName}/metric`\n\t\t\t}\n\t\t};\n\t}\n\n\tconst headers: IHttpHeaders = {};\n\tHttpHeaderHelper.buildId(\n\t\theaders,\n\t\trequest.body.id,\n\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`)\n\t);\n\n\treturn {\n\t\tstatusCode: HttpStatusCode.created,\n\t\theaders\n\t};\n}\n\n/**\n * Gets a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryGetMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryGetMetricRequest\n): Promise<ITelemetryGetMetricResponse> {\n\tGuards.object<ITelemetryGetMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryGetMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst result = await component.getMetric(request.pathParams.id);\n\treturn { body: result };\n}\n\n/**\n * Updates a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryUpdateMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryUpdateMetricRequest\n): Promise<INoContentResponse> {\n\tGuards.object<ITelemetryUpdateMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryUpdateMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.object<ITelemetryUpdateMetricRequest[\"body\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.body),\n\t\trequest.body\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.updateMetric({\n\t\tid: request.pathParams.id,\n\t\tlabel: request.body.label,\n\t\tdescription: request.body.description,\n\t\tunit: request.body.unit\n\t});\n\n\treturn { statusCode: HttpStatusCode.noContent };\n}\n\n/**\n * Add a telemetry metric value.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @param baseRouteName The base route name for the API.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryAddMetricValue(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryAddMetricValueRequest,\n\tbaseRouteName: string\n): Promise<ICreatedResponse> {\n\tGuards.object<ITelemetryAddMetricValueRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryAddMetricValueRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.object<ITelemetryAddMetricValueRequest[\"body\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.body),\n\t\trequest.body\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst id = await component.addMetricValue(\n\t\trequest.pathParams.id,\n\t\trequest.body.value,\n\t\trequest.body.customData\n\t);\n\n\tconst contextIds = await ContextIdStore.getContextIds();\n\tconst publicOrigin = contextIds?.[HttpContextIdKeys.PublicOrigin];\n\n\tconst headers: IHttpHeaders = {};\n\tHttpHeaderHelper.buildId(\n\t\theaders,\n\t\tid,\n\t\tHttpUrlHelper.combineOriginPath(publicOrigin, `${baseRouteName}/:id`)\n\t);\n\n\treturn { statusCode: HttpStatusCode.created, headers };\n}\n\n/**\n * Removes a telemetry metric.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryRemoveMetric(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryRemoveMetricRequest\n): Promise<INoContentResponse> {\n\tGuards.object<ITelemetryRemoveMetricRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryRemoveMetricRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tawait component.removeMetric(request.pathParams.id);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Get a list of the telemetry metrics.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryMetricList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryListRequest\n): Promise<ITelemetryListResponse> {\n\tGuards.object<ITelemetryListRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryListRequest[\"query\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.query),\n\t\trequest.query\n\t);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\n\tconst itemsAndCursor = await component.query(\n\t\tCoerce.number(request?.query?.type) as MetricType,\n\t\trequest?.query?.cursor,\n\t\tCoerce.number(request?.query?.limit)\n\t);\n\n\treturn {\n\t\tbody: itemsAndCursor\n\t};\n}\n\n/**\n * Gets a specific telemetry metric value.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryGetMetricValue(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryGetMetricValueRequest\n): Promise<ITelemetryGetMetricValueResponse> {\n\tGuards.object<ITelemetryGetMetricValueRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryGetMetricValueRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.valueId), request.pathParams.valueId);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\tconst result = await component.getMetricValue(request.pathParams.id, request.pathParams.valueId);\n\treturn { body: result };\n}\n\n/**\n * Get a list of the telemetry metric values.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function telemetryMetricValueList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: ITelemetryValuesListRequest\n): Promise<ITelemetryValuesListResponse> {\n\tGuards.object<ITelemetryValuesListRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<ITelemetryValuesListRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.query),\n\t\trequest.query\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<ITelemetryComponent>(componentName);\n\n\tconst itemsAndCursor = await component.queryValues(\n\t\trequest?.pathParams.id,\n\t\tCoerce.number(request?.query?.timeStart),\n\t\tCoerce.number(request?.query?.timeEnd),\n\t\trequest?.query?.cursor,\n\t\tCoerce.number(request?.query?.limit)\n\t);\n\n\treturn {\n\t\tbody: itemsAndCursor\n\t};\n}\n"]}
|
|
@@ -35,12 +35,12 @@ export class TelemetryService {
|
|
|
35
35
|
return TelemetryService.CLASS_NAME;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
* Create
|
|
39
|
-
* @param metric The metric details.
|
|
40
|
-
* @returns A promise that resolves when the
|
|
38
|
+
* Create one or more metrics.
|
|
39
|
+
* @param metric The metric details, or the details of several metrics.
|
|
40
|
+
* @returns A promise that resolves when the metrics have been created.
|
|
41
41
|
*/
|
|
42
42
|
async createMetric(metric) {
|
|
43
|
-
Guards.
|
|
43
|
+
Guards.defined(TelemetryService.CLASS_NAME, "metric", metric);
|
|
44
44
|
// If we don't have a tenant context ID and the tenant component is multi-tenant,
|
|
45
45
|
// we consider the entry as per-tenant and run it in all the tenant context to ensure
|
|
46
46
|
// the metric is created for each tenant.
|
|
@@ -115,6 +115,27 @@ export class TelemetryService {
|
|
|
115
115
|
}
|
|
116
116
|
return this._telemetryConnector.addMetricValue(id, value, customData);
|
|
117
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Add multiple metric values.
|
|
120
|
+
* @param values The metric values to add.
|
|
121
|
+
* @returns The created metric value ids. When fanned out per-tenant, these are the ids from the last tenant written.
|
|
122
|
+
*/
|
|
123
|
+
async addMetricValues(values) {
|
|
124
|
+
Guards.arrayValue(TelemetryService.CLASS_NAME, "values", values);
|
|
125
|
+
// If we don't have a tenant context ID and the tenant component is multi-tenant,
|
|
126
|
+
// we consider the entries as per-tenant and run them in all the tenant context to ensure
|
|
127
|
+
// the values are recorded for each tenant.
|
|
128
|
+
const contextIds = (await ContextIdStore.getContextIds()) ?? {};
|
|
129
|
+
const perTenant = !Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();
|
|
130
|
+
if (perTenant) {
|
|
131
|
+
let valueIds = [];
|
|
132
|
+
await this._platformComponent.execute(async () => {
|
|
133
|
+
valueIds = await this._telemetryConnector.addMetricValues(values);
|
|
134
|
+
});
|
|
135
|
+
return valueIds;
|
|
136
|
+
}
|
|
137
|
+
return this._telemetryConnector.addMetricValues(values);
|
|
138
|
+
}
|
|
118
139
|
/**
|
|
119
140
|
* Remove metric.
|
|
120
141
|
* @param id The id of the metric.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetryService.js","sourceRoot":"","sources":["../../src/telemetryService.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,EAAE,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAEnF,OAAO,EACN,yBAAyB,EAOzB,MAAM,4BAA4B,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,UAAU,sBAAsC;IAEvE;;;OAGG;IACc,mBAAmB,CAAsB;IAE1D;;;OAGG;IACc,kBAAkB,CAAqB;IAExD;;;OAGG;IACH,YAAY,OAA6C;QACxD,IAAI,CAAC,mBAAmB,GAAG,yBAAyB,CAAC,GAAG,CACvD,OAAO,EAAE,sBAAsB,IAAI,WAAW,CAC9C,CAAC;QACF,IAAI,CAAC,kBAAkB,GAAG,gBAAgB,CAAC,GAAG,CAC7C,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAC5C,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,SAAS;QACf,OAAO,gBAAgB,CAAC,UAAU,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,YAAY,CAAC,MAAwB;QACjD,MAAM,CAAC,MAAM,CAAmB,gBAAgB,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAErF,iFAAiF;QACjF,qFAAqF;QACrF,yCAAyC;QACzC,M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AAgB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;QAC3E,CAAC;QACD,OAAO,gBAAgB,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAChE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IPlatformComponent } from \"@twin.org/api-models\";\nimport { ContextIdKeys, ContextIdStore } from \"@twin.org/context\";\nimport { ComponentFactory, Guards, Is, NotImplementedError } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tTelemetryConnectorFactory,\n\ttype ITelemetryComponent,\n\ttype ITelemetryConnector,\n\ttype ITelemetryMetric,\n\ttype ITelemetryMetricValue,\n\ttype MetricCounterOperation,\n\ttype MetricType\n} from \"@twin.org/telemetry-models\";\nimport type { ITelemetryServiceConstructorOptions } from \"./models/ITelemetryServiceConstructorOptions.js\";\n\n/**\n * Service for performing telemetry operations to a connector.\n */\nexport class TelemetryService implements ITelemetryComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<TelemetryService>();\n\n\t/**\n\t * Telemetry connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _telemetryConnector: ITelemetryConnector;\n\n\t/**\n\t * Platform component.\n\t * @internal\n\t */\n\tprivate readonly _platformComponent: IPlatformComponent;\n\n\t/**\n\t * Create a new instance of TelemetryService.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: ITelemetryServiceConstructorOptions) {\n\t\tthis._telemetryConnector = TelemetryConnectorFactory.get(\n\t\t\toptions?.telemetryConnectorType ?? \"telemetry\"\n\t\t);\n\t\tthis._platformComponent = ComponentFactory.get<IPlatformComponent>(\n\t\t\toptions?.platformComponentType ?? \"platform\"\n\t\t);\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn TelemetryService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Create a new metric.\n\t * @param metric The metric details.\n\t * @returns A promise that resolves when the metric has been created.\n\t */\n\tpublic async createMetric(metric: ITelemetryMetric): Promise<void> {\n\t\tGuards.object<ITelemetryMetric>(TelemetryService.CLASS_NAME, nameof(metric), metric);\n\n\t\t// If we don't have a tenant context ID and the tenant component is multi-tenant,\n\t\t// we consider the entry as per-tenant and run it in all the tenant context to ensure\n\t\t// the metric is created for each tenant.\n\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\tconst perTenant =\n\t\t\t!Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();\n\n\t\tif (perTenant) {\n\t\t\tawait this._platformComponent.execute(async () =>\n\t\t\t\tthis._telemetryConnector.createMetric(metric)\n\t\t\t);\n\t\t} else {\n\t\t\tawait this._telemetryConnector.createMetric(metric);\n\t\t}\n\t}\n\n\t/**\n\t * Get the metric details and it's most recent value.\n\t * @param id The metric id.\n\t * @returns The metric details and it's most recent value.\n\t */\n\tpublic async getMetric(id: string): Promise<{\n\t\tmetric: ITelemetryMetric;\n\t\tvalue?: ITelemetryMetricValue;\n\t}> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\n\t\tconst boundGetMetric = this._telemetryConnector.getMetric?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundGetMetric)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"getMetric\");\n\t\t}\n\t\treturn boundGetMetric(id);\n\t}\n\n\t/**\n\t * Get a specific metric value by its id.\n\t * @param id The id of the metric.\n\t * @param valueId The id of the metric value.\n\t * @returns The metric value.\n\t */\n\tpublic async getMetricValue(id: string, valueId: string): Promise<ITelemetryMetricValue> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(valueId), valueId);\n\n\t\tconst boundGetMetricValue = this._telemetryConnector.getMetricValue?.bind(\n\t\t\tthis._telemetryConnector\n\t\t);\n\t\tif (Is.undefined(boundGetMetricValue)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"getMetricValue\");\n\t\t}\n\t\treturn boundGetMetricValue(id, valueId);\n\t}\n\n\t/**\n\t * Update metric.\n\t * @param metric The metric details.\n\t * @returns A promise that resolves when the metric has been updated.\n\t */\n\tpublic async updateMetric(metric: Omit<ITelemetryMetric, \"type\">): Promise<void> {\n\t\tGuards.object<ITelemetryMetric>(TelemetryService.CLASS_NAME, nameof(metric), metric);\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(metric.id), metric.id);\n\t\treturn this._telemetryConnector.updateMetric(metric);\n\t}\n\n\t/**\n\t * Add a metric value.\n\t * @param id The id of the metric.\n\t * @param value The value for the add operation.\n\t * @param customData The custom data for the add operation.\n\t * @returns The created metric value id. When fanned out per-tenant, this is the id from the last tenant written.\n\t */\n\tpublic async addMetricValue(\n\t\tid: string,\n\t\tvalue: MetricCounterOperation | number,\n\t\tcustomData?: { [key: string]: unknown }\n\t): Promise<string> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\tGuards.defined(TelemetryService.CLASS_NAME, nameof(value), value);\n\n\t\t// If we don't have a tenant context ID and the tenant component is multi-tenant,\n\t\t// we consider the entry as per-tenant and run it in all the tenant context to ensure\n\t\t// the value is recorded for each tenant.\n\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\tconst perTenant =\n\t\t\t!Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();\n\n\t\tif (perTenant) {\n\t\t\tlet valueId = \"\";\n\t\t\tawait this._platformComponent.execute(async () => {\n\t\t\t\tvalueId = await this._telemetryConnector.addMetricValue(id, value, customData);\n\t\t\t});\n\t\t\treturn valueId;\n\t\t}\n\n\t\treturn this._telemetryConnector.addMetricValue(id, value, customData);\n\t}\n\n\t/**\n\t * Remove metric.\n\t * @param id The id of the metric.\n\t * @returns A promise that resolves when the metric and all its values have been removed.\n\t */\n\tpublic async removeMetric(id: string): Promise<void> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\treturn this._telemetryConnector.removeMetric(id);\n\t}\n\n\t/**\n\t * Query the metrics.\n\t * @param type The type of the metric.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit Limit the number of entities to return.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t * @throws NotImplementedError if the implementation does not support retrieval.\n\t */\n\tpublic async query(\n\t\ttype?: MetricType,\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The metrics.\n\t\t */\n\t\tentities: ITelemetryMetric[];\n\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more values.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tconst boundQuery = this._telemetryConnector.query?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundQuery)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"query\");\n\t\t}\n\t\treturn boundQuery(type, cursor, limit);\n\t}\n\n\t/**\n\t * Query the metric values.\n\t * @param id The id of the metric.\n\t * @param timeStart The inclusive time as the start of the metric entries.\n\t * @param timeEnd The inclusive time as the end of the metric entries.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit Limit the number of entities to return.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t * @throws NotImplementedError if the implementation does not support retrieval.\n\t */\n\tpublic async queryValues(\n\t\tid: string,\n\t\ttimeStart?: number,\n\t\ttimeEnd?: number,\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The metric details.\n\t\t */\n\t\tmetric: ITelemetryMetric;\n\t\t/**\n\t\t * The values for the metric.\n\t\t */\n\t\tentities: ITelemetryMetricValue[];\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more values.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\n\t\tconst boundQueryValues = this._telemetryConnector.queryValues?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundQueryValues)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"queryValues\");\n\t\t}\n\t\treturn boundQueryValues(id, timeStart, timeEnd, cursor, limit);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"telemetryService.js","sourceRoot":"","sources":["../../src/telemetryService.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,EAAE,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAEnF,OAAO,EACN,yBAAyB,EAQzB,MAAM,4BAA4B,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,UAAU,sBAAsC;IAEvE;;;OAGG;IACc,mBAAmB,CAAsB;IAE1D;;;OAGG;IACc,kBAAkB,CAAqB;IAExD;;;OAGG;IACH,YAAY,OAA6C;QACxD,IAAI,CAAC,mBAAmB,GAAG,yBAAyB,CAAC,GAAG,CACvD,OAAO,EAAE,sBAAsB,IAAI,WAAW,CAC9C,CAAC;QACF,IAAI,CAAC,kBAAkB,GAAG,gBAAgB,CAAC,GAAG,CAC7C,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAC5C,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,SAAS;QACf,OAAO,gBAAgB,CAAC,UAAU,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,YAAY,CAAC,MAA6C;QACtE,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAEpE,iFAAiF;QACjF,qFAAqF;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IACI,KAAK,CAAC,KAAK,CACjB,IAAiB,EACjB,MAAe,EACf,KAAc;QAYd,MAAM,UAAU,GAAG,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAClF,IAAI,EAAE,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;YAC9B,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QACrE,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;;;OAUG;IACI,KAAK,CAAC,WAAW,CACvB,EAAU,EACV,SAAkB,EAClB,OAAgB,EAChB,MAAe,EACf,KAAc;QAed,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,UAAU,QAAc,EAAE,CAAC,CAAC;QAEhE,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,CAAC,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAC9F,IAAI,EAAE,CAAC,SAAS,CAAC,gBAAgB,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;QAC3E,CAAC;QACD,OAAO,gBAAgB,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAChE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IPlatformComponent } from \"@twin.org/api-models\";\nimport { ContextIdKeys, ContextIdStore } from \"@twin.org/context\";\nimport { ComponentFactory, Guards, Is, NotImplementedError } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tTelemetryConnectorFactory,\n\ttype ITelemetryComponent,\n\ttype ITelemetryConnector,\n\ttype ITelemetryMetric,\n\ttype ITelemetryMetricValueEntry,\n\ttype ITelemetryMetricValue,\n\ttype MetricCounterOperation,\n\ttype MetricType\n} from \"@twin.org/telemetry-models\";\nimport type { ITelemetryServiceConstructorOptions } from \"./models/ITelemetryServiceConstructorOptions.js\";\n\n/**\n * Service for performing telemetry operations to a connector.\n */\nexport class TelemetryService implements ITelemetryComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<TelemetryService>();\n\n\t/**\n\t * Telemetry connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _telemetryConnector: ITelemetryConnector;\n\n\t/**\n\t * Platform component.\n\t * @internal\n\t */\n\tprivate readonly _platformComponent: IPlatformComponent;\n\n\t/**\n\t * Create a new instance of TelemetryService.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: ITelemetryServiceConstructorOptions) {\n\t\tthis._telemetryConnector = TelemetryConnectorFactory.get(\n\t\t\toptions?.telemetryConnectorType ?? \"telemetry\"\n\t\t);\n\t\tthis._platformComponent = ComponentFactory.get<IPlatformComponent>(\n\t\t\toptions?.platformComponentType ?? \"platform\"\n\t\t);\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn TelemetryService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Create one or more metrics.\n\t * @param metric The metric details, or the details of several metrics.\n\t * @returns A promise that resolves when the metrics have been created.\n\t */\n\tpublic async createMetric(metric: ITelemetryMetric | ITelemetryMetric[]): Promise<void> {\n\t\tGuards.defined(TelemetryService.CLASS_NAME, nameof(metric), metric);\n\n\t\t// If we don't have a tenant context ID and the tenant component is multi-tenant,\n\t\t// we consider the entry as per-tenant and run it in all the tenant context to ensure\n\t\t// the metric is created for each tenant.\n\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\tconst perTenant =\n\t\t\t!Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();\n\n\t\tif (perTenant) {\n\t\t\tawait this._platformComponent.execute(async () =>\n\t\t\t\tthis._telemetryConnector.createMetric(metric)\n\t\t\t);\n\t\t} else {\n\t\t\tawait this._telemetryConnector.createMetric(metric);\n\t\t}\n\t}\n\n\t/**\n\t * Get the metric details and it's most recent value.\n\t * @param id The metric id.\n\t * @returns The metric details and it's most recent value.\n\t */\n\tpublic async getMetric(id: string): Promise<{\n\t\tmetric: ITelemetryMetric;\n\t\tvalue?: ITelemetryMetricValue;\n\t}> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\n\t\tconst boundGetMetric = this._telemetryConnector.getMetric?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundGetMetric)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"getMetric\");\n\t\t}\n\t\treturn boundGetMetric(id);\n\t}\n\n\t/**\n\t * Get a specific metric value by its id.\n\t * @param id The id of the metric.\n\t * @param valueId The id of the metric value.\n\t * @returns The metric value.\n\t */\n\tpublic async getMetricValue(id: string, valueId: string): Promise<ITelemetryMetricValue> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(valueId), valueId);\n\n\t\tconst boundGetMetricValue = this._telemetryConnector.getMetricValue?.bind(\n\t\t\tthis._telemetryConnector\n\t\t);\n\t\tif (Is.undefined(boundGetMetricValue)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"getMetricValue\");\n\t\t}\n\t\treturn boundGetMetricValue(id, valueId);\n\t}\n\n\t/**\n\t * Update metric.\n\t * @param metric The metric details.\n\t * @returns A promise that resolves when the metric has been updated.\n\t */\n\tpublic async updateMetric(metric: Omit<ITelemetryMetric, \"type\">): Promise<void> {\n\t\tGuards.object<ITelemetryMetric>(TelemetryService.CLASS_NAME, nameof(metric), metric);\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(metric.id), metric.id);\n\t\treturn this._telemetryConnector.updateMetric(metric);\n\t}\n\n\t/**\n\t * Add a metric value.\n\t * @param id The id of the metric.\n\t * @param value The value for the add operation.\n\t * @param customData The custom data for the add operation.\n\t * @returns The created metric value id. When fanned out per-tenant, this is the id from the last tenant written.\n\t */\n\tpublic async addMetricValue(\n\t\tid: string,\n\t\tvalue: MetricCounterOperation | number,\n\t\tcustomData?: { [key: string]: unknown }\n\t): Promise<string> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\tGuards.defined(TelemetryService.CLASS_NAME, nameof(value), value);\n\n\t\t// If we don't have a tenant context ID and the tenant component is multi-tenant,\n\t\t// we consider the entry as per-tenant and run it in all the tenant context to ensure\n\t\t// the value is recorded for each tenant.\n\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\tconst perTenant =\n\t\t\t!Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();\n\n\t\tif (perTenant) {\n\t\t\tlet valueId = \"\";\n\t\t\tawait this._platformComponent.execute(async () => {\n\t\t\t\tvalueId = await this._telemetryConnector.addMetricValue(id, value, customData);\n\t\t\t});\n\t\t\treturn valueId;\n\t\t}\n\n\t\treturn this._telemetryConnector.addMetricValue(id, value, customData);\n\t}\n\n\t/**\n\t * Add multiple metric values.\n\t * @param values The metric values to add.\n\t * @returns The created metric value ids. When fanned out per-tenant, these are the ids from the last tenant written.\n\t */\n\tpublic async addMetricValues(values: ITelemetryMetricValueEntry[]): Promise<string[]> {\n\t\tGuards.arrayValue<ITelemetryMetricValueEntry>(\n\t\t\tTelemetryService.CLASS_NAME,\n\t\t\tnameof(values),\n\t\t\tvalues\n\t\t);\n\n\t\t// If we don't have a tenant context ID and the tenant component is multi-tenant,\n\t\t// we consider the entries as per-tenant and run them in all the tenant context to ensure\n\t\t// the values are recorded for each tenant.\n\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\tconst perTenant =\n\t\t\t!Is.stringValue(contextIds[ContextIdKeys.Tenant]) && this._platformComponent.isMultiTenant();\n\n\t\tif (perTenant) {\n\t\t\tlet valueIds: string[] = [];\n\t\t\tawait this._platformComponent.execute(async () => {\n\t\t\t\tvalueIds = await this._telemetryConnector.addMetricValues(values);\n\t\t\t});\n\t\t\treturn valueIds;\n\t\t}\n\n\t\treturn this._telemetryConnector.addMetricValues(values);\n\t}\n\n\t/**\n\t * Remove metric.\n\t * @param id The id of the metric.\n\t * @returns A promise that resolves when the metric and all its values have been removed.\n\t */\n\tpublic async removeMetric(id: string): Promise<void> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\t\treturn this._telemetryConnector.removeMetric(id);\n\t}\n\n\t/**\n\t * Query the metrics.\n\t * @param type The type of the metric.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit Limit the number of entities to return.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t * @throws NotImplementedError if the implementation does not support retrieval.\n\t */\n\tpublic async query(\n\t\ttype?: MetricType,\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The metrics.\n\t\t */\n\t\tentities: ITelemetryMetric[];\n\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more values.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tconst boundQuery = this._telemetryConnector.query?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundQuery)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"query\");\n\t\t}\n\t\treturn boundQuery(type, cursor, limit);\n\t}\n\n\t/**\n\t * Query the metric values.\n\t * @param id The id of the metric.\n\t * @param timeStart The inclusive time as the start of the metric entries.\n\t * @param timeEnd The inclusive time as the end of the metric entries.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit Limit the number of entities to return.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t * @throws NotImplementedError if the implementation does not support retrieval.\n\t */\n\tpublic async queryValues(\n\t\tid: string,\n\t\ttimeStart?: number,\n\t\ttimeEnd?: number,\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The metric details.\n\t\t */\n\t\tmetric: ITelemetryMetric;\n\t\t/**\n\t\t * The values for the metric.\n\t\t */\n\t\tentities: ITelemetryMetricValue[];\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more values.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tGuards.stringValue(TelemetryService.CLASS_NAME, nameof(id), id);\n\n\t\tconst boundQueryValues = this._telemetryConnector.queryValues?.bind(this._telemetryConnector);\n\t\tif (Is.undefined(boundQueryValues)) {\n\t\t\tthrow new NotImplementedError(TelemetryService.CLASS_NAME, \"queryValues\");\n\t\t}\n\t\treturn boundQueryValues(id, timeStart, timeEnd, cursor, limit);\n\t}\n}\n"]}
|
|
@@ -12,6 +12,11 @@ export interface IMetricsCollectorServiceConstructorOptions {
|
|
|
12
12
|
* @default platform
|
|
13
13
|
*/
|
|
14
14
|
platformComponentType?: string;
|
|
15
|
+
/**
|
|
16
|
+
* The type of the telemetry component the collected values are recorded with.
|
|
17
|
+
* @default telemetry
|
|
18
|
+
*/
|
|
19
|
+
telemetryComponentType?: string;
|
|
15
20
|
/**
|
|
16
21
|
* The configuration options for the metrics collector service.
|
|
17
22
|
*/
|
|
@@ -12,7 +12,7 @@ export declare const tagsTelemetry: ITag[];
|
|
|
12
12
|
*/
|
|
13
13
|
export declare function generateRestRoutesTelemetry(baseRouteName: string, componentName: string): IRestRoute[];
|
|
14
14
|
/**
|
|
15
|
-
* Create
|
|
15
|
+
* Create one or more telemetry metrics.
|
|
16
16
|
* @param httpRequestContext The request context for the API.
|
|
17
17
|
* @param componentName The name of the component to use in the routes.
|
|
18
18
|
* @param request The request.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ITelemetryComponent, type ITelemetryMetric, type ITelemetryMetricValue, type MetricCounterOperation, type MetricType } from "@twin.org/telemetry-models";
|
|
1
|
+
import { type ITelemetryComponent, type ITelemetryMetric, type ITelemetryMetricValueEntry, type ITelemetryMetricValue, type MetricCounterOperation, type MetricType } from "@twin.org/telemetry-models";
|
|
2
2
|
import type { ITelemetryServiceConstructorOptions } from "./models/ITelemetryServiceConstructorOptions.js";
|
|
3
3
|
/**
|
|
4
4
|
* Service for performing telemetry operations to a connector.
|
|
@@ -19,11 +19,11 @@ export declare class TelemetryService implements ITelemetryComponent {
|
|
|
19
19
|
*/
|
|
20
20
|
className(): string;
|
|
21
21
|
/**
|
|
22
|
-
* Create
|
|
23
|
-
* @param metric The metric details.
|
|
24
|
-
* @returns A promise that resolves when the
|
|
22
|
+
* Create one or more metrics.
|
|
23
|
+
* @param metric The metric details, or the details of several metrics.
|
|
24
|
+
* @returns A promise that resolves when the metrics have been created.
|
|
25
25
|
*/
|
|
26
|
-
createMetric(metric: ITelemetryMetric): Promise<void>;
|
|
26
|
+
createMetric(metric: ITelemetryMetric | ITelemetryMetric[]): Promise<void>;
|
|
27
27
|
/**
|
|
28
28
|
* Get the metric details and it's most recent value.
|
|
29
29
|
* @param id The metric id.
|
|
@@ -56,6 +56,12 @@ export declare class TelemetryService implements ITelemetryComponent {
|
|
|
56
56
|
addMetricValue(id: string, value: MetricCounterOperation | number, customData?: {
|
|
57
57
|
[key: string]: unknown;
|
|
58
58
|
}): Promise<string>;
|
|
59
|
+
/**
|
|
60
|
+
* Add multiple metric values.
|
|
61
|
+
* @param values The metric values to add.
|
|
62
|
+
* @returns The created metric value ids. When fanned out per-tenant, these are the ids from the last tenant written.
|
|
63
|
+
*/
|
|
64
|
+
addMetricValues(values: ITelemetryMetricValueEntry[]): Promise<string[]>;
|
|
59
65
|
/**
|
|
60
66
|
* Remove metric.
|
|
61
67
|
* @param id The id of the metric.
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.9.3-next.5](https://github.com/iotaledger/twin-telemetry/compare/telemetry-service-v0.9.3-next.4...telemetry-service-v0.9.3-next.5) (2026-09-09)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **telemetry-service:** Synchronize repo versions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/telemetry-models bumped from 0.9.3-next.4 to 0.9.3-next.5
|
|
16
|
+
|
|
17
|
+
## [0.9.3-next.4](https://github.com/iotaledger/twin-telemetry/compare/telemetry-service-v0.9.3-next.3...telemetry-service-v0.9.3-next.4) (2026-09-08)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* metric values in background ([#118](https://github.com/iotaledger/twin-telemetry/issues/118)) ([4a13bc4](https://github.com/iotaledger/twin-telemetry/commit/4a13bc465847c64bd99ec19c2f6fbb10e303164e))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/telemetry-models bumped from 0.9.3-next.3 to 0.9.3-next.4
|
|
30
|
+
|
|
3
31
|
## [0.9.3-next.3](https://github.com/iotaledger/twin-telemetry/compare/telemetry-service-v0.9.3-next.2...telemetry-service-v0.9.3-next.3) (2026-09-07)
|
|
4
32
|
|
|
5
33
|
|
package/docs/open-api/spec.json
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"/telemetry/metric": {
|
|
25
25
|
"post": {
|
|
26
26
|
"operationId": "telemetryCreateMetric",
|
|
27
|
-
"summary": "Create
|
|
27
|
+
"summary": "Create one or more telemetry metrics",
|
|
28
28
|
"tags": [
|
|
29
29
|
"Telemetry"
|
|
30
30
|
],
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
}
|
|
35
35
|
],
|
|
36
36
|
"requestBody": {
|
|
37
|
-
"description": "Create
|
|
37
|
+
"description": "Create one or more telemetry metrics.",
|
|
38
38
|
"required": true,
|
|
39
39
|
"content": {
|
|
40
40
|
"application/json": {
|
|
41
41
|
"schema": {
|
|
42
|
-
"$ref": "#/components/schemas/
|
|
42
|
+
"$ref": "#/components/schemas/TelemetryCreateMetricRequest"
|
|
43
43
|
},
|
|
44
44
|
"examples": {
|
|
45
45
|
"telemetryCreateMetricRequestExample": {
|
|
@@ -48,8 +48,24 @@
|
|
|
48
48
|
"label": "My Counter",
|
|
49
49
|
"description": "This is my counter",
|
|
50
50
|
"type": 0,
|
|
51
|
-
"unit": "KG"
|
|
51
|
+
"unit": "KG",
|
|
52
|
+
"maxHistory": 1000
|
|
52
53
|
}
|
|
54
|
+
},
|
|
55
|
+
"telemetryCreateMetricsRequestExample": {
|
|
56
|
+
"value": [
|
|
57
|
+
{
|
|
58
|
+
"id": "my-counter",
|
|
59
|
+
"label": "My Counter",
|
|
60
|
+
"type": 0,
|
|
61
|
+
"maxHistory": 1000
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"id": "my-gauge",
|
|
65
|
+
"label": "My Gauge",
|
|
66
|
+
"type": 2
|
|
67
|
+
}
|
|
68
|
+
]
|
|
53
69
|
}
|
|
54
70
|
}
|
|
55
71
|
}
|
|
@@ -1048,6 +1064,20 @@
|
|
|
1048
1064
|
],
|
|
1049
1065
|
"description": "The data to be used in the update."
|
|
1050
1066
|
},
|
|
1067
|
+
"TelemetryCreateMetricRequest": {
|
|
1068
|
+
"anyOf": [
|
|
1069
|
+
{
|
|
1070
|
+
"$ref": "#/components/schemas/TelemetryMetric"
|
|
1071
|
+
},
|
|
1072
|
+
{
|
|
1073
|
+
"type": "array",
|
|
1074
|
+
"items": {
|
|
1075
|
+
"$ref": "#/components/schemas/TelemetryMetric"
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
],
|
|
1079
|
+
"description": "The data to be used in the create. A single metric fails if it already exists; an array\ndeclares the set that should exist, creating the ones that are missing."
|
|
1080
|
+
},
|
|
1051
1081
|
"TelemetryGetMetricResponse": {
|
|
1052
1082
|
"type": "object",
|
|
1053
1083
|
"properties": {
|
|
@@ -58,21 +58,21 @@ The class name of the component.
|
|
|
58
58
|
|
|
59
59
|
> **createMetric**(`metric`): `Promise`\<`void`\>
|
|
60
60
|
|
|
61
|
-
Create
|
|
61
|
+
Create one or more metrics.
|
|
62
62
|
|
|
63
63
|
#### Parameters
|
|
64
64
|
|
|
65
65
|
##### metric
|
|
66
66
|
|
|
67
|
-
`ITelemetryMetric`
|
|
67
|
+
`ITelemetryMetric` \| `ITelemetryMetric`[]
|
|
68
68
|
|
|
69
|
-
The metric details.
|
|
69
|
+
The metric details, or the details of several metrics.
|
|
70
70
|
|
|
71
71
|
#### Returns
|
|
72
72
|
|
|
73
73
|
`Promise`\<`void`\>
|
|
74
74
|
|
|
75
|
-
A promise that resolves when the
|
|
75
|
+
A promise that resolves when the metrics have been created.
|
|
76
76
|
|
|
77
77
|
#### Implementation of
|
|
78
78
|
|
|
@@ -200,6 +200,32 @@ The created metric value id. When fanned out per-tenant, this is the id from the
|
|
|
200
200
|
|
|
201
201
|
***
|
|
202
202
|
|
|
203
|
+
### addMetricValues() {#addmetricvalues}
|
|
204
|
+
|
|
205
|
+
> **addMetricValues**(`values`): `Promise`\<`string`[]\>
|
|
206
|
+
|
|
207
|
+
Add multiple metric values.
|
|
208
|
+
|
|
209
|
+
#### Parameters
|
|
210
|
+
|
|
211
|
+
##### values
|
|
212
|
+
|
|
213
|
+
`ITelemetryMetricValueEntry`[]
|
|
214
|
+
|
|
215
|
+
The metric values to add.
|
|
216
|
+
|
|
217
|
+
#### Returns
|
|
218
|
+
|
|
219
|
+
`Promise`\<`string`[]\>
|
|
220
|
+
|
|
221
|
+
The created metric value ids. When fanned out per-tenant, these are the ids from the last tenant written.
|
|
222
|
+
|
|
223
|
+
#### Implementation of
|
|
224
|
+
|
|
225
|
+
`ITelemetryComponent.addMetricValues`
|
|
226
|
+
|
|
227
|
+
***
|
|
228
|
+
|
|
203
229
|
### removeMetric() {#removemetric}
|
|
204
230
|
|
|
205
231
|
> **removeMetric**(`id`): `Promise`\<`void`\>
|
|
@@ -26,6 +26,20 @@ platform
|
|
|
26
26
|
|
|
27
27
|
***
|
|
28
28
|
|
|
29
|
+
### telemetryComponentType? {#telemetrycomponenttype}
|
|
30
|
+
|
|
31
|
+
> `optional` **telemetryComponentType?**: `string`
|
|
32
|
+
|
|
33
|
+
The type of the telemetry component the collected values are recorded with.
|
|
34
|
+
|
|
35
|
+
#### Default
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
telemetry
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
***
|
|
42
|
+
|
|
29
43
|
### config? {#config}
|
|
30
44
|
|
|
31
45
|
> `optional` **config?**: [`IMetricsCollectorServiceConfig`](IMetricsCollectorServiceConfig.md)
|
package/locales/en.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"error": {
|
|
3
3
|
"metricsCollectorService": {
|
|
4
|
-
"producerCollectionFailed": "Failed to collect metrics from producer \"{producer}\""
|
|
4
|
+
"producerCollectionFailed": "Failed to collect metrics from producer \"{producer}\"",
|
|
5
|
+
"metricRecordFailed": "Failed to record the collected metric values",
|
|
6
|
+
"collectionCycleFailed": "Failed to run the metrics collection cycle"
|
|
5
7
|
}
|
|
6
8
|
}
|
|
7
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/telemetry-service",
|
|
3
|
-
"version": "0.9.3-next.
|
|
3
|
+
"version": "0.9.3-next.5",
|
|
4
4
|
"description": "Telemetry service implementation with REST entry points.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@twin.org/entity": "next",
|
|
21
21
|
"@twin.org/logging-models": "next",
|
|
22
22
|
"@twin.org/nameof": "next",
|
|
23
|
-
"@twin.org/telemetry-models": "0.9.3-next.
|
|
23
|
+
"@twin.org/telemetry-models": "0.9.3-next.5",
|
|
24
24
|
"@twin.org/web": "next"
|
|
25
25
|
},
|
|
26
26
|
"main": "./dist/es/index.js",
|