|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# This file is part of REANA. |
| 4 | +# Copyright (C) 2024 CERN. |
| 5 | +# |
| 6 | +# REANA is free software; you can redistribute it and/or modify it |
| 7 | +# under the terms of the MIT License; see LICENSE file for more details. |
| 8 | + |
| 9 | +"""OpenSearch client and log fetcher.""" |
| 10 | + |
| 11 | +import logging |
| 12 | +from opensearchpy import OpenSearch |
| 13 | + |
| 14 | +from reana_workflow_controller.config import ( |
| 15 | + REANA_OPENSEARCH_CA_CERTS, |
| 16 | + REANA_OPENSEARCH_HOST, |
| 17 | + REANA_OPENSEARCH_PASSWORD, |
| 18 | + REANA_OPENSEARCH_PORT, |
| 19 | + REANA_OPENSEARCH_URL_PREFIX, |
| 20 | + REANA_OPENSEARCH_USE_SSL, |
| 21 | + REANA_OPENSEARCH_USER, |
| 22 | + REANA_OPENSEARCH_ENABLED, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def build_opensearch_client( |
| 27 | + host: str = REANA_OPENSEARCH_HOST, |
| 28 | + port: str = REANA_OPENSEARCH_PORT, |
| 29 | + url_prefix: str = REANA_OPENSEARCH_URL_PREFIX, |
| 30 | + http_auth: tuple | None = (REANA_OPENSEARCH_USER, REANA_OPENSEARCH_PASSWORD), |
| 31 | + use_ssl: bool = REANA_OPENSEARCH_USE_SSL, |
| 32 | + ca_certs: str | None = REANA_OPENSEARCH_CA_CERTS, |
| 33 | +) -> OpenSearch: |
| 34 | + """ |
| 35 | + Build an OpenSearch client object. |
| 36 | +
|
| 37 | + :param host: OpenSearch host. |
| 38 | + :param port: OpenSearch port. |
| 39 | + :param url_prefix: URL prefix. |
| 40 | + :param http_auth: HTTP authentication credentials. |
| 41 | + :param use_ssl: Use SSL/TLS for connection. |
| 42 | + :param ca_certs: Path to CA certificates. |
| 43 | +
|
| 44 | + :return: OpenSearch client object. |
| 45 | + """ |
| 46 | + opensearch_client = OpenSearch( |
| 47 | + hosts=f"{host}:{port}", |
| 48 | + http_compress=True, # enables gzip compression for request bodies |
| 49 | + http_auth=http_auth, |
| 50 | + use_ssl=use_ssl, |
| 51 | + ca_certs=ca_certs, |
| 52 | + url_prefix=url_prefix, |
| 53 | + verify_certs=True, |
| 54 | + ) |
| 55 | + return opensearch_client |
| 56 | + |
| 57 | + |
| 58 | +class OpenSearchLogFetcher(object): |
| 59 | + """Retrieves job and workflow logs from OpenSearch API.""" |
| 60 | + |
| 61 | + def __init__( |
| 62 | + self, |
| 63 | + os_client: OpenSearch | None = None, |
| 64 | + job_index: str = "fluentbit-job_log", |
| 65 | + workflow_index: str = "fluentbit-workflow_log", |
| 66 | + max_rows: int = 5000, |
| 67 | + log_key: str = "log", |
| 68 | + order: str = "asc", |
| 69 | + job_log_matcher: str = "kubernetes.labels.job-name.keyword", |
| 70 | + workflow_log_matcher: str = "kubernetes.labels.reana-run-batch-workflow-uuid.keyword", |
| 71 | + timeout: int = 5, |
| 72 | + ) -> None: |
| 73 | + """ |
| 74 | + Initialize the OpenSearchLogFetcher object. |
| 75 | +
|
| 76 | + :param os_client: OpenSearch client object. |
| 77 | + :param job_index: Index name for job logs. |
| 78 | + :param workflow_index: Index name for workflow logs. |
| 79 | + :param max_rows: Maximum number of rows to fetch. |
| 80 | + :param log_key: Key for log message in the response. |
| 81 | + :param order: Order of logs (asc/desc). |
| 82 | + :param job_log_matcher: Job log matcher. |
| 83 | + :param workflow_log_matcher: Workflow log matcher. |
| 84 | + :param timeout: Timeout for OpenSearch queries. |
| 85 | +
|
| 86 | + :return: None |
| 87 | + """ |
| 88 | + if os_client is None: |
| 89 | + os_client = build_opensearch_client() |
| 90 | + |
| 91 | + self.os_client = os_client |
| 92 | + self.job_index = job_index |
| 93 | + self.workflow_index = workflow_index |
| 94 | + self.max_rows = max_rows |
| 95 | + self.log_key = log_key |
| 96 | + self.order = order |
| 97 | + self.job_log_matcher = job_log_matcher |
| 98 | + self.workflow_log_matcher = workflow_log_matcher |
| 99 | + self.timeout = timeout |
| 100 | + |
| 101 | + def fetch_logs(self, id: str, index: str, match: str) -> str | None: |
| 102 | + """ |
| 103 | + Fetch logs of a specific job or workflow. |
| 104 | +
|
| 105 | + :param id: Job or workflow ID. |
| 106 | + :param index: Index name for logs. |
| 107 | + :param match: Matcher for logs. |
| 108 | +
|
| 109 | + :return: Job or workflow logs. |
| 110 | + """ |
| 111 | + query = { |
| 112 | + "query": {"match": {match: id}}, |
| 113 | + "sort": [{"@timestamp": {"order": self.order}}], |
| 114 | + } |
| 115 | + |
| 116 | + try: |
| 117 | + response = self.os_client.search( |
| 118 | + index=index, body=query, size=self.max_rows, timeout=self.timeout |
| 119 | + ) |
| 120 | + except Exception as e: |
| 121 | + logging.error("Failed to fetch logs for {0}: {1}".format(id, e)) |
| 122 | + return None |
| 123 | + |
| 124 | + return self._concat_rows(response["hits"]["hits"]) |
| 125 | + |
| 126 | + def fetch_job_logs(self, backend_job_id: str) -> str: |
| 127 | + """ |
| 128 | + Fetch logs of a specific job. |
| 129 | +
|
| 130 | + :param backend_job_id: Job ID. |
| 131 | +
|
| 132 | + :return: Job logs. |
| 133 | + """ |
| 134 | + return self.fetch_logs( |
| 135 | + backend_job_id, |
| 136 | + self.job_index, |
| 137 | + self.job_log_matcher, |
| 138 | + ) |
| 139 | + |
| 140 | + def fetch_workflow_logs(self, workflow_id: str) -> str | None: |
| 141 | + """ |
| 142 | + Fetch logs of a specific workflow. |
| 143 | +
|
| 144 | + :param workflow_id: Workflow ID. |
| 145 | +
|
| 146 | + :return: Workflow logs. |
| 147 | + """ |
| 148 | + return self.fetch_logs( |
| 149 | + workflow_id, |
| 150 | + self.workflow_index, |
| 151 | + self.workflow_log_matcher, |
| 152 | + ) |
| 153 | + |
| 154 | + def _concat_rows(self, rows: list) -> str | None: |
| 155 | + """ |
| 156 | + Concatenate log messages from rows. |
| 157 | +
|
| 158 | + :param rows: List of rows. |
| 159 | +
|
| 160 | + :return: Concatenated log messages. |
| 161 | + """ |
| 162 | + logs = "" |
| 163 | + |
| 164 | + for hit in rows: |
| 165 | + logs += hit["_source"][self.log_key] + "\n" |
| 166 | + |
| 167 | + return logs |
| 168 | + |
| 169 | + |
| 170 | +def build_opensearch_log_fetcher() -> OpenSearchLogFetcher | None: |
| 171 | + """ |
| 172 | + Build OpenSearchLogFetcher object. |
| 173 | +
|
| 174 | + :return: OpenSearchLogFetcher object. |
| 175 | + """ |
| 176 | + return OpenSearchLogFetcher() if REANA_OPENSEARCH_ENABLED else None |
0 commit comments