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

NH-90184: add doc for add_tracer api call #174

Merged
merged 11 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ Note that if `OpenTelemetry::SDK.configure` is used to set up a `TracerProvider`

Several convenience and vendor-specific APIs are availabe to an application where `solarwinds_apm` has been loaded, below is a quick overview of the features provided. The full reference can be found at the [RubyDoc page for this gem](https://rubydoc.info/github/solarwinds/apm-ruby).

#### Convenience Method for in_span
#### Convenience Method: `in_span` and `add_tracer`

This method acquires the correct `Tracer` so a new span can be created in a single call, below is a simple Rails controller example:
`in_span` acquires the correct `Tracer` so a new span can be created in a single call.

For example, using it in a Rails controller:

```ruby
class StaticController < ApplicationController
Expand All @@ -78,6 +80,40 @@ class StaticController < ApplicationController
end
```

`add_tracer` can add a custom span to the specified instance or class method that is already defined. It requires the custom span name and optionally takes the span kind and additional attributes in hash format:
xuan-cao-swi marked this conversation as resolved.
Show resolved Hide resolved

```ruby
add_tracer :method_name, 'custom_span_name', { attributes: { 'any' => 'attributes' }, kind: :span_kind }
```

For example, if you want to instrument class or instance method `create_session` inside an application controller:

```ruby
class SessionsController < ApplicationController
include SolarWindsAPM::API::Tracer

def create
user = User.find_by(email: params[:session][:email].downcase)
create_session(user)
end

def create_session(user)
end

def self.create_session(user)
end

# instrument instance method create_session
add_tracer :create_session, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }

# instrument class method create_session
class << self
include SolarWindsAPM::API::Tracer
add_tracer :create_session, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }
end
end
```

#### Get Curent Trace Context Information

The `current_trace_info` method returns a `TraceInfo` object containing string representations of the current trace context that can be used in logging or manual propagation of context. This is a convenience method that wraps the OTel API `::OpenTelemetry::Trace.current_span`.
Expand Down
9 changes: 0 additions & 9 deletions gemfiles/rails_6x.gemfile

This file was deleted.

31 changes: 31 additions & 0 deletions lib/solarwinds_apm/api/custom_instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ def self.included(base)
end

module ClassMethods
# Helper function to instrument custom function
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be consistent and just use "method" instead of sometimes "function"? Please check for all instances in this docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use method

#
# `add_tracer` can add a custom span to the specified instance or class method that is already defined.
# It requires the custom span name and optionally takes the span kind and additional attributes
# in hash format.
#
# === Argument:
#
# * +method_name+ - (String) A non-empty string that match the function name that need to be instrumented
# * +span_name+ - (String, optional, default = method_name) A non-empty string that define the span name (default to method_name)
# * +options+ - (Hash, optional, default = {}) A hash with desired attributes
Copy link
Contributor

Choose a reason for hiding this comment

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

and span kind?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

span kind is part of options hash. Updated the docstring.

#
# === Example:
Copy link
Contributor

Choose a reason for hiding this comment

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

in this example there is no include SolarWindsAPM::API::Tracer, should there be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, updated.

#
# class DogfoodsController < ApplicationController
#
# def create
# @dogfood = Dogfood.new(params.permit(:brand, :name))
# @dogfood.save
# custom_function
# end
#
# def custom_function
# end
# add_tracer :custom_function, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }
#
# end
#
# === Returns:
# * nil
#
def add_tracer(method_name, span_name = nil, options = {})
span_name = name.nil? ? "#{to_s.split(':').last&.tr('>', '')}/#{__method__}" : "#{name}/#{__method__}" if span_name.nil?

Expand Down
1 change: 0 additions & 1 deletion test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ check_status() {

gemfiles=(
"gemfiles/unit.gemfile"
"gemfiles/rails_6x.gemfile"
)

##
Expand Down
Loading