Skip to content

Commit 20175ec

Browse files
Merge pull request #1 from GlueOps/feature/update-logging
Feature/update logging
2 parents a8eb634 + 095a86f commit 20175ec

File tree

6 files changed

+97
-83
lines changed

6 files changed

+97
-83
lines changed

glueops/aws.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
from typing import List
2+
13
import boto3
2-
import glueops.logging
4+
from botocore.client import BaseClient
5+
36

4-
logger = glueops.logging.configure()
7+
def create_aws_client(service: str, region: str = 'us-east-1') -> BaseClient:
8+
return boto3.client(service, region_name=region)
59

610

7-
def create_aws_client(service):
8-
return boto3.client(service, region_name='us-east-1')
11+
def get_resource_arns_using_tags(
12+
tags: List[dict],
13+
aws_resource_filters: List[str]
14+
) -> List[str]:
15+
"""Retrieve ARNs of resources with specific tags.
916
17+
Args:
18+
tags (List[dict]): _in format {"Key": "<the-key>", "Value": "<the-value>"}
19+
aws_resource_filters (List[str]): AWS resource filter list
1020
11-
def get_resource_arns_using_tags(tags, aws_resource_filters):
12-
"""Retrieve ARNs of resources with specific tags."""
13-
logger.info(
14-
f"Checking to see if this arn was already requested/created with tags: {tags}")
21+
Returns:
22+
List[str]: ARNs of resources
23+
"""
1524
tagging = create_aws_client('resourcegroupstaggingapi')
1625
tags = {item['Key']: item['Value'] for item in tags}
1726
response = tagging.get_resources(
@@ -20,8 +29,7 @@ def get_resource_arns_using_tags(tags, aws_resource_filters):
2029
],
2130
ResourceTypeFilters=aws_resource_filters
2231
)
23-
24-
arns = [item['ResourceARN']
25-
for item in response.get('ResourceTagMappingList', [])]
26-
27-
return arns
32+
return [
33+
item['ResourceARN']
34+
for item in response.get('ResourceTagMappingList', [])
35+
]

glueops/certificates.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
from cryptography.exceptions import UnsupportedAlgorithm, InvalidSignature
44

55

6-
def extract_serial_number_from_cert_string(cert_string):
7-
try:
8-
certificate = x509.load_pem_x509_certificate(cert_string.encode(), default_backend())
9-
decimal_serial = certificate.serial_number
10-
11-
# Convert to hexadecimal
12-
hex_serial = format(decimal_serial, 'X')
13-
14-
# Ensure an even number of digits for correct byte representation
15-
if len(hex_serial) % 2 != 0:
16-
hex_serial = '0' + hex_serial
17-
18-
# Insert colons between every 2 characters
19-
colon_separated_serial = ":".join(hex_serial[i:i+2] for i in range(0, len(hex_serial), 2))
20-
return colon_separated_serial
6+
def extract_serial_number_from_cert_string(cert_string: str) -> str:
7+
certificate = x509.load_pem_x509_certificate(cert_string.encode(), default_backend())
8+
decimal_serial = certificate.serial_number
219

22-
except (ValueError, UnsupportedAlgorithm, InvalidSignature) as e:
23-
# You can specify more exceptions as needed based on the specific errors you're encountering.
24-
raise ValueError("Provided certificate string is not parsable: " + str(e))
10+
# Convert to hexadecimal
11+
hex_serial = format(decimal_serial, 'X')
12+
13+
# Ensure an even number of digits for correct byte representation
14+
if len(hex_serial) % 2 != 0:
15+
hex_serial = '0' + hex_serial
16+
17+
# Return serial with colons between every 2 characters
18+
return ":".join(hex_serial[i:i+2] for i in range(0, len(hex_serial), 2)).upper()

glueops/checksum_tools.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import zlib
22

3-
def string_to_crc32(input_string):
3+
def string_to_crc32(input_string: str) -> str:
44
"""Compute CRC32 checksum for a given string and return it in hexadecimal format."""
55
# Encode the string to bytes
66
data_bytes = input_string.encode('utf-8')
@@ -9,6 +9,4 @@ def string_to_crc32(input_string):
99
checksum = zlib.crc32(data_bytes) & 0xffffffff
1010

1111
# Convert checksum to hexadecimal
12-
hex_checksum = hex(checksum)
13-
14-
return hex_checksum
12+
return hex(checksum)

glueops/fastapi.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

glueops/logging.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

glueops/setup_logging.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import inspect
2+
import json
3+
import logging
4+
from typing import Union
5+
6+
7+
class JsonFormatter(logging.Formatter):
8+
def format(self, record):
9+
log_entry = {
10+
'timestamp': self.formatTime(record, self.datefmt),
11+
'name': record.name,
12+
'level': record.levelname,
13+
'message': record.getMessage(),
14+
}
15+
16+
if record.exc_info:
17+
log_entry['exception'] = self.formatException(record.exc_info)
18+
19+
return json.dumps(log_entry)
20+
21+
22+
def configure(
23+
name=None,
24+
level: Union[str, int]=logging.ERROR,
25+
*handlers: logging.Handler
26+
) -> logging.Logger:
27+
"""Configure and return a logger with GlueOps default configuration
28+
29+
Args:
30+
name (_type_, optional): The name of the logger. Defaults to None.
31+
level (Union[str, int], optional): The log level, as an int or str. Defaults to logging.ERROR.
32+
Must be less restrictive than the level applied to additional handlers for those handlers to receive logs
33+
*handlers (logging.Handler, optional): Add any additional handlers that may be desired.
34+
35+
Returns:
36+
logging.Logger: Instance of configured logger
37+
"""
38+
if name is None:
39+
frame = inspect.stack()[1]
40+
module = inspect.getmodule(frame[0])
41+
name = module.__name__ if module else "defaultLogger"
42+
43+
logger = logging.getLogger(name)
44+
logger.setLevel(level)
45+
46+
# create default formatter and handler
47+
default_handler = logging.StreamHandler()
48+
json_formatter = JsonFormatter()
49+
default_handler.setFormatter(json_formatter)
50+
51+
# remove handlers not configured by this module
52+
for handler in logger.handlers[:]:
53+
logger.removeHandler(handler)
54+
55+
# add default handler
56+
logger.addHandler(default_handler)
57+
58+
# add custom handlers
59+
for handler in handlers:
60+
logger.addHandler(handler)
61+
62+
return logger

0 commit comments

Comments
 (0)