sentry-lambda 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e61cf6580a8711e896c89807873d6fc321de3daffc3f1e5dfc72347bd3287d78
4
+ data.tar.gz: fde79717cd1ffea4730d8cd3eac687a35222674b46f64401a3b687ef70f91de0
5
+ SHA512:
6
+ metadata.gz: 43ec6a0c8600129794fa37558d99512af9b84cf7546c2a565862b11d5b8e0515f1b2fe8c37c8a908ea26c49a203cf7edf8ce6f999915df79eacddc6d389ad6f3
7
+ data.tar.gz: 03e62de3dbbbba48b674fbf27a368b35339f43f3c513235bb70ed87c224e46119b32b1a4b2734a6f8c470f6980e472ad3708423dd96c21c3b010a08e62dc77ed
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 st0012
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ <p align="center">
2
+ <a href="https://sentry.io" target="_blank" align="center">
3
+ <img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
4
+ </a>
5
+ <br>
6
+ </p>
7
+
8
+ # sentry-lambda, the AWS Lambda integration for Sentry's Ruby client
9
+
10
+ ---
11
+
12
+
13
+
14
+ The official Ruby-language client and integration layer for the [Sentry](https://github.com/getsentry/sentry) error reporting API.
15
+
16
+
17
+ ## Requirements
18
+
19
+ Ruby version >= 2.5
20
+
21
+ ## Getting Started
22
+
23
+ ### Install
24
+
25
+ ```ruby
26
+ gem "sentry-lambda"
27
+ ```
28
+
29
+ ### Usage
30
+ ```ruby
31
+ def lambda_handler(event:, context:)
32
+ Sentry::Lambda.wrap_handler(event: event, context: context) do
33
+ # Function logic here....
34
+ end
35
+ end
36
+ ```
37
+ #### Timeout Warnings
38
+ It can be important to know when a function is about to time out and to have sentry-level
39
+ details when this occurs. In order to give a Lambda Function time to do so, a warning message can
40
+ be enabled with `capture_timeout_warning: true` like so:
41
+ ```ruby
42
+ def lambda_handler(event:, context:)
43
+ Sentry::Lambda.wrap_handler(event: event, context: context, capture_timeout_warning: true) do
44
+ # Function logic here....
45
+ end
46
+ end
47
+ ```
48
+
49
+ ### Integration Specific Configuration
50
+
51
+ This gem has a few Lambda-specific configuration options.
52
+
53
+ Using an AWS CloudWatch LogGroup Trigger. This approach requires having a separate Lambda Function
54
+ which is triggered by this log. Simply outputting the `Sentry::Event` data as a log allows for the
55
+ event to be captured synchronously elsewhere and also gives the current Lambda more time to finish.
56
+ ```ruby
57
+ Sentry.init do |config|
58
+ # Put a log which will be caught by Trigger `?"SENTRY Event" ?"Task timed out after"`
59
+ config.async = lambda do |event, hint|
60
+ puts "SENTRY Event: #{event.to_json} Hint: #{hint.to_json}"
61
+ end
62
+ end
63
+ ```
64
+
65
+ Sending to Sentry Synchronously:
66
+ ```ruby
67
+ Sentry.init { |config| config.background_worker_threads = 0 } # all events will be sent synchronously
68
+ ```
@@ -0,0 +1,114 @@
1
+ module Sentry
2
+ module Lambda
3
+ class CaptureExceptions
4
+ TIMEOUT_WARNING_BUFFER = 1500 # Buffer time required to send timeout warning to Sentry
5
+
6
+ def initialize(aws_event:, aws_context:, capture_timeout_warning: false)
7
+ @aws_event = aws_event
8
+ @aws_context = aws_context
9
+ @capture_timeout_warning = capture_timeout_warning
10
+ end
11
+
12
+ def call(&block)
13
+ return yield unless Sentry.initialized?
14
+
15
+ if @capture_timeout_warning && (@aws_context.get_remaining_time_in_millis > TIMEOUT_WARNING_BUFFER)
16
+ Thread.new do
17
+ configured_timeout_seconds = @aws_context.get_remaining_time_in_millis / 1000.0
18
+ sleep_timeout_seconds = ((@aws_context.get_remaining_time_in_millis - TIMEOUT_WARNING_BUFFER) / 1000.0)
19
+
20
+ timeout_message = "WARNING : Function is expected to get timed out. "\
21
+ "Configured timeout duration = #{configured_timeout_seconds.round} seconds."
22
+
23
+ sleep(sleep_timeout_seconds)
24
+ Sentry.capture_message(timeout_message)
25
+ end
26
+ end
27
+
28
+ # make sure the current thread has a clean hub
29
+ Sentry.clone_hub_to_current_thread
30
+
31
+ Sentry.with_scope do |scope|
32
+ start_time = Time.now.utc
33
+ initial_remaining_time_in_milis = @aws_context.get_remaining_time_in_millis
34
+ execution_expiration_time = Time.now.utc + ((initial_remaining_time_in_milis || 0)/1000.0)
35
+
36
+ scope.clear_breadcrumbs
37
+ scope.set_transaction_name(@aws_context.function_name)
38
+
39
+ scope.add_event_processor do |event, hint|
40
+ event_time = event.timestamp.is_a?(Float) ? Time.at(event.timestamp) : Time.parse(event.timestamp)
41
+ remaining_time_in_millis = ((execution_expiration_time - event_time) * 1000).round
42
+ execution_duration_in_millis = ((event_time - start_time) * 1000).round
43
+ event.extra = event.extra.merge(
44
+ lambda: {
45
+ function_name: @aws_context.function_name,
46
+ function_version: @aws_context.function_version,
47
+ invoked_function_arn: @aws_context.invoked_function_arn,
48
+ aws_request_id: @aws_context.aws_request_id,
49
+ execution_duration_in_millis: execution_duration_in_millis,
50
+ remaining_time_in_millis: remaining_time_in_millis
51
+ }
52
+ )
53
+
54
+ event.extra = event.extra.merge(
55
+ "cloudwatch logs": {
56
+ url: _get_cloudwatch_logs_url(@aws_context, start_time),
57
+ log_group: @aws_context.log_group_name,
58
+ log_stream: @aws_context.log_stream_name
59
+ }
60
+ )
61
+
62
+ event
63
+ end
64
+
65
+ transaction = start_transaction(@aws_event, @aws_context, scope.transaction_name)
66
+ scope.set_span(transaction) if transaction
67
+
68
+ begin
69
+ response = yield
70
+ rescue Sentry::Error
71
+ finish_transaction(transaction, 500)
72
+ raise # Don't capture Sentry errors
73
+ rescue Exception => e
74
+ capture_exception(e)
75
+ finish_transaction(transaction, 500)
76
+ raise
77
+ end
78
+
79
+ status_code = response&.dig(:statusCode) || response&.dig('statusCode')
80
+ finish_transaction(transaction, status_code)
81
+
82
+ response
83
+ end
84
+ end
85
+
86
+ def start_transaction(event, context, transaction_name)
87
+ sentry_trace = event["HTTP_SENTRY_TRACE"]
88
+ options = { name: transaction_name, op: 'serverless.function' }
89
+ transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, **options) if sentry_trace
90
+ Sentry.start_transaction(transaction: transaction, **options)
91
+ end
92
+
93
+ def finish_transaction(transaction, status_code)
94
+ return unless transaction
95
+
96
+ transaction.set_http_status(status_code)
97
+ transaction.finish
98
+ end
99
+
100
+ def capture_exception(exception)
101
+ Sentry.capture_exception(exception)
102
+ end
103
+
104
+ def _get_cloudwatch_logs_url(aws_context, start_time)
105
+ formatstring = "%Y-%m-%dT%H:%M:%SZ"
106
+ region = ENV['AWS_REGION']
107
+
108
+ "https://console.aws.amazon.com/cloudwatch/home?region=#{region}" \
109
+ "#logEventViewer:group=#{aws_context.log_group_name};stream=#{aws_context.log_stream_name}" \
110
+ ";start=#{start_time.strftime(formatstring)};end=#{(Time.now.utc + 2).strftime(formatstring)}"
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,5 @@
1
+ module Sentry
2
+ module Lambda
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ require "sentry-ruby"
2
+ require "sentry/integrable"
3
+ require "sentry/lambda/capture_exceptions"
4
+
5
+ module Sentry
6
+ module Lambda
7
+ extend Integrable
8
+ register_integration name: 'lambda', version: Sentry::Lambda::VERSION
9
+
10
+ def self.wrap_handler(event:, context:, capture_timeout_warning: false)
11
+ CaptureExceptions.new(
12
+ aws_event: event,
13
+ aws_context: context,
14
+ capture_timeout_warning: capture_timeout_warning
15
+ ).call do
16
+ yield
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ require "sentry/lambda/version"
2
+ require "sentry/lambda"
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sentry-lambda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sentry Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sentry-ruby-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.4.0.pre.beta
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.4.0.pre.beta
27
+ description: A gem that provides AWS Lambda integration for the Sentry error logger
28
+ email: accounts@sentry.io
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE.txt
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/sentry-lambda.rb
38
+ - lib/sentry/lambda.rb
39
+ - lib/sentry/lambda/capture_exceptions.rb
40
+ - lib/sentry/lambda/version.rb
41
+ homepage: https://github.com/getsentry/sentry-ruby
42
+ licenses:
43
+ - Apache-2.0
44
+ metadata:
45
+ homepage_uri: https://github.com/getsentry/sentry-ruby
46
+ source_code_uri: https://github.com/getsentry/sentry-ruby
47
+ changelog_uri: https://github.com/getsentry/sentry-ruby/blob/master/sentry-lambda/CHANGELOG.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '2.4'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A gem that provides AWS Lambda integration for the Sentry error logger
67
+ test_files: []