Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loki.push): allow any Iterable values (such as Generator) #214

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.from(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
Loading