Skip to content
Open
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
5 changes: 5 additions & 0 deletions Matrixfile
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
'redis-4' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
'redis-3' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
},
'view_component' => {
'view_component-min' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
'view_component-3' => '❌ 2.5 / ❌ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
'view_component-4' => '❌ 2.5 / ❌ 2.6 / ❌ 2.7 / ❌ 3.0 / ❌ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
},
'appsec:active_record' => {
'relational_db' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ 3.5 / ✅ jruby',
},
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ namespace :spec do
:stripe,
:sucker_punch,
:suite,
:trilogy
:trilogy,
:view_component
].each do |contrib|
desc '' # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
Expand Down
1 change: 1 addition & 0 deletions appraisal/jruby-9.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
build_coverage_matrix('rest-client')
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('view_component', (3..4), min: '2.34.0')
# NOTE: JRuby bundler failed to install some dependencies https://github.com/ruby/psych/issues/700
# and it could be re-enabled when upstream fix the issue
# build_coverage_matrix('devise', min: '3.2.1')
Expand Down
1 change: 1 addition & 0 deletions appraisal/jruby-9.3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
build_coverage_matrix('rest-client')
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('view_component', (3..4), min: '2.34.0')
# NOTE: JRuby bundler failed to install some dependencies https://github.com/ruby/psych/issues/700
# and it could be re-enabled when upstream fix the issue
# build_coverage_matrix('devise', min: '3.2.1')
Expand Down
1 change: 1 addition & 0 deletions appraisal/jruby-9.4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('view_component', (3..4), min: '2.34.0')

appraise 'karafka-min' do
gem 'karafka', '= 2.3.0'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-2.5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli')
build_coverage_matrix('devise', min: '3.2.1', meta: { min: { 'bigdecimal' => '1.3.4' } })
build_coverage_matrix('view_component', min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 5'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-2.6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('devise', min: '3.2.1', meta: { min: { 'bigdecimal' => '1.4.1' } })
build_coverage_matrix('view_component', min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 6.0.0'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-2.7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', [3], min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 6.1.0'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
build_coverage_matrix('mongo', min: '2.1.0')
build_coverage_matrix('dalli', [2])
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', [3], min: '2.34.0')

appraise 'karafka-min' do
gem 'karafka', '= 2.3.0'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', [3], min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 7'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', (3..4), min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 7'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', (3..4), min: '2.34.0')

appraise 'relational_db' do
gem 'activerecord', '~> 7'
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', (3..4), min: '2.34.0')

appraise 'relational_db' do
# ActiveRecord locked because tests are failing with 7.1, which was attempted as a part of Ruby 3.4 testing in CI.
Expand Down
1 change: 1 addition & 0 deletions appraisal/ruby-3.5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
build_coverage_matrix('dalli', [2])
build_coverage_matrix('karafka', min: '2.3.0')
build_coverage_matrix('devise', min: '3.2.1')
build_coverage_matrix('view_component', (3..4), min: '2.34.0')

appraise 'relational_db' do
# ActiveRecord locked because tests are failing with 7.1, which was attempted as a part of Ruby 3.4 testing in CI.
Expand Down
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|
Copy link
Member

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.

Copy link
Member

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 /

Copy link
Author

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

o.type :string
o.default 'components/'
end
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions lib/datadog/tracing/contrib/view_component/event.rb
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
32 changes: 32 additions & 0 deletions lib/datadog/tracing/contrib/view_component/events.rb
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
52 changes: 52 additions & 0 deletions lib/datadog/tracing/contrib/view_component/events/render.rb
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)
Copy link
Member

Choose a reason for hiding this comment

The 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 component_path?

Copy link
Author

Choose a reason for hiding this comment

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

We definitely could; I had based the name component on the value that's passed in the ActiveSupport's payload; so that the APM telemetry matched the instrumentation data. But if you'd like me to change it; just let me know.

Reference: https://viewcomponent.org/guide/instrumentation.html

end

# Measure service stats
Contrib::Analytics.set_measured(span)
end
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/datadog/tracing/contrib/view_component/ext.rb
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
48 changes: 48 additions & 0 deletions lib/datadog/tracing/contrib/view_component/integration.rb
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
34 changes: 34 additions & 0 deletions lib/datadog/tracing/contrib/view_component/patcher.rb
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
Loading