Skip to content

Commit

Permalink
feat(loki.push): allow any Iterable values (such as Generator)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Jan 5, 2025
1 parent 5e6cb66 commit 693d64f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/Loki.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async series<T = Record<string, string>>(
): Promise<T[]>
```

### push(logs: LokiIngestLogs): Promise< void >
### push(logs: Iterable< LokiIngestLogs >): Promise< void >
Send log entries to Loki.

```ts
Expand Down
8 changes: 6 additions & 2 deletions src/class/Loki.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,16 @@ export class Loki {
return listSeries.status === "success" ? listSeries.data : [];
}

async push(logs: LokiIngestLogs[]): Promise<void> {
async push(
logs: Iterable<LokiIngestLogs>
): Promise<void> {
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.isArray(logs) ? logs : [...logs]
},
headers: {
...headers,
"Content-Type": "application/json"
Expand Down
25 changes: 24 additions & 1 deletion test/Loki.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LokiIngestLogs> {
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())
);
});
});
});
Expand Down

0 comments on commit 693d64f

Please sign in to comment.