-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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 | ||
end | ||
|
||
def subscribe! | ||
all.each(&:subscribe!) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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) | ||
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'm not sure this really is an identifier, since it's basically a partial file path. Do we want to name it something like 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. We definitely could; I had based the name Reference: https://viewcomponent.org/guide/instrumentation.html |
||
end | ||
|
||
# Measure service stats | ||
Contrib::Analytics.set_measured(span) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
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 |
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 |
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