Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/data-pump/data-pump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export interface FlowcoreDataPumpOptions {
url: string
intervalMs?: number
pathwayId: string
/** Log level for successful pulses. Defaults to 'debug'. */
successLogLevel?: "debug" | "info" | "warn" | "error"
/** Log level for pulse failures. Defaults to 'warn'. */
failureLogLevel?: "debug" | "info" | "warn" | "error"
}
}

Expand Down Expand Up @@ -179,6 +183,8 @@ export class FlowcoreDataPump {
intervalMs: options.pulse.intervalMs,
auth: options.auth,
logger: options.logger,
successLogLevel: options.pulse.successLogLevel,
failureLogLevel: options.pulse.failureLogLevel,
},
() => {
const snapshot = pump.getSnapshot()
Expand Down
29 changes: 28 additions & 1 deletion src/data-pump/pulse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import { SendPumpPulseCommand } from "@flowcore/sdk"
import type { FlowcoreDataPumpAuth, FlowcoreLogger } from "./types.ts"
import { getFlowcoreClient } from "./flowcore-client.ts"

/**
* Log level for pulse emitter events. Corresponds to FlowcoreLogger methods.
*/
export type PulseLogLevel = "debug" | "info" | "warn" | "error"

export interface PulseEmitterOptions {
url: string
intervalMs?: number
auth: FlowcoreDataPumpAuth
logger?: FlowcoreLogger
/** Log level for successful pulses. Defaults to 'debug'. */
successLogLevel?: PulseLogLevel
/** Log level for pulse failures. Defaults to 'warn'. */
failureLogLevel?: PulseLogLevel
}

export interface PulseSnapshot {
Expand All @@ -29,13 +38,17 @@ export class PulseEmitter {
private startTimeout: ReturnType<typeof setTimeout> | null = null
private readonly intervalMs: number
private readonly logger?: FlowcoreLogger
private readonly successLogLevel: PulseLogLevel
private readonly failureLogLevel: PulseLogLevel

constructor(
private readonly options: PulseEmitterOptions,
private readonly getSnapshot: () => PulseSnapshot | null,
) {
this.intervalMs = options.intervalMs ?? 30_000
this.logger = options.logger
this.successLogLevel = options.successLogLevel ?? "debug"
this.failureLogLevel = options.failureLogLevel ?? "warn"
}

start(): void {
Expand Down Expand Up @@ -63,8 +76,14 @@ export class PulseEmitter {

private emitSafe(): void {
this.emit().catch((err) => {
const snapshot = this.getSnapshot()
const msg = err instanceof Error ? err.message : String(err)
this.logger?.warn?.(`Pulse emission failed: ${msg}`)
this.logger?.[this.failureLogLevel]?.("Pulse emission failed", {
error: msg,
url: this.options.url,
pathwayId: snapshot?.pathwayId,
flowType: snapshot?.flowType,
})
})
}

Expand Down Expand Up @@ -94,5 +113,13 @@ export class PulseEmitter {
uptimeMs: snapshot.uptimeMs,
}),
)

this.logger?.[this.successLogLevel]?.("Pulse sent", {
pathwayId: snapshot.pathwayId,
flowType: snapshot.flowType,
timeBucket: snapshot.timeBucket,
isLive: snapshot.isLive,
bufferDepth: snapshot.bufferDepth,
})
}
}
Loading