def add_metadata_to_lambda_log(event): """Mutate log dict to add functionname tag, host, and service from the existing Lambda attribute If the event arg is not a Lambda log then this returns without doing anything Args: event (dict): the event we are adding Lambda metadata to """ lambda_log_metadata = event.get('lambda', {}) lambda_log_arn = lambda_log_metadata.get('arn') # Do not mutate the event if it's not from Lambda if not lambda_log_arn: return # Function name is the sixth piece of the ARN function_name = lambda_log_arn.split(':')[6] event[DD_HOST] = lambda_log_arn event[DD_SERVICE] = function_name tags = ['functionname:{}'.format(function_name)] # Add any enhanced tags from metadata if IS_ENHANCED_METRICS_FILE_PRESENT: tags += get_enriched_lambda_log_tags(event) # Dedup tags, so we don't end up with functionname twice tags = list(set(tags)) event[DD_CUSTOM_TAGS] = ",".join([event[DD_CUSTOM_TAGS]] + tags)
def add_metadata_to_lambda_log(event): """Mutate log dict to add tags, host, and service metadata * tags for functionname, aws_account, region * host from the Lambda ARN * service from the Lambda name If the event arg is not a Lambda log then this returns without doing anything Args: event (dict): the event we are adding Lambda metadata to """ lambda_log_metadata = event.get("lambda", {}) lambda_log_arn = lambda_log_metadata.get("arn") # Do not mutate the event if it's not from Lambda if not lambda_log_arn: return # Set Lambda ARN to "host" event[DD_HOST] = lambda_log_arn # Function name is the seventh piece of the ARN function_name = lambda_log_arn.split(":")[6] tags = [f"functionname:{function_name}"] # Get custom tags of the Lambda function custom_lambda_tags = get_enriched_lambda_log_tags(event) # Set the `service` tag and metadata field. If the Lambda function is # tagged with a `service` tag, use it, otherwise use the function name. service_tag = next( (tag for tag in custom_lambda_tags if tag.startswith("service:")), f"service:{function_name}", ) tags.append(service_tag) event[DD_SERVICE] = service_tag.split(":")[1] # Check if one of the Lambda's custom tags is env # If an env tag exists, remove the env:none placeholder custom_env_tag = next( (tag for tag in custom_lambda_tags if tag.startswith("env:")), None ) if custom_env_tag is not None: event[DD_CUSTOM_TAGS] = event[DD_CUSTOM_TAGS].replace("env:none", "") tags += custom_lambda_tags # Dedup tags, so we don't end up with functionname twice tags = list(set(tags)) tags.sort() # Keep order deterministic event[DD_CUSTOM_TAGS] = ",".join([event[DD_CUSTOM_TAGS]] + tags)
def add_metadata_to_lambda_log(event): """Mutate log dict to add tags, host, and service metadata * tags for functionname, aws_account, region * host from the Lambda ARN * service from the Lambda name If the event arg is not a Lambda log then this returns without doing anything Args: event (dict): the event we are adding Lambda metadata to """ lambda_log_metadata = event.get("lambda", {}) lambda_log_arn = lambda_log_metadata.get("arn") # Do not mutate the event if it's not from Lambda if not lambda_log_arn: return # Function name is the sixth piece of the ARN function_name = lambda_log_arn.split(":")[6] event[DD_HOST] = lambda_log_arn event[DD_SERVICE] = function_name tags = ["functionname:{}".format(function_name)] # Add any enhanced tags from metadata if IS_ENHANCED_METRICS_FILE_PRESENT: custom_lambda_tags = get_enriched_lambda_log_tags(event) # Check if one of the Lambda's custom tags is env # If an env tag exists, remove the env:none placeholder custom_env_tag = next( (tag for tag in custom_lambda_tags if tag.startswith("env:")), None) if custom_env_tag is not None: event[DD_CUSTOM_TAGS] = event[DD_CUSTOM_TAGS].replace( "env:none", "") tags += custom_lambda_tags # Dedup tags, so we don't end up with functionname twice tags = list(set(tags)) tags.sort() # Keep order deterministic event[DD_CUSTOM_TAGS] = ",".join([event[DD_CUSTOM_TAGS]] + tags)
def add_metadata_to_lambda_log(event): """Mutate log dict to add tags, host, and service metadata * tags for functionname, aws_account, region * host from the Lambda ARN * service from the Lambda name If the event arg is not a Lambda log then this returns without doing anything Args: event (dict): the event we are adding Lambda metadata to """ lambda_log_metadata = event.get("lambda", {}) lambda_log_arn = lambda_log_metadata.get("arn") # Do not mutate the event if it's not from Lambda if not lambda_log_arn: return # Function name is the sixth piece of the ARN function_name = lambda_log_arn.split(":")[6] event[DD_HOST] = lambda_log_arn event[DD_SERVICE] = function_name tags = ["functionname:{}".format(function_name)] # Add any enhanced tags from metadata if IS_ENHANCED_METRICS_FILE_PRESENT: tags += get_enriched_lambda_log_tags(event) # Dedup tags, so we don't end up with functionname twice tags = list(set(tags)) tags.sort() # Keep order deterministic event[DD_CUSTOM_TAGS] = ",".join([event[DD_CUSTOM_TAGS]] + tags)