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

Class Method Details

.activate_rack!(datadog_config, sinatra_config) ⇒ Object

Apply relevant configuration from Sinatra to Rack



23
24
25
26
27
28
29
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 23

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



32
33
34
35
36
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 32

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



61
62
63
64
65
66
67
68
69
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 61

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



50
51
52
53
54
55
56
57
58
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 50

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



43
44
45
46
47
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 43

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



18
19
20
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 18

def self.config_with_defaults(datadog_config)
  datadog_config.tracing[:sinatra]
end

.include_middleware?(middleware, builder) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 94

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.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 81

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



107
108
109
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 107

def self.inspect_middlewares(builder)
  Datadog.logger.debug { 'Sinatra middlewares: ' << middlewares(builder).map(&:inspect).inspect }
end

.middlewares(builder) ⇒ Object

Introspect middlewares from a builder



99
100
101
102
103
104
105
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 99

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

.setupObject

Configure Rack from Sinatra, but only if Rack has not been configured manually beforehand



11
12
13
14
15
16
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 11

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)



74
75
76
# File 'lib/datadog/tracing/contrib/sinatra/framework.rb', line 74

def self.wrap_middleware(middleware, *args, &block)
  proc { |app| middleware.new(app, *args, &block) }
end