Skip to content

Commit f94e8af

Browse files
delnerivoanjo
authored andcommitted
Added: bin/ddtracerb with exec command.
1 parent 8021b91 commit f94e8af

File tree

8 files changed

+155
-2
lines changed

8 files changed

+155
-2
lines changed

bin/ddtracerb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
require 'ddtrace/tasks/exec'
3+
require 'ddtrace/tasks/help'
4+
5+
command = ARGV.shift
6+
7+
case command
8+
when 'exec'
9+
Datadog::Tasks::Exec.new(ARGV).run
10+
when 'help', '--help'
11+
Datadog::Tasks::Help.new.run
12+
else
13+
puts "Command '#{command}' is not valid for ddtrace."
14+
Datadog::Tasks::Help.new.run
15+
end

ddtrace.gemspec

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ Gem::Specification.new do |spec|
2929
end
3030

3131
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32-
spec.bindir = 'exe'
33-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32+
spec.executables = ['ddtracerb']
3433
spec.require_paths = ['lib']
3534

3635
if RUBY_VERSION >= '2.2.0'

lib/ddtrace/profiling/preload.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'ddtrace/profiling/tasks/setup'
2+
Datadog::Profiling::Tasks::Setup.new.execute

lib/ddtrace/profiling/tasks/setup.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Datadog
2+
module Profiling
3+
module Tasks
4+
class Setup
5+
def execute
6+
# TODO
7+
end
8+
end
9+
end
10+
end
11+
end

lib/ddtrace/tasks/exec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Datadog
2+
module Tasks
3+
# Wraps command with Datadog tracing
4+
class Exec
5+
attr_reader :args
6+
7+
def initialize(args)
8+
@args = args
9+
end
10+
11+
def run
12+
set_rubyopt!
13+
Kernel.exec(*args)
14+
end
15+
16+
def rubyopts
17+
[
18+
'-rddtrace/profiling/preload'
19+
]
20+
end
21+
22+
private
23+
24+
def set_rubyopt!
25+
if ENV.key?('RUBYOPT')
26+
ENV['RUBYOPT'] += " #{rubyopts.join(' ')}"
27+
else
28+
ENV['RUBYOPT'] = rubyopts.join(' ')
29+
end
30+
end
31+
end
32+
end
33+
end

lib/ddtrace/tasks/help.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Datadog
2+
module Tasks
3+
# Prints help message for usage of `ddtrace`
4+
class Help
5+
def run
6+
puts %(
7+
Usage: ddtrace [command] [arguments]
8+
exec [command]: Executes command with tracing & profiling preloaded.
9+
help: Prints this help message.
10+
)
11+
end
12+
end
13+
end
14+
end

spec/ddtrace/tasks/exec_spec.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'spec_helper'
2+
require 'ddtrace/tasks/exec'
3+
4+
RSpec.describe Datadog::Tasks::Exec do
5+
subject(:task) { described_class.new(args) }
6+
let(:args) { ['ruby', '-e', '"RUBY_VERSION"'] }
7+
8+
describe '::new' do
9+
it { is_expected.to have_attributes(args: args) }
10+
end
11+
12+
describe '#run' do
13+
subject(:run) { task.run }
14+
let(:result) { double('result') }
15+
16+
around do |example|
17+
# Make sure RUBYOPT is returned to its original state.
18+
original_opts = ENV['RUBYOPT']
19+
example.run
20+
ENV['RUBYOPT'] = original_opts
21+
end
22+
23+
before do
24+
# Must stub the call out or test will prematurely terminate.
25+
expect(Kernel).to receive(:exec)
26+
.with(*args)
27+
.and_return(result)
28+
end
29+
30+
context 'when RUBOPT is not defined' do
31+
it 'runs the task with preloads' do
32+
is_expected.to be(result)
33+
34+
# Expect preloading to have been attached
35+
task.rubyopts.each do |opt|
36+
expect(ENV['RUBYOPT']).to include(opt)
37+
end
38+
end
39+
end
40+
41+
context 'when RUBYOPT is defined' do
42+
before { ENV['RUBYOPT'] = start_opts }
43+
let(:start_opts) { 'start_opts' }
44+
45+
it 'runs the task with additional preloads' do
46+
is_expected.to be(result)
47+
48+
# Expect original RUBYOPT to have been preserved
49+
expect(ENV['RUBYOPT']).to include(start_opts)
50+
51+
# Expect preloading to have been attached
52+
task.rubyopts.each do |opt|
53+
expect(ENV['RUBYOPT']).to include(opt)
54+
end
55+
end
56+
end
57+
end
58+
59+
describe '#rubyopts' do
60+
subject(:rubyopts) { task.rubyopts }
61+
it { is_expected.to eq(['-rddtrace/profiling/preload']) }
62+
end
63+
end

spec/ddtrace/tasks/help_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
require 'ddtrace/tasks/help'
3+
4+
RSpec.describe Datadog::Tasks::Help do
5+
subject(:task) { described_class.new }
6+
7+
describe '#run' do
8+
it 'prints a help message to STDOUT' do
9+
expect(STDOUT).to receive(:puts) do |message|
10+
expect(message).to include('Usage: ddtrace')
11+
end
12+
13+
task.run
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)