import {z} from 'zod' export type GetApiInternalV1MetricsProjectIdExploreArgs = {project_id: number, time_start: string, time_end: string, search?: string | undefined, instrument?: Array | undefined, library_name?: Array | undefined, attr_key_exists?: Array | undefined}; export type GetApiInternalV1MetricsProjectIdExploreOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const exploredMetric = z.object({name: z.string(), instrument: z.enum(["histogram", "summary", "counter", "prom-counter", "gauge", "additive"]), unit: z.string().optional(), description: z.string().optional(), attrKeys: z.array(z.string()).optional(), libraryName: z.string().optional(), libraryVersion: z.string().optional(), numTimeseries: z.number().int().optional()}); export const exploreMetricsResponse = z.object({metrics: z.array(exploredMetric), hasMore: z.boolean(), search: z.array(z.record(z.string(), z.unknown())).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 }; /** * Explore metrics * * GET /internal/v1/metrics/{project_id}/explore * * Also accepts auth: authTokenQuery */ export const getApiInternalV1MetricsProjectIdExplore = async (args: GetApiInternalV1MetricsProjectIdExploreArgs, options?: GetApiInternalV1MetricsProjectIdExploreOptions) => { const {baseUrl = "https://api.uptrace.dev", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.time_start != null) query.append("time_start", String(args.time_start)) if (args.time_end != null) query.append("time_end", String(args.time_end)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.instrument ?? []) query.append("instrument", String(item)) for (const item of args.library_name ?? []) query.append("library_name", String(item)) for (const item of args.attr_key_exists ?? []) query.append("attr_key_exists", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/internal/v1/metrics/${encodeURIComponent(String(args.project_id))}/explore${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(exploreMetricsResponse, await res.json()) };