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

Update handler.py #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 5 additions & 23 deletions src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import re
from dateutil import parser


logger = logging.getLogger()

US_LOGGING_INGEST_HOST = "https://log-api.newrelic.com/log/v1"
Expand All @@ -24,11 +23,9 @@
'version': LOGGING_LAMBDA_VERSION
}


class InvalidArgumentException(Exception):
pass


def _format_error(e, text):
return "{}. {}".format(e, text)

Expand Down Expand Up @@ -79,28 +76,24 @@ def _get_additional_attributes(attributes=None):

completed_requests = 0


class MaxRetriesException(Exception):
pass


class BadRequestException(Exception):
pass


def _is_ignore_log_file(key=None, regex_pattern=None):
"""
This functions checks whether this log file should be ignored based on regex pattern.
This function checks whether this log file should be ignored based on regex pattern.
"""
if not regex_pattern:
regex_pattern = _get_optional_env("S3_IGNORE_PATTERN", "$^")

return bool(re.search(regex_pattern, key))


def _isCloudTrail(key=None, regex_pattern=None):
"""
This functions checks whether this log file is a CloudTrail log based on regex pattern.
This function checks whether this log file is a CloudTrail log based on regex pattern.
"""
if not regex_pattern:
regex_pattern = _get_optional_env(
Expand All @@ -110,7 +103,7 @@ def _isCloudTrail(key=None, regex_pattern=None):

def _isCloudTrailDigest(key=None):
"""
This functions checks whether this log file is a CloudTrail-Digest based on regex pattern.
This function checks whether this log file is a CloudTrail-Digest based on regex pattern.
"""
return bool(re.search(".*_CloudTrail-Digest_.*\.json.gz$", key))

Expand All @@ -123,28 +116,26 @@ def _convert_float(s):

def _get_batch_size_factor(batch_size_factor=None):
"""
This functions gets BATCH_SIZE_FACTOR from env vars.
This function gets BATCH_SIZE_FACTOR from env vars.
"""
if batch_size_factor:
return batch_size_factor
return _convert_float(_get_optional_env("BATCH_SIZE_FACTOR", BATCH_SIZE_FACTOR))

def _get_license_key(license_key=None):
"""
This functions gets New Relic's license key from env vars.
This function gets New Relic's license key from env vars.
"""
if license_key:
return license_key
return _get_optional_env("LICENSE_KEY", "")


def _get_log_type(log_type=None):
"""
This functions gets the New Relic logtype from env vars.
"""
return log_type or _get_optional_env("LOG_TYPE", "")


def _setting_console_logging_level():
"""
Determines whether or not debug logging should be enabled based on the env var.
Expand All @@ -156,7 +147,6 @@ def _setting_console_logging_level():
else:
logger.setLevel(logging.INFO)


def _get_logging_endpoint(ingest_url=None):
"""
Service url is determined by the license key's region.
Expand All @@ -172,7 +162,6 @@ def _get_logging_endpoint(ingest_url=None):
else US_LOGGING_INGEST_HOST
)


def _compress_payload(data):
"""
Return a list of payloads to be sent to New Relic.
Expand All @@ -184,7 +173,6 @@ def _compress_payload(data):
logger.debug(f"compressed size: {sys.getsizeof(payload)}")
return payload


def _package_log_payload(data):
"""
Packages up a MELT request for log messages
Expand All @@ -211,15 +199,13 @@ def _package_log_payload(data):
}]
return packaged_payload


def create_request(payload, ingest_url=None, license_key=None):
req = request.Request(_get_logging_endpoint(ingest_url), payload)
req.add_header("X-License-Key", _get_license_key(license_key))
req.add_header("X-Event-Source", "logs")
req.add_header("Content-Encoding", "gzip")
return req


async def send_log(session, url, data, headers):
global completed_requests
backoff = INITIAL_BACKOFF
Expand Down Expand Up @@ -264,14 +250,12 @@ async def send_log(session, url, data, headers):

raise MaxRetriesException()


def create_log_payload_request(data, session):
payload = _package_log_payload(data)
payload = _compress_payload(payload)
req = create_request(payload)
return send_log(session, req.get_full_url(), req.data, req.headers)


async def _fetch_data_from_s3(bucket, key, context):
"""
Stream data from S3 bucket. Create batches of size MAX_PAYLOAD_SIZE
Expand Down Expand Up @@ -331,7 +315,6 @@ async def _fetch_data_from_s3(bucket, key, context):
end = time.time()
logger.debug(f"time elapsed to send to NR Logs: {end - start}")


####################
# Lambda handler #
####################
Expand Down Expand Up @@ -372,6 +355,5 @@ def lambda_handler(event, context):
else:
return {'statusCode': 200, 'message': 'Uploaded logs to New Relic'}


if __name__ == "__main__":
lambda_handler('', '')