appsignal_extensions 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ad075af5017ee836dccf6dd9f843e49f424244bc2bac82218e592c5bfa1a9e5
4
+ data.tar.gz: c2316f2602ffaaf98992b53e9c0e0b889825a1298e73616a804b307ac1d473d4
5
+ SHA512:
6
+ metadata.gz: 6ffab21dd7fc9262a8eda73fa0610ef855fd21efe242899760066e742b3fecf2f750bd6d09253e347df658d92b7c21bdae8e573cbfbc56ac531daafef5d4fb0b
7
+ data.tar.gz: b65d17ffc37ce7c6f4e5e7db5107f35da70dc6da8160273b9aca856f5e3bc1135c1c5f076c92df28f853ef6571edf4673a6df3834fae0ed39c1f7b4416bc4ccc
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ # rcov generated
2
+ coverage
3
+ coverage.data
4
+
5
+ # rdoc generated
6
+ rdoc
7
+
8
+ # yard generated
9
+ doc
10
+ .yardoc
11
+
12
+ # bundler
13
+ .bundle
14
+ Gemfile.lock
15
+
16
+ # jeweler generated
17
+ pkg
18
+
19
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
20
+ #
21
+ # * Create a file at ~/.gitignore
22
+ # * Include files you want ignored
23
+ # * Run: git config --global core.excludesfile ~/.gitignore
24
+ #
25
+ # After doing this, these files will be ignored in all your git projects,
26
+ # saving you from having to 'pollute' every project you touch with them
27
+ #
28
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
29
+ #
30
+ # For MacOS:
31
+ #
32
+ #.DS_Store
33
+
34
+ # For TextMate
35
+ #*.tmproj
36
+ #tmtags
37
+
38
+ # For emacs:
39
+ #*~
40
+ #\#*
41
+ #.\#*
42
+
43
+ # For vim:
44
+ #*.swp
45
+
46
+ # For redcar:
47
+ #.redcar
48
+
49
+ # For rubinius:
50
+ #*.rbc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 WeTransfer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # appsignal_extensions
2
+
3
+ When you want to do something more to Appsignal than the gem provides out of the box.
4
+
5
+ ## The Rack middleware
6
+
7
+ The gem provides a customized Appsignal middleware, which makes a number of extra things
8
+ possible. Simplest use is just like the standard Rack listener in Appsignal:
9
+
10
+ use AppsignalExtensions::Middleware
11
+
12
+ Just make sure Appsignal is configured and started at some point in the code. That means
13
+ that for information to come in `Appsignal.active?` should be true.
14
+
15
+ The transaction is going to be kept open as long as the iteraion over the Rack body
16
+ object continues. Therefore, you can use this middleware for long-polling too (long bodies
17
+ which `yield` chunked content and so on).
18
+
19
+ If you need more specific long response support, you can output a special header with your
20
+ response called 'appsignal.suspend'. If you set that header to a truthy value, the transaction
21
+ is not going to be closed for you. You can then close the transaction with your Thin `errback`
22
+ proc or similar, or close it explicitly in the `close` handler of your response body.
23
+
24
+ The transaction is also going to be placed in the `appsignal.transaction` Rack env variable,
25
+ and you can do things to the transaction manually. For instance:
26
+
27
+ ->(env) {
28
+ env['appsignal.transaction'].set_action('MyHandler#perform')
29
+ ...
30
+ }
31
+
32
+ If Appsignal is not enabled or not configured, the middleware is going to supply you with a
33
+ special `NullTransaction` object instead which responds to all the same methods as a real
34
+ `Appsignal::Transaction`, so that you can avoid redundant nil checks.
35
+
36
+ ## Contributing to appsignal_extensions
37
+
38
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
39
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
40
+ * Fork the project.
41
+ * Start a feature/bugfix branch.
42
+ * Commit and push until you are happy with your contribution.
43
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
44
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
45
+
46
+ ## Copyright
47
+
48
+ Copyright (c) 2016 WeTransfer. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ require 'bundler/gem_tasks'
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ task :default => :spec
13
+
14
+ require 'rdoc/task'
15
+ Rake::RDocTask.new do |rdoc|
16
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
17
+
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = "appsignal_extensions #{version}"
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'appsignal_extensions/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "appsignal_extensions"
7
+ s.version = AppsignalExtensions::VERSION
8
+
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.require_paths = ["lib"]
11
+ s.authors = ["Julik Tarkhanov"]
12
+ s.date = "2016-03-17"
13
+ s.description = "Doing some more with Appsignal"
14
+ s.email = "me@julik.nl"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ s.homepage = "https://gitlab.wetransfer.net/julik/appsignal_extensions"
23
+ s.licenses = ["MIT"]
24
+ s.rubygems_version = "2.2.2"
25
+ s.summary = "Suspend an Appsignal transaction for long responses"
26
+ s.specification_version = 4
27
+ s.add_runtime_dependency(%q<appsignal>, ["~> 1"])
28
+ s.add_development_dependency(%q<rake>, ["~> 10"])
29
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
30
+ s.add_development_dependency(%q<rspec>, ["~> 3.2.0"])
31
+ s.add_development_dependency(%q<rdoc>, ["~> 6"])
32
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
33
+ end
@@ -0,0 +1,6 @@
1
+ require 'appsignal'
2
+
3
+ module AppsignalExtensions
4
+ require_relative 'appsignal_extensions/version'
5
+ require_relative 'appsignal_extensions/middleware'
6
+ end
@@ -0,0 +1,139 @@
1
+ require 'delegate'
2
+
3
+ # Used to open an Appsignal transaction, but to let the callee close
4
+ # it when it is done. The standard Rack middleware for Appsignal
5
+ # closes the transaction as soon as the response triplet gets returned,
6
+ # we need to keep the transaction open as long as the response is being read.
7
+ class AppsignalExtensions::Middleware
8
+
9
+ def self.appsignal_defined_and_active?
10
+ !!(defined?(Appsignal) && Appsignal.active?)
11
+ end
12
+
13
+ # Appsignal::Transaction has no #close method, you have to use a global
14
+ # function call instead. We wrap it with a simple proxy that provides
15
+ # close
16
+ class Close < SimpleDelegator
17
+ # Closes the current Appsignal transaction
18
+ def close
19
+ if AppsignalExtensions::Middleware.appsignal_defined_and_active?
20
+ Appsignal::Transaction.complete_current!
21
+ end
22
+ end
23
+ end
24
+
25
+ # Acts as a null-object replacement for the Appsignal transaction if there is no
26
+ # transaction to provide (when Appsignal is not defined under jRuby or when
27
+ # Appsignal is not configured or disabled). Supports the basic method set
28
+ # that is important for us.
29
+ class NullTransaction
30
+ # @return [void]
31
+ def set_action(*);end
32
+
33
+ # @return [void]
34
+ def set_metadata(*);end
35
+
36
+ # @return [void]
37
+ def set_http_or_background_queue_start(*);end
38
+
39
+ # @return [void]
40
+ def set_error(*); end
41
+
42
+ # @return [void]
43
+ def close(*);end
44
+ end
45
+
46
+ # Acts as a wrapper for Rack response bodies. Ensures that the transaction attached to
47
+ # the request gets closed after the body #each returns or raises
48
+ class TransactionClosingBody
49
+ def initialize(body, transaction)
50
+ @body, @transaction = body, transaction
51
+ end
52
+
53
+ def each
54
+ @body.each{|b| yield(b) }
55
+ rescue Exception => e
56
+ @transaction.set_error(e)
57
+ raise e
58
+ ensure
59
+ @transaction.close
60
+ end
61
+
62
+ def close
63
+ @body.close if @body.respond_to?(:close)
64
+ end
65
+ end
66
+
67
+ # Creates a new Appsignal middleware handler with the given Rack app as a callee
68
+ #
69
+ # @param app[#call] the Rack app
70
+ def initialize(app)
71
+ @app = app
72
+ end
73
+
74
+ # Calls the application, captures errors, sets up wrappers and so forth
75
+ #
76
+ # @param env[Hash] the Rack env
77
+ # @return [Array] the Rack response triplet from upstream
78
+ def call(env)
79
+ request = ::Rack::Request.new(env)
80
+ env['action_dispatch.request_id'] ||= SecureRandom.uuid
81
+ if self.class.appsignal_defined_and_active?
82
+ call_with_appsignal(env, request)
83
+ else
84
+ call_with_null_transaction(env, request)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def call_and_capture(env, transaction, request)
91
+ env['appsignal.transaction'] = transaction
92
+ app_name = @app.is_a?(Module) ? @app.to_s : @app.class.to_s # Set the class name properly
93
+ transaction.set_action('%s#%s' % [app_name, 'call'])
94
+ transaction.set_metadata('path', request.path)
95
+ transaction.set_metadata('method', request.request_method)
96
+ transaction.set_http_or_background_queue_start
97
+ s, h, b = @app.call(env)
98
+
99
+ # If the app we called wants to close the transaction on it's own, return the response. This
100
+ # is useful if the app will clean up or close the transaction within an async.callback block,
101
+ # or within the long response body, or within a hijack proc.
102
+ return [s, h, b] if h.delete('appsignal.suspend')
103
+
104
+ # If the app didn't ask for the explicit suspend, Wrap the response in a self-closing wrapper
105
+ # so that the transaction is closed once the response is read in full. This wrapper only works
106
+ # with response bodies that support #each().
107
+ closing_wrapper = TransactionClosingBody.new(b, transaction)
108
+ [s, h, closing_wrapper]
109
+ rescue Exception => e
110
+ # If the raise happens immediately (not in the response read cycle)
111
+ # set the error and close the transaction so that the data gets sent
112
+ # to Appsignal right away, and ensure it gets closed
113
+ transaction.set_error(e)
114
+ transaction.close
115
+ raise e
116
+ end
117
+
118
+ def call_with_null_transaction(env, request)
119
+ # Supply the app with a null transaction
120
+ call_and_capture(env, NullTransaction.new, request)
121
+ end
122
+
123
+ def call_with_appsignal(env, request)
124
+ bare_transaction = Appsignal::Transaction.create(
125
+ env.fetch('action_dispatch.request_id'),
126
+ Appsignal::Transaction::HTTP_REQUEST,
127
+ request
128
+ )
129
+
130
+ # Let the app do something to the appsignal transaction if it wants to
131
+ # Instrument a `process_action`, to set params/action name
132
+ transaction = Close.new(bare_transaction)
133
+ status, headers, body = ActiveSupport::Notifications.instrument('process_action.rack') do
134
+ call_and_capture(env, transaction, request)
135
+ end
136
+
137
+ [status, headers, body]
138
+ end
139
+ end
@@ -0,0 +1,3 @@
1
+ module AppsignalExtensions
2
+ VERSION = '0.0.4'
3
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: appsignal_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Julik Tarkhanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appsignal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: Doing some more with Appsignal
98
+ email: me@julik.nl
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.md
104
+ files:
105
+ - ".document"
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - appsignal_extensions.gemspec
113
+ - lib/appsignal_extensions.rb
114
+ - lib/appsignal_extensions/middleware.rb
115
+ - lib/appsignal_extensions/version.rb
116
+ homepage: https://gitlab.wetransfer.net/julik/appsignal_extensions
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.7.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Suspend an Appsignal transaction for long responses
140
+ test_files: []