Tracer.cr CI Tracer.cr Build Docs

GitHub release GitHub commits since latest release (by SemVer)

Tracer.cr

Tracer.cr attaches hooks to existing Crystal methods at compile time. It wraps each matching overload with a new definition that delegates through previous_def. Use it as a building block for instrumentation, debugging, or auditing.

Requires Crystal 1.19.0 or later. CI checks Crystal 1.19.0 and 1.22.3.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      tracer:
        github: wyhaines/tracer.cr
        version: ~> 1.0
  2. Run shards install

Usage

Place trace after the method definitions you want to wrap.

require "tracer"

class Worker
  def run(job : String)
    job.upcase
  end

  trace("run") do
    puts "#{__trace_method_name__} #{__trace_phase__}: #{job}"
  end
end

Worker.new.run("build")

Use "run" for an instance method of the current type, "self.run" for a class method of the current type, or "::MyApp::Worker.run" for a class method of a named receiver. The macro wraps matching concrete overloads that have already been defined on that receiver.

Block hooks

The hook body runs with __trace_phase__ set to :before. It runs again with :after from an ensure clause, including when the method raises. The body can use the wrapped method's arguments and self, as well as these locals.

| Local | Value | | --- | --- | | __trace_method_name__ | The unqualified method name as a String. | | __trace_method_identifier__ | A String containing receiver, method name, and definition coordinates, for example Worker__run__4X3. Edits that move a definition can change its identifier. | | __trace_method_receiver__ | The type that declares the wrapped method, such as Worker. | | __trace_method_call_counter__ | The prior value of the process-wide atomic UInt64 counter, exposed to hook code as a UInt128. Counted wrappers increment it once per invocation and see the same value in both phases. |

Use these locals directly in the block body. Proc literals are the form that supports callback parameters. Wrappers that use the count share Tracer::METHOD_COUNTER with named callbacks. Synchronize any shared state the hooks modify when traced code can run concurrently.

To control when the original method runs, call previous_def directly in the hook. A hook with a manual call runs once with __trace_phase__ set to :before and can return the original result or replace it.

trace("run") do
  start = Time.instant
  begin
    previous_def
  ensure
    puts "run took #{Time.instant - start}"
  end
end

Bare previous_def forwards the current arguments and caller block. Use an explicit block when replacing it with a different block or passing explicit arguments to a yielding method. The optional block_def: keyword supplies the generated block parameter declaration, for example block_def: "&callback : String -> Nil".

Callback hooks

trace("run", "my_callback") calls my_callback before and after the method. Define the callback with these five arguments in order.

def my_callback(method_name, phase, method_identifier, method_counter, caller)
  puts "#{method_name} #{phase} (call #{method_counter})"
end

A Proc literal can request the first zero to five arguments in the same order. For example, trace("run", ->(name : String, phase : Symbol) { puts "#{name} #{phase}" }) uses the first two. Callback hooks receive the target string supplied to trace, including the receiver prefix if one was supplied.

Lower-level hooks and metadata

add_method_hooks("run") do ... end supplies the whole wrapper body. It provides the same method locals as the block form of trace. The caller controls phases and delegation.

Tracer::TRACED_METHODS is a tuple of {method_name, receiver_type} entries. Tracer::TRACED_METHODS_BY_RECEIVER is a hash from receiver types to tuples of method names. Entries are grouped by receiver and name across overloads. When there are no traced methods, both collections are empty.

Development

If you want to help with the development, email me, and fork the repo. Work on your changes in a branch out of your own repo, and when it is ready (with documentation and specs), send me a pull request telling me what you have done, why you have done it, and what you have done to make sure that it works as expected.

Contributing

  1. Fork it (https://github.com/wyhaines/tracer.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

GitHub code size in bytes GitHub issues