Module: Datadog::Contrib::Rails::LogInjection
- Defined in:
- lib/ddtrace/contrib/rails/log_injection.rb
Overview
Rails log injection helper methods
Class Method Summary collapse
- .add_as_tagged_logging_logger(app) ⇒ Object
- .add_lograge_logger(app) ⇒ Object
- .datadog_trace_log_hash(correlation) ⇒ Object
Class Method Details
.add_as_tagged_logging_logger(app) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ddtrace/contrib/rails/log_injection.rb', line 49 def add_as_tagged_logging_logger(app) # we want to check if the current logger is a tagger logger instance # log_tags defaults to nil so we have to set as an array if nothing exists yet if ( = app.config.).nil? app.config. = [proc { Datadog.tracer.active_correlation.to_s }] # if existing log_tags configuration exists, append to the end of the array elsif .is_a?(Array) app.config. << proc { Datadog.tracer.active_correlation.to_s } end rescue StandardError => e # TODO: can we use Datadog.logger at this point? Datadog.logger.warn("Unable to add Datadog Trace context to ActiveSupport::TaggedLogging: #{e.}") false end |
.add_lograge_logger(app) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ddtrace/contrib/rails/log_injection.rb', line 9 def add_lograge_logger(app) # custom_options defaults to nil and can be either a hash or a lambda which returns a hash # https://github.com/roidrage/lograge/blob/1729eab7956bb95c5992e4adab251e4f93ff9280/lib/lograge.rb#L28 if ( = app.config.lograge.).nil? # if it's not set, we set to a lambda that returns DD tracing context app.config.lograge. = lambda do |_event| # Retrieves trace information for current thread correlation = Datadog.tracer.active_correlation datadog_trace_log_hash(correlation) end # check if lambda, if so then define a new lambda which invokes the original lambda and # merges the returned hash with the the DD tracing context hash. elsif .respond_to?(:call) app.config.lograge. = lambda do |event| # invoke original lambda result = .call(event) # Retrieves trace information for current thread correlation = Datadog.tracer.active_correlation # merge original lambda with datadog context result.merge(datadog_trace_log_hash(correlation)) end # otherwise if it's just a static hash, we have to wrap that hash in a lambda to retrieve # the DD tracing context, then merge the tracing context with the original static hash. # don't modify if custom_options is not an accepted format. elsif .is_a?(Hash) app.config.lograge. = lambda do |_event| # Retrieves trace information for current thread correlation = Datadog.tracer.active_correlation # merge original lambda with datadog context .merge(datadog_trace_log_hash(correlation)) end end rescue StandardError => e # TODO: can we use Datadog.logger at this point? Datadog.logger.warn("Unable to add Datadog Trace context to Lograge: #{e.}") false end |
.datadog_trace_log_hash(correlation) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ddtrace/contrib/rails/log_injection.rb', line 64 def datadog_trace_log_hash(correlation) { # Adds IDs as tags to log output dd: { # To preserve precision during JSON serialization, use strings for large numbers trace_id: correlation.trace_id.to_s, span_id: correlation.span_id.to_s, env: correlation.env.to_s, service: correlation.service.to_s, version: correlation.version.to_s }, ddsource: ['ruby'] } end |