mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Add typing for serialized collector type
Setup TypeScript tyings and JSON schema for serializeResult and deserializeResult and use these in the metrics related requests.
This commit is contained in:
1 parent
b66138587b
commit
b78ca31b09
7 files changed
+46
-26
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import { jsonArray, StringEnum } from "./composites";
|
||||
import { CollectorResultSerialized } from "../prometheus";
|
||||
|
||||
export class HostDetails {
|
||||
constructor(
|
||||
@@ -75,11 +76,11 @@ export class HostMetricsRequest {
|
||||
static dst = "host" as const;
|
||||
static Response = class Response { // TODO: Use JSON class pattern in Prometheus
|
||||
constructor(
|
||||
public results: object[],
|
||||
public results: CollectorResultSerialized[],
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"results": Type.Array(Type.Object({})),
|
||||
"results": Type.Array(CollectorResultSerialized),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { JsonString, StringEnum, jsonArray } from "./composites";
|
||||
import { RawConfig } from "../config";
|
||||
import type { User } from "../users";
|
||||
import type { MessageRequest } from "./messages_core";
|
||||
import type { SerializedConfig } from "../config";
|
||||
import { CollectorResultSerialized } from "../prometheus";
|
||||
|
||||
|
||||
export type InstanceStatus =
|
||||
@@ -216,11 +216,11 @@ export class InstanceMetricsRequest {
|
||||
|
||||
static Response = class Response { // TODO: Use JSON class pattern in Prometheus
|
||||
constructor(
|
||||
public results: object[],
|
||||
public results: CollectorResultSerialized[],
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"results": Type.Array(Type.Object({})),
|
||||
"results": Type.Array(CollectorResultSerialized),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
* app.listen(9100);
|
||||
* @module lib/prometheus
|
||||
*/
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import { StringEnum } from "./data/composites";
|
||||
|
||||
/**
|
||||
* Result from collecting a {@link Collector}
|
||||
@@ -1611,7 +1613,9 @@ function escapeHelp(help: string) {
|
||||
;
|
||||
}
|
||||
|
||||
async function* expositionLines(resultsIterator: AsyncIterable<CollectorResult>) {
|
||||
async function* expositionLines(
|
||||
resultsIterator: AsyncIterable<CollectorResult> | Iterable<CollectorResult>
|
||||
) {
|
||||
let first = true;
|
||||
for await (let result of resultsIterator) {
|
||||
if (first) {
|
||||
@@ -1665,7 +1669,9 @@ async function* expositionLines(resultsIterator: AsyncIterable<CollectorResult>)
|
||||
* defaultRegistry}.
|
||||
* @returns Prometheus exposition.
|
||||
*/
|
||||
export async function exposition(resultsIterator = defaultRegistry.collect()) {
|
||||
export async function exposition(
|
||||
resultsIterator: AsyncIterable<CollectorResult> | Iterable<CollectorResult> = defaultRegistry.collect()
|
||||
) {
|
||||
let lines = "";
|
||||
for await (let line of expositionLines(resultsIterator)) {
|
||||
lines += line;
|
||||
@@ -1678,6 +1684,24 @@ export async function exposition(resultsIterator = defaultRegistry.collect()) {
|
||||
*/
|
||||
export const expositionContentType = "text/plain; version=0.0.4";
|
||||
|
||||
export const CollectorResultSerialized = Type.Object({
|
||||
metric: Type.Object({
|
||||
type: StringEnum(["counter", "gauge", "histogram", "summary", "untyped"]),
|
||||
name: Type.String(),
|
||||
help: Type.String(),
|
||||
labels: Type.Array(Type.String()),
|
||||
}),
|
||||
samples: Type.Array(
|
||||
Type.Tuple([
|
||||
Type.String(),
|
||||
Type.Array(
|
||||
Type.Tuple([Type.String(), Type.Number()])
|
||||
),
|
||||
]),
|
||||
),
|
||||
});
|
||||
export type CollectorResultSerialized = Static<typeof CollectorResultSerialized>;
|
||||
|
||||
/**
|
||||
* Serialize CollectorResult into a plain object
|
||||
*
|
||||
@@ -1702,7 +1726,7 @@ export function serializeResult(
|
||||
metricName?: string,
|
||||
metricHelp?: string,
|
||||
} = {}
|
||||
) {
|
||||
): CollectorResultSerialized {
|
||||
let {
|
||||
addLabels,
|
||||
metricName = result.metric.name,
|
||||
@@ -1756,7 +1780,7 @@ export function serializeResult(
|
||||
* @returns deserialized result.
|
||||
*/
|
||||
export function deserializeResult(
|
||||
serializedResult: ReturnType<typeof serializeResult>
|
||||
serializedResult: CollectorResultSerialized
|
||||
): CollectorResult {
|
||||
return {
|
||||
metric: new Metric(
|
||||
|
||||
Reference in new issue
Block a user