Skip to content

Prefer Traces.trace rather than modify the current class. #8

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

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Traces::Provider(MyClass) do
'foo' => 'bar'
}

trace('my_method', attributes: attributes) do
Traces.trace('my_method', attributes: attributes) do
super
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/traces/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Traces
def self.require_backend(env = ENV)
if backend = env['TRACES_BACKEND']
require(backend)

Traces.extend(Backend::Interface)
end
end
end
Expand Down
19 changes: 18 additions & 1 deletion lib/traces/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def traces_provider
end
end

module Deprecated
def trace(...)
warn "Traces::Provider.trace is deprecated. Please use Traces.trace instead."
Traces.trace(...)
Comment on lines +25 to +27
Copy link

Choose a reason for hiding this comment

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

FYI this makes the minimum Ruby version of this gem be >= 2.7 but that is not reflected in the gemspec

end

def trace_context
warn "Traces::Provider.trace_context is deprecated. Please use Traces.trace_context instead."
Traces.trace_context
end

def trace_context=(value)
warn "Traces::Provider.trace_context= is deprecated. Please use Traces.trace_context= instead."
Traces.trace_context = value
end
end

private_constant :Singleton

# Bail out if there is no backend configured.
Expand All @@ -30,7 +47,7 @@ def self.Provider(klass, &block)
klass.extend(Singleton)

provider = klass.traces_provider
provider.prepend(Backend::Interface)
provider.prepend(Deprecated)

klass.prepend(provider)

Expand Down
28 changes: 14 additions & 14 deletions test/traces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ def my_other_method(argument)

Traces::Provider(MyClass) do
def my_method(argument)
trace('my_method', attributes: {argument: argument}) {super}
Traces.trace('my_method', attributes: {argument: argument}) {super}
end

def my_method_with_result(result)
trace('my_method_with_result') do |span|
Traces.trace('my_method_with_result') do |span|
super.tap do |result|
span["result"] = result
end
end
end

def my_method_with_attributes(attributes)
trace('my_method_with_attributes', attributes: attributes) {super}
Traces.trace('my_method_with_attributes', attributes: attributes) {super}
end
end

Traces::Provider(MySubClass) do
def my_other_method(argument)
trace('my_other_method', attributes: {argument: argument}) {super}
Traces.trace('my_other_method', attributes: {argument: argument}) {super}
end
end

Expand All @@ -62,7 +62,7 @@ def my_other_method(argument)
let(:instance) {MyClass.new}

it "can invoke trace wrapper" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect(instance.my_method(10)).to be == 10
end
Expand All @@ -71,7 +71,7 @@ def my_other_method(argument)
let(:result) {"result"}

it "can invoke trace wrapper" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect(instance.my_method_with_result(result)).to be == result
end
Expand All @@ -81,7 +81,7 @@ def my_other_method(argument)
let(:attributes) {{"name" => "value"}}

it "can invoke trace wrapper" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect(instance.my_method_with_attributes(attributes)).to be == attributes
end
Expand All @@ -91,8 +91,8 @@ def my_other_method(argument)
let(:context) {Traces::Context.local}

it "can create child trace context" do
instance.trace_context = context
expect(instance.trace_context).to be == context
Traces.trace_context = context
expect(Traces.trace_context).to be == context
end
end
end
Expand All @@ -101,7 +101,7 @@ def my_other_method(argument)
let(:instance) {MySubClass.new}

it "can invoke trace wrapper" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect(instance.my_method(10)).to be == 20
end
Expand All @@ -120,7 +120,7 @@ def my_other_method(argument)
let(:attributes) {{Object.new => "value"}}

it "fails with exception" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect do
instance.my_method_with_attributes(attributes)
Expand All @@ -138,7 +138,7 @@ def my_other_method(argument)
let(:attributes) {{"key" => value}}

it "fails with exception" do
expect(instance).to receive(:trace)
expect(Traces).to receive(:trace)

expect do
instance.my_method_with_attributes(attributes)
Expand All @@ -149,15 +149,15 @@ def my_other_method(argument)
with 'missing block' do
it "fails with exception" do
expect do
instance.trace('foo')
Traces.trace('foo')
end.to raise_exception(ArgumentError)
end
end

with 'invalid name' do
it "fails with exception" do
expect do
instance.trace(Object.new) {}
Traces.trace(Object.new) {}
end.to raise_exception(ArgumentError)
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/traces/backend/.capture/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def nested(&block)

Traces::Provider(App) do
def call
trace("my_trace", resource: "my_resource", attributes: {foo: "bar"}) do |span|
Traces.trace("my_trace", resource: "my_resource", attributes: {foo: "bar"}) do |span|
span[:foo] = "baz"
super
end
end

def nested
trace("nested") do
context = self.trace_context
Traces.trace("nested") do
context = Traces.trace_context

super do
self.trace_context = context
Traces.trace_context = context

yield
end
Expand Down
6 changes: 3 additions & 3 deletions test/traces/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
it "can yield span" do
Traces::Provider(my_class) do
def make_span
trace('test.span') do |span|
Traces.trace('test.span') do |span|
return span
end
end
Expand All @@ -33,8 +33,8 @@ def make_span
it "can get current trace context" do
Traces::Provider(my_class) do
def span
trace('test.span') do |span|
return trace_context
Traces.trace('test.span') do |span|
return Traces.trace_context
end
end
end
Expand Down