-
Notifications
You must be signed in to change notification settings - Fork 66
vdk-structlog: add syslog handler #2985
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
14 commits
Select commit
Hold shift + click to select a range
8f66c94
add syslog constants and config
duyguHsnHsn ad17e46
add syslog to structlog class
duyguHsnHsn fbaf0db
fix
duyguHsnHsn 059513d
add facility
duyguHsnHsn a34a4b1
add some tests
duyguHsnHsn ab8f2e1
remove context from syslog config
duyguHsnHsn 8667a05
remove unused imports
duyguHsnHsn 3920eb3
adjust tests
duyguHsnHsn d63bec3
fix
duyguHsnHsn d15e05b
use atribute adder instead of custom one
duyguHsnHsn 8c61d06
modify current tests
duyguHsnHsn c792bbd
add handler to the next hook
duyguHsnHsn 4064b54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d94d47e
vdk-structlog: add test for syslog
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
53 changes: 53 additions & 0 deletions
53
projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/syslog_config.py
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,53 @@ | ||
| # Copyright 2021-2024 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import logging.handlers | ||
|
|
||
| from vdk.plugin.structlog.constants import SYSLOG_PROTOCOLS | ||
| from vdk.plugin.structlog.filters import AttributeAdder | ||
|
|
||
|
|
||
| DETAILED_FORMAT = ( | ||
| "%(asctime)s [VDK] %(job_name)s [%(levelname)-5.5s] %(name)-30.30s %(filename)20.20s:%(" | ||
| "lineno)-4.4s %(funcName)-16.16s[id:%(attempt_id)s]- %(message)s" | ||
| ) | ||
|
|
||
|
|
||
| def configure_syslog_handler( | ||
| syslog_enabled, | ||
| syslog_host, | ||
| syslog_port, | ||
| syslog_protocol, | ||
| job_name="", | ||
| attempt_id="no-id", | ||
| ): | ||
| if not syslog_enabled: | ||
| return None | ||
|
|
||
| if syslog_protocol not in SYSLOG_PROTOCOLS: | ||
| raise ValueError( | ||
| f"Provided configuration variable for SYSLOG_PROTOCOL has an invalid value. " | ||
| f"VDK was run with SYSLOG_PROTOCOL={syslog_protocol}, however, " | ||
| f"{syslog_protocol} is an invalid value for this variable. " | ||
| f"Provide a valid value for SYSLOG_PROTOCOL. " | ||
| f"Currently possible values are {list(SYSLOG_PROTOCOLS.keys())}" | ||
| ) | ||
|
|
||
| syslog_socktype = SYSLOG_PROTOCOLS[syslog_protocol.upper()] | ||
| syslog_handler = logging.handlers.SysLogHandler( | ||
| address=(syslog_host, syslog_port), | ||
| facility=logging.handlers.SysLogHandler.LOG_DAEMON, | ||
| socktype=syslog_socktype, | ||
| ) | ||
|
|
||
| formatter = logging.Formatter(DETAILED_FORMAT) | ||
| syslog_handler.setFormatter(formatter) | ||
|
|
||
| job_name_adder = AttributeAdder("job_name", job_name) | ||
| attempt_id_adder = AttributeAdder("attempt_id", attempt_id) | ||
|
|
||
| syslog_handler.addFilter(job_name_adder) | ||
| syslog_handler.addFilter(attempt_id_adder) | ||
|
|
||
| syslog_handler.setLevel("DEBUG") | ||
DeltaMichael marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return syslog_handler | ||
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
84 changes: 84 additions & 0 deletions
84
projects/vdk-plugins/vdk-structlog/tests/test_syslog_config.py
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,84 @@ | ||
| # Copyright 2021-2024 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import logging.handlers | ||
| import socket | ||
| from unittest.mock import MagicMock | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from vdk.plugin.structlog.filters import AttributeAdder | ||
| from vdk.plugin.structlog.syslog_config import configure_syslog_handler | ||
| from vdk.plugin.structlog.syslog_config import DETAILED_FORMAT | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_job_context(): | ||
| job_context = MagicMock() | ||
| job_context.name = "test_job" | ||
| job_context.core_context.state.get.return_value = "12345" | ||
| job_context.core_context.configuration.get_value.side_effect = lambda key: { | ||
| "SYSLOG_ENABLED": True, | ||
| "SYSLOG_HOST": "localhost", | ||
| "SYSLOG_PORT": 514, | ||
| "SYSLOG_PROTOCOL": "UDP", | ||
| }.get(key, None) | ||
| return job_context | ||
|
|
||
|
|
||
| def test_configure_syslog_handler_enabled(mock_job_context): | ||
| syslog_handler = configure_syslog_handler( | ||
| mock_job_context.core_context.configuration.get_value("SYSLOG_ENABLED"), | ||
| mock_job_context.core_context.configuration.get_value("SYSLOG_HOST"), | ||
| mock_job_context.core_context.configuration.get_value("SYSLOG_PORT"), | ||
| mock_job_context.core_context.configuration.get_value("SYSLOG_PROTOCOL"), | ||
| mock_job_context.name, | ||
| mock_job_context.core_context.state.get(), | ||
| ) | ||
| assert isinstance(syslog_handler, logging.handlers.SysLogHandler) | ||
| assert syslog_handler.level == logging.DEBUG | ||
| assert any(isinstance(filter, AttributeAdder) for filter in syslog_handler.filters) | ||
| assert syslog_handler.formatter._fmt == DETAILED_FORMAT | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("protocol", ["UDP", "TCP"]) | ||
| @patch("socket.socket") | ||
| def test_configure_syslog_handler_with_different_protocols( | ||
| mock_socket, protocol, mock_job_context | ||
| ): | ||
| mock_socket_instance = MagicMock() | ||
| mock_socket.return_value = mock_socket_instance | ||
|
|
||
| syslog_handler = configure_syslog_handler( | ||
| True, | ||
| "localhost", | ||
| 514, | ||
| protocol, | ||
| mock_job_context.name, | ||
| mock_job_context.core_context.state.get(), | ||
| ) | ||
|
|
||
| expected_socktype = socket.SOCK_DGRAM if protocol == "UDP" else socket.SOCK_STREAM | ||
| assert mock_socket.called | ||
| assert mock_socket.call_args[0][1] == expected_socktype | ||
| assert any( | ||
| isinstance(filter, AttributeAdder) and filter._attr_key == "job_name" | ||
| for filter in syslog_handler.filters | ||
| ) | ||
| assert any( | ||
| isinstance(filter, AttributeAdder) and filter._attr_key == "attempt_id" | ||
| for filter in syslog_handler.filters | ||
| ) | ||
|
|
||
|
|
||
| def test_configure_syslog_handler_disabled(mock_job_context): | ||
| syslog_handler = configure_syslog_handler( | ||
| False, "localhost", 514, "UDP", "test_job", "12345" | ||
| ) | ||
| assert syslog_handler is None | ||
|
|
||
|
|
||
| def test_configure_syslog_handler_invalid_protocol(mock_job_context): | ||
| with pytest.raises(ValueError): | ||
| configure_syslog_handler( | ||
| True, "localhost", 514, "invalid_protocol", "test_job", "12345" | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.