Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #762 from DataDog/refactor/extract_actionview_from…
…_rails Extract ActionView from Rails
- Loading branch information
Showing
18 changed files
with
421 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'ddtrace/contrib/configuration/settings' | ||
require 'ddtrace/contrib/action_view/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActionView | ||
module Configuration | ||
# Custom settings for the ActionView integration | ||
class Settings < Contrib::Configuration::Settings | ||
option :analytics_enabled, | ||
default: -> { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) }, | ||
lazy: true | ||
|
||
option :analytics_sample_rate, | ||
default: -> { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) }, | ||
lazy: true | ||
|
||
option :service_name, default: Ext::SERVICE_NAME | ||
option :template_base_path, default: 'views/' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Datadog | ||
module Contrib | ||
module ActionView | ||
# ActionView integration constants | ||
module Ext | ||
APP = 'action_view'.freeze | ||
ENV_ANALYTICS_ENABLED = 'DD_ACTION_VIEW_ANALYTICS_ENABLED'.freeze | ||
ENV_ANALYTICS_SAMPLE_RATE = 'DD_ACTION_VIEW_ANALYTICS_SAMPLE_RATE'.freeze | ||
SERVICE_NAME = 'action_view'.freeze | ||
SPAN_RENDER_PARTIAL = 'rails.render_partial'.freeze | ||
SPAN_RENDER_TEMPLATE = 'rails.render_template'.freeze | ||
TAG_LAYOUT = 'rails.layout'.freeze | ||
TAG_TEMPLATE_NAME = 'rails.template_name'.freeze | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
require 'ddtrace/contrib/action_view/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActionView | ||
# Defines instrumentation for ActionView | ||
module Instrumentation | ||
# Instrumentation for template rendering | ||
module TemplateRenderer | ||
# Rails < 3.1 template rendering | ||
module Rails30 | ||
# rubocop:disable Metrics/MethodLength | ||
def self.prepended(base) | ||
# rubocop:disable Metrics/BlockLength | ||
base.class_eval do | ||
def render_with_datadog(*args, &block) | ||
# NOTE: This check exists purely for Rails 3.0 compatibility. | ||
# The 'if' part can be removed when support for Rails 3.0 is removed. | ||
if active_datadog_span | ||
render_without_datadog(*args, &block) | ||
else | ||
datadog_tracer.trace( | ||
Ext::SPAN_RENDER_TEMPLATE, | ||
span_type: Datadog::Ext::HTTP::TEMPLATE | ||
) do |span| | ||
with_datadog_span(span) { render_without_datadog(*args, &block) } | ||
end | ||
end | ||
end | ||
|
||
def render_template_with_datadog(*args) | ||
begin | ||
template = args[0] | ||
layout_name = args[1] | ||
|
||
# update the tracing context with computed values before the rendering | ||
template_name = template.try('identifier') | ||
template_name = Utils.normalize_template_name(template_name) | ||
|
||
if template_name | ||
active_datadog_span.set_tag( | ||
Ext::TAG_TEMPLATE_NAME, | ||
template_name | ||
) | ||
end | ||
|
||
if layout_name | ||
active_datadog_span.set_tag( | ||
Ext::TAG_LAYOUT, | ||
layout_name | ||
) | ||
end | ||
rescue StandardError => e | ||
Datadog::Tracer.log.debug(e.message) | ||
end | ||
|
||
# execute the original function anyway | ||
render_template_without_datadog(*args) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :active_datadog_span | ||
|
||
def datadog_tracer | ||
Datadog.configuration[:action_view][:tracer] | ||
end | ||
|
||
def with_datadog_span(span) | ||
self.active_datadog_span = span | ||
yield | ||
ensure | ||
self.active_datadog_span = nil | ||
end | ||
|
||
# method aliasing to patch the class | ||
alias_method :render_without_datadog, :render | ||
alias_method :render, :render_with_datadog | ||
|
||
alias_method :render_template_without_datadog, :_render_template | ||
alias_method :_render_template, :render_template_with_datadog | ||
end | ||
end | ||
end | ||
|
||
# Rails >= 3.1 template rendering | ||
module Rails31Plus | ||
def render(*args, &block) | ||
datadog_tracer.trace( | ||
Ext::SPAN_RENDER_TEMPLATE, | ||
span_type: Datadog::Ext::HTTP::TEMPLATE | ||
) do |span| | ||
with_datadog_span(span) { super(*args, &block) } | ||
end | ||
end | ||
|
||
def render_template(*args) | ||
begin | ||
# arguments based on render_template signature (stable since Rails 3.2) | ||
template = args[0] | ||
layout_name = args[1] | ||
|
||
# update the tracing context with computed values before the rendering | ||
template_name = template.try('identifier') | ||
template_name = Utils.normalize_template_name(template_name) | ||
layout = layout_name.try(:[], 'virtual_path') | ||
|
||
if template_name | ||
active_datadog_span.set_tag( | ||
Ext::TAG_TEMPLATE_NAME, | ||
template_name | ||
) | ||
end | ||
|
||
if layout | ||
active_datadog_span.set_tag( | ||
Ext::TAG_LAYOUT, | ||
layout | ||
) | ||
end | ||
rescue StandardError => e | ||
Datadog::Tracer.log.debug(e.message) | ||
end | ||
|
||
# execute the original function anyway | ||
super(*args) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :active_datadog_span | ||
|
||
def datadog_tracer | ||
Datadog.configuration[:action_view][:tracer] | ||
end | ||
|
||
def with_datadog_span(span) | ||
self.active_datadog_span = span | ||
yield | ||
ensure | ||
self.active_datadog_span = nil | ||
end | ||
end | ||
end | ||
|
||
# Instrumentation for partial rendering | ||
module PartialRenderer | ||
def render(*args, &block) | ||
datadog_tracer.trace( | ||
Ext::SPAN_RENDER_PARTIAL, | ||
span_type: Datadog::Ext::HTTP::TEMPLATE | ||
) do |span| | ||
with_datadog_span(span) { super(*args) } | ||
end | ||
end | ||
|
||
def render_partial(*args) | ||
begin | ||
template_name = Utils.normalize_template_name(@template.try('identifier')) | ||
if template_name | ||
active_datadog_span.set_tag( | ||
Ext::TAG_TEMPLATE_NAME, | ||
template_name | ||
) | ||
end | ||
rescue StandardError => e | ||
Datadog::Tracer.log.debug(e.message) | ||
end | ||
|
||
# execute the original function anyway | ||
super(*args) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :active_datadog_span | ||
|
||
def datadog_tracer | ||
Datadog.configuration[:action_view][:tracer] | ||
end | ||
|
||
def with_datadog_span(span) | ||
self.active_datadog_span = span | ||
yield | ||
ensure | ||
self.active_datadog_span = nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.