Repository navigation
Fix/logstash tests and docs #310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
156 changes: 156 additions & 0 deletions
156
packages/logstash-http/src/LogStashHttpAppender.spec.ts
This file contains hidden or 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,156 @@ | ||
| import {AppendersRegistry, Logger} from "@tsed/logger"; | ||
| import axios from "axios"; | ||
| import axiosRetry from "axios-retry"; | ||
| import {vi} from "vitest"; | ||
|
|
||
| import {LogStashHttpAppender} from "./LogStashHttpAppender.js"; | ||
|
|
||
| vi.mock("axios"); | ||
| vi.mock("axios-retry"); | ||
|
|
||
| function createLogger(options: Record<string, unknown> = {}) { | ||
| const client = vi.fn().mockResolvedValue({status: 200, data: {errors: false}}); | ||
|
|
||
| vi.mocked(axios.create).mockReturnValue(client as never); | ||
|
|
||
| const logger = new Logger("test"); | ||
|
|
||
| logger.appenders.set("logstash", { | ||
| type: "logstash-http", | ||
| options: { | ||
| url: "http://localhost:9200/_bulk", | ||
| application: "logstash-test", | ||
| logType: "application", | ||
| logChannel: "node", | ||
| ...options | ||
| } | ||
| }); | ||
|
|
||
| return {logger, client}; | ||
| } | ||
|
|
||
| function getLines(client: ReturnType<typeof vi.fn>, call = 0) { | ||
| return client.mock.calls[call][0].data | ||
| .split("\n") | ||
| .filter(Boolean) | ||
| .map((line: string) => JSON.parse(line)); | ||
| } | ||
|
|
||
| describe("LogStashHttpAppender", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("should be registered under the logstash-http name", () => { | ||
| expect(AppendersRegistry.get("logstash-http")!.provide).toBe(LogStashHttpAppender); | ||
| }); | ||
|
|
||
| it("should create the HTTP client from the options", () => { | ||
| createLogger({ | ||
| auth: {username: "user", password: "pass"}, | ||
| timeout: 1000, | ||
| params: {pipeline: "main"}, | ||
| headers: {"x-api-key": "key"}, | ||
| retryOptions: {retries: 5} | ||
| }); | ||
|
|
||
| expect(axios.create).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| baseURL: "http://localhost:9200/_bulk", | ||
| auth: {username: "user", password: "pass"}, | ||
| timeout: 1000, | ||
| params: {pipeline: "main"}, | ||
| headers: {"x-api-key": "key"} | ||
| }) | ||
| ); | ||
| expect(axiosRetry).toHaveBeenCalledWith(expect.any(Function), expect.objectContaining({retries: 5})); | ||
| }); | ||
|
|
||
| it("should post each log as a bulk request when no buffer is configured", () => { | ||
| const {logger, client} = createLogger(); | ||
|
|
||
| logger.info({event: "STARTED"}); | ||
|
|
||
| expect(client).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| method: "POST", | ||
| headers: {"Content-Type": "application/x-ndjson"} | ||
| }) | ||
| ); | ||
|
|
||
| const [action, document] = getLines(client); | ||
|
|
||
| expect(action.index).toMatchObject({ | ||
| _index: "logstash-test", | ||
| _type: "application", | ||
| _id: expect.any(String) | ||
| }); | ||
| expect(document).toMatchObject({ | ||
| event: "STARTED", | ||
| level_name: "info", | ||
| channel: "node", | ||
| datetime: expect.any(String) | ||
| }); | ||
| }); | ||
|
|
||
| it("should resolve the index from a function", () => { | ||
| const {logger, client} = createLogger({application: () => "dynamic-index"}); | ||
|
|
||
| logger.info("hello"); | ||
|
|
||
| expect(getLines(client)[0].index._index).toEqual("dynamic-index"); | ||
| }); | ||
|
|
||
| it("should wait for bufferMax logs before posting", () => { | ||
| const {logger, client} = createLogger({bufferMax: 2}); | ||
|
|
||
| logger.info("first"); | ||
|
|
||
| expect(client).not.toHaveBeenCalled(); | ||
|
|
||
| logger.info("second"); | ||
|
|
||
| expect(client).toHaveBeenCalledTimes(1); | ||
| expect(getLines(client)).toHaveLength(4); | ||
| }); | ||
|
|
||
| it("should flush the buffer on shutdown", async () => { | ||
| const {logger, client} = createLogger({bufferMax: 10}); | ||
|
|
||
| logger.info("first"); | ||
|
|
||
| expect(client).not.toHaveBeenCalled(); | ||
|
|
||
| await logger.shutdown(); | ||
|
|
||
| expect(client).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should report a response error", async () => { | ||
| const {logger, client} = createLogger({bufferMax: 10}); | ||
| const error = vi.spyOn(console, "error").mockReturnValue(undefined); | ||
|
|
||
| client.mockRejectedValue({response: {status: 500, data: {reason: "failed"}}}); | ||
|
|
||
| logger.info("first"); | ||
| await logger.shutdown(); | ||
|
|
||
| expect(error).toHaveBeenCalledWith(expect.stringContaining('error posting to http://localhost:9200/_bulk: 500 - {"reason":"failed"}')); | ||
|
|
||
| error.mockRestore(); | ||
| }); | ||
|
|
||
| it("should report a network error", async () => { | ||
| const {logger, client} = createLogger({bufferMax: 10}); | ||
| const error = vi.spyOn(console, "error").mockReturnValue(undefined); | ||
|
|
||
| client.mockRejectedValue(new Error("ECONNREFUSED")); | ||
|
|
||
| logger.info("first"); | ||
| await logger.shutdown(); | ||
|
|
||
| expect(error).toHaveBeenCalledWith("Ts.ED Logger.logstash-http Appender error: ECONNREFUSED"); | ||
|
|
||
| error.mockRestore(); | ||
| }); | ||
| }); |
This file contains hidden or 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,19 @@ | ||
| // @ts-ignore | ||
| import {presets} from "@tsed/vitest/presets"; | ||
| import {defineConfig} from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| ...presets, | ||
| test: { | ||
| ...presets.test, | ||
| coverage: { | ||
| ...presets.test.coverage, | ||
| thresholds: { | ||
| statements: 0, | ||
| branches: 0, | ||
| functions: 0, | ||
| lines: 0 | ||
| } | ||
| } | ||
| } | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this workspace is installed or tested in isolation (for example through a focused workspace install), the new
testscript cannot findvitest, and its configuration also imports the undeclared@tsed/vitest/presets; the full monorepo currently masks both omissions through dependencies hoisted from other workspaces. Addvitestand@tsed/vitestto this package's devDependencies, and make the same correction inpackages/logstash-udp/package.json.Useful? React with 👍 / 👎.