Skip to content

Commit 12aa1be

Browse files
committed
Revert "feat(cloud-watch-logger): use the beacon api"
This reverts commit e980b7b.
1 parent a90cce5 commit 12aa1be

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

libs/core/src/lib/logger/cloud-watch-log/cloud-watch-log-api.service.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// eslint-disable-next-line max-classes-per-file
22
import { inject, Injectable } from '@angular/core'
3-
import { CommonHttpHeader, ContentType } from '@shiftcode/utilities'
3+
import { ContentType } from '@shiftcode/utilities'
4+
import { CommonHttpHeader } from '@shiftcode/utilities'
45

56
import { CLOUD_WATCH_LOG_V2_CONFIG } from './cloud-watch-log-config.injection-token'
67

@@ -69,17 +70,14 @@ export class CloudWatchLogV2ApiService {
6970
return (await result.json()) as LogStream
7071
}
7172

72-
writeLogs(logStreamName: string, logs: LogEvent[]): Promise<void> {
73-
const url = new URL(`${ApiPath.STREAMS}/${logStreamName}/${ApiPath.STREAM_LOGS}`, this.apiUrl)
74-
75-
// since the sendBeacon does not support providing headers directly,
76-
// we use a blob, to achieve the same effect (content-type application/json)
77-
const data = new Blob([JSON.stringify({ logEvents: logs } satisfies WriteLogEvents)], { type: 'application/json' })
78-
79-
// we use the beacon api to ensure log sending requests are not cancelled on page unload
80-
const ok = navigator.sendBeacon(url, data)
81-
82-
return ok ? Promise.resolve() : Promise.reject(new Error('Failed to send logs via sendBeacon'))
73+
async writeLogs(logStreamName: string, logs: LogEvent[]): Promise<void> {
74+
// todo: use beaconApi ?
75+
const resp = await fetch(new URL(`${ApiPath.STREAMS}/${logStreamName}/${ApiPath.STREAM_LOGS}`, this.apiUrl), {
76+
method: 'POST',
77+
headers: { [CommonHttpHeader.CONTENT_TYPE]: ContentType.JSON },
78+
body: JSON.stringify({ logEvents: logs } satisfies WriteLogEvents),
79+
})
80+
await this.handleError(resp)
8381
}
8482

8583
private async handleError(resp: Response): Promise<void> {

libs/core/src/lib/logger/cloud-watch-log/cloud-watch-log.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class CloudWatchLogV2Service {
3030

3131
private readonly logRequestInfoFn = inject(LOG_REQUEST_INFO_FN, { optional: true })
3232
private readonly config = inject(CLOUD_WATCH_LOG_V2_CONFIG)
33-
private readonly jsonStringifyReplacer = this.config.jsonStringifyReplacer ?? jsonMapSetStringifyReplacer
34-
private readonly bufferSize = this.config.bufferSize ?? 100
33+
private readonly jsonStringifyReplacer = this.config.jsonStringifyReplacer || jsonMapSetStringifyReplacer
34+
private readonly bufferSize = this.config.bufferSize || 100
3535

3636
/** Ring buffer for log events below the configured level, flushed on ERROR */
3737
private pendingBuffer: LogEvent[] = []
@@ -145,7 +145,7 @@ export class CloudWatchLogV2Service {
145145
// outer observable only for retry logic -- see below
146146
return defer(() => this.api.writeLogs(this.clientId, events)).pipe(
147147
catchError((err) => {
148-
console.error('unable to put logs to CloudWatch --> we try again with the next batch', err)
148+
console.warn('unable to put logs to CloudWatch --> we try again with the next batch', err)
149149
return of(void 0)
150150
}),
151151
)

0 commit comments

Comments
 (0)