Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opensearch logs introduction #1839

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service_status/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pymysql==0.10.1
aws-xray-sdk==2.4.2
grpcio-tools==1.32.0
grpcio-health-checking==1.32.0
opensearch-py
39 changes: 37 additions & 2 deletions service_status/service_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from datetime import datetime as dt
from datetime import timedelta

from opensearchpy import OpenSearch
import grpc
from grpc_health.v1 import health_pb2 as heartb_pb2
from grpc_health.v1 import health_pb2_grpc as heartb_pb2_grpc
Expand All @@ -11,7 +11,7 @@
from common.logger import get_logger
from common.utils import Utils
from resources.certificates.root_certificate import certificate
from service_status.config import REGION_NAME, NOTIFICATION_ARN, SLACK_HOOK, NETWORKS, NETWORK_ID, \
from service_status.config import REGION_NAME, NOTIFICATION_ARN, SLACK_HOOK, NETWORKS, NETWORK_ID, HOST, AUTH, \
MAXIMUM_INTERVAL_IN_HOUR, MINIMUM_INTERVAL_IN_HOUR, NETWORK_NAME, BASE_URL_TO_RESET_SERVICE_HEALTH
from service_status.constant import SRVC_STATUS_GRPC_TIMEOUT, LIMIT

Expand Down Expand Up @@ -78,6 +78,40 @@ def _update_service_failed_status_count(self, failed_status_count, row_id):
response = self.repo.execute(update_query, [failed_status_count, row_id])
return response

def _send_logs_to_opensearch(self, service_id, debug_error_string, endpoint):
client = OpenSearch(
http_compress = True,
hosts = [{'host': HOST, 'port': 443}],
http_auth = AUTH,
use_ssl = True,
verify_certs = True,
ssl_assert_hostname = False,
ssl_show_warn = False,
)
timestamp = dt.datetime.now(dt.timezone.utc)
index_name = f"services-logs-{NETWORKS[NETWORK_ID]['name']}-{timestamp.strftime('%Y.%m.%d')}"
if not client.indices.exists(index_name):
index_body = {
'settings': {
'index': {
'number_of_shards': 1
}
}
}
response = client.indices.create(index_name, body=index_body)
document = {
'@timestamp': timestamp,
'Log': debug_error_string,
'Service': service_id,
'Endpoint': endpoint
}
response = client.index(
index = index_name,
body = document,
refresh = True
)
return response

def _update_service_status_stats(self, org_id, service_id, old_status, status):
previous_state = "UP" if (old_status == 1) else "DOWN"
current_state = "UP" if (status == 1) else "DOWN"
Expand Down Expand Up @@ -117,6 +151,7 @@ def update_service_status(self):
org_id = record["org_id"]
service_id = record["service_id"]
recipients = self._get_service_provider_email(org_id=org_id, service_id=service_id)
self._send_logs_to_opensearch(service_id=service_id, debug_error_string=debug_error_string, endpoint=record["endpoint"])
if failed_status_count <= 10:
self._send_notification(org_id=org_id, service_id=service_id, recipients=recipients,
endpoint=record["endpoint"], error_details=error_details,
Expand Down
Loading