Skip to content
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

Implement method tracing API #2241

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from
Draft

Implement method tracing API #2241

wants to merge 28 commits into from

Conversation

jennchenn
Copy link
Member

@jennchenn jennchenn commented Aug 24, 2022

WIP: Some tasks to be done

  • System tests are failing
  • Instrumentation API does not support ruby 3.2 at the moment
  • disable/enable functionality does not seem to be working as intended on instrumentation gem side (i.e. even when hook is disabled, it still produces traces)
  • Sorbet checks are failing

What does this PR do?

This PR adds a method tracing API that can be used by:

  1. Customers tracing their own code
  2. Customers looking to trace code from gems we don't yet instrument
  3. Us when developing new integrations

Motivation

Method tracing has been requested for a long time by the community. This implementation makes use of the instrumentation API to provide users with an easy interface to implement custom instrumentation.

Additional Notes

The number of changed files is inflated because of the changes to the gemfile. Main change is in the tracing/contrib/hook.rb file.

How to test the change?

Unit tests have been added to test the changes. pg and mysql2 instrumentations were modified as a POC that the API works (the original instrumentation code was not deleted in this branch).

@jennchenn jennchenn added integrations Involves tracing integrations do-not-merge/WIP Not ready for merge dev/refactor Involves refactoring existing components labels Aug 24, 2022
Copy link
Contributor

@delner delner left a comment

Choose a reason for hiding this comment

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

This is looking very nice. Clean, easy to understand, docs are good... solid work.

Path to merge involves:

  • Moving some internals around, to make sure we're not introducing co-dependencies and are preserving the relationship between namespaces e.g. Tracing and Contrib.
  • Describe this as a "beta" feature: that it's subject to breaking changes.
  • Adding some kind of check & warning when trace_method is used without datadog-instrumentation (as it currently must be included manually as a soft dependency by users).

Let me know if I can clarify the asks/needs here at all!

module Tracing
module Contrib
# Class defining hook used to implement method tracing
class Hook
Copy link
Contributor

Choose a reason for hiding this comment

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

Overall looks very clean! May want to add some YARD doc comments if possible.

# @param [String] target Defines the method to be traced
# @param [Hash] span_options Optional value to be added to the produced span
# @return [Datadog::Tracing::Contrib::Hook] The newly defined Datadog::Tracing::Contrib::Hook object
def trace_method(target, name, span_options = {})
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably need to migrate this to Contrib::Extensions or declare this within module Tracing from a file within the contrib directory.

Reason being we would like the tracing core to be independent of the integrations. Thus Contrib can depend on things in Tracing but not vice versa.

Also, given this function doesn't actually use the Tracer at all, I think extracting this method will help keep this class a bit simpler (fewer responsibilities).


To generate traces, use the `Datadog::Tracing.trace_method` method:
```ruby
Datadog::Tracing.trace_method(name, target, span_options).around do |env, span, trace, &block|
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be updated to reflect target, name instead of name, target?

ddtrace.gemspec Outdated
@@ -69,5 +69,8 @@ Gem::Specification.new do |spec|
# Used by profiling (and possibly others in the future)
spec.add_dependency 'libdatadog', '~> 0.7.0.1.1'

# Used by method tracing API
spec.add_dependency 'datadog-instrumentation'
Copy link
Contributor

Choose a reason for hiding this comment

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

Because this package is not published to RubyGems we can't merge this with this dependency as-is.

Eventually we should publish this, but in the interim, we could introduce it as a soft dependency: remove it from this file, and add a check to trace_method that looks for the presence of Datadog::Instrumentation.

  • If present, use the package and proceed as normal.
  • If not present, log a warning and suggestion to add datadog-instrumentation to the Gemfile.

This way, users could opt-in to use this as a "beta" feature while we polish up datadog-instrumentation for it's debut, without causing errors.

@delner delner self-assigned this Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev/refactor Involves refactoring existing components do-not-merge/WIP Not ready for merge integrations Involves tracing integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants