Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sathishkumar-p committed Jan 18, 2024
1 parent 2ce5a83 commit b4a2972
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 15 deletions.
22 changes: 11 additions & 11 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "Upload GitHub Action workflows logs to elastic"
description: "Downloads the workflow logs and uploads to Elastic"
author: "Shahar Glazner"
name: "Upload GitHub Action workflows logs to opensearch"
description: "Downloads the workflow logs and uploads to opensearch"
author: "Sathishkumar-P"
inputs:
github_token:
description: "GitHub PAT"
Expand All @@ -12,14 +12,14 @@ inputs:
description: "Github HOST API url, For enterprise will be different"
github_run_id:
description: "The workflow specific run id to read the logs from"
elastic_api_key_id:
description: "Elastic api key id"
elastic_api_key:
description: "Elastic api key"
elastic_host:
description: "The elastic host"
elastic_index:
description: "The elastic index"
opensearch_username:
description: "opensearch api key id"
opensearch_password:
description: "opensearch api key"
opensearch_host:
description: "The opensearch host"
opensearch_index:
description: "The opensearch index"

outputs:
result:
Expand Down
8 changes: 4 additions & 4 deletions elastic_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from opensearchpy import OpenSearch
from opensearchpy.helpers import bulk

ELASTIC_HOST = os.environ.get("INPUT_ELASTIC_HOST")
ELASTIC_API_KEY_ID = os.environ.get("INPUT_ELASTIC_API_KEY_ID")
ELASTIC_API_KEY = os.environ.get("INPUT_ELASTIC_API_KEY")
ELASTIC_INDEX = os.environ.get("INPUT_ELASTIC_INDEX")
ELASTIC_HOST = os.environ.get("INPUT_OPENSEARCH_HOST")
ELASTIC_API_KEY_ID = os.environ.get("INPUT_OPENSEARCH_USERNAME")
ELASTIC_API_KEY = os.environ.get("INPUT_OPENSEARCH_PASSWORD")
ELASTIC_INDEX = os.environ.get("INPUT_OPENSEARCH_INDEX")

try:
assert ELASTIC_HOST not in (None, '')
Expand Down
98 changes: 98 additions & 0 deletions opensearch_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# builtins
import logging
import os
import datetime
import sys
# third party
from opensearchpy import OpenSearch
from opensearchpy.helpers import bulk

OPENSEARCH_HOST = os.environ.get("INPUT_OPENSEARCH_HOST")
OPENSEARCH_USERNAME = os.environ.get("INPUT_OPENSEARCH_USERNAME")
OPENSEARCH_PASSWORD = os.environ.get("INPUT_OPENSEARCH_PASSWORD")
OPENSEARCH_INDEX = os.environ.get("INPUT_OPENSEARCH_INDEX")

try:
assert OPENSEARCH_HOST not in (None, '')
except:
output = "The input OPENSEARCH_HOST is not set"
print(f"Error: {output}")
sys.exit(-1)

try:
assert OPENSEARCH_USERNAME not in (None, '')
except:
output = "The input OPENSEARCH_USERNAME is not set"
print(f"Error: {output}")
sys.exit(-1)

try:
assert OPENSEARCH_PASSWORD not in (None, '')
except:
output = "The input OPENSEARCH_PASSWORD is not set"
print(f"Error: {output}")
sys.exit(-1)

try:
assert OPENSEARCH_INDEX not in (None, '')
now = datetime.datetime.now()
opensearch_index = f"{OPENSEARCH_INDEX}-{now.month}-{now.day}"
except:
output = "The input OPENSEARCH_INDEX is not set"
print(f"Error: {output}")
sys.exit(-1)

try:
es = OpenSearch(
[OPENSEARCH_HOST],
http_auth=(OPENSEARCH_USERNAME, OPENSEARCH_PASSWORD),
use_ssl=True,
verify_certs=False,
ssl_show_warn=False,
)
except opensearch.exceptions.AuthorizationException as exc:
output = "Authentication to opensearch failed"
print(f"Error: {output}")
sys.exit(-1)


class OpensearchHandler(logging.Handler):

def __init__(self, *args, **kwargs):
super(OpensearchHandler, self).__init__(*args, **kwargs)
self.buffer = []

def emit(self, record):
try:
record_dict = record.__dict__
record_dict["@timestamp"] = int(record_dict.pop("created") * 1000)
self.buffer.append({
"_index": opensearch_index,
**record_dict
})
except ValueError as e:
output = f"Error inserting to Opensearch {str(e)}"
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
return

def flush(self):
# if the index is not exist, create it with mapping:
if not es.indices.exists(index=opensearch_index):
mapping = '''
{
"mappings":{
"properties": {
"@timestamp": {
"type": "date",
"format": "epoch_millis"
}
}
}
}'''
es.indices.create(index=opensearch_index, body=mapping)
# commit the logs to opensearch
bulk(
client=es,
actions=self.buffer
)

0 comments on commit b4a2972

Please sign in to comment.