|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'; |
| 18 | +import * as z from 'zod'; |
| 19 | + |
| 20 | +import { InstrumentationLibrarySchema } from './trace'; |
| 21 | + |
| 22 | +extendZodWithOpenApi(z); |
| 23 | + |
| 24 | +/** |
| 25 | + * Zod schema for log record data. |
| 26 | + */ |
| 27 | +export const LogRecordSchema = z |
| 28 | + .object({ |
| 29 | + logId: z.string(), // Server-generated if omitted |
| 30 | + traceId: z.string().optional(), |
| 31 | + spanId: z.string().optional(), |
| 32 | + timestamp: z |
| 33 | + .number() |
| 34 | + .describe('log recorded time in milliseconds since the epoch'), |
| 35 | + severityNumber: z.number().optional(), |
| 36 | + severityText: z.string().optional(), |
| 37 | + body: z.unknown().optional(), // Represents any value: string, number, boolean, array, or object |
| 38 | + attributes: z.record(z.string(), z.unknown()).optional(), |
| 39 | + instrumentationLibrary: InstrumentationLibrarySchema.optional(), |
| 40 | + }) |
| 41 | + .openapi('LogRecordData'); |
| 42 | + |
| 43 | +export type LogRecordData = z.infer<typeof LogRecordSchema>; |
| 44 | + |
| 45 | +/** |
| 46 | + * Log query parameters. |
| 47 | + */ |
| 48 | +export const LogQuerySchema = z.object({ |
| 49 | + limit: z.coerce.number().optional(), |
| 50 | + continuationToken: z.string().optional(), |
| 51 | + traceId: z.string().optional(), |
| 52 | + spanId: z.string().optional(), |
| 53 | +}); |
| 54 | + |
| 55 | +export type LogQuery = z.infer<typeof LogQuerySchema>; |
| 56 | + |
| 57 | +export interface LogQueryResponse { |
| 58 | + logs: LogRecordData[]; |
| 59 | + continuationToken?: string; |
| 60 | +} |
| 61 | + |
| 62 | +export interface LogStore { |
| 63 | + init(): Promise<void>; |
| 64 | + save(logs: LogRecordData[]): Promise<void>; |
| 65 | + list(query?: LogQuery): Promise<LogQueryResponse>; |
| 66 | +} |
0 commit comments