Skip to content

Commit 2dc6cf3

Browse files
fix: improve hec batch to be not a JSON array to conform specification (#904)
improve hec batch to be not a JSON array to conform specification: https://docs.splunk.com/Documentation/Splunk/latest/Data/FormateventsforHTTPEventCollector#Example_3:_Batched_data (ADDON-79329) --------- Co-authored-by: mkolasinski-splunk <mkolasinski@splunk.com>
1 parent cd0c088 commit 2dc6cf3

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

pytest_splunk_addon/event_ingestors/hec_event_ingestor.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
import json
17+
1618
from .base_event_ingestor import EventIngestor
1719
import requests
1820
from time import time, mktime
@@ -52,7 +54,7 @@ def ingest(self, events, thread_count):
5254
"""
5355
Ingests event and metric data into splunk using HEC token via event endpoint.
5456
55-
For batch ingestion of events in a single request at event endpoint provide a list of event dict to be ingested.
57+
For batch ingestion of events in a single request at event endpoint provide stacked events one after the other to be ingested.
5658
5759
The format of dictionary for ingesting a single event::
5860
@@ -63,22 +65,20 @@ def ingest(self, events, thread_count):
6365
"event": "event_str"
6466
}
6567
66-
The format of dictionary for ingesting a batch of events::
67-
68-
[
69-
{
70-
"sourcetype": "sample_HEC",
71-
"source": "sample_source",
72-
"host": "sample_host",
73-
"event": "event_str1"
74-
},
75-
{
76-
"sourcetype": "sample_HEC",
77-
"source": "sample_source",
78-
"host": "sample_host",
79-
"event": "event_str2"
80-
},
81-
]
68+
The format for ingesting a batch of events::
69+
70+
{
71+
"sourcetype": "sample_HEC",
72+
"source": "sample_source",
73+
"host": "sample_host",
74+
"event": "event_str1"
75+
}
76+
{
77+
"sourcetype": "sample_HEC",
78+
"source": "sample_source",
79+
"host": "sample_host",
80+
"event": "event_str2"
81+
}
8282
8383
Args:
8484
events (list): List of events (SampleEvent) to be ingested
@@ -115,20 +115,21 @@ def ingest(self, events, thread_count):
115115

116116
def __ingest(self, data):
117117
try:
118+
batch_data = "\n".join(json.dumps(obj) for obj in data)
118119
LOGGER.info(
119120
"Making a HEC event request with the following params:\nhec_uri:{}\nheaders:{}".format(
120121
str(self.hec_uri), str(self.session_headers)
121122
)
122123
)
123124
LOGGER.debug(
124125
"Creating the following sample event to be ingested via HEC event endoipnt:{}".format(
125-
str(data)
126+
str(batch_data)
126127
)
127128
)
128129
response = requests.post( # nosemgrep: splunk.disabled-cert-validation
129130
"{}/{}".format(self.hec_uri, "event"),
130131
auth=None,
131-
json=data,
132+
data=batch_data,
132133
headers=self.session_headers,
133134
verify=False,
134135
)

tests/unit/tests_standard_lib/test_event_ingestors/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,26 @@ def modinput_posts_sent():
6868
return [
6969
(
7070
f"POST {HEC_URI}/event",
71-
"[{"
71+
"{"
7272
'"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
7373
'"source": "pytest-splunk-addon:modinput", '
7474
'"event": "test_modinput_1 host=modinput_host_event_time_plugin.samples_1", '
7575
'"index": "main", '
7676
'"host": "modinput_host_event_time_plugin.samples_1"'
77-
"}, {"
77+
"}\n{"
7878
'"sourcetype": "test:indextime:sourcetype:modinput_host_event_time_plugin", '
7979
'"source": "pytest-splunk-addon:modinput", '
8080
'"event": "test_modinput_2 host=modinput_host_event_time_plugin.samples_2", '
8181
'"index": "main", '
8282
'"host": "modinput_host_event_time_plugin.samples_2"'
83-
"}, {"
83+
"}\n{"
8484
'"sourcetype": "pytest_splunk_addon", '
8585
'"source": "pytest_splunk_addon:hec:event", '
8686
'"event": "fake event nothing happened", '
8787
'"index": "fake_index", '
8888
'"host": "fake host", '
8989
'"time": 1234.5678'
90-
"}]",
90+
"}",
9191
)
9292
]
9393

0 commit comments

Comments
 (0)