import {z} from 'zod' export type GetApiV2OrganizationsOrganizationTelemetrySourcesSourceMetricsArgs = {organization: string, source: string}; export type GetApiV2OrganizationsOrganizationTelemetrySourcesSourceMetricsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v2Measure = z.object({name: z.string().optional(), unit: z.string().optional()}); export const v2MetricTypeType = z.enum(["INVALID_METRIC_TYPE", "GAUGE", "COUNTER", "AVERAGE", "PERCENT", "APDEX", "HEATMAPS", "LABELED_COUNTER", "PERCENTILE", "CPM", "MAX"]); export const v2MetricTypeLabel = z.object({key: z.string().optional(), value: z.string().optional()}); export const v2MetricType = z.object({name: v2MetricTypeType.optional(), labels: z.array(v2MetricTypeLabel).optional()}); export const v2MetricOrigin = z.enum(["INVALID_METRIC_ORIGIN", "MESH_CONTROLLED", "AGENT_OBSERVED", "MESH_IMPORTED", "EXTERNAL_IMPORTED"]); export const v2MetricDetectionPoint = z.enum(["INVALID_METRIC_DETECTION_POINT", "IN_SERVICE", "CLIENT_SIDE", "SERVER_SIDE"]); export const v2Metric = z.object({fqn: z.string().optional(), displayName: z.string().optional(), etag: z.string().optional(), description: z.string().optional(), observedResource: z.string().optional(), measure: v2Measure.optional(), type: v2MetricType.optional(), origin: v2MetricOrigin.optional(), detectionPoint: v2MetricDetectionPoint.optional()}); export const v2ListMetricsResponse = z.object({metrics: z.array(v2Metric).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 }; /** * List the telemetry metrics that are available for the requested telemetry source. * * GET /v2/organizations/{organization}/telemetry/sources/{source}/metrics */ export const getApiV2OrganizationsOrganizationTelemetrySourcesSourceMetrics = async (args: GetApiV2OrganizationsOrganizationTelemetrySourcesSourceMetricsArgs, options: GetApiV2OrganizationsOrganizationTelemetrySourcesSourceMetricsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization))}/telemetry/sources/${encodeURIComponent(String(args.source))}/metrics${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(v2ListMetricsResponse, await res.json()) };