import {z} from 'zod' export type GetApiV1TelemetryLogsArgs = {time_range?: ('15m' | '30m' | '1h' | '2h' | '6h' | '1d' | '7d' | '30d') | undefined, service_name?: (string | null) | undefined, level?: (string | null) | undefined, job?: (string | null) | undefined, exporter?: (string | null) | undefined, agent_configuration_id?: (string | null) | undefined, evaluation_id?: (string | null) | undefined, test_id?: (string | null) | undefined, probe_id?: (string | null) | undefined, search?: (string | null) | undefined, logql?: (string | null) | undefined, limit?: number | undefined, direction?: ('forward' | 'backward') | undefined}; export type GetApiV1TelemetryLogsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const logEntryResponse = z.object({timestamp: z.number().int(), line: z.string()}); export const logStreamResponse = z.object({labels: z.record(z.string(), z.string()).optional(), entries: z.array(logEntryResponse).optional()}); export const logsQueryResponse = z.object({query: z.string(), direction: z.enum(["forward", "backward"]), start_ns: z.number().int(), end_ns: z.number().int(), streams: z.array(logStreamResponse).optional(), next_timestamp: z.number().int().nullable().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 Logs * * GET /v1/telemetry/logs */ export const getApiV1TelemetryLogs = async (args: GetApiV1TelemetryLogsArgs, options: GetApiV1TelemetryLogsOptions) => { 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.service_name != null) query.append("service_name", String(args.service_name)) if (args.level != null) query.append("level", String(args.level)) if (args.job != null) query.append("job", String(args.job)) if (args.exporter != null) query.append("exporter", String(args.exporter)) if (args.agent_configuration_id != null) query.append("agent_configuration_id", String(args.agent_configuration_id)) 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.search != null) query.append("search", String(args.search)) if (args.logql != null) query.append("logql", String(args.logql)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.direction != null) query.append("direction", String(args.direction)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/telemetry/logs${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(logsQueryResponse, await res.json()) };