import {z} from 'zod' export type SharedFilterCondition = {field?: string | undefined, operator?: ('equals' | 'not_equals' | 'contains' | 'not_contains' | 'in' | 'not_in') | undefined, value?: string | undefined, values?: Array | undefined}; export type TypesLogicalOperator = 'AND' | 'OR'; export type RequestGetMetricsRequest = {from_timestamp_sec?: number | undefined, to_timestamp_sec?: number | undefined, cursor?: number | undefined, environments?: Array | undefined, filters?: Array | undefined, limit?: number | undefined, logical_operator?: TypesLogicalOperator | undefined, metric_name?: string | undefined, metric_source?: string | undefined, sort_by?: string | undefined, sort_order?: string | undefined}; export type CreateApiV2MetricsArgs = {body: RequestGetMetricsRequest}; export type CreateApiV2MetricsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseMetricPoint = z.object({bool_value: z.boolean().optional(), enum_value: z.string().optional(), numeric_value: z.number().optional(), event_time: z.number().int().optional(), labels: z.record(z.string(), z.string()).optional()}); export const responseMetricGroup = z.object({metric_name: z.string().optional(), organization_id: z.string().optional(), points: z.array(responseMetricPoint).optional()}); export const responsePaginatedMetricsResponse = z.object({data: z.array(responseMetricGroup).optional(), next_cursor: z.string().optional(), total_points: z.number().int().optional(), total_results: z.number().int().optional()}); export const responseGetMetricsResponse = z.object({metrics: responsePaginatedMetricsResponse.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 metrics with filtering and grouping * * POST /v2/metrics */ export const createApiV2Metrics = async (args: CreateApiV2MetricsArgs, options?: CreateApiV2MetricsOptions) => { const {baseUrl = "https://api.traceloop.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/metrics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(responseGetMetricsResponse, await res.json()) };