You can download this code by clicking the button below.
This code is now available for download.
This function uses the Graylog API client to query the stream ID for a given stream name. It accepts the stream name, query, and an instance of the Graylog API client as parameters, and returns the found stream ID or None. If the query does not return any results, None is returned.
Technology Stack : Graylog API client
Code Type : Function
Code Difficulty : Intermediate
def graylog_query_stream_id(stream_name, query, graylog_client):
"""
Fetches the stream ID for a given stream name using a Graylog client.
:param stream_name: Name of the stream to query.
:param query: The search query to execute.
:param graylog_client: An instance of the Graylog API client.
:return: The stream ID or None if not found.
"""
# Initialize the search query
search_query = f'stream="{stream_name}" AND {query}'
# Execute the search query
search_response = graylog_client.search.search(query=search_query, per_page=1)
# Check if the search returned any results
if search_response.messages:
# Extract the stream ID from the first result
stream_id = search_response.messages[0].message['stream_id']
return stream_id
else:
return None