Module: ActiveSupport::Notifications

### Home Classes Methods

### In Files

- lib/active_support/notifications.rb
- lib/active_support/notifications/fanout.rb
- lib/active_support/notifications/instrumenter.rb

### Namespace

- CLASS ActiveSupport::Notifications::Event
- CLASS ActiveSupport::Notifications::Fanout
- CLASS ActiveSupport::Notifications::Instrumenter

### Methods

- ::instrument
- ::instrumenter
- ::publish
- ::subscribe
- ::subscribed
- ::unsubscribe

### Class/Module Index

Quicksearch

- ActiveSupport
- ActiveSupport::Autoload
- ActiveSupport::BacktraceCleaner
- ActiveSupport::Base64
- ActiveSupport::BasicObject
- ActiveSupport::Benchmarkable
- ActiveSupport::BufferedLogger
- ActiveSupport::BufferedLogger::Severity
- ActiveSupport::Cache
- ActiveSupport::Cache::Entry
- ActiveSupport::Cache::FileStore
- ActiveSupport::Cache::MemCacheStore
- ActiveSupport::Cache::MemoryStore
- ActiveSupport::Cache::NullStore
- ActiveSupport::Cache::Store
- ActiveSupport::Cache::Strategy
- ActiveSupport::Cache::Strategy::LocalCache
- ActiveSupport::Cache::Strategy::LocalCache::LocalStore
- ActiveSupport::Callbacks
- ActiveSupport::Callbacks::ClassMethods
- ActiveSupport::Concern
- ActiveSupport::Configurable
- ActiveSupport::Configurable::ClassMethods
- ActiveSupport::Configurable::Configuration
- ActiveSupport::Dependencies
- ActiveSupport::Dependencies::ClassCache
- ActiveSupport::Dependencies::WatchStack
- ActiveSupport::Deprecation
- ActiveSupport::DescendantsTracker
- ActiveSupport::Duration
- ActiveSupport::FileUpdateChecker
- ActiveSupport::FileWatcher
- ActiveSupport::FileWatcher::Backend
- ActiveSupport::Gzip
- ActiveSupport::Gzip::Stream
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::Inflector
- ActiveSupport::Inflector::Inflections
- ActiveSupport::InheritableOptions
- ActiveSupport::JSON
- ActiveSupport::JSON::Encoding
- ActiveSupport::JSON::Encoding::CircularReferenceError
- ActiveSupport::JSON::Encoding::Encoder
- ActiveSupport::JSON::Variable
- ActiveSupport::LogSubscriber
- ActiveSupport::LogSubscriber::TestHelper
- ActiveSupport::LogSubscriber::TestHelper::MockLogger
- ActiveSupport::Memoizable
- ActiveSupport::Memoizable::InstanceMethods
- ActiveSupport::MessageEncryptor
- ActiveSupport::MessageEncryptor::InvalidMessage
- ActiveSupport::MessageVerifier
- ActiveSupport::MessageVerifier::InvalidSignature
- ActiveSupport::Multibyte
- ActiveSupport::Multibyte::Chars
- ActiveSupport::Multibyte::EncodingError
- ActiveSupport::Multibyte::Unicode
- ActiveSupport::Multibyte::Unicode::Codepoint
- ActiveSupport::Multibyte::Unicode::UnicodeDatabase
- ActiveSupport::Notifications
- ActiveSupport::Notifications::Event
- ActiveSupport::Notifications::Fanout
- ActiveSupport::Notifications::Instrumenter
- ActiveSupport::OrderedHash
- ActiveSupport::OrderedOptions
- ActiveSupport::Railtie
- ActiveSupport::Rescuable
- ActiveSupport::Rescuable::ClassMethods
- ActiveSupport::SafeBuffer
- ActiveSupport::SafeBuffer::SafeConcatError
- ActiveSupport::StringInquirer
- ActiveSupport::TaggedLogging
- ActiveSupport::TestCase
- ActiveSupport::Testing
- ActiveSupport::Testing::Assertions
- ActiveSupport::Testing::Declarative
- ActiveSupport::Testing::Isolation
- ActiveSupport::Testing::Isolation::Forking
- ActiveSupport::Testing::Isolation::MiniTest
- ActiveSupport::Testing::Isolation::Subprocess
- ActiveSupport::Testing::Isolation::TestUnit
- ActiveSupport::Testing::Pending
- ActiveSupport::Testing::Performance
- ActiveSupport::Testing::Performance::Benchmarker
- ActiveSupport::Testing::Performance::ForClassicTestUnit
- ActiveSupport::Testing::Performance::ForMiniTest
- ActiveSupport::Testing::Performance::Metrics
- ActiveSupport::Testing::Performance::Metrics::Amount
- ActiveSupport::Testing::Performance::Metrics::Base
- ActiveSupport::Testing::Performance::Metrics::CpuTime
- ActiveSupport::Testing::Performance::Metrics::DigitalInformationUnit
- ActiveSupport::Testing::Performance::Metrics::GcRuns
- ActiveSupport::Testing::Performance::Metrics::GcTime
- ActiveSupport::Testing::Performance::Metrics::Memory
- ActiveSupport::Testing::Performance::Metrics::Objects
- ActiveSupport::Testing::Performance::Metrics::ProcessTime
- ActiveSupport::Testing::Performance::Metrics::Time
- ActiveSupport::Testing::Performance::Metrics::UserTime
- ActiveSupport::Testing::Performance::Metrics::WallTime
- ActiveSupport::Testing::Performance::Performer
- ActiveSupport::Testing::Performance::Profiler
- ActiveSupport::Testing::ProxyTestResult
- ActiveSupport::Testing::RemoteError
- ActiveSupport::Testing::SetupAndTeardown
- ActiveSupport::Testing::SetupAndTeardown::ClassMethods
- ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit
- ActiveSupport::Testing::SetupAndTeardown::ForMiniTest
- ActiveSupport::TimeWithZone
- ActiveSupport::TimeZone
- ActiveSupport::XmlMini
- ActiveSupport::XmlMini_LibXMLSAX
- ActiveSupport::XmlMini_LibXMLSAX::HashBuilder
- ActiveSupport::XmlMini_NokogiriSAX
- ActiveSupport::XmlMini_NokogiriSAX::HashBuilder
- Test
- Test::Unit
- Test::Unit::Collector
- Test::Unit::Collector::ObjectSpace
- Object
- Object::HashWithIndifferentAccess
- Object::MissingSourceFile
- ERB
- ERB::Util
- I18n
- I18n::Railtie
- LoadError
- LoadError
- Logger
- Logger::SimpleFormatter
- Array
- Base64
- Benchmark
- BigDecimal
- Class
- Date
- DateTime
- Enumerable
- FalseClass
- File
- Float
- Hash
- Integer
- Kernel
- Module
- NameError
- NilClass
- Numeric
- Process
- QualifiedConstUtils
- Range
- Regexp
- String
- Symbol
- Time
- TrueClass
- URI

