From e34276fa922a36515b0a09a93b1cd5e17784dd06 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 16 Jun 2021 21:02:00 +0200 Subject: [PATCH] callback_splunk - Add user-configurable event correlation id (#2790) (#2817) * Initial commit * Adding changelog fragment * Updating batch description * Update plugins/callback/splunk.py Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein (cherry picked from commit 3ca98c2edd2cd878b635b347c44b7de30b3522b7) Co-authored-by: Ajpantuso --- .../2790-callback_splunk-batch-option.yml | 3 +++ plugins/callback/splunk.py | 22 ++++++++++++++++++- tests/unit/plugins/callback/test_splunk.py | 10 +++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/2790-callback_splunk-batch-option.yml diff --git a/changelogs/fragments/2790-callback_splunk-batch-option.yml b/changelogs/fragments/2790-callback_splunk-batch-option.yml new file mode 100644 index 0000000000..70ee61ed64 --- /dev/null +++ b/changelogs/fragments/2790-callback_splunk-batch-option.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - splunk callback plugin - add ``batch`` option for user-configurable correlation ID's (https://github.com/ansible-collections/community.general/issues/2790). diff --git a/plugins/callback/splunk.py b/plugins/callback/splunk.py index f782161765..cb63d3b23f 100644 --- a/plugins/callback/splunk.py +++ b/plugins/callback/splunk.py @@ -68,6 +68,16 @@ DOCUMENTATION = ''' type: bool default: false version_added: 2.0.0 + batch: + description: + - Correlation ID which can be set across multiple playbook executions. + env: + - name: SPLUNK_BATCH + ini: + - section: callback_splunk + key: batch + type: str + version_added: 3.3.0 ''' EXAMPLES = ''' @@ -107,7 +117,7 @@ class SplunkHTTPCollectorSource(object): self.ip_address = socket.gethostbyname(socket.gethostname()) self.user = getpass.getuser() - def send_event(self, url, authtoken, validate_certs, include_milliseconds, state, result, runtime): + def send_event(self, url, authtoken, validate_certs, include_milliseconds, batch, state, result, runtime): if result._task_fields['args'].get('_ansible_check_mode') is True: self.ansible_check_mode = True @@ -126,6 +136,8 @@ class SplunkHTTPCollectorSource(object): data = {} data['uuid'] = result._task._uuid data['session'] = self.session + if batch is not None: + data['batch'] = batch data['status'] = state if include_milliseconds: @@ -175,6 +187,7 @@ class CallbackModule(CallbackBase): self.authtoken = None self.validate_certs = None self.include_milliseconds = None + self.batch = None self.splunk = SplunkHTTPCollectorSource() def _runtime(self, result): @@ -212,6 +225,8 @@ class CallbackModule(CallbackBase): self.include_milliseconds = self.get_option('include_milliseconds') + self.batch = self.get_option('batch') + def v2_playbook_on_start(self, playbook): self.splunk.ansible_playbook = basename(playbook._file_name) @@ -227,6 +242,7 @@ class CallbackModule(CallbackBase): self.authtoken, self.validate_certs, self.include_milliseconds, + self.batch, 'OK', result, self._runtime(result) @@ -238,6 +254,7 @@ class CallbackModule(CallbackBase): self.authtoken, self.validate_certs, self.include_milliseconds, + self.batch, 'SKIPPED', result, self._runtime(result) @@ -249,6 +266,7 @@ class CallbackModule(CallbackBase): self.authtoken, self.validate_certs, self.include_milliseconds, + self.batch, 'FAILED', result, self._runtime(result) @@ -260,6 +278,7 @@ class CallbackModule(CallbackBase): self.authtoken, self.validate_certs, self.include_milliseconds, + self.batch, 'FAILED', result, self._runtime(result) @@ -271,6 +290,7 @@ class CallbackModule(CallbackBase): self.authtoken, self.validate_certs, self.include_milliseconds, + self.batch, 'UNREACHABLE', result, self._runtime(result) diff --git a/tests/unit/plugins/callback/test_splunk.py b/tests/unit/plugins/callback/test_splunk.py index df4db38d56..3230228da1 100644 --- a/tests/unit/plugins/callback/test_splunk.py +++ b/tests/unit/plugins/callback/test_splunk.py @@ -43,7 +43,10 @@ class TestSplunkClient(unittest.TestCase): mock_datetime.utcnow.return_value = datetime(2020, 12, 1) result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields) - self.splunk.send_event(url='endpoint', authtoken='token', validate_certs=False, include_milliseconds=True, state='OK', result=result, runtime=100) + self.splunk.send_event( + url='endpoint', authtoken='token', validate_certs=False, include_milliseconds=True, + batch="abcefghi-1234-5678-9012-abcdefghijkl", state='OK', result=result, runtime=100 + ) args, kwargs = open_url_mock.call_args sent_data = json.loads(args[1]) @@ -58,7 +61,10 @@ class TestSplunkClient(unittest.TestCase): mock_datetime.utcnow.return_value = datetime(2020, 12, 1) result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields) - self.splunk.send_event(url='endpoint', authtoken='token', validate_certs=False, include_milliseconds=False, state='OK', result=result, runtime=100) + self.splunk.send_event( + url='endpoint', authtoken='token', validate_certs=False, include_milliseconds=False, + batch="abcefghi-1234-5678-9012-abcdefghijkl", state='OK', result=result, runtime=100 + ) args, kwargs = open_url_mock.call_args sent_data = json.loads(args[1])