Skip to content

Commit 44bc041

Browse files
committed
Allow providing the elastic search index in the configuration
Signed-off-by: Pedro Algarvio <[email protected]>
1 parent cd00e58 commit 44bc041

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/saf/forward/elasticsearch.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import List
1818
from typing import Optional
1919
from typing import Type
20+
from typing import cast
2021

2122
from pydantic import Field
2223

@@ -74,6 +75,7 @@ class ElasticSearchConfig(ForwardConfigBase):
7475
username: Optional[str] = None
7576
password: Optional[str] = None
7677
timeout: int = 10
78+
index: Optional[str] = None
7779

7880

7981
def get_config_schema() -> Type[ElasticSearchConfig]:
@@ -86,7 +88,7 @@ def get_config_schema() -> Type[ElasticSearchConfig]:
8688
async def forward(
8789
*,
8890
ctx: PipelineRunContext[ElasticSearchConfig],
89-
event: ElasticSearchEvent,
91+
event: ElasticSearchEvent | CollectedEvent,
9092
) -> None:
9193
"""
9294
Method called to forward the event.
@@ -106,8 +108,31 @@ async def forward(
106108
connection_info = await client.info()
107109
log.warning("ES Connection Info:\n%s", pprint.pformat(connection_info))
108110
client = ctx.cache["client"]
109-
data = event.data.copy()
111+
if isinstance(event, ElasticSearchEvent):
112+
es_id = event.id
113+
es_index = event.index
114+
else:
115+
es_id = str(uuid.uuid4())
116+
if ctx.config.index is None:
117+
msg = (
118+
"Event to forward is not an instance of ElasticSearchEvent and the "
119+
"elasticsearch plugin index configuration is not defined."
120+
)
121+
raise RuntimeError(msg)
122+
es_index = ctx.config.index
123+
data = cast(Dict[str, Any], event.data).copy()
110124
if "@timestamp" not in data:
111125
data["@timestamp"] = event.timestamp
112-
ret = await client.index(index=event.index, id=event.id, body=data)
113-
log.warning("ES SEND:\n%s", pprint.pformat(ret))
126+
log.debug(
127+
"ElasticSearch Forward Details:\nindex: %s\nid: %s\ndata:\n%s",
128+
es_index,
129+
es_id,
130+
pprint.pformat(data),
131+
)
132+
ret = await client.index(index=es_index, id=es_id, body=data)
133+
log.debug(
134+
"ElasticSearch Forward Response(index: %s; id: %s):\n%s",
135+
es_index,
136+
es_id,
137+
pprint.pformat(ret),
138+
)

0 commit comments

Comments
 (0)