Skip to content

Commit

Permalink
Move circuit notifications from README.md to docs/circuit_notificatio…
Browse files Browse the repository at this point in the history
…ns.md
  • Loading branch information
matthewshafer committed May 4, 2023
1 parent 89c2b95 commit 6fec862
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 49 deletions.
50 changes: 1 addition & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,55 +130,7 @@ some pre-requisits need to be satisfied first:

## Notifications

Circuitbox has two built in notifiers, active support and null.
The active support notifier is used if `ActiveSupport::Notifications` is defined when circuitbox is loaded.
If `ActiveSupport::Notifications` is not defined the null notifier is used.
The null notifier does not send notifications anywhere.

The default notifier can be changed to use a specific built in notifier or a custom notifier, see [global configuration](#global-configuration).

### ActiveSupport
Usage example:

**Circuit open/close:**

```ruby
ActiveSupport::Notifications.subscribe('open.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
Rails.logger.warn("Open circuit for: #{circuit_name}")
end

ActiveSupport::Notifications.subscribe('close.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
Rails.logger.info("Close circuit for: #{circuit_name}")
end
```

**Circuit run:**

```ruby
ActiveSupport::Notifications.subscribe('run.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit_name]

Rails.logger.info("Circuit: #{circuit_name} Runtime: #{event.duration}")
end
```

**Circuit Warnings:**
In case of misconfiguration, only when a circuit is initialized, circuitbox will send a `warning.circuitbox` notification.

```ruby
ActiveSupport::Notifications.subscribe('warning.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
warning = event.payload[:message]
Rails.logger.warning("Circuit warning for: #{circuit_name} Message: #{warning}")
end

```
See [Circuit Notifications](docs/circuit_notifications.md)

## Faraday

Expand Down
80 changes: 80 additions & 0 deletions docs/circuit_notifications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Circuit Notifications

Circuitbox supports sending notifications when various events occur.
The following events are sent to the notifier:

* Circuit block runs (this is where notifiers do timing)
* Circuit is skipped
* Circuit run is successful
* Circuit run is failed
* Circuit is opened
* Circuit is closed
* Circuit is not configured correctly

There are two types of notifiers built into circuitbox, null and active support.
The null notifier does not send notifications.
The active support notifier sends notifications through `ActiveSupport::Notifications`.

## Active Support Notifications

There are three different types of notification payloads which are defined below

All notifications contain `:circuit` in the payload.
The value of `:circuit` is the name of the circuit.

The first type of notifications are:

* `open.circuitbox` - Sent when the circuit moves to the open state.
* `close.circuitbox` - Sent when the circuit moves to the closed state.
* `skipped.circuitbox` - Sent when the circuit is run and in the open state.
* `success.circuitbox` - Sent when the circuit is run and the run succeeds.
* `failure.circuitbox` - Sent when the circuit is run and the run fails.

The second type of notifications are contain a `:message` in the payload, in addition to `:circuit`.
The value of `:message` is a string.

* `warning.circuitbox` - Sent when there is a misconfiguration of the circuit.

The third type of notifications can be used for timing of the circuit.
The timing is done by `ActiveSupport::Notifications`.

* `run.circuitbox` - Sent after the circuit is run.

### Examples

#### Open/Close/Skipped/Success/Failure

```ruby
ActiveSupport::Notifications.subscribe('open.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
Rails.logger.warn("Open circuit for: #{circuit_name}")
end

ActiveSupport::Notifications.subscribe('close.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
Rails.logger.info("Close circuit for: #{circuit_name}")
end
```

#### Warning

```ruby
ActiveSupport::Notifications.subscribe('warning.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit]
warning = event.payload[:message]
Rails.logger.warning("Circuit warning for: #{circuit_name} Message: #{warning}")
end
```

#### Timing
```ruby
ActiveSupport::Notifications.subscribe('run.circuitbox') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
circuit_name = event.payload[:circuit_name]

Rails.logger.info("Circuit: #{circuit_name} Runtime: #{event.duration}")
end
```

0 comments on commit 6fec862

Please sign in to comment.