Skip to content

Commit 7f8b54f

Browse files
committed
docs
1 parent 0627714 commit 7f8b54f

File tree

3 files changed

+38
-67
lines changed

3 files changed

+38
-67
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ See the [Auth Plugins](docs/auth_plugins.md) documentation for even more informa
294294

295295
See the [Handler Plugins](docs/handler_plugins.md) documentation for in-depth information about handler plugins and how you can create your own to extend the functionality of the Hooks framework for your own deployment.
296296

297+
### Lifecycle Plugins
298+
299+
See the [Lifecycle Plugins](docs/lifecycle_plugins.md) documentation for information on how to create lifecycle plugins that can hook into the request/response/error lifecycle of the Hooks framework, allowing you to add custom behavior at various stages of processing webhook requests.
300+
301+
### Instrument Plugins
302+
303+
See the [Instrument Plugins](docs/instrument_plugins.md) documentation for information on how to create instrument plugins that can be used to collect metrics or report exceptions during webhook processing. These plugins can be used to integrate with monitoring and alerting systems.
304+
297305
## Contributing 🤝
298306

299307
See the [Contributing](CONTRIBUTING.md) document for information on how to contribute to the Hooks project, including setting up your development environment, running tests, and releasing new versions.

docs/instrument_plugins.md

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,35 @@
11
# Instrument Plugins
22

3-
Instrument plugins provide global components for cross-cutting concerns like metrics collection and error reporting. The hooks framework includes two built-in instrument types: `stats` for metrics and `failbot` for error reporting.
3+
Instrument plugins provide global components for cross-cutting concerns like metrics collection and error reporting. The hooks framework includes two built-in instrument types: `stats` for metrics and `failbot` for error reporting. By default, these instruments are no-op implementations that do not require any external dependencies. You can create custom implementations to integrate with your preferred monitoring and error reporting services.
44

55
## Overview
66

77
By default, the framework provides no-op stub implementations that do nothing. This allows you to write code that calls instrument methods without requiring external dependencies. You can replace these stubs with real implementations that integrate with your monitoring and error reporting services.
88

99
The instrument plugins are accessible throughout the entire application:
10+
1011
- In handlers via `stats` and `failbot` methods
1112
- In auth plugins via `stats` and `failbot` class methods
1213
- In lifecycle plugins via `stats` and `failbot` methods
1314

14-
## Built-in Instruments
15-
16-
### Stats
17-
18-
The stats instrument provides methods for metrics collection:
19-
20-
```ruby
21-
# Increment counters
22-
stats.increment("webhook.processed", { handler: "MyHandler" })
23-
24-
# Record values
25-
stats.record("webhook.payload_size", 1024, { event: "push" })
26-
27-
# Record timing manually
28-
stats.timing("webhook.duration", 0.5, { handler: "MyHandler" })
29-
30-
# Measure execution time automatically
31-
result = stats.measure("database.query", { table: "webhooks" }) do
32-
# Database operation here
33-
perform_database_query
34-
end
35-
```
36-
37-
### Failbot
38-
39-
The failbot instrument provides methods for error reporting:
15+
## Creating Custom Instruments
4016

