Skip to content

Commit

Permalink
revision
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan-cao-swi committed Jan 8, 2025
1 parent 4ec3ef9 commit 9fc232b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ add_tracer :method_name, 'custom_span_name', { attributes: { 'any' => 'attribute

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

To instrument instance method
```ruby
class SessionsController < ApplicationController
include SolarWindsAPM::API::Tracer
Expand All @@ -100,11 +101,21 @@ class SessionsController < ApplicationController
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 }
end
```

To instrument class method
```ruby
class SessionsController < ApplicationController
def create
user = User.find_by(email: params[:session][:email].downcase)
create_session(user)
end

def self.create_session(user)
end

# instrument class method create_session
class << self
Expand Down
30 changes: 24 additions & 6 deletions lib/solarwinds_apm/api/custom_instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,49 @@ def self.included(base)
end

module ClassMethods
# Helper function to instrument custom function
# Helper method to instrument custom 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
# * +method_name+ - (String) A non-empty string that match the method 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
# * +options+ - (Hash, optional, default = {}) A hash with desired options include attributes and span kind e.g. {attributes: {}, kind: :consumer}
#
# === Example:
#
# class DogfoodsController < ApplicationController
# include SolarWindsAPM::API::Tracer
#
# def create
# @dogfood = Dogfood.new(params.permit(:brand, :name))
# @dogfood.save
# custom_function
# custom_method
# end
#
# def custom_function
# def custom_method
# end
# add_tracer :custom_method, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }
#
# end
#

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
# class DogfoodsController < ApplicationController
# def create
# @dogfood = Dogfood.new(params.permit(:brand, :name))
# @dogfood.save
# custom_method
# end
#
# def self.custom_method
# end
#
# class << self
# include SolarWindsAPM::API::Tracer
# add_tracer :custom_method, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }
# end
# add_tracer :custom_function, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :consumer }
#
# end
#
Expand Down

0 comments on commit 9fc232b

Please sign in to comment.