Skip to content

Commit

Permalink
Deprecate the NR_TAGS and NR_ENV_DELIMITER configuration options
Browse files Browse the repository at this point in the history
The new `ADDITIONAL_ATTRIBUTES` provides a full replacement, so the logic now is
to use what is configured in that variable, and only if that is unset fall back
to parsing `NR_TAGS` and `NR_ENV_DELIMITER`.

Note that `ADDITIONAL_ATTRIBUTES` is a JSON string, and doesn't contain the logic
to not add `aws:`- and `plugin:`-prefixed properties: If those aren't wanted they
shouldn't be provided.
  • Loading branch information
ankon committed Aug 2, 2024
1 parent 48b29d4 commit ef2fb53
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ def _get_optional_env(key, default):
"""
return os.getenv(key, default) or default


def _get_newrelic_tags(attributes=None):
"""
This functions gets New Relic's tags from env vars and adds it to the payload
A tag is a key value pair. Multiple tags can be specified.
Key and value are colon delimited. Multiple key value pairs are semi-colon delimited.
e.g. env:prod;team:myTeam
Deprecated: Use the standard `ADDITIONAL_ATTRIBUTES` environment variable instead.
"""
if attributes:
return attributes
nr_tags_str = os.getenv("NR_TAGS", "")
nr_delimiter = os.getenv("NR_ENV_DELIMITER", ";")
if nr_tags_str:
nr_tags = dict(
item.split(":")
for item in nr_tags_str.split(nr_delimiter)
if not item.startswith(tuple(["aws:", "plugin:"]))
)
return nr_tags


def _get_additional_attributes(attributes=None):
"""
This function gets Environment variable 'ADDITIONAL_ATTRIBUTES' and parses the same as a json object. Defaults
Expand All @@ -60,7 +83,7 @@ def _get_additional_attributes(attributes=None):
"bytes or bytearray)"))


additional_attributes = _get_additional_attributes()
additional_attributes = _get_newrelic_tags(_get_additional_attributes())
# Maximum number of retries
MAX_RETRIES = 5
# Initial backoff (in seconds) between retries
Expand Down Expand Up @@ -185,24 +208,6 @@ def _compress_payload(data):
return payload


def _get_newrelic_tags(payload):
"""
This functions gets New Relic's tags from env vars and adds it to the payload
A tag is a key value pair. Multiple tags can be specified.
Key and value are colon delimited. Multiple key value pairs are semi-colon delimited.
e.g. env:prod;team:myTeam
"""
nr_tags_str = os.getenv("NR_TAGS", "")
nr_delimiter = os.getenv("NR_ENV_DELIMITER", ";")
if nr_tags_str:
nr_tags = dict(
item.split(":")
for item in nr_tags_str.split(nr_delimiter)
if not item.startswith(tuple(["aws:", "plugin:"]))
)
payload[0]["common"]["attributes"].update(nr_tags)


def _package_log_payload(data):
"""
Packages up a MELT request for log messages
Expand Down Expand Up @@ -375,7 +380,7 @@ def lambda_handler(event, context):
object_key = urllib.parse.unquote_plus(
s3_event["object"]["key"], encoding="utf-8")

# Allow user to skip log file using regex pattern set in env variable: S3_IGNORE_PATTERN
# Allow user to skip log file using regex pattern set in env variable: S3_IGNORE_PATTERN
if _is_ignore_log_file(key):
logger.debug(f"Ignore log file based on S3_IGNORE_PATTERN: {key}")
return {'statusCode': 200, 'message': 'ignored this log'}
Expand Down

0 comments on commit ef2fb53

Please sign in to comment.