Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Jul 5, 2024
1 parent 64f2487 commit fff04df
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def cache_backend(store)
# Despite a given application only ever having 1-3 store types,
# we limit the size of the `@cache_backend` just in case, because
# the user can create custom Cache store classes themselves.
if @cache_backend.size < 50
@cache_backend[store] = name
end
@cache_backend[store] = name if @cache_backend.size < 50

name
end
Expand Down
10 changes: 4 additions & 6 deletions lib/datadog/tracing/contrib/active_support/cache/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ def patch_redis?(meth)
def cache_store_class(meth)
if patch_redis?(meth)
[::ActiveSupport::Cache::RedisStore, ::ActiveSupport::Cache::Store]
else
if defined?(::ActiveSupport::Cache::RedisCacheStore) \
elsif defined?(::ActiveSupport::Cache::RedisCacheStore) \
&& ::ActiveSupport::Cache::RedisCacheStore.instance_methods(false).include?(meth)
[::ActiveSupport::Cache::RedisCacheStore, ::ActiveSupport::Cache::Store]
else
super
end
[::ActiveSupport::Cache::RedisCacheStore, ::ActiveSupport::Cache::Store]
else
super
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,27 @@ def subscribe!

# Creates a subscription and immediately activates it.
def subscribe(pattern, span_name, span_options = {}, on_start: nil, on_finish: nil, trace: nil)
subscription(span_name, span_options, on_start: on_start, on_finish: on_finish, trace: trace).tap do |subscription|
subscription(
span_name,
span_options,
on_start: on_start,
on_finish: on_finish,
trace: trace
).tap do |subscription|
subscription.subscribe(pattern)
end
end

# Creates a subscription without activating it.
# Subscription is added to the inheriting class' list of subscriptions.
def subscription(span_name, span_options = {}, on_start: nil, on_finish: nil, trace: nil)
Subscription.new(span_name, span_options, on_start: on_start, on_finish: on_finish, trace: trace).tap do |subscription|
Subscription.new(
span_name,
span_options,
on_start: on_start,
on_finish: on_finish,
trace: trace
).tap do |subscription|
subscriptions << subscription
end
end
Expand Down
18 changes: 18 additions & 0 deletions sig/datadog/tracing/contrib/active_support/cache/event.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Datadog
module Tracing
module Contrib
module ActiveSupport
module Cache
module Event
def self.included: (untyped base) -> untyped
module ClassMethods
def span_options: () -> ::Hash[untyped, untyped]

def configuration: () -> untyped
end
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions sig/datadog/tracing/contrib/active_support/cache/events.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Datadog
module Tracing
module Contrib
module ActiveSupport
module Cache
module Events
ALL: ::Array[untyped]

def self?.all: () -> untyped

def self?.subscriptions: () -> untyped

def self?.subscribe!: () -> untyped
end
end
end
end
end
end
32 changes: 32 additions & 0 deletions sig/datadog/tracing/contrib/active_support/cache/events/cache.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Datadog
module Tracing
module Contrib
module ActiveSupport
module Cache
module Events
module Cache
@cache_backend: untyped

include ActiveSupport::Cache::Event
def self?.subscribe!: () -> untyped

def self?.event_name: () -> ::Regexp

def self?.span_name: () -> untyped

def self?.span_options: () -> { type: untyped }
MAPPING: ::Hash[::String, { resource: untyped } | { resource: untyped, multi_key: true }]

def self?.trace?: (untyped event, untyped _payload) -> (false | untyped)

def self?.on_start: (untyped span, untyped event, untyped _id, untyped payload) -> untyped

def self?.set_cache_key: (untyped span, untyped key, untyped multi_key) -> untyped
def self?.cache_backend: (untyped store) -> untyped
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
let(:callbacks) do
{
on_start: test_class.method(:on_start),
on_finish: test_class.method(:on_finish)
on_finish: test_class.method(:on_finish),
trace: test_class.method(:trace?)
}
end

Expand Down Expand Up @@ -99,16 +100,19 @@
end

