Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Rubocop for style guidelines #3

Merged
merged 5 commits into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 80 characters is a nice goal, but not worth currently changing in existing
# code for the sake of changing it to conform to a length set in 1928 (IBM).
Metrics/LineLength:
Max: 100

# These exceptions are good goals to attain, and probably will over time,
# so periodic disabling and re-running to inspect values is suggested.

Metrics/AbcSize:
Max: 40

# TODO: As refactors continue, this should drop. However, the goal of
# 10 lines in a method may be a little lofty.
Metrics/MethodLength:
Max: 36

# TODO: this is not compliant with the Ruby community style guide. We
# should enable again this rule but it will change the public API because
# we're using set_ methods. We should work on that because also Rails
# honors this rule.
Style/AccessorMethodName:
Enabled: false
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source 'https://rubygems.org'


gemspec
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.3.0)
minitest (5.9.1)
parser (2.3.1.4)
ast (~> 2.2)
powerpack (0.1.1)
rainbow (2.1.0)
rake (10.5.0)
rubocop (0.43.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are just dev deps right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Actually the tracer requires third-party libraries only for development: https://github.com/DataDog/dd-trace-rb/pull/3/files#diff-b06e29a984e208fededfd0c6e60ca9ecR35

When the GEM is created, these dependencies are not installed.

parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
unicode-display_width (1.1.1)

PLATFORMS
ruby
Expand All @@ -17,6 +30,7 @@ DEPENDENCIES
ddtrace!
minitest (~> 5.0)
rake (~> 10.0)
rubocop (~> 0.43)

BUNDLED WITH
1.12.5
9 changes: 7 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rubocop/rake_task'
require 'rake/testtask'

Rake::TestTask.new(:test) do |task|
task.libs << %w(test lib)
task.test_files = FileList['test/**/*_test.rb']
end

task :default => :test
RuboCop::RakeTask.new(:rubocop) do |t|
t.patterns = ['lib/**/*.rb', 'test/**/*.rb', 'Gemfile', 'Rakefile']
end

task default: :test
1 change: 1 addition & 0 deletions ddtrace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ EOS

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rubocop", "~> 0.43"
spec.add_development_dependency "minitest", "~> 5.0"
end
30 changes: 12 additions & 18 deletions examples/loop.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@

require "ddtrace/tracer"
require 'ddtrace/tracer'

# Generate a fake trace with the given tracer.
def trace(tracer)
urls = ["/home", "/login", "/logout"]
resource = urls.sample()
urls = ['/home', '/login', '/logout']
resource = urls.sample

# rake web request.
tracer.trace("web.request", :service=>"web", :resource=>resource) do
tracer.trace('web.request', service: 'web', resource: resource) do
sleep rand(0..0.1)

# fake query.
tracer.trace("db.query", :service=>"db") do
tracer.trace('db.query', service: 'db') do
sleep rand(0..0.1)
end

# fake template.
tracer.trace("web.template") do
tracer.trace('web.template') do
r = rand(0..1.0)
if r < 0.25
1/0
end
1 / 0 if r < 0.25
end
end
rescue ZeroDivisionError => e
puts "error #{e}"
puts "error #{e}"
end

# Generate fake traces.
def run()
tracer = Datadog::Tracer.new()
def run
tracer = Datadog::Tracer.new
loop do
trace(tracer)
sleep 0.0001
puts "traced #{tracer.writer.stats()}"
puts "traced #{tracer.writer.stats}"
end
end


if __FILE__ == $0
run()
end
run if __FILE__ == $PROGRAM_NAME
9 changes: 3 additions & 6 deletions lib/ddtrace/ext/errors.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

module Datadog

module Errors
MSG = "error.msg"
TYPE = "error.type"
STACK = "error.stack"
MSG = 'error.msg'.freeze
TYPE = 'error.type'.freeze
STACK = 'error.stack'.freeze
end

end
18 changes: 6 additions & 12 deletions lib/ddtrace/local.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@


require 'thread'

module Datadog

class SpanBuffer

# Set the current active span.
def set(span)
Thread.current[:datadog_span] = span
end

# Return the current active span or nil.
def get()
return Thread.current[:datadog_span]
def get
Thread.current[:datadog_span]
end

# Pop the current active span.
def pop()
s = self.get()
self.set(nil)
return s
def pop
s = get
set(nil)
s
end

end

end
94 changes: 40 additions & 54 deletions lib/ddtrace/span.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@

require 'json'
require 'time'



module Datadog

class Span

attr_accessor :name, :service, :resource,
:start_time, :end_time,
:span_id, :trace_id, :parent_id,
:meta, :status, :parent

# Create a new span linked to the given tracer.
def initialize(tracer, name, options={})
def initialize(tracer, name, options = {})
@tracer = tracer

@name = name
@service = options[:service]
@resource = options[:resource] || name

@span_id = Datadog::next_id()
@span_id = Datadog.next_id
@parent_id = options[:parent_id] || 0
@trace_id = options[:trace_id] || @span_id

Expand All @@ -34,104 +29,95 @@ def initialize(tracer, name, options={})
@end_time = nil
end

def trace()
begin
if block_given?
yield(self)
end
rescue StandardError => e
self.set_error(e)
raise
ensure
self.finish()
end
def trace
yield(self) if block_given?
rescue StandardError => e
set_error(e)
raise
ensure
finish
end

def set_tag(key, value)
return @meta[key] = value
@meta[key] = value
end

# Return the tag wth the given key, nil if it doesn't exist.
def get_tag(key)
return @meta[key]
@meta[key]
end

# Mark the span with the given error.
def set_error(e)
if e != nil
unless e.nil?
@status = 1
@meta["error.msg"] = e.message
@meta["error.type"] = e.class.to_s
@meta["error.stack"] = e.backtrace.join("\n")
@meta['error.msg'] = e.message
@meta['error.type'] = e.class.to_s
@meta['error.stack'] = e.backtrace.join("\n")
end
end

# Mark the span finished at the current time and submit it.
def finish()
return self.finish_at(Time.now.utc)
def finish
finish_at(Time.now.utc)
end

# Mark the span finished at the given time and submit it.
def finish_at(end_time)
@end_time = end_time

if !@tracer.nil?
@tracer.record(self)
end
@tracer.record(self) unless @tracer.nil?

return self
self
end

# Return a string representation of the span.
def to_s()
return "Span(name:#{@name},sid:#{@span_id},tid:#{@trace_id},pid:#{@parent_id})"
def to_s
"Span(name:#{@name},sid:#{@span_id},tid:#{@trace_id},pid:#{@parent_id})"
end

# Set this span's parent, inheriting any properties not explicitly set.
def set_parent(parent)
@parent = parent
if parent != nil
unless parent.nil?
@trace_id = parent.trace_id
@parent_id = parent.span_id
@service = @service || parent.service
@service ||= parent.service
end
end

def to_hash()
def to_hash
h = {
:span_id => @span_id,
:parent_id => @parent_id,
:trace_id => @trace_id,
:name => @name,
:service => @service,
:resource => @resource,
:type => "FIXME",
:meta => @meta,
:error => @status,
span_id: @span_id,
parent_id: @parent_id,
trace_id: @trace_id,
name: @name,
service: @service,
resource: @resource,
type: 'FIXME',
meta: @meta,
error: @status
}

if @start_time != nil && @end_time != nil
if !@start_time.nil? && !@end_time.nil?
h[:start] = (@start_time.to_f * 1e9).to_i
h[:duration] = ((@end_time - @start_time) * 1e9).to_i
end

return h
h
end

end

@@max_id = 2**64-1
@@max_id = 2**64 - 1

# Return a span id.
def self.next_id()
return rand(@@max_id)
def self.next_id
rand(@@max_id)
end

# Encode the given set of spans.
def self.encode_spans(spans)
hashes = spans.map{|s| s.to_hash()}
return JSON.dump(hashes)
hashes = spans.map(&:to_hash)
JSON.dump(hashes)
end


end
Loading