Module: Datadog::Tracing::Contrib::Sinatra::Framework
- Defined in:
- lib/datadog/tracing/contrib/sinatra/framework.rb
Overview
Sinatra framework code, used to essentially:
-
handle configuration entries which are specific to Datadog tracing
-
instrument parts of the framework when needed
Class Method Summary collapse
-
.activate_rack!(datadog_config, sinatra_config) ⇒ Object
Apply relevant configuration from Sinatra to Rack.
-
.add_middleware(middleware, builder, *args, &block) ⇒ Object
(also: unshift_middleare)
Add Rack middleware at the top of the stack.
-
.add_middleware_after(after, middleware, builder, *args, &block) ⇒ Object
Add Rack middleware after another in the the stack.
-
.add_middleware_before(before, middleware, builder, *args, &block) ⇒ Object
Add Rack middleware before another in the stack.
-
.append_middleware(middleware, builder, *args, &block) ⇒ Object
Add Rack middleware at the top of the stack.
- .config_with_defaults(datadog_config) ⇒ Object
- .include_middleware?(middleware, builder) ⇒ Boolean
-
.insert_middleware(builder, middleware, args, block) ⇒ Object
Insert a middleware class in the builder as it expects it internally.
- .inspect_middlewares(builder) ⇒ Object
-
.middlewares(builder) ⇒ Object
Introspect middlewares from a builder.
-
.setup ⇒ Object
Configure Rack from Sinatra, but only if Rack has not been configured manually beforehand.
-
.wrap_middleware(middleware, *args, &block) ⇒ Object
Wrap the middleware class instantiation in a proc, like Sinatra does internally The ‘middleware` local variable name in the proc is important for introspection (see Framework#middlewares).
Class Method Details
.activate_rack!(datadog_config, sinatra_config) ⇒ Object
Apply relevant configuration from Sinatra to Rack
25 26 27 28 29 30 31 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 25 def self.activate_rack!(datadog_config, sinatra_config) datadog_config.tracing.instrument( :rack, service_name: sinatra_config[:service_name], distributed_tracing: sinatra_config[:distributed_tracing], ) end |
.add_middleware(middleware, builder, *args, &block) ⇒ Object Also known as: unshift_middleare
Add Rack middleware at the top of the stack
34 35 36 37 38 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 34 def self.add_middleware(middleware, builder, *args, &block) insert_middleware(builder, middleware, args, block) do |proc_, use| use.insert(0, proc_) end end |
.add_middleware_after(after, middleware, builder, *args, &block) ⇒ Object
Add Rack middleware after another in the the stack
63 64 65 66 67 68 69 70 71 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 63 def self.add_middleware_after(after, middleware, builder, *args, &block) index = middlewares(builder).index(after) raise "middleware #{after} not found" unless index insert_middleware(builder, middleware, args, block) do |proc_, use| use.insert(index + 1, proc_) end end |
.add_middleware_before(before, middleware, builder, *args, &block) ⇒ Object
Add Rack middleware before another in the stack
52 53 54 55 56 57 58 59 60 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 52 def self.add_middleware_before(before, middleware, builder, *args, &block) index = middlewares(builder).index(before) raise "middleware #{before} not found" unless index insert_middleware(builder, middleware, args, block) do |proc_, use| use.insert(index, proc_) end end |
.append_middleware(middleware, builder, *args, &block) ⇒ Object
Add Rack middleware at the top of the stack
45 46 47 48 49 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 45 def self.append_middleware(middleware, builder, *args, &block) insert_middleware(builder, middleware, args, block) do |proc_, use| use.append(proc_) end end |
.config_with_defaults(datadog_config) ⇒ Object
20 21 22 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 20 def self.config_with_defaults(datadog_config) datadog_config.tracing[:sinatra] end |
.include_middleware?(middleware, builder) ⇒ Boolean
96 97 98 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 96 def self.include_middleware?(middleware, builder) middlewares(builder).include?(middleware) end |
.insert_middleware(builder, middleware, args, block) ⇒ Object
Insert a middleware class in the builder as it expects it internally. The block gets passed prepared arguments for the caller to decide how to insert.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 83 def self.insert_middleware(builder, middleware, args, block) use = builder.instance_variable_get('@use') wrapped = wrap_middleware(middleware, *args, &block) # Makes the insert idempotent # The block can also throw :skip with its own logic catch(:skip) do throw(:skip) if middlewares(builder).include?(middleware) yield(wrapped, use) end end |
.inspect_middlewares(builder) ⇒ Object
109 110 111 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 109 def self.inspect_middlewares(builder) Datadog.logger.debug { +'Sinatra middlewares: ' << middlewares(builder).map(&:inspect).inspect } end |
.middlewares(builder) ⇒ Object
Introspect middlewares from a builder
101 102 103 104 105 106 107 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 101 def self.middlewares(builder) builder.instance_variable_get(:@use).map do |proc_| next :unknown unless proc_.respond_to?(:binding) && proc_.binding.local_variable_defined?(:middleware) proc_.binding.local_variable_get(:middleware) end end |
.setup ⇒ Object
Configure Rack from Sinatra, but only if Rack has not been configured manually beforehand
13 14 15 16 17 18 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 13 def self.setup Datadog.configure do |datadog_config| sinatra_config = config_with_defaults(datadog_config) activate_rack!(datadog_config, sinatra_config) end end |
.wrap_middleware(middleware, *args, &block) ⇒ Object
Wrap the middleware class instantiation in a proc, like Sinatra does internally The ‘middleware` local variable name in the proc is important for introspection (see Framework#middlewares)
76 77 78 |
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 76 def self.wrap_middleware(middleware, *args, &block) proc { |app| middleware.new(app, *args, &block) } end |