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

[rails] fix non-finishing span problem #48

Merged
merged 2 commits into from
Jan 3, 2017
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
43 changes: 25 additions & 18 deletions lib/ddtrace/contrib/rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,33 @@ def self.start_processing(*)
def self.process_action(_name, start, finish, _id, payload)
tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
span = tracer.active_span()
span.resource = "#{payload.fetch(:controller)}##{payload.fetch(:action)}"
span.set_tag(Datadog::Ext::HTTP::URL, payload.fetch(:path))
span.set_tag(Datadog::Ext::HTTP::METHOD, payload.fetch(:method))
span.set_tag('rails.route.action', payload.fetch(:action))
span.set_tag('rails.route.controller', payload.fetch(:controller))
return unless span

if payload[:exception].nil?
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, payload.fetch(:status).to_s)
else
error = payload[:exception]
# TODO[manu]: it's right to have a 500? there are cases in Rails that let
# user to recover the error after this point?
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, '500')
end
begin
span.resource = "#{payload.fetch(:controller)}##{payload.fetch(:action)}"
span.set_tag(Datadog::Ext::HTTP::URL, payload.fetch(:path))
span.set_tag(Datadog::Ext::HTTP::METHOD, payload.fetch(:method))
span.set_tag('rails.route.action', payload.fetch(:action))
span.set_tag('rails.route.controller', payload.fetch(:controller))

if payload[:exception].nil?
# [christian] in some cases :status is not defined,
# rather than firing an error, simply acknowledge we don't know it.
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, payload.fetch(:status, '?').to_s)
else
error = payload[:exception]
# TODO[manu]: it's right to have a 500? there are cases in Rails that let
# user to recover the error after this point?
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, '500')
end

span.start_time = start
span.finish_at(finish)
ensure
span.start_time = start
span.finish_at(finish)
end
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
end
Expand Down
52 changes: 31 additions & 21 deletions lib/ddtrace/contrib/rails/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,24 @@ def self.render_template(_name, start, finish, _id, payload)
# finish the tracing and update the execution time
tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
span = tracer.active_span()
template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
span.set_tag('rails.template_name', template_name)
span.set_tag('rails.layout', payload.fetch(:layout))
return unless span

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end
begin
template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
span.set_tag('rails.template_name', template_name)
span.set_tag('rails.layout', payload.fetch(:layout))

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end

span.start_time = start
span.finish_at(finish)
ensure
span.start_time = start
span.finish_at(finish)
end
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
end
Expand All @@ -68,18 +73,23 @@ def self.render_partial(_name, start, finish, _id, payload)
# finish the tracing and update the execution time
tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
span = tracer.active_span()
template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
span.set_tag('rails.template_name', template_name)
return unless span

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end
begin
template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(payload.fetch(:identifier))
span.set_tag('rails.template_name', template_name)

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end

span.start_time = start
span.finish_at(finish)
ensure
span.start_time = start
span.finish_at(finish)
end
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
end
Expand Down
28 changes: 16 additions & 12 deletions lib/ddtrace/contrib/rails/active_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,24 @@ def self.trace_cache(resource, _name, start, finish, _id, payload)
end
span = tracer.active_span()
return unless span
# finish the tracing and update the execution time
span.resource = resource
span.set_tag('rails.cache.backend', ::Rails.configuration.cache_store)
span.set_tag('rails.cache.key', payload.fetch(:key))

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end
begin
# finish the tracing and update the execution time
span.resource = resource
span.set_tag('rails.cache.backend', ::Rails.configuration.cache_store)
span.set_tag('rails.cache.key', payload.fetch(:key))

if payload[:exception]
error = payload[:exception]
span.status = 1
span.set_tag(Datadog::Ext::Errors::TYPE, error[0])
span.set_tag(Datadog::Ext::Errors::MSG, error[1])
end

span.start_time = start
span.finish_at(finish)
ensure
span.start_time = start
span.finish_at(finish)
end
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
end
Expand Down