|
| 1 | +/** |
| 2 | + * Winston transport that sends logs to Fireworks tracing gateway. |
| 3 | + */ |
| 4 | + |
| 5 | +import Transport from 'winston-transport'; |
| 6 | +import type { TransformableInfo } from 'logform'; |
| 7 | +const LEVEL = Symbol.for('level'); |
| 8 | + |
| 9 | +interface FireworksLogInfo extends TransformableInfo { |
| 10 | + rollout_id?: string; |
| 11 | + experiment_id?: string; |
| 12 | + run_id?: string; |
| 13 | + rollout_ids?: string[]; |
| 14 | + status?: any; |
| 15 | + program?: string; |
| 16 | + logger_name?: string; |
| 17 | + [key: string]: any; |
| 18 | +} |
| 19 | + |
| 20 | +interface StatusInfo { |
| 21 | + code?: number; |
| 22 | + message?: string; |
| 23 | + details?: any[]; |
| 24 | +} |
| 25 | + |
| 26 | +interface FireworksPayload { |
| 27 | + program: string; |
| 28 | + status?: StatusInfo | null; |
| 29 | + message: string; |
| 30 | + tags: string[]; |
| 31 | + extras: { |
| 32 | + logger_name: string; |
| 33 | + level: string; |
| 34 | + timestamp: string; |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +export class FireworksTransport extends Transport { |
| 39 | + private gatewayBaseUrl: string; |
| 40 | + private rolloutIdEnv: string; |
| 41 | + private apiKey?: string; |
| 42 | + private waitUntil?: (promise: Promise<any>) => void; |
| 43 | + |
| 44 | + constructor(opts: { |
| 45 | + gatewayBaseUrl?: string; |
| 46 | + rolloutIdEnv?: string; |
| 47 | + waitUntil?: (promise: Promise<any>) => void; |
| 48 | + } = {}) { |
| 49 | + super(); |
| 50 | + |
| 51 | + this.gatewayBaseUrl = |
| 52 | + opts.gatewayBaseUrl || |
| 53 | + process.env.FW_TRACING_GATEWAY_BASE_URL || |
| 54 | + 'https://tracing.fireworks.ai'; |
| 55 | + |
| 56 | + this.rolloutIdEnv = opts.rolloutIdEnv || 'EP_ROLLOUT_ID'; |
| 57 | + this.apiKey = process.env.FIREWORKS_API_KEY; |
| 58 | + this.waitUntil = opts.waitUntil; |
| 59 | + } |
| 60 | + |
| 61 | + log(info: FireworksLogInfo, callback: () => void) { |
| 62 | + setImmediate(() => { |
| 63 | + this.emit('logged', info); |
| 64 | + }); |
| 65 | + |
| 66 | + const sendPromise = this.sendToFireworks(info).catch((error) => { |
| 67 | + this.emit('error', error); |
| 68 | + }); |
| 69 | + |
| 70 | + // Use waitUntil for ALL logs when available so Fireworks logging |
| 71 | + // can complete even after the HTTP response is sent. |
| 72 | + if (this.waitUntil) { |
| 73 | + this.waitUntil(sendPromise); |
| 74 | + } |
| 75 | + |
| 76 | + callback(); |
| 77 | + } |
| 78 | + |
| 79 | + private async sendToFireworks(info: FireworksLogInfo): Promise<void> { |
| 80 | + if (!this.gatewayBaseUrl) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const rolloutId = this.getRolloutId(info); |
| 85 | + if (!rolloutId) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const payload = this.buildPayload(info, rolloutId); |
| 90 | + const baseUrl = this.gatewayBaseUrl.replace(/\/$/, ''); |
| 91 | + const url = `${baseUrl}/logs`; |
| 92 | + |
| 93 | + // Debug logging |
| 94 | + if (process.env.EP_DEBUG === 'true') { |
| 95 | + const tagsLen = Array.isArray(payload.tags) ? payload.tags.length : 0; |
| 96 | + const msgPreview = typeof payload.message === 'string' |
| 97 | + ? payload.message.substring(0, 80) |
| 98 | + : payload.message; |
| 99 | + const payloadSize = JSON.stringify(payload).length; |
| 100 | + const hasStatus = !!payload.status; |
| 101 | + console.log(`[FW_LOG] POST ${url} rollout_id=${rolloutId} tags=${tagsLen} msg=${msgPreview} size=${payloadSize} hasStatus=${hasStatus}`); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + const headers: HeadersInit = { |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + 'User-Agent': 'winston-fireworks-transport/1.0.0', |
| 108 | + }; |
| 109 | + |
| 110 | + if (this.apiKey) { |
| 111 | + headers['Authorization'] = `Bearer ${this.apiKey}`; |
| 112 | + } |
| 113 | + |
| 114 | + const response = await fetch(url, { |
| 115 | + method: 'POST', |
| 116 | + headers, |
| 117 | + body: JSON.stringify(payload), |
| 118 | + // No timeout signal for compatibility |
| 119 | + }); |
| 120 | + |
| 121 | + if (process.env.EP_DEBUG === 'true') { |
| 122 | + console.log(`[FW_LOG] resp=${response.status} for rollout_id=${rolloutId}`); |
| 123 | + } |
| 124 | + |
| 125 | + // Fallback to /v1/logs if /logs is not found |
| 126 | + if (response.status === 404) { |
| 127 | + const altUrl = `${baseUrl}/v1/logs`; |
| 128 | + |
| 129 | + if (process.env.EP_DEBUG === 'true') { |
| 130 | + const tagsLen = Array.isArray(payload.tags) ? payload.tags.length : 0; |
| 131 | + console.log(`[FW_LOG] RETRY POST ${altUrl} rollout_id=${rolloutId} tags=${tagsLen}`); |
| 132 | + } |
| 133 | + |
| 134 | + const retryResponse = await fetch(altUrl, { |
| 135 | + method: 'POST', |
| 136 | + headers, |
| 137 | + body: JSON.stringify(payload), |
| 138 | + // No timeout signal for compatibility |
| 139 | + }); |
| 140 | + |
| 141 | + if (process.env.EP_DEBUG === 'true') { |
| 142 | + console.log(`[FW_LOG] retry resp=${retryResponse.status}`); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + } catch (error: any) { |
| 147 | + // Silently handle errors - logging should not break the application |
| 148 | + if (process.env.EP_DEBUG === 'true') { |
| 149 | + console.error(`[FW_LOG] Error sending to Fireworks:`, error.message); |
| 150 | + console.error(`[FW_LOG] Payload was:`, JSON.stringify(payload, null, 2)); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private getRolloutId(info: FireworksLogInfo): string | null { |
| 156 | + // Check if rollout_id is in the log info |
| 157 | + if (info.rollout_id && typeof info.rollout_id === 'string') { |
| 158 | + return info.rollout_id; |
| 159 | + } |
| 160 | + |
| 161 | + // Fallback to environment variable |
| 162 | + return process.env[this.rolloutIdEnv] || null; |
| 163 | + } |
| 164 | + |
| 165 | + private getStatusInfo(info: FireworksLogInfo): StatusInfo | null { |
| 166 | + if (!info.status) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + const status = info.status; |
| 171 | + |
| 172 | + // Handle Status class instances (with code and message properties) |
| 173 | + if (typeof status === 'object' && status !== null && 'code' in status && 'message' in status) { |
| 174 | + return { |
| 175 | + code: typeof status.code === 'number' ? status.code : undefined, |
| 176 | + message: typeof status.message === 'string' ? status.message : undefined, |
| 177 | + details: Array.isArray(status.details) ? status.details : [], |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + return null; |
| 182 | + } |
| 183 | + |
| 184 | + private buildPayload(info: FireworksLogInfo, rolloutId: string): FireworksPayload { |
| 185 | + const timestamp = new Date().toISOString(); |
| 186 | + // Ensure message is always a string for Fireworks payload |
| 187 | + const message: string = typeof info.message === 'string' ? info.message : ''; |
| 188 | + const level = (info as any)[LEVEL] || info.level || 'info'; |
| 189 | + |
| 190 | + const tags: string[] = [`rollout_id:${rolloutId}`]; |
| 191 | + |
| 192 | + // Optional additional tags |
| 193 | + if (info.experiment_id && typeof info.experiment_id === 'string') { |
| 194 | + tags.push(`experiment_id:${info.experiment_id}`); |
| 195 | + } |
| 196 | + if (info.run_id && typeof info.run_id === 'string') { |
| 197 | + tags.push(`run_id:${info.run_id}`); |
| 198 | + } |
| 199 | + |
| 200 | + // Groupwise list of rollout_ids |
| 201 | + if (Array.isArray(info.rollout_ids)) { |
| 202 | + for (const rid of info.rollout_ids) { |
| 203 | + if (typeof rid === 'string') { |
| 204 | + tags.push(`rollout_id:${rid}`); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + const program = (typeof info.program === 'string' ? info.program : null) || 'eval_protocol'; |
| 210 | + |
| 211 | + return { |
| 212 | + program, |
| 213 | + status: this.getStatusInfo(info), |
| 214 | + message, |
| 215 | + tags, |
| 216 | + extras: { |
| 217 | + logger_name: info.logger_name || 'winston', |
| 218 | + level: level.toUpperCase(), |
| 219 | + timestamp, |
| 220 | + }, |
| 221 | + }; |
| 222 | + } |
| 223 | +} |
0 commit comments