import {z} from 'zod' export type GetApiV1TelemetryCountersMetricNameTotalArgs = {metric_name: string, time_range?: ('15m' | '30m' | '1h' | '2h' | '6h' | '1d' | '7d' | '30d') | undefined, evaluation_id?: (string | null) | undefined, test_id?: (string | null) | undefined, probe_id?: (string | null) | undefined, agent_configuration_id?: (string | null) | undefined}; export type GetApiV1TelemetryCountersMetricNameTotalOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const metricLabels = z.object({agent_id: z.string().nullable().optional(), deployment_environment: z.string().nullable().optional(), exported_job: z.string().nullable().optional(), job: z.string().nullable().optional(), service_name: z.string().nullable().optional(), service_namespace: z.string().nullable().optional(), service_version: z.string().nullable().optional(), team_id: z.string().nullable().optional(), user_id: z.string().nullable().optional(), evaluation_id: z.string().nullable().optional()}); export const counterTotalResponse = z.object({metric: z.string(), total_count: z.number(), labels: metricLabels.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Counter Total * * GET /v1/telemetry/counters/{metric_name}/total */ export const getApiV1TelemetryCountersMetricNameTotal = async (args: GetApiV1TelemetryCountersMetricNameTotalArgs, options: GetApiV1TelemetryCountersMetricNameTotalOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.time_range != null) query.append("time_range", String(args.time_range)) if (args.evaluation_id != null) query.append("evaluation_id", String(args.evaluation_id)) if (args.test_id != null) query.append("test_id", String(args.test_id)) if (args.probe_id != null) query.append("probe_id", String(args.probe_id)) if (args.agent_configuration_id != null) query.append("agent_configuration_id", String(args.agent_configuration_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/telemetry/counters/${encodeURIComponent(String(args.metric_name))}/total${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(counterTotalResponse, await res.json()) };