-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add log level filtering to transports
- Loading branch information
1 parent
e35921a
commit dd991ac
Showing
8 changed files
with
63 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
import type { Axiom } from "@axiomhq/js"; | ||
import { Axiom, AxiomWithoutBatching } from '@axiomhq/js'; | ||
import { LogLevel } from '../logger'; | ||
import { Transport } from './transport'; | ||
|
||
export class AxiomJSTransport { | ||
private axiom: Axiom; | ||
private dataset: string; | ||
interface AxiomJSTransportConfig { | ||
axiom: Axiom | AxiomWithoutBatching; | ||
dataset: string; | ||
logLevel?: LogLevel; | ||
} | ||
export class AxiomJSTransport implements Transport { | ||
private config: AxiomJSTransportConfig; | ||
private promises: Promise<any>[] = []; | ||
|
||
constructor(axiom: Axiom, dataset: string) { | ||
this.axiom = axiom; | ||
this.dataset = dataset; | ||
constructor(config: AxiomJSTransportConfig) { | ||
this.config = config; | ||
} | ||
|
||
log(logs: any[]) { | ||
this.axiom.ingest(this.dataset, logs); | ||
const filteredLogs = logs.filter( | ||
(log) => | ||
(LogLevel[log.level as keyof typeof LogLevel] ?? LogLevel.info) >= (this.config.logLevel ?? LogLevel.info), | ||
); | ||
|
||
if (filteredLogs.length === 0) { | ||
return; | ||
} | ||
|
||
if (this.config.axiom instanceof Axiom) { | ||
this.config.axiom.ingest(this.config.dataset, filteredLogs); | ||
} else if (this.config.axiom instanceof AxiomWithoutBatching) { | ||
this.promises.push(this.config.axiom.ingest(this.config.dataset, filteredLogs)); | ||
} | ||
} | ||
|
||
async flush() { | ||
await this.axiom.flush(); | ||
if (this.config.axiom instanceof Axiom) { | ||
await this.config.axiom.flush(); | ||
} else { | ||
await Promise.allSettled(this.promises); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { Transport } from '.'; | ||
import { LogLevel } from '../logger'; | ||
import { Transport } from './transport'; | ||
import { SimpleFetchTransport } from './fetch'; | ||
interface AxiomProxyConfig { | ||
url: string; | ||
autoFlush?: boolean | number; | ||
logLevel?: LogLevel; | ||
} | ||
export class AxiomProxyTransport extends SimpleFetchTransport implements Transport { | ||
constructor(config: AxiomProxyConfig) { | ||
super({ | ||
input: config.url, | ||
autoFlush: config.autoFlush, | ||
logLevel: config.logLevel, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Transport { | ||
log: (logs: any[]) => Promise<void> | void; | ||
flush: () => Promise<void> | void; | ||
} |