Skip to content

Adds method tracing helper#670

Closed
ronan-mch wants to merge 1 commit into
DataDog:masterfrom
ronan-mch:method_tracing
Closed

Adds method tracing helper#670
ronan-mch wants to merge 1 commit into
DataDog:masterfrom
ronan-mch:method_tracing

Conversation

@ronan-mch

Copy link
Copy Markdown

This PR adds a new module to ddtrace. MethodTracing allows developers to quickly add tracing to their classes without muddying up their code with trace blocks.

Example usage

class FizzBuzz

  extend Datadog::MethodTracing
  trace_methods self, :fizz, :buzz

  ...
end

@delner delner added core Involves Datadog core libraries community Was opened by a community member feature Involves a product feature labels Jan 15, 2019
@ronan-mch
ronan-mch requested a review from a team January 6, 2020 19:21
Comment thread .rubocop.yml Outdated

@marcotc marcotc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for this PR @ronan-mch, I really liked your approach to prepend a module for tracing, instead of simply overriding and wrapping the existing method.

It's very cool that the user can declare the methods they want to trace even before they are actually declared.

I left a few comments, but it looks great overall.

Comment thread lib/ddtrace/method_tracing.rb Outdated
Comment thread lib/ddtrace/method_tracing.rb Outdated
Comment thread spec/ddtrace/method_tracing_spec.rb Outdated
@marcotc

marcotc commented Jan 7, 2020

Copy link
Copy Markdown
Member

Also, so much has changed since you last rebased, you might want to update your branch with master too.

@ronan-mch
ronan-mch force-pushed the method_tracing branch 2 times, most recently from fbd7131 to bbb811b Compare January 11, 2020 21:38
Comment thread lib/ddtrace/method_tracing.rb Outdated
@ronan-mch
ronan-mch force-pushed the method_tracing branch 2 times, most recently from ea01c5e to 30ae527 Compare January 12, 2020 07:57
Co-authored-by: Benjamin Quorning <[email protected]>
@ronan-mch

Copy link
Copy Markdown
Author

@marcotc - is this mergeable?

@delner

delner commented Feb 3, 2020

Copy link
Copy Markdown
Contributor

I'll take a look at this, see if we can merge it.

@ronan-mch

Copy link
Copy Markdown
Author

I'll take a look at this, see if we can merge it.

@delner - any update on this?

@delner

delner commented Feb 25, 2020

Copy link
Copy Markdown
Contributor

Sorry haven't had the chance yet. Will try to make some time.

@delner delner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a great start for a very useful feature! I have two basic areas where I suggest we might want to change:

  • Minimizing the footprint (number of modules) we leave on the classes we patch
  • A minor change to the API for user simplicity (remove self)

Feel free to check out my comments and let me know what you think!

#
# class Foo
# extend Datadog::MethodTracing
# trace_methods(self, :method1, :method2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should extending this require you to pass self? Seems redundant. I think it would be better if self was implied here. Might require implementing some kind of helper module via def self.extended.

methods.each { |m| trace_method(klass, m) }
end

def trace_class_methods(klass, *methods)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it'd be a good idea to add some more robust comments above the def line here for each of these functions, that explain how these functions are used and what they produce.

end

def trace_method(klass, method)
metric = "#{klass}##{method}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor, but if this is only used once, would it be simpler to just merge this into line 33? Over declaring the variable? Or there a scope issue where this helps side-step local variables dropping out of scope when defining Module#new?

end

def trace_class_methods(klass, *methods)
methods.each { |m| trace_class_method(klass, m) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This implementation might be problematic when done in bulk if I'm tracing dozens of functions. If I'm understanding it correctly, it would create and prepend a module per function, no?

I think it would be worthwhile making sure all the traced methods end up in one prepended module instead of many.

There are a number of strategies you could employ, but I think the challenge with how the current API is designed is that the user invokes trace_methods and trace_class_methods separately, such that each is an atomic operation which would needfully create a module per invocation.

If you're willing to change the API a little bit, you could consider employing a builder pattern instead, in which you build each in transaction before committing the result. e.g this could look like:

def MyClass
  extend Datadog::MethodTracing

  datadog_tracing do
    trace_methods :foo, :bar
    trace_class_methods :baz
  end
end

This is just the first idea that came to mind, so there might be a better/simpler way which would still allow us to avoid adding multiple modules. Let me know your thoughts!

@ronan-mch

Copy link
Copy Markdown
Author

Closing as I'm no longer working in a company that uses Datadog.

@ronan-mch ronan-mch closed this May 2, 2024
@ivoanjo

ivoanjo commented May 7, 2024

Copy link
Copy Markdown
Member

Thanks for the contribution and food for thought! We definitely will reference this PR and discussion in the future, and I wish you well in your new endeavour :)

@ronan-mch

Copy link
Copy Markdown
Author

Thank you! I'm ashamed to admit that I'd forgotten all about this PR until I reviewed my open PRs recently...

@ivoanjo

ivoanjo commented May 7, 2024

Copy link
Copy Markdown
Member

Thank you! I'm ashamed to admit that I'd forgotten all about this PR until I reviewed my open PRs recently...

🤣 yeah, definitely can relate on this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Was opened by a community member core Involves Datadog core libraries feature Involves a product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants