8000 feat: add cloudwatch logs URL to lambda additional data by flyinbutrs · Pull Request #618 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

feat: add cloudwatch logs URL to lambda additional data #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime, timedelta
from os import environ
import sys

from sentry_sdk.hub import Hub, _should_send_default_pii
Expand Down Expand Up @@ -158,17 +160,25 @@ def inner(*args, **kwargs):

def _make_request_event_processor(aws_event, aws_context):
# type: (Any, Any) -> EventProcessor
def event_processor(event, hint):
# type: (Event, Hint) -> Optional[Event]
start_time = datetime.now()

def event_processor(event, hint, start_time=start_time):
# type: (Event, Hint, datetime) -> Optional[Event]
extra = event.setdefault("extra", {})
extra["lambda"] = {
"remaining_time_in_millis": aws_context.get_remaining_time_in_millis(),
"function_name": aws_context.function_name,
"function_version": aws_context.function_version,
"invoked_function_arn": aws_context.invoked_function_arn,
"remaining_time_in_millis": aws_context.get_remaining_time_in_millis(),
"aws_request_id": aws_context.aws_request_id,
}

extra["cloudwatch logs"] = {
"url": _get_cloudwatch_logs_url(aws_context, start_time),
"log_group": aws_context.log_group_name,
"log_stream": aws_context.log_stream_name,
}

request = event.get("request", {})

if "httpMethod" in aws_event:
Expand Down Expand Up @@ -214,3 +224,31 @@ def _get_url(event, context):
if proto and host and path:
return "{}://{}{}".format(proto, host, path)
return "awslambda:///{}".format(context.function_name)


def _get_cloudwatch_logs_url(context, start_time):
# type: (Any, datetime) -> str
"""
Generates a CloudWatchLogs console URL based on the context object

Arguments:
context {Any} -- context from lambda handler

Returns:
str -- AWS Console URL to logs.
"""
formatstring = "%Y-%m-%dT%H:%M:%S"

url = (
"https://console.aws.amazon.com/cloudwatch/home?region={region}"
"#logEventViewer:group={log_group};stream={log_stream}"
";start={start_time};end={end_time}"
).format(
region=environ.get("AWS_REGION"),
log_group=context.log_group_name,
log_stream=context.log_stream_name,
start_time=(start_time - timedelta(seconds=1)).strftime(formatstring),
end_time=(datetime.now() + timedelta(seconds=2)).strftime(formatstring),
)

return url
0