File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 1717
1818logger = logging .getLogger (__name__ )
1919
20+ def put_payload_cloudwatch (payload ):
21+ try :
22+ cloudwatch_logging = __import__ ('newrelic_lambda.cloudwatch_logging' , fromlist = ['put_log_to_cloudwatch' ])
23+ cloudwatch_logging .put_log_to_cloudwatch (payload )
24+ except Exception as e :
25+ print (f"Failed to send payload to CloudWatch: { e } , resorting to print payload." )
26+ print (payload )
2027
2128if ServerlessModeProtocol is not None :
2229 # New Relic Agent >=5.16
@@ -44,7 +51,10 @@ def protocol_finalize(self):
4451 "Failed to write to named pipe %s: %s" % (NAMED_PIPE_PATH , e )
4552 )
4653 else :
47- print (payload )
54+ if os .getenv ("NEW_RELIC_MAX_PAYLOAD" , "false" ).lower () == "true" :
55+ put_payload_cloudwatch (payload )
56+ else :
57+ print (payload )
4858
4959 return payload
5060
Original file line number Diff line number Diff line change 1+ import os
2+ import boto3
3+ from time import time
4+
5+ log_group_name = os .getenv ("AWS_LAMBDA_LOG_GROUP_NAME" , "" )
6+ log_stream_name = os .getenv ("AWS_LAMBDA_LOG_STREAM_NAME" , "" )
7+ log_level = os .getenv ("NEW_RELIC_LOG_LEVEL" , "info" ).lower ()
8+
9+ def put_log_to_cloudwatch (payload ):
10+ logs_client = boto3 .client ('logs' )
11+
12+ def ensure_log_stream_exists (log_group_name , log_stream_name ):
13+ try :
14+ logs_client .create_log_stream (logGroupName = log_group_name , logStreamName = log_stream_name )
15+ except Exception as e :
16+ if log_level == "debug" :
17+ print (f"Attempt create log stream { log_stream_name } in log group { log_group_name } : { e } " )
18+
19+ ensure_log_stream_exists (log_group_name , log_stream_name )
20+
21+ log_event = {
22+ 'timestamp' : int (time () * 1000 ),
23+ 'message' : payload ,
24+ }
25+
26+ logs_client .put_log_events (
27+ logGroupName = log_group_name ,
28+ logStreamName = log_stream_name ,
29+ logEvents = [log_event ]
30+ )
31+ if log_level == "debug" :
32+ print (f"Log event successfully sent to CloudWatch: { len (payload )} bytes, log group: { log_group_name } , log stream: { log_stream_name } " )
You can’t perform that action at this time.
0 commit comments