import {z} from 'zod' export type OTLPAttributeValue = {arrayValue?: ({[key: string]: unknown} | null) | undefined, boolValue?: (boolean | null) | undefined, bytesValue?: (string | null) | undefined, doubleValue?: (number | null) | undefined, intValue?: (string | null) | undefined, stringValue?: (string | null) | undefined}; export type OTLPAttribute = {key: string, value: OTLPAttributeValue}; export type OTLPResource = {attributes?: Array | undefined}; export type OTLPBodyValue = {arrayValue?: ({[key: string]: unknown} | null) | undefined, boolValue?: (boolean | null) | undefined, bytesValue?: (string | null) | undefined, doubleValue?: (number | null) | undefined, intValue?: (string | null) | undefined, stringValue?: (string | null) | undefined}; export type OTLPLogRecord = {attributes?: Array | undefined, body?: (OTLPBodyValue | null) | undefined, severityNumber?: (number | null) | undefined, severityText?: (string | null) | undefined, spanId?: (string | null) | undefined, timeUnixNano?: string | undefined, traceId?: (string | null) | undefined}; export type OTLPScope = {name?: string | undefined, version?: string | undefined}; export type OTLPScopeLogs = {logRecords?: Array | undefined, scope?: (OTLPScope | null) | undefined}; export type OTLPResourceLogs = {resource?: (OTLPResource | null) | undefined, scopeLogs?: Array | undefined}; export type OTLPLogIngestionRequest = {resourceLogs: Array}; export type CreateApiLogsOtlpArgs = {body: OTLPLogIngestionRequest}; export type CreateApiLogsOtlpOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const oTLPLogIngestionResponse = z.object({logs_count: z.number().int(), status: z.string().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 Agent Logs (OTLP Format) * * POST /logs/otlp */ export const createApiLogsOtlp = async (args: CreateApiLogsOtlpArgs, options: CreateApiLogsOtlpOptions) => { const {baseUrl, 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(/\/+$/, '')}/logs/otlp${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(oTLPLogIngestionResponse, await res.json()) };