diff --git a/docs/Loki.md b/docs/Loki.md index 16864b3..ebacaf5 100644 --- a/docs/Loki.md +++ b/docs/Loki.md @@ -222,7 +222,7 @@ async series>( ): Promise ``` -### push(logs: LokiIngestLogs): Promise< void > +### push(logs: Iterable< LokiIngestLogs >): Promise< void > Send log entries to Loki. ```ts diff --git a/src/class/Loki.class.ts b/src/class/Loki.class.ts index b759c25..ac5c9cf 100644 --- a/src/class/Loki.class.ts +++ b/src/class/Loki.class.ts @@ -237,12 +237,16 @@ export class Loki { return listSeries.status === "success" ? listSeries.data : []; } - async push(logs: LokiIngestLogs[]): Promise { + async push( + logs: Iterable + ): Promise { const uri = new URL("loki/api/v1/push", this.remoteApiURL); const { headers } = this.credential.httpOptions; await httpie.post(uri, { - body: { streams: logs }, + body: { + streams: Array.from(logs) + }, headers: { ...headers, "Content-Type": "application/json" diff --git a/test/Loki.spec.ts b/test/Loki.spec.ts index 6dae70b..6715452 100644 --- a/test/Loki.spec.ts +++ b/test/Loki.spec.ts @@ -366,7 +366,30 @@ describe("GrafanaApi.Loki", () => { }).reply(204); const sdk = new GrafanaApi({ remoteApiURL: kDummyURL }); - await assert.doesNotReject(async() => await sdk.Loki.push(dummyLogs)); + await assert.doesNotReject( + async() => await sdk.Loki.push(dummyLogs) + ); + }); + + it("should call POST /loki/api/v1/push with the provided logs (but with an Iterable)", async() => { + function* gen(): IterableIterator { + yield { + stream: { app: "foo" }, + values: [["173532887432100000", "hello world"]] + }; + } + + const agentPoolInterceptor = kMockAgent.get(kDummyURL); + agentPoolInterceptor + .intercept({ + path: (path) => path.includes("loki/api/v1/push"), + method: "POST" + }).reply(204); + + const sdk = new GrafanaApi({ remoteApiURL: kDummyURL }); + await assert.doesNotReject( + async() => await sdk.Loki.push(gen()) + ); }); }); });