Skip to content

Commit

Permalink
Created logger and constants file to avoid duplicating the APP_NAME v…
Browse files Browse the repository at this point in the history
…ariable everywhere
  • Loading branch information
Marc-Antoine Hinse committed Nov 1, 2024
1 parent 62b27dd commit 0cccb0d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
22 changes: 22 additions & 0 deletions packages/flare/python/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from datetime import timedelta
from enum import Enum


APP_NAME = "flare"
HOST = "localhost"
SPLUNK_PORT = 8089
REALM = APP_NAME + "_realm"
KV_COLLECTION_NAME = "event_ingestion_collection"
CRON_JOB_THRESHOLD_SINCE_LAST_FETCH = timedelta(minutes=10)


class PasswordKeys(Enum):
API_KEY = "api_key"
TENANT_ID = "tenant_id"


class CollectionKeys(Enum):
CURRENT_TENANT_ID = "current_tenant_id"
START_DATE = "start_date"
NEXT_TOKEN = "next_"
TIMESTAMP_LAST_FETCH = "timestamp_last_fetch"
42 changes: 17 additions & 25 deletions packages/flare/python/cron_job_ingest_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,26 @@

from datetime import date
from datetime import datetime
from datetime import timedelta
from enum import Enum
from typing import Any
from typing import Optional


sys.path.insert(0, os.path.join(os.path.dirname(__file__), "vendor"))
import vendor.splunklib.client as client

from constants import APP_NAME
from constants import CRON_JOB_THRESHOLD_SINCE_LAST_FETCH
from constants import HOST
from constants import KV_COLLECTION_NAME
from constants import SPLUNK_PORT
from constants import CollectionKeys
from constants import PasswordKeys
from flare import FlareAPI


APP_NAME = "flare"
HOST = "localhost"
SPLUNK_PORT = 8089
REALM = APP_NAME + "_realm"
KV_COLLECTION_NAME = "event_ingestion_collection"
CRON_JOB_THRESHOLD_SINCE_LAST_FETCH = timedelta(minutes=10)


class PasswordKeys(Enum):
API_KEY = "api_key"
TENANT_ID = "tenant_id"


class CollectionKeys(Enum):
CURRENT_TENANT_ID = "current_tenant_id"
START_DATE = "start_date"
NEXT_TOKEN = "next_"
TIMESTAMP_LAST_FETCH = "timestamp_last_fetch"
from logger import Logger


def main() -> None:
logger = Logger(class_name=__file__)
try:
splunk_service = client.connect(
host=HOST,
Expand All @@ -46,7 +33,7 @@ def main() -> None:
token=sys.stdin.readline().strip(),
)
except Exception as e:
print(str(e), file=sys.stderr)
logger.error(str(e))
raise Exception(str(e))

app: client.Application = splunk_service.apps[APP_NAME]
Expand All @@ -57,6 +44,11 @@ def main() -> None:
if last_fetched_timestamp and last_fetched_timestamp < (
datetime.now() - CRON_JOB_THRESHOLD_SINCE_LAST_FETCH
):
logger.debug(
"Fetched events less than {} minutes ago, exiting".format(
(CRON_JOB_THRESHOLD_SINCE_LAST_FETCH.seconds) / 60
)
)
return

api_key: Optional[str] = None
Expand Down Expand Up @@ -88,7 +80,7 @@ def main() -> None:
time.sleep(1)

if response.status_code != 200:
print(response.text, file=sys.stderr)
logger.error(response.text)
return

event_feed = response.json()
Expand All @@ -99,7 +91,7 @@ def main() -> None:
for item in event_feed["items"]:
print(json.dumps(item))
except Exception as e:
print("Exception={}".format(e))
logger.error("Exception={}".format(e))


def get_next(app: client.Application, tenant_id: int) -> Optional[str]:
Expand Down
3 changes: 0 additions & 3 deletions packages/flare/python/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from vendor.requests.auth import AuthBase


APP_NAME = "flare"


def ensure_str(value: t.Union[str, bytes]) -> str:
if isinstance(value, bytes):
return value.decode("utf8")
Expand Down
7 changes: 6 additions & 1 deletion packages/flare/python/flare_external_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from typing import Any


sys.path.insert(0, os.path.dirname(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "vendor"))
from flareio import FlareApiClient
from logger import Logger


def parseParams(payload: str) -> dict[str, Any]:
Expand All @@ -20,9 +22,12 @@ def parseParams(payload: str) -> dict[str, Any]:

class FlareUserTenants(splunk.rest.BaseRestHandler):
def handle_POST(self) -> None:
logger = Logger(class_name=__file__)
payload = self.request["payload"]
params = parseParams(payload)
self.flare_client = FlareApiClient(api_key=params["apiKey"])
user_tenants_response = self.flare_client.get("firework/v2/me/tenants")
self.response.setHeader("Content-Type", "application/json")
self.response.write(json.dumps(user_tenants_response.json()))
tenants_response = user_tenants_response.json()
logger.debug(tenants_response)
self.response.write(json.dumps(tenants_response))
56 changes: 56 additions & 0 deletions packages/flare/python/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging
import os
import tempfile

from constants import APP_NAME
from logging.handlers import TimedRotatingFileHandler
from typing import Any


class Logger:
def __init__(self, *, class_name: str) -> None:
SPLUNK_HOME = os.environ.get("SPLUNK_HOME")
is_local_build = False
log_filepath : str = ""
if SPLUNK_HOME:
log_filepath = os.path.join(
SPLUNK_HOME, "var", "log", "splunk", "{}.log".format(APP_NAME)
)
APPLICATION_FOLDER = os.path.join(SPLUNK_HOME, "etc", "apps", APP_NAME)
is_local_build = os.path.islink(APPLICATION_FOLDER)
else:
log_filepath = os.path.join(tempfile.gettempdir(), "{}.log".format(APP_NAME))

self.tag_name = os.path.splitext(os.path.basename(class_name))[0]
self._logger = logging.getLogger("flare-{}".format(self.tag_name))


if is_local_build:
# If the application is a symlink, it's been installed locally
self._logger.setLevel(logging.DEBUG)
else:
self._logger.setLevel(logging.ERROR)
formatter = logging.Formatter("%(asctime)s %(levelname)-5s %(message)s")
handler = TimedRotatingFileHandler(
log_filepath, when="d", interval=1, backupCount=5
)
handler.setFormatter(formatter)
self._logger.addHandler(handler)

def debug(self, msg: Any) -> None:
self._logger.debug(msg="{}: {}".format(self.tag_name, msg))

def info(self, msg: Any) -> None:
self._logger.info(msg="{}: {}".format(self.tag_name, msg))

def warning(self, msg: Any) -> None:
self._logger.warning(msg="{}: {}".format(self.tag_name, msg))

def error(self, msg: Any) -> None:
self._logger.error(msg="{}: {}".format(self.tag_name, msg))

def exception(self, msg: Any) -> None:
self._logger.exception(msg="{}: {}".format(self.tag_name, msg))

def critical(self, msg: Any) -> None:
self._logger.critical(msg="{}: {}".format(self.tag_name, msg))

0 comments on commit 0cccb0d

Please sign in to comment.