rack-honeycomb 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack-honeycomb.rb +7 -0
- data/lib/rack-honeycomb/auto_install.rb +53 -0
- data/lib/rack/honeycomb/middleware.rb +40 -4
- data/lib/rack/honeycomb/version.rb +1 -1
- data/rack-honeycomb.gemspec +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46fc6f3a8fbb4c2bb926c92bcd101313e3ec79795735664c851e230e55579bea
|
4
|
+
data.tar.gz: ed408d7ce0baf8aa98eeff6ae6461527692199a1c7685739acf794e36f42d122
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa105613b9fcd20e15bee4e52c17e1917bce0cd5220cc95c4966f3db51b1b86529296d6647209c4be5f9a8f92148c5f386e5003119b3f2507558cc771a3c4dbb
|
7
|
+
data.tar.gz: ed4bc78029b0e442ea125284791ed92e806deaa1018630481a341aa0fc359a64dd486824c1f8904261a03abca89ec64d75948e427a462efac899dac7cdb2d548
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rack
|
2
|
+
module Honeycomb
|
3
|
+
# @api private
|
4
|
+
module AutoInstall
|
5
|
+
class << self
|
6
|
+
def available?(logger: nil)
|
7
|
+
gem 'rack'
|
8
|
+
gem 'sinatra'
|
9
|
+
true
|
10
|
+
rescue Gem::LoadError => e
|
11
|
+
if e.name == 'sinatra'
|
12
|
+
logger.debug "Couldn't detect web framework, not autoinitialising rack-honeycomb" if logger
|
13
|
+
end
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def auto_install!(honeycomb_client:, logger: nil)
|
18
|
+
require 'rack'
|
19
|
+
require 'sinatra/base'
|
20
|
+
|
21
|
+
require 'rack-honeycomb'
|
22
|
+
|
23
|
+
class << ::Sinatra::Base
|
24
|
+
alias build_without_honeycomb build
|
25
|
+
end
|
26
|
+
|
27
|
+
::Sinatra::Base.define_singleton_method(:build) do |*args, &block|
|
28
|
+
if !AutoInstall.already_added
|
29
|
+
self.use Rack::Honeycomb::Middleware, client: honeycomb_client
|
30
|
+
AutoInstall.already_added = true
|
31
|
+
else
|
32
|
+
# In the case of nested Sinatra apps - apps composed of other apps
|
33
|
+
# (in addition to just handlers and middleware) - our .build hook
|
34
|
+
# above will fire multiple times, for the parent app and also for
|
35
|
+
# each child app. In that case, it's hard to hook in our
|
36
|
+
# middleware reliably - so instead, we just want to warn the user
|
37
|
+
# and avoid doing anything silly.
|
38
|
+
|
39
|
+
unless AutoInstall.already_warned
|
40
|
+
warn "Honeycomb auto-instrumentation of Sinatra will probably not work, try manual installation"
|
41
|
+
AutoInstall.already_warned = true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
build_without_honeycomb(*args, &block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
attr_accessor :already_added
|
49
|
+
attr_accessor :already_warned
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "libhoney"
|
2
|
+
|
2
3
|
require "rack/honeycomb/version"
|
3
4
|
|
4
5
|
module Rack
|
@@ -23,7 +24,15 @@ module Rack
|
|
23
24
|
def initialize(app, options = {})
|
24
25
|
@app, @options = app, options
|
25
26
|
|
26
|
-
@honeycomb =
|
27
|
+
@honeycomb = if client = options.delete(:client)
|
28
|
+
client
|
29
|
+
elsif defined?(::Honeycomb.client)
|
30
|
+
::Honeycomb.client
|
31
|
+
else
|
32
|
+
Libhoney::Client.new(options.merge(user_agent_addition: USER_AGENT_SUFFIX))
|
33
|
+
end
|
34
|
+
|
35
|
+
@service_name = options.delete(:service_name) || :rack
|
27
36
|
end
|
28
37
|
|
29
38
|
def add_field(ev, field, value)
|
@@ -37,7 +46,9 @@ module Rack
|
|
37
46
|
def call(env)
|
38
47
|
ev = @honeycomb.event
|
39
48
|
request_started_at = Time.now
|
40
|
-
status, headers, response =
|
49
|
+
status, headers, response = adding_span_metadata_if_available(ev, env) do
|
50
|
+
@app.call(env)
|
51
|
+
end
|
41
52
|
request_ended_at = Time.now
|
42
53
|
|
43
54
|
ev.add(headers)
|
@@ -46,7 +57,7 @@ module Rack
|
|
46
57
|
ev.add_field('Content-Length', headers['Content-Length'].to_i)
|
47
58
|
end
|
48
59
|
add_field(ev, 'HTTP_STATUS', status)
|
49
|
-
add_field(ev, '
|
60
|
+
add_field(ev, 'durationMs', (request_ended_at - request_started_at) * 1000)
|
50
61
|
|
51
62
|
# Pull arbitrary metadata off `env` if the caller attached anything
|
52
63
|
# inside the Rack handler.
|
@@ -80,9 +91,34 @@ module Rack
|
|
80
91
|
add_env(ev, env, 'HTTP_ACCEPT')
|
81
92
|
add_env(ev, env, 'HTTP_ACCEPT_LANGUAGE')
|
82
93
|
add_env(ev, env, 'REMOTE_ADDR')
|
83
|
-
ev.send
|
84
94
|
|
85
95
|
[status, headers, response]
|
96
|
+
rescue Exception => e
|
97
|
+
if ev
|
98
|
+
ev.add_field('exception_class', e.class.name)
|
99
|
+
ev.add_field('exception_message', e.message)
|
100
|
+
end
|
101
|
+
raise
|
102
|
+
ensure
|
103
|
+
if ev
|
104
|
+
ev.send
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
def adding_span_metadata_if_available(event, env)
|
110
|
+
return yield unless defined?(::Honeycomb.with_trace_id)
|
111
|
+
|
112
|
+
::Honeycomb.with_trace_id do |trace_id|
|
113
|
+
event.add_field :traceId, trace_id
|
114
|
+
event.add_field :serviceName, @service_name
|
115
|
+
event.add_field :name, "#{env['REQUEST_METHOD']} #{env['REQUEST_PATH']}"
|
116
|
+
span_id = trace_id # so this shows up as a root span
|
117
|
+
event.add_field :id, span_id
|
118
|
+
::Honeycomb.with_span_id(span_id) do
|
119
|
+
yield
|
120
|
+
end
|
121
|
+
end
|
86
122
|
end
|
87
123
|
end
|
88
124
|
end
|
data/rack-honeycomb.gemspec
CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_development_dependency "webmock", "~> 2.1"
|
35
35
|
spec.add_development_dependency "minitest", "~> 5.0"
|
36
36
|
spec.add_development_dependency "yardstick", "~> 0.9"
|
37
|
-
spec.
|
37
|
+
spec.add_development_dependency 'rack', '>= 1.0.0'
|
38
38
|
spec.add_runtime_dependency 'libhoney', '>= 1.5.0'
|
39
39
|
spec.add_development_dependency 'rack-test'
|
40
40
|
spec.add_development_dependency 'yard'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-honeycomb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- The Honeycomb.io Team
|
@@ -101,7 +101,7 @@ dependencies:
|
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: 1.0.0
|
104
|
-
type: :
|
104
|
+
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
@@ -162,6 +162,8 @@ files:
|
|
162
162
|
- Gemfile
|
163
163
|
- LICENSE
|
164
164
|
- README.md
|
165
|
+
- lib/rack-honeycomb.rb
|
166
|
+
- lib/rack-honeycomb/auto_install.rb
|
165
167
|
- lib/rack/honeycomb.rb
|
166
168
|
- lib/rack/honeycomb/middleware.rb
|
167
169
|
- lib/rack/honeycomb/version.rb
|