import {z} from 'zod' export type GetApiApiOrgIdTracesServiceGraphTopologyCurrentArgs = {org_id: string, start_time?: number | undefined, end_time?: number | undefined, stream_name?: string | undefined}; export type GetApiApiOrgIdTracesServiceGraphTopologyCurrentOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceEdge = z.object({baseline_p50_latency_ns: z.number().int().gte(0).nullable().optional(), baseline_p95_latency_ns: z.number().int().gte(0).nullable().optional(), baseline_p99_latency_ns: z.number().int().gte(0).nullable().optional(), connection_type: z.string().nullable().optional(), error_rate: z.number(), failed_requests: z.number().int().gte(0), from: z.string().nullable().optional(), p50_latency_ns: z.number().int().gte(0), p95_latency_ns: z.number().int().gte(0), p99_latency_ns: z.number().int().gte(0), to: z.string(), total_requests: z.number().int().gte(0)}); export const unresolvedCounts = z.object({ambiguous: z.number().int().gte(0), cardinality: z.number().int().gte(0), ip_only: z.number().int().gte(0), no_peer: z.number().int().gte(0)}); export const topologyMeta = z.object({degraded: z.array(z.string()).optional(), processed_up_to: z.number().int().nullable().optional(), source: z.string(), unresolved: unresolvedCounts}); export const serviceNode = z.object({error_rate: z.number(), errors: z.number().int().gte(0), id: z.string(), instances: z.number().int().gte(0).nullable().optional(), kind: z.string().optional(), label: z.string(), p50_server_ns: z.number().int().gte(0).nullable().optional(), p95_server_ns: z.number().int().gte(0).nullable().optional(), p99_server_ns: z.number().int().gte(0).nullable().optional(), requests: z.number().int().gte(0), service_type: z.string().nullable().optional(), stream_name: z.string().nullable().optional(), unattributed_inbound: z.number().int().nullable().optional()}); export const serviceGraphData = z.object({edges: z.array(serviceEdge), meta: topologyMeta.nullable().optional(), nodes: z.array(serviceNode)}); 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 current service graph topology * * GET /api/{org_id}/traces/service_graph/topology/current */ export const getApiApiOrgIdTracesServiceGraphTopologyCurrent = async (args: GetApiApiOrgIdTracesServiceGraphTopologyCurrentArgs, options: GetApiApiOrgIdTracesServiceGraphTopologyCurrentOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.stream_name != null) query.append("stream_name", String(args.stream_name)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/traces/service_graph/topology/current${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(serviceGraphData, await res.json()) };