No matching classes.

# ActiveSupport::Notifications

# Notifications

+ActiveSupport::Notifications+ provides an instrumentation API for Ruby.

## Instrumenters

To instrument an event you just need to do:

    ActiveSupport::Notifications.instrument("render", :extra => :information) do
      render :text => "Foo"
    end

That executes the block first and notifies all subscribers once done.

In the example above “render” is the name of the event, and the rest is called the payload. The payload is a mechanism that allows instrumenters to pass extra information to subscribers. Payloads consist of a hash whose contents are arbitrary and generally depend on the event.

## Subscribers

You can consume those events and the information they provide by registering a subscriber. For instance, let’s store all “render” events in an array:

    events = []

    ActiveSupport::Notifications.subscribe("render") do |*args|
      events << ActiveSupport::Notifications::Event.new(*args)
    end

That code returns right away, you are just subscribing to “render” events. The block will be called asynchronously whenever someone instruments “render”:

    ActiveSupport::Notifications.instrument("render", :extra => :information) do
      render :text => "Foo"
    end

    event = events.first
    event.name      # => "render"
    event.duration  # => 10 (in milliseconds)
    event.payload   # => { :extra => :information }

The block in the `subscribe` call gets the name of the event, start timestamp, end timestamp, a string with a unique identifier for that event (something like “535801666f04d0298cd6”), and a hash with the payload, in that order.

If an exception happens during that particular instrumentation the payload will have a key `:exception` with an array of two elements as value: a string with the name of the exception class, and the exception message.

As the previous example depicts, the class +ActiveSupport::Notifications::Event+ is able to take the arguments as they come and provide an object-oriented interface to that data.

You can also subscribe to all events whose name matches a certain regexp:

    ActiveSupport::Notifications.subscribe(/render/) do |*args|
      ...
    end

and even pass no argument to `subscribe`, in which case you are subscribing to all events.

## Temporary Subscriptions

Sometimes you do not want to subscribe to an event for the entire life of the application. There are two ways to unsubscribe.

WARNING: The instrumentation framework is designed for long-running subscribers, use this feature sparingly because it wipes some internal caches and that has a negative impact on performance.

### Subscribe While a Block Runs

You can subscribe to some event temporarily while some block runs. For example, in

    callback = lambda {|*args| ... }
    ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
      ...
    end

the callback will be called for all “sql.active_record” events instrumented during the execution of the block. The callback is unsubscribed automatically after that.

### Manual Unsubscription

The `subscribe` method returns a subscriber object:

    subscriber = ActiveSupport::Notifications.subscribe("render") do |*args|
      ...
    end

To prevent that block from being called anymore, just unsubscribe passing that reference:

    ActiveSupport::Notifications.unsubscribe(subscriber)

## Default Queue

Notifications ships with a queue implementation that consumes and publish events to log subscribers in a thread. You can use any queue implementation you want.

### Attributes

notifier[RW]

### Public Class Methods

instrument(name, payload = {}) click to toggle source

    # File lib/active_support/notifications.rb, line 121
    def instrument(name, payload = {})
      if @instrumenters[name]
        instrumenter.instrument(name, payload) { yield payload if block_given? }
      else
        yield payload if block_given?
      end
    end

instrumenter() click to toggle source

    # File lib/active_support/notifications.rb, line 147
    def instrumenter
      Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
    end

publish(name, *args) click to toggle source

    # File lib/active_support/notifications.rb, line 117
    def publish(name, *args)
      notifier.publish(name, *args)
    end

subscribe(*args, &block) click to toggle source

    # File lib/active_support/notifications.rb, line 129
    def subscribe(*args, &block)
      notifier.subscribe(*args, &block).tap do
        @instrumenters.clear
      end
    end

subscribed(callback, *args, &block) click to toggle source

    # File lib/active_support/notifications.rb, line 135
    def subscribed(callback, *args, &block)
      subscriber = subscribe(*args, &callback)
      yield
    ensure
      unsubscribe(subscriber)
    end

unsubscribe(args) click to toggle source

    # File lib/active_support/notifications.rb, line 142
    def unsubscribe(args)
      notifier.unsubscribe(args)
      @instrumenters.clear
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.