import {z} from 'zod' export type OtlpKeyValue = {key: string, value: {stringValue?: string | undefined, intValue?: string | undefined, doubleValue?: number | undefined, boolValue?: boolean | undefined, arrayValue?: ({values?: Array> | undefined}) | undefined}}; export type OtlpSpan = {traceId: string, spanId: string, parentSpanId?: string | undefined, name: string, kind?: (0 | 1 | 2 | 3 | 4 | 5) | undefined, startTimeUnixNano: string, endTimeUnixNano?: string | undefined, attributes?: Array | undefined, status?: ({code?: (0 | 1 | 2) | undefined, message?: string | undefined}) | undefined, events?: (Array<{timeUnixNano?: string | undefined, name?: string | undefined, attributes?: Array | undefined}>) | undefined, links?: (Array<{traceId?: string | undefined, spanId?: string | undefined, attributes?: Array | undefined}>) | undefined}; export type OtlpExportTraceServiceRequest = {resourceSpans?: (Array<{resource?: ({attributes?: Array | undefined}) | undefined, scopeSpans?: (Array<{scope?: ({name?: string | undefined, version?: string | undefined}) | undefined, spans?: Array | undefined}>) | undefined}>) | undefined}; export type CreateApiV1TracesArgs = {'Content-Type': 'application/x-protobuf' | 'application/json', 'Content-Encoding'?: 'gzip' | undefined, close_after?: boolean | undefined, body: OtlpExportTraceServiceRequest}; export type CreateApiV1TracesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const otlpExportTraceServiceResponse = z.object({partialSuccess: z.object({rejectedSpans: z.number().int().optional(), errorMessage: z.string().optional()}).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 }; /** * Ingest Traces (OTLP) * * POST /v1/traces */ export const createApiV1Traces = async (args: CreateApiV1TracesArgs, options: CreateApiV1TracesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args['Content-Encoding'] != null) headers.set("Content-Encoding", String(args['Content-Encoding'])) if (args.close_after != null) query.append("close_after", String(args.close_after)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/traces${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(otlpExportTraceServiceResponse, await res.json()) };