-
Notifications
You must be signed in to change notification settings - Fork 395
Add view_component
integration for Datadog::Tracing
#4977
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
Open
tcannonfodder
wants to merge
2
commits into
DataDog:master
Choose a base branch
from
practical-computer:tcannonfodder/add-view_component-tracing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
lib/datadog/tracing/contrib/view_component/configuration/settings.rb
This file contains hidden or 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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'datadog/tracing/configuration/settings' | ||
require_relative '../ext' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
module Configuration | ||
# Custom settings for the ViewComponent integration | ||
# @public_api | ||
class Settings < Contrib::Configuration::Settings | ||
option :enabled do |o| | ||
o.type :bool | ||
o.env Ext::ENV_ENABLED | ||
o.default true | ||
end | ||
|
||
# @!visibility private | ||
option :analytics_enabled do |o| | ||
o.type :bool | ||
o.env Ext::ENV_ANALYTICS_ENABLED | ||
o.default false | ||
end | ||
|
||
option :analytics_sample_rate do |o| | ||
o.type :float | ||
o.env Ext::ENV_ANALYTICS_SAMPLE_RATE | ||
o.default 1.0 | ||
end | ||
|
||
option :service_name | ||
option :component_base_path do |o| | ||
o.type :string | ||
o.default 'components/' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'datadog/tracing/contrib/active_support/notifications/event' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
# Defines basic behavior for an ViewComponent event. | ||
module Event | ||
def self.included(base) | ||
base.include(ActiveSupport::Notifications::Event) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
# Class methods for ViewComponent events. | ||
module ClassMethods | ||
def configuration | ||
Datadog.configuration.tracing[:view_component] | ||
end | ||
|
||
def record_exception(span, payload) | ||
if payload[:exception_object] | ||
span.set_error(payload[:exception_object]) | ||
elsif payload[:exception] | ||
# Fallback for ActiveSupport < 5.0 | ||
span.set_error(payload[:exception]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'events/render' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
# Defines collection of instrumented ViewComponent events | ||
module Events | ||
ALL = [ | ||
Events::Render | ||
].freeze | ||
|
||
module_function | ||
|
||
def all | ||
self::ALL | ||
end | ||
|
||
def subscriptions | ||
all.collect(&:subscriptions).collect(&:to_a).flatten | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it could be a |
||
end | ||
|
||
def subscribe! | ||
all.each(&:subscribe!) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
lib/datadog/tracing/contrib/view_component/events/render.rb
This file contains hidden or 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'datadog/tracing' | ||
require 'datadog/tracing/metadata/ext' | ||
require 'datadog/tracing/analytics' | ||
require_relative '../ext' | ||
require_relative '../event' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
module Events | ||
# Defines instrumentation for render.view_component event | ||
module Render | ||
include ViewComponent::Event | ||
|
||
EVENT_NAME = 'render.view_component' | ||
|
||
module_function | ||
|
||
def event_name | ||
self::EVENT_NAME | ||
end | ||
|
||
def span_name | ||
Ext::SPAN_RENDER | ||
end | ||
|
||
def on_start(span, _event, _id, payload) | ||
span.service = configuration[:service_name] if configuration[:service_name] | ||
span.type = Tracing::Metadata::Ext::HTTP::TYPE_TEMPLATE | ||
|
||
span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT) | ||
span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_RENDER) | ||
|
||
span.resource = payload[:name] | ||
span.set_tag(Ext::TAG_COMPONENT_NAME, payload[:name]) | ||
|
||
if (identifier = Utils.normalize_component_identifier(payload[:identifier])) | ||
span.set_tag(Ext::TAG_COMPONENT_IDENTIFIER, identifier) | ||
tcannonfodder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
# Measure service stats | ||
Contrib::Analytics.set_measured(span) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
# ViewComponent integration constants | ||
# @public_api Changing resource names, tag names, or environment variables creates breaking changes. | ||
module Ext | ||
ENV_ENABLED = 'DD_TRACE_VIEW_COMPONENT_ENABLED' | ||
# @!visibility private | ||
ENV_ANALYTICS_ENABLED = 'DD_TRACE_VIEW_COMPONENT_ANALYTICS_ENABLED' | ||
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_VIEW_COMPONENT_ANALYTICS_SAMPLE_RATE' | ||
SPAN_RENDER = 'view_component.render' | ||
TAG_COMPONENT = 'view_component' | ||
TAG_OPERATION_RENDER = 'render' | ||
TAG_COMPONENT_IDENTIFIER = 'view_component.component_identifier' | ||
TAG_COMPONENT_NAME = 'view_component.component_name' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'configuration/settings' | ||
require_relative 'patcher' | ||
require 'datadog/tracing/contrib/integration' | ||
require 'datadog/tracing/contrib/rails/ext' | ||
require 'datadog/core/contrib/rails/utils' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
# Describes the ViewComponent integration | ||
class Integration | ||
include Contrib::Integration | ||
|
||
MINIMUM_VERSION = "2.34.0" | ||
|
||
# @public_api Changing the integration name or integration options can cause breaking changes | ||
register_as :view_component, auto_patch: false | ||
def self.gem_name | ||
'view_component' | ||
end | ||
|
||
def self.version | ||
Gem.loaded_specs['view_component']&.version | ||
end | ||
|
||
def self.loaded? | ||
!defined?(::ViewComponent).nil? | ||
end | ||
|
||
def self.compatible? | ||
super && version >= MINIMUM_VERSION | ||
end | ||
|
||
def new_configuration | ||
Configuration::Settings.new | ||
end | ||
|
||
def patcher | ||
ViewComponent::Patcher | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'datadog/core' | ||
require 'datadog/tracing/contrib/patcher' | ||
require_relative 'events' | ||
require_relative 'ext' | ||
require_relative 'utils' | ||
|
||
module Datadog | ||
module Tracing | ||
module Contrib | ||
module ViewComponent | ||
# Patcher enables patching of ViewComponent module. | ||
module Patcher | ||
include Contrib::Patcher | ||
|
||
module_function | ||
|
||
def target_version | ||
Integration.version | ||
end | ||
|
||
def patch | ||
patch_renderer | ||
end | ||
|
||
def patch_renderer | ||
Events.subscribe! | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
components_base_path
be better? Since it's a path containing multiple components.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think we would need to ensure the correct format here - when calculating the component identifier, we are relying that this path will end with
/
, otherwise component identifiers will start with a/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both this (and the comment below) are essentially straight ports from the
action_view
integration; which felt like the right approach to take here because these components are run together (you often render ViewComponents inside ActionView), so it feels right that they are configured and behave similarly.But! Y’all have the final say here; so just lemme know what makes sense for the repo. :) I just wanted to explain my reasoning for these decisions.
References: https://github.com/DataDog/dd-trace-rb/blob/master/lib/datadog/tracing/contrib/action_view/configuration/settings.rb#L34-L37