Module: Datadog::Tracing::Contrib::Rails::Runner

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

Overview

Instruments the ‘bin/rails runner` command. This command executes the provided code with the host Rails application loaded. The command can be either:

  • ‘-`: for code provided through the STDIN.

  • File path: for code provided through a local file.

  • ‘inline code`: for code provided directly as a command line argument.

Instance Method Summary collapse

Instance Method Details

#eval(*args) ⇒ Object

Capture the executed source code when provided from STDIN.



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

def eval(*args)
  span = Datadog::Tracing.active_span
  if span&.name == Ext::SPAN_RUNNER_STDIN
    source = args[0]
    span.set_tag(
      Ext::TAG_RUNNER_SOURCE,
      Core::Utils.truncate(source, MAX_TAG_VALUE_SIZE)
    )
  end

  super
end

#runner(code_or_file = nil, *_command_argv) ⇒ Object



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
48
49
50
51
52
53
54
55
56
# File 'lib/datadog/tracing/contrib/rails/runner.rb', line 19

def runner(code_or_file = nil, *_command_argv)
  if code_or_file == '-'
    name = Ext::SPAN_RUNNER_STDIN
    resource = nil
    operation = Ext::TAG_OPERATION_STDIN
    # The source is not yet available for STDIN, but it will be captured in `eval`.
  elsif File.exist?(code_or_file)
    name = Ext::SPAN_RUNNER_FILE
    resource = code_or_file
    operation = Ext::TAG_OPERATION_FILE
    source = File.read(code_or_file)
  else
    name = Ext::SPAN_RUNNER_INLINE
    resource = nil
    operation = Ext::TAG_OPERATION_INLINE
    source = code_or_file
  end

  Tracing.trace(
    name,
    service: Datadog.configuration.tracing[:rails][:service_name],
    resource: resource,
    tags: {
      Tracing::Metadata::Ext::TAG_COMPONENT => Ext::TAG_COMPONENT,
      Tracing::Metadata::Ext::TAG_OPERATION => operation,
    }
  ) do |span|
    if source
      span.set_tag(
        Ext::TAG_RUNNER_SOURCE,
        Core::Utils.truncate(source, MAX_TAG_VALUE_SIZE)
      )
    end
    Contrib::Analytics.set_rate!(span, Datadog.configuration.tracing[:rails])

    super
  end
end