context 'when given options' do
subject(:subscription) { test_class.subscription(span_name, options, on_start: on_start, on_finish: on_finish) }
subject(:subscription) do
test_class.subscription(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
end

let(:span_name) { double('span name') }
let(:options) { double('options') }
let(:on_start) { double('on_start') }
let(:on_finish) { double('on_finish') }
let(:trace) { double('trace') }

before do
expect(Datadog::Tracing::Contrib::ActiveSupport::Notifications::Subscription).to receive(:new)
.with(span_name, options, on_start: on_start, on_finish: on_finish)
.with(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
.and_call_original
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
describe 'behavior' do
let(:on_start) { double('on start') }
let(:on_finish) { double('on finish') }
let(:trace) { double('trace') }

describe '#subscriptions' do
subject(:subscriptions) { test_class.subscriptions }
Expand Down Expand Up @@ -92,7 +93,15 @@

describe '#subscribe' do
subject(:subscription) do
test_class.send(:subscribe, pattern, span_name, options, on_start: on_start, on_finish: on_finish)
test_class.send(
:subscribe,
pattern,
span_name,
options,
on_start: on_start,
on_finish: on_finish,
trace: trace
)
end

let(:pattern) { double('pattern') }
Expand All @@ -101,7 +110,7 @@

before do
expect(Datadog::Tracing::Contrib::ActiveSupport::Notifications::Subscription).to receive(:new)
.with(span_name, options, on_start: on_start, on_finish: on_finish)
.with(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
.and_call_original

expect_any_instance_of(Datadog::Tracing::Contrib::ActiveSupport::Notifications::Subscription)
Expand All @@ -115,15 +124,15 @@

describe '#subscription' do
subject(:subscription) do
test_class.send(:subscription, span_name, options, on_start: on_start, on_finish: on_finish)
test_class.send(:subscription, span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
end

let(:span_name) { double('span name') }
let(:options) { double('options') }

before do
expect(Datadog::Tracing::Contrib::ActiveSupport::Notifications::Subscription).to receive(:new)
.with(span_name, options, on_start: on_start, on_finish: on_finish)
.with(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
.and_call_original
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

RSpec.describe Datadog::Tracing::Contrib::ActiveSupport::Notifications::Subscription do
describe 'instance' do
subject(:subscription) { described_class.new(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace) }
subject(:subscription) do
described_class.new(span_name, options, on_start: on_start, on_finish: on_finish, trace: trace)
end

let(:span_name) { double('span_name') }
let(:options) { { resource: 'dummy_resource' } }
let(:on_start) { proc { |span_op, name, id, payload| on_start_spy.call(span_op, name, id, payload) } }
let(:on_finish) { proc { |span_op, name, id, payload| on_finish_spy.call(span_op, name, id, payload) } }
let(:trace) { proc { |name, payload| true } }
let(:trace) { proc { |_name, _payload| true } }
let(:payload) { {} }

let(:on_start_spy) { double('on_start_spy') }
Expand Down Expand Up @@ -47,7 +49,7 @@
end

context 'with trace? returning false' do
let(:trace) { proc { |name, payload| false } }
let(:trace) { proc { |_name, _payload| false } }

it 'does not start a span operation nor call the callback' do
expect(Datadog::Tracing).not_to receive(:trace)
Expand All @@ -74,7 +76,7 @@
end

context 'without a span started' do
let(:payload) { { } }
let(:payload) { {} }

it 'does not call the callback' do
expect(on_finish_spy).to_not receive(:call)
Expand Down Expand Up @@ -121,7 +123,7 @@
end

context 'with trace? returning false' do
let(:trace) { proc { |name, payload| false } }
let(:trace) { proc { |_name, _payload| false } }

it 'does not call the callback' do
expect(callback_spy).not_to receive(:call)
Expand Down Expand Up @@ -172,7 +174,7 @@
end

context 'with trace? returning false' do
let(:trace) { proc { |name, payload| false } }
let(:trace) { proc { |_name, _payload| false } }

it 'does not call the callback' do
expect(callback_spy).not_to receive(:call)
Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/tracing/contrib/rails/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@
end

context 'with custom cache_service' do
before {
before do
Datadog.configuration.tracing[:active_support][:cache_service] = 'service-cache'
}
end

it 'uses the proper service name' do
write
Expand Down

0 comments on commit fff04df

Please sign in to comment.