-
Notifications
You must be signed in to change notification settings - Fork 87
feat: add observability abstractions and wire to runtime #2147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: refactor
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import type { CoreOptions } from "../types"; | ||
| import { ObservabilityClient, type LogRecord } from "./client"; | ||
| import type { LogSource, ObservabilitySourceResolverRegistry } from "./resolver"; | ||
| import type { LogSearchQuery, LogTailQuery, RawLogRecord, SourceReader } from "./sourceReader"; | ||
|
|
||
| const SOURCE: LogSource = { | ||
| provider: "cloudwatch", | ||
| logGroupName: "/aws/runtime-1", | ||
| }; | ||
| const OPTIONS = { region: "us-east-1" }; | ||
|
|
||
| async function collect(records: AsyncIterable<LogRecord>) { | ||
| const result: LogRecord[] = []; | ||
| for await (const record of records) result.push(record); | ||
| return result; | ||
| } | ||
|
|
||
| function createClient(rawRecords: RawLogRecord[]) { | ||
| const calls: { method: string; args: unknown[] }[] = []; | ||
| const resolvers: ObservabilitySourceResolverRegistry = { | ||
| runtime: { | ||
| resolve: async (...args) => { | ||
| calls.push({ method: "resolve", args }); | ||
| return { | ||
| resource: { | ||
| kind: "runtime", | ||
| id: args[0].id, | ||
| qualifier: args[0].qualifier ?? "DEFAULT", | ||
| }, | ||
| logs: [SOURCE], | ||
| }; | ||
| }, | ||
| }, | ||
| }; | ||
| const reader: SourceReader = { | ||
| async *searchLogs( | ||
| source: LogSource, | ||
| query: LogSearchQuery, | ||
| options: CoreOptions, | ||
| signal?: AbortSignal, | ||
| ) { | ||
| calls.push({ | ||
| method: "searchLogs", | ||
| args: [source, query, options, signal], | ||
| }); | ||
| yield* rawRecords; | ||
| }, | ||
| async *tailLogs( | ||
| source: LogSource, | ||
| query: LogTailQuery, | ||
| options: CoreOptions, | ||
| signal: AbortSignal, | ||
| ) { | ||
| calls.push({ | ||
| method: "tailLogs", | ||
| args: [source, query, options, signal], | ||
| }); | ||
| yield* rawRecords; | ||
| }, | ||
| }; | ||
| return { client: new ObservabilityClient(resolvers, reader), calls }; | ||
| } | ||
|
|
||
| describe("ObservabilityClient", () => { | ||
| test("resolves, reads, and normalizes common log metadata", async () => { | ||
| const raw = { | ||
| timestamp: 1_709_391_000_000, | ||
| ingestionTime: 1_709_391_000_100, | ||
| logStreamName: "runtime-stream", | ||
| message: JSON.stringify({ | ||
| traceId: "trace-1", | ||
| spanId: "span-1", | ||
| parentSpanId: "parent-1", | ||
| severityText: "INFO", | ||
| attributes: { "session.id": "session-1" }, | ||
| }), | ||
| raw: { eventId: "event-1" }, | ||
| }; | ||
| const { client, calls } = createClient([raw]); | ||
| const signal = new AbortController().signal; | ||
| const query = { startTimeMs: 1, endTimeMs: 2 }; | ||
|
|
||
| const records = await collect( | ||
| client.searchLogs( | ||
| { kind: "runtime", id: "runtime-1", qualifier: "blue" }, | ||
| query, | ||
| OPTIONS, | ||
| signal, | ||
| ), | ||
| ); | ||
|
|
||
| expect(calls.map((call) => call.method)).toEqual(["resolve", "searchLogs"]); | ||
| expect(records).toEqual([ | ||
| { | ||
| timestamp: new Date(1_709_391_000_000), | ||
| ingestionTime: new Date(1_709_391_000_100), | ||
| message: raw.message, | ||
| correlation: { | ||
| traceId: "trace-1", | ||
| spanId: "span-1", | ||
| parentSpanId: "parent-1", | ||
| sessionId: "session-1", | ||
| }, | ||
| severity: "INFO", | ||
| source: { | ||
| provider: "cloudwatch", | ||
| resource: { | ||
| kind: "runtime", | ||
| id: "runtime-1", | ||
| qualifier: "blue", | ||
| }, | ||
| logGroupName: SOURCE.logGroupName, | ||
| logStreamName: "runtime-stream", | ||
| }, | ||
| raw: { eventId: "event-1" }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("uses the same orchestration path for Live Tail records", async () => { | ||
| const { client, calls } = createClient([ | ||
| { | ||
| timestamp: 1, | ||
| message: "plain text", | ||
| raw: { message: "plain text" }, | ||
| }, | ||
| ]); | ||
| const signal = new AbortController().signal; | ||
|
|
||
| const records = await collect( | ||
| client.tailLogs( | ||
| { kind: "runtime", id: "runtime-1" }, | ||
| { filterPattern: "ERROR" }, | ||
| OPTIONS, | ||
| signal, | ||
| ), | ||
| ); | ||
|
|
||
| expect(calls.map((call) => call.method)).toEqual(["resolve", "tailLogs"]); | ||
| expect(records[0]).not.toHaveProperty("correlation"); | ||
| expect(records[0]).not.toHaveProperty("severity"); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import type { CoreOptions } from "../types"; | ||
| import type { | ||
| LogSource, | ||
| ObservableResourceRef, | ||
| ObservabilitySourceResolver, | ||
| ObservabilitySourceResolverRegistry, | ||
| ResolvedObservabilityTarget, | ||
| ResolvedResourceIdentity, | ||
| } from "./resolver"; | ||
| import type { LogSearchQuery, LogTailQuery, RawLogRecord, SourceReader } from "./sourceReader"; | ||
|
|
||
| export interface LogRecord { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is ONLY storing data, please declare it as a type.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this importable from a package somewhere? Is this what all log records look like in AC? |
||
| timestamp: Date; | ||
| message: string; | ||
| correlation?: { | ||
| traceId?: string; | ||
| spanId?: string; | ||
| parentSpanId?: string; | ||
| sessionId?: string; | ||
| }; | ||
| severity?: string; | ||
| ingestionTime?: Date; | ||
| source: { | ||
| provider: "cloudwatch"; | ||
| resource: ResolvedResourceIdentity; | ||
| logGroupName: string; | ||
| logStreamName?: string; | ||
| }; | ||
| raw?: unknown; | ||
| } | ||
|
|
||
| export interface CoreObservabilityClient { | ||
| searchLogs( | ||
| resource: ObservableResourceRef, | ||
| query: LogSearchQuery, | ||
| options: CoreOptions, | ||
| signal?: AbortSignal, | ||
| ): AsyncIterable<LogRecord>; | ||
|
|
||
| tailLogs( | ||
| resource: ObservableResourceRef, | ||
| query: LogTailQuery, | ||
| options: CoreOptions, | ||
| signal: AbortSignal, | ||
| ): AsyncIterable<LogRecord>; | ||
| } | ||
|
|
||
| /** | ||
| * Shared entry point for logs. It orchestrates resolution and provider reads, | ||
| * then normalizes provider events into the stable record contract. | ||
| */ | ||
| export class ObservabilityClient implements CoreObservabilityClient { | ||
| constructor( | ||
| private readonly resolvers: ObservabilitySourceResolverRegistry, | ||
| private readonly sourceReader: SourceReader, | ||
| ) {} | ||
|
|
||
| async *searchLogs( | ||
| resource: ObservableResourceRef, | ||
| query: LogSearchQuery, | ||
| options: CoreOptions, | ||
| signal?: AbortSignal, | ||
| ): AsyncGenerator<LogRecord, void> { | ||
| const target = await this.resolve(resource, options, signal); | ||
| for (const source of target.logs) { | ||
| for await (const raw of this.sourceReader.searchLogs(source, query, options, signal)) { | ||
| yield toLogRecord(target.resource, source, raw); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async *tailLogs( | ||
| resource: ObservableResourceRef, | ||
| query: LogTailQuery, | ||
| options: CoreOptions, | ||
| signal: AbortSignal, | ||
| ): AsyncGenerator<LogRecord, void> { | ||
| const target = await this.resolve(resource, options, signal); | ||
| for (const source of target.logs) { | ||
| for await (const raw of this.sourceReader.tailLogs(source, query, options, signal)) { | ||
| yield toLogRecord(target.resource, source, raw); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private resolve( | ||
| resource: ObservableResourceRef, | ||
| options: CoreOptions, | ||
| signal?: AbortSignal, | ||
| ): Promise<ResolvedObservabilityTarget> { | ||
| const resolver = this.resolvers[resource.kind] as ObservabilitySourceResolver<typeof resource>; | ||
| return resolver.resolve(resource, options, signal); | ||
| } | ||
| } | ||
|
|
||
| function toLogRecord( | ||
| resource: ResolvedResourceIdentity, | ||
| source: LogSource, | ||
| record: RawLogRecord, | ||
| ): LogRecord { | ||
| const metadata = extractCommonMetadata(record.message); | ||
| return { | ||
| timestamp: new Date(record.timestamp), | ||
| message: record.message, | ||
| ...metadata, | ||
| ...(record.ingestionTime !== undefined | ||
| ? { ingestionTime: new Date(record.ingestionTime) } | ||
| : {}), | ||
| source: { | ||
| provider: source.provider, | ||
| resource, | ||
| logGroupName: source.logGroupName, | ||
| ...(record.logStreamName ? { logStreamName: record.logStreamName } : {}), | ||
| }, | ||
| raw: record.raw, | ||
| }; | ||
| } | ||
|
|
||
| function extractCommonMetadata(message: string): Pick<LogRecord, "correlation" | "severity"> { | ||
| let parsed: Record<string, unknown>; | ||
| try { | ||
| parsed = JSON.parse(message) as Record<string, unknown>; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {}; | ||
|
|
||
| const attributes = | ||
| parsed.attributes && typeof parsed.attributes === "object" && !Array.isArray(parsed.attributes) | ||
| ? (parsed.attributes as Record<string, unknown>) | ||
| : {}; | ||
| const stringValue = (value: unknown): string | undefined => | ||
| typeof value === "string" ? value : undefined; | ||
| const correlation = { | ||
| traceId: stringValue(parsed.traceId), | ||
| spanId: stringValue(parsed.spanId), | ||
| parentSpanId: stringValue(parsed.parentSpanId), | ||
| sessionId: stringValue(parsed.sessionId) ?? stringValue(attributes["session.id"]), | ||
| }; | ||
| const hasCorrelation = Object.values(correlation).some((value) => value !== undefined); | ||
| const severity = | ||
| stringValue(parsed.severityText) ?? stringValue(parsed.severity) ?? stringValue(parsed.level); | ||
|
|
||
| return { | ||
| ...(hasCorrelation ? { correlation } : {}), | ||
| ...(severity ? { severity } : {}), | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export { ObservabilityClient, type CoreObservabilityClient, type LogRecord } from "./client"; | ||
| export { | ||
| DEFAULT_RUNTIME_QUALIFIER, | ||
| RuntimeSourceResolver, | ||
| runtimeLogGroup, | ||
| type LogSource, | ||
| type ObservableResourceRef, | ||
| type ObservabilitySourceResolver, | ||
| type ObservabilitySourceResolverRegistry, | ||
| type ResolvedObservabilityTarget, | ||
| type ResolvedResourceIdentity, | ||
| } from "./resolver"; | ||
| export { | ||
| CloudWatchSourceReader, | ||
| type LogSearchQuery, | ||
| type LogTailQuery, | ||
| type RawLogRecord, | ||
| type SourceReader, | ||
| } from "./sourceReader"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use Golden tests here. Runtime logs handler tests should cover this.