Skip to content

Commit

Permalink
Add http request timeout, improve logging (#77)
Browse files Browse the repository at this point in the history
* logging, timeout

* makefile, aws template

* shipper

* makefile, aws template

* shipper, template

* gitignore, remove git-secrets workflow, add pre commit config

* update cw makefile

* readme

* reamde bump version in links

* fix template, readme
  • Loading branch information
mirii1994 authored May 28, 2023
1 parent 660c73a commit e026e93
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 62 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/git-secrets.yml

This file was deleted.

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.prettierignore
.prettierignore
.idea
.DS_Store
*/*/dist
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- repo: https://github.com/cycodehq-public/cycode-cli
rev: 0.1.6
hooks:
- id: cycode
language_version: python3
stages:
- commit
4 changes: 3 additions & 1 deletion python3/cloudwatch/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
build:
mkdir -p dist/python3/shipper
mkdir -p dist/python3/custom_logger
cp src/lambda_function.py dist
cp ../shipper/shipper.py dist/python3/shipper
cp ../custom_logger/custom_logger.py dist/python3/custom_logger

(cd dist/ && zip -r logzio-cloudwatch-log-shipper.zip lambda_function.py python3/shipper/*)
(cd dist/ && zip -r logzio-cloudwatch-log-shipper.zip lambda_function.py python3/shipper/* python3/custom_logger/*)

clean:
rm -rf dist/
42 changes: 25 additions & 17 deletions python3/cloudwatch/README.md

Large diffs are not rendered by default.

35 changes: 31 additions & 4 deletions python3/cloudwatch/sam-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ Parameters:
logzioListener:
Type: "String"
Description: "The Logz.io listener URL for your region. You can find explanations here: https://docs.logz.io/user-guide/accounts/account-region.html"
Default: ""
Default: 'https://listener.logz.io:8071'
AllowedValues:
- 'https://listener.logz.io:8071'
- 'https://listener-au.logz.io:8071'
- 'https://listener-ca.logz.io:8071'
- 'https://listener-eu.logz.io:8071'
- 'https://listener-nl.logz.io:8071'
- 'https://listener-uk.logz.io:8071'
- 'https://listener-wa.logz.io:8071'

logzioToken:
Type: "String"
Expand Down Expand Up @@ -45,6 +53,23 @@ Parameters:
Description: "CloudWatch Log Group name from where you want to send logs."
Default: ""

shipperLogLevel:
Type: "String"
Description: "Log level of the shipping Lambda function."
AllowedValues:
- "DEBUG"
- "INFO"
- "WARNING"
- "ERROR"
- "CRITICAL"
Default: "INFO"

requestTimeout:
Type: "Number"
Description: "Timeout in seconds for each http request for sending logs into logz.io"
MinValue: 0
Default: 15

Outputs:
LogzioCloudwatchLogsLambda:
Description: "Logz.io CW logs lambda ARN"
Expand All @@ -54,12 +79,12 @@ Resources:
LogzioCloudwatchLogsLambda:
Type: 'AWS::Serverless::Function'
Properties:
Runtime: python3.7
Runtime: python3.9
Handler: lambda_function.lambda_handler
CodeUri:
Bucket: logzio-aws-integrations-<<REGION>>
Key: cloudwatch-auto-deployment/<<VERSION>>/logzio-cloudwatch.zip
FunctionName: logzio-cloudwatch-log-shipper
FunctionName: !Ref AWS::StackName
MemorySize: 512
Timeout: 60
Events:
Expand All @@ -75,4 +100,6 @@ Resources:
TYPE: !Ref logzioType
FORMAT: !Ref logzioFormat
COMPRESS: !Ref logzioCompress
ENRICH: !Ref logzioEnrich
ENRICH: !Ref logzioEnrich
SHIPPER_LOG_LEVEL: !Ref shipperLogLevel
REQUEST_TIMEOUT: !Ref requestTimeout
7 changes: 4 additions & 3 deletions python3/cloudwatch/src/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import base64
import gzip
import json
import logging
from dateutil import parser
import os
from io import BytesIO

from python3.shipper.shipper import LogzioShipper
from python3.custom_logger import custom_logger

KEY_INDEX = 0
VALUE_INDEX = 1
Expand Down Expand Up @@ -37,8 +37,7 @@


# set logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger = custom_logger.get_logger(__name__)


def _extract_aws_logs_data(event):
Expand Down Expand Up @@ -171,7 +170,9 @@ def _get_additional_logs_data(aws_logs_data, context):
def lambda_handler(event, context):
# type (dict, 'LambdaContext') -> None

logger.debug(f'Handling event: {event}')
aws_logs_data = _extract_aws_logs_data(event)
logger.debug(f'Logs data: {aws_logs_data}')
additional_data = _get_additional_logs_data(aws_logs_data, context)
shipper = LogzioShipper()

Expand Down
Empty file.
17 changes: 17 additions & 0 deletions python3/custom_logger/custom_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging
import os

LOG_LEVEL_ENV = 'SHIPPER_LOG_LEVEL'
DEFAULT_LEVEL = 'INFO'


def get_logger(name):
level_str = os.getenv(LOG_LEVEL_ENV, DEFAULT_LEVEL)
logger = logging.getLogger(name)
try:
level = logging.getLevelName(level_str)
logger.setLevel(level)
except Exception as e:
logger.setLevel(logging.INFO)
logger.warning(f'Could not set logger to level {level_str}, setting to default level {DEFAULT_LEVEL}: {e}')
return logger
26 changes: 22 additions & 4 deletions python3/shipper/shipper.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import gzip
import io
import json
import logging
import os
import sys
import time
import urllib
import urllib.request

from python3.custom_logger import custom_logger

# set logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger = custom_logger.get_logger(__name__)


class MaxRetriesException(Exception):
Expand Down Expand Up @@ -125,6 +125,8 @@ class LogzioShipper(object):
URL_ENV = 'LISTENER_URL'
BASE_URL = "https://listener.logz.io:8071"
region = None
TIMEOUT_ENV = 'REQUEST_TIMEOUT'
default_timeout = 15 # seconds

def __init__(self):
self._logzio_url = self.BASE_URL
Expand Down Expand Up @@ -152,6 +154,21 @@ def __init__(self):
if self._compress \
else StringLogRequest(self.MAX_BULK_SIZE_IN_BYTES)

self.timeout = self._get_timeout()
logger.debug(f'Request timeout is set to: {self.timeout} seconds')

def _get_timeout(self):
timeout_str = os.getenv(self.TIMEOUT_ENV)
if timeout_str != '':
try:
timeout = int(timeout_str)
if timeout > 0:
return timeout
logger.warning(f'Timeout input from user is invalid, reverting to default value {self.default_timeout}')
except TypeError:
logger.warning(f'Could not parse timeout input {timeout_str}, reverting to default value {self.default_timeout}')
return self.default_timeout

def get_base_api_url(self):
return self.BASE_URL.replace("listener.", "listener{}.".format(self.get_region_code()))

Expand Down Expand Up @@ -221,9 +238,10 @@ def _send_to_logzio(self):
def do_request():
self._logs.close()
logs = self._logs.bytes()
logger.info(f'About to send {len(logs)} bytes')
request = urllib.request.Request(self._logzio_url, data=self._logs.bytes(),
headers=self._logs.http_headers())
return urllib.request.urlopen(request)
return urllib.request.urlopen(request, timeout=self.timeout)

try:
do_request()
Expand Down

0 comments on commit e026e93

Please sign in to comment.