41-
```ruby
42-
# Report exceptions
43-
begin
44-
risky_operation
45-
rescue => e
46-
failbot.report(e, { context: "webhook_processing" })
47-
end
17+
To create custom instrument implementations, inherit from the appropriate base class and implement the required methods.
4818

49-
# Report critical errors
50-
failbot.critical("Database connection lost", { service: "postgres" })
19+
To actually have `stats` and `failbot` do something useful, you need to create custom classes that inherit from the base classes provided by the framework. Here’s an example of how to implement custom stats and failbot plugins.
5120

52-
# Report warnings
53-
failbot.warning("Slow response time detected", { duration: 2.5 })
21+
You would then set the following attribute in your `hooks.yml` configuration file to point to these custom instrument plugins:
5422

55-
# Capture exceptions automatically
56-
result = failbot.capture({ operation: "webhook_validation" }) do
57-
validate_webhook_payload(payload)
58-
end
23+
```yaml
24+
# hooks.yml
25+
instruments_plugin_dir: ./plugins/instruments
5926
```
6027
61-
## Creating Custom Instruments
62-
63-
To create custom instrument implementations, inherit from the appropriate base class and implement the required methods.
64-
6528
### Custom Stats Implementation
6629
6730
```ruby
68-
# custom_stats.rb
69-
class CustomStats < Hooks::Plugins::Instruments::StatsBase
31+
# plugins/instruments/stats.rb
32+
class Stats < Hooks::Plugins::Instruments::StatsBase
7033
def initialize
7134
# Initialize your metrics client
7235
@client = MyMetricsService.new(
@@ -107,8 +70,8 @@ end
10770
### Custom Failbot Implementation
10871

10972
```ruby
110-
# custom_failbot.rb
111-
class CustomFailbot < Hooks::Plugins::Instruments::FailbotBase
73+
# plugins/instruments/failbot.rb
74+
class Failbot < Hooks::Plugins::Instruments::FailbotBase
11275
def initialize
11376
# Initialize your error reporting client
11477
@client = MyErrorService.new(
@@ -168,11 +131,11 @@ lifecycle_plugin_dir: ./plugins/lifecycle
168131
169132
Place your instrument plugin files in the specified directory:
170133
171-
```
134+
```text
172135
plugins/
173136
└── instruments/
174-
├── custom_stats.rb
175-
└── custom_failbot.rb
137+
├── stats.rb
138+
└── failbot.rb
176139
```
177140

178141
## File Naming and Class Detection
@@ -183,11 +146,11 @@ The framework automatically detects which type of instrument you're creating bas
183146
- Classes inheriting from `FailbotBase` become the `failbot` instrument
184147

185148
File naming follows snake_case to PascalCase conversion:
186-
- `custom_stats.rb``CustomStats`
187-
- `datadog_stats.rb``DatadogStats`
149+
150+
- `stats.rb``stats`
188151
- `sentry_failbot.rb``SentryFailbot`
189152

190-
You can only have one stats plugin and one failbot plugin loaded. If multiple plugins of the same type are found, the last one loaded will be used.
153+
You can only have one `stats` plugin and one `failbot` plugin loaded. If multiple plugins of the same type are found, the last one loaded will be used.
191154

192155
## Usage in Your Code
193156

@@ -364,4 +327,4 @@ expect(test_stats.recorded_metrics).to include(
364327
4. **Validate configuration**: Check for required environment variables in `initialize`
365328
5. **Document custom methods**: If you add methods beyond the base interface, document them
366329
6. **Consider performance**: Instruments are called frequently, so keep operations fast
367-
7. **Use connection pooling**: For high-throughput scenarios, use connection pooling for external services
330+
7. **Use connection pooling**: For high-throughput scenarios, use connection pooling for external services

docs/lifecycle_plugins.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ end
104104

105105
```ruby
106106
def on_request(env)
107-
# Increment counters
107+
# Increment counters (example)
108108
stats.increment("webhook.requests", { event: env["HTTP_X_GITHUB_EVENT"] })
109109

110-
# Record values
110+
# Record values (example)
111111
stats.record("webhook.payload_size", env["CONTENT_LENGTH"].to_i)
112112

113-
# Measure execution time
113+
# Measure execution time (example)
114114
stats.measure("webhook.processing", { handler: env["hooks.handler"] }) do
115115
# Processing happens in the handler
116116
end
117117
end
118118

119119
def on_response(env, response)
120-
# Record timing from environment
120+
# Record timing from environment (example)
121121
stats.timing("webhook.duration", env["hooks.processing_time"])
122122
end
123123
```
@@ -126,22 +126,22 @@ end
126126

127127
```ruby
128128
def on_error(exception, env)
129-
# Report errors with context
129+
# Report errors with context (example)
130130
failbot.report(exception, {
131131
endpoint: env["PATH_INFO"],
132132
event_type: env["HTTP_X_GITHUB_EVENT"],
133133
handler: env["hooks.handler"]
134134
})
135135

136-
# Report critical errors
136+
# Report critical errors (example)
137137
failbot.critical("Handler crashed", { handler: env["hooks.handler"] })
138138

139-
# Report warnings
139+
# Report warnings (example)
140140
failbot.warning("Slow webhook processing", { duration: env["hooks.processing_time"] })
141141
end
142142

143143
def on_request(env)
144-
# Capture and report exceptions during processing
144+
# Capture and report exceptions during processing (example)
145145
failbot.capture({ context: "request_validation" }) do
146146
validate_webhook_signature(env)
147147
end
@@ -161,7 +161,7 @@ auth_plugin_dir: ./plugins/auth
161161
162162
Place your lifecycle plugin files in the specified directory:
163163
164-
```
164+
```text
165165
plugins/
166166
└── lifecycle/
167167
├── metrics_lifecycle.rb
@@ -252,4 +252,4 @@ end
252252
2. **Handle errors gracefully**: Lifecycle plugins should not cause webhook processing to fail
253253
3. **Use appropriate log levels**: Debug for detailed info, info for normal flow, warn for issues, error for failures
254254
4. **Include relevant context**: Add useful tags and context to metrics and error reports
255-
5. **Test thoroughly**: Lifecycle plugins run for every webhook request, so bugs have high impact
255+
5. **Test thoroughly**: Lifecycle plugins run for every webhook request, so bugs have high impact

0 commit comments

Comments
 (0)