Module: Datadog::Tracing::Contrib::Rails::Framework

Defined in:
lib/datadog/tracing/contrib/rails/framework.rb

Overview

Rails 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_action_cable!(datadog_config, rails_config) ⇒ Object



77
78
79
80
81
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 77

def self.activate_action_cable!(datadog_config, rails_config)
  return unless defined?(::ActionCable)

  datadog_config.tracing.instrument(:action_cable)
end

.activate_action_mailer!(datadog_config, rails_config) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 83

def self.activate_action_mailer!(datadog_config, rails_config)
  return unless defined?(::ActionMailer)

  datadog_config.tracing.instrument(
    :action_mailer,
    service_name: rails_config[:service_name]
  )
end

.activate_action_pack!(datadog_config, rails_config) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 92

def self.activate_action_pack!(datadog_config, rails_config)
  return unless defined?(::ActionPack)

  datadog_config.tracing.instrument(
    :action_pack,
    service_name: rails_config[:service_name]
  )
end

.activate_action_view!(datadog_config, rails_config) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 101

def self.activate_action_view!(datadog_config, rails_config)
  return unless defined?(::ActionView)

  datadog_config.tracing.instrument(
    :action_view,
    service_name: rails_config[:service_name]
  )
end

.activate_active_job!(datadog_config, rails_config) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 110

def self.activate_active_job!(datadog_config, rails_config)
  return unless defined?(::ActiveJob)

  datadog_config.tracing.instrument(
    :active_job,
    service_name: rails_config[:service_name]
  )
end

.activate_active_record!(datadog_config, rails_config) ⇒ Object



119
120
121
122
123
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 119

def self.activate_active_record!(datadog_config, rails_config)
  return unless defined?(::ActiveRecord)

  datadog_config.tracing.instrument(:active_record)
end

.activate_active_support!(datadog_config, rails_config) ⇒ Object



71
72
73
74
75
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 71

def self.activate_active_support!(datadog_config, rails_config)
  return unless defined?(::ActiveSupport)

  datadog_config.tracing.instrument(:active_support)
end

.activate_lograge!(datadog_config, rails_config) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 125

def self.activate_lograge!(datadog_config, rails_config)
  return unless defined?(::Lograge)

  if datadog_config.tracing.log_injection
    datadog_config.tracing.instrument(
      :lograge
    )
  end
end

.activate_rack!(datadog_config, rails_config) ⇒ Object



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

def self.activate_rack!(datadog_config, rails_config)
  datadog_config.tracing.instrument(
    :rack,
    application: ::Rails.application,
    service_name: rails_config[:service_name],
    middleware_names: rails_config[:middleware_names],
    distributed_tracing: rails_config[:distributed_tracing],
    request_queuing: rails_config[:request_queuing]
  )
end

.activate_semantic_logger!(datadog_config, rails_config) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 135

def self.activate_semantic_logger!(datadog_config, rails_config)
  return unless defined?(::SemanticLogger)

  if datadog_config.tracing.log_injection
    datadog_config.tracing.instrument(
      :semantic_logger
    )
  end
end

.setupObject

After the Rails application finishes initializing, we configure the Rails integration and all its sub-components with the application information available. We do this after the initialization because not all the information we require is available before then.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/tracing/contrib/rails/framework.rb', line 29

def self.setup
  # NOTE: #configure has the side effect of rebuilding trace components.
  #       During a typical Rails application lifecycle, we will see trace
  #       components initialized twice because of this. This is necessary
  #       because key configuration is not available until after the Rails
  #       application has fully loaded, and some of this configuration is
  #       used to reconfigure tracer components with Rails-sourced defaults.
  #       This is a trade-off we take to get nice defaults.
  Datadog.configure do |datadog_config|
    rails_config = datadog_config.tracing[:rails]

    # By default, default service would be guessed from the script
    # being executed, but here we know better, get it from Rails config.
    # Don't set this if service has been explicitly provided by the user.
    if datadog_config.service_without_fallback.nil?
      datadog_config.service = rails_config[:service_name] || Utils.app_name
    end

    activate_rack!(datadog_config, rails_config)
    activate_action_cable!(datadog_config, rails_config)
    activate_action_mailer!(datadog_config, rails_config)
    activate_active_support!(datadog_config, rails_config)
    activate_action_pack!(datadog_config, rails_config)
    activate_action_view!(datadog_config, rails_config)
    activate_active_job!(datadog_config, rails_config)
    activate_active_record!(datadog_config, rails_config)
    activate_lograge!(datadog_config, rails_config)
    activate_semantic_logger!(datadog_config, rails_config)
  end
end