-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improves the metrics broadcast interface, in step with the web browse…
…r visualization project
- Loading branch information
Showing
15 changed files
with
359 additions
and
34 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
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,17 @@ | ||
require "./inspector/*" | ||
|
||
module Mosquito::Inspector | ||
def self.list_queues : Array(Queue) | ||
Mosquito.backend.list_queues | ||
.map { |name| Queue.new name } | ||
end | ||
|
||
def self.list_runners : Array(Runner) | ||
Mosquito.backend.list_runners | ||
.map { |name| Runner.new name } | ||
end | ||
|
||
def self.event_receiver : Channel(Backend::BroadcastMessage) | ||
Mosquito.backend.subscribe "mosquito:*" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Mosquito::Inspector | ||
class Queue | ||
include Comparable(self) | ||
|
||
getter name : String | ||
|
||
private property backend : Mosquito::Backend | ||
|
||
def initialize(@name) | ||
@backend = Mosquito.backend.named name | ||
end | ||
|
||
{% for name in Mosquito::Backend::QUEUES %} | ||
def {{name.id}}_job_runs : Array(JobRun) | ||
backend.dump_{{name.id}}_q | ||
.map { |task_id| JobRun.new task_id } | ||
end | ||
{% end %} | ||
|
||
def <=>(other) | ||
name <=> other.name | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module Mosquito::Inspector | ||
class Runner | ||
include Comparable(self) | ||
|
||
getter name : String | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def <=>(other) | ||
name <=> other.name | ||
end | ||
|
||
def config | ||
key = Mosquito.backend.build_key "runners", name | ||
config = Mosquito.backend.retrieve key | ||
end | ||
|
||
def current_job : JobRun? | ||
job_run = config["current_work"]? | ||
return unless job_run && ! job_run.blank? | ||
JobRun.new job_run | ||
end | ||
|
||
def last_heartbeat : Time? | ||
unix_ms = config["heartbeat_at"]? | ||
return unless unix_ms && ! unix_ms.blank? | ||
Time.unix(unix_ms.to_i) | ||
end | ||
|
||
def last_active : String | ||
if timestamp = last_heartbeat | ||
seconds = (Time.utc - timestamp).total_seconds.to_i | ||
|
||
if seconds < 21 | ||
colorize_by_last_heartbeat seconds, "online" | ||
else | ||
colorize_by_last_heartbeat seconds, "seen #{seconds}s ago" | ||
end | ||
|
||
else | ||
colorize_by_last_heartbeat 301, "expired" | ||
end | ||
end | ||
|
||
def colorize_by_last_heartbeat(seconds : Int32, word : String) : String | ||
if seconds < 30 | ||
word.colorize(:green) | ||
elsif seconds < 200 | ||
word.colorize(:yellow) | ||
else | ||
word.colorize(:red) | ||
end.to_s | ||
end | ||
|
||
def status : String | ||
if job_run = current_job | ||
"job run: #{job_run.type}" | ||
else | ||
"idle" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Mosquito::Inspector | ||
class JobRun | ||
getter id : String | ||
|
||
def initialize(@id : String) | ||
end | ||
|
||
def config_key | ||
Mosquito.backend.build_key Mosquito::JobRun::CONFIG_KEY_PREFIX, id | ||
end | ||
|
||
def type : String | ||
Mosquito.backend.retrieve(config_key)["type"]? || "unknown" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module Mosquito | ||
class PublishContext | ||
property originator : String | ||
property context : String | ||
|
||
def initialize(context : Array(String | Symbol)) | ||
@context = KeyBuilder.build context | ||
@originator = KeyBuilder.build "mosquito", @context | ||
end | ||
|
||
def initialize(parent : self, context : Array(String | Symbol)) | ||
@context = KeyBuilder.build context | ||
@originator = KeyBuilder.build "mosquito", parent.context, context | ||
end | ||
end | ||
|
||
class Metrics | ||
Log = ::Log.for self | ||
|
||
module Shorthand | ||
def metric | ||
if Mosquito.configuration.send_metrics | ||
with Metrics.instance yield | ||
end | ||
end | ||
end | ||
|
||
property send_metrics : Bool | ||
|
||
def self.instance | ||
@@instance ||= new | ||
end | ||
|
||
def initialize | ||
@send_metrics = Mosquito.configuration.send_metrics | ||
end | ||
|
||
def beat_heart(metadata : Metadata) : Nil | ||
return unless send_metrics | ||
Log.info { "Beating Heart" } | ||
|
||
# update the timestamp | ||
metadata["heartbeat_at"] = Time.utc.to_unix.to_s | ||
|
||
metadata.delete(Mosquito.configuration.heartbeat_interval * 10) | ||
end | ||
|
||
def publish(context : PublishContext, data : NamedTuple) : Nil | ||
Mosquito.backend.publish( | ||
context.originator, | ||
data.to_json | ||
) | ||
end | ||
|
||
def tick_metrics(stage : String) : Nil | ||
time = Time.utc | ||
|
||
Mosquito.backend.tap do |backend| | ||
daily_bucket = metrics_key({stage, "daily", time.month, time.day}) | ||
hourly_bucket = metrics_key({stage, "hourly", time.hour}) | ||
minutely_bucket = metrics_key({stage, "minutely", time.hour, time.minute}) | ||
|
||
backend.increment daily_bucket, Backend.build_key(time.month, time.day) | ||
backend.increment hourly_bucket, Backend.build_key(time.day, time.hour) | ||
backend.increment minutely_bucket, Backend.build_key(time.hour, time.minute) | ||
|
||
backend.delete daily_bucket, in: 2.days | ||
backend.delete hourly_bucket, in: 24.hours | ||
backend.delete minutely_bucket, in: 1.hour | ||
end | ||
end | ||
|
||
def metrics_key(key_parts : Tuple) | ||
Backend.build_key "metrics", "runner" | ||
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
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.