-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1293 from appsignal/implement-heartbeat-checkins
Implement heartbeat checkins
- Loading branch information
Showing
10 changed files
with
586 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
bump: minor | ||
type: add | ||
--- | ||
|
||
Add support for heartbeat check-ins. | ||
|
||
Use the `Appsignal::CheckIn.heartbeat` method to send a single heartbeat check-in event from your application. This can be used, for example, in your application's main loop: | ||
|
||
```ruby | ||
loop do | ||
Appsignal::CheckIn.heartbeat("job_processor") | ||
process_job | ||
end | ||
``` | ||
|
||
Heartbeats are deduplicated and sent asynchronously, without blocking the current thread. Regardless of how often the `.heartbeat` method is called, at most one heartbeat with the same identifier will be sent every ten seconds. | ||
|
||
Pass `continuous: true` as the second argument to send heartbeats continuously during the entire lifetime of the current process. This can be used, for example, after your application has finished its boot process: | ||
|
||
```ruby | ||
def main | ||
start_app | ||
Appsignal::CheckIn.heartbeat("my_app", continuous: true) | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module CheckIn | ||
# @api private | ||
class Event | ||
class << self | ||
def new(check_in_type:, identifier:, digest: nil, kind: nil) | ||
{ | ||
:identifier => identifier, | ||
:digest => digest, | ||
:kind => kind, | ||
:timestamp => Time.now.utc.to_i, | ||
:check_in_type => check_in_type | ||
}.compact | ||
end | ||
|
||
def cron(identifier:, digest:, kind:) | ||
new( | ||
:check_in_type => "cron", | ||
:identifier => identifier, | ||
:digest => digest, | ||
:kind => kind | ||
) | ||
end | ||
|
||
def heartbeat(identifier:) | ||
new( | ||
:check_in_type => "heartbeat", | ||
:identifier => identifier | ||
) | ||
end | ||
|
||
def redundant?(event, other) | ||
return false if | ||
other[:check_in_type] != event[:check_in_type] || | ||
other[:identifier] != event[:identifier] | ||
|
||
return false if event[:check_in_type] == "cron" && ( | ||
other[:digest] != event[:digest] || | ||
other[:kind] != event[:kind] | ||
) | ||
|
||
return false if | ||
event[:check_in_type] != "cron" && | ||
event[:check_in_type] != "heartbeat" | ||
|
||
true | ||
end | ||
|
||
def describe(events) | ||
if events.empty? | ||
# This shouldn't happen. | ||
"no check-in events" | ||
elsif events.length > 1 | ||
"#{events.length} check-in events" | ||
else | ||
event = events.first | ||
if event[:check_in_type] == "cron" | ||
"cron check-in `#{event[:identifier] || "unknown"}` " \ | ||
"#{event[:kind] || "unknown"} event (digest #{event[:digest] || "unknown"})" | ||
elsif event[:check_in_type] == "heartbeat" | ||
"heartbeat check-in `#{event[:identifier] || "unknown"}` event" | ||
else | ||
"unknown check-in event" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.