From 43f16eabacc7819a9786007a6d3e33ce965adecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=80=D0=B0=D1=81=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=90=D0=BD=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Fri, 3 May 2024 07:20:24 +0000 Subject: [PATCH] [DEX-2163] feat: autostart yabeda server --- CHANGELOG.md | 6 +++ README.md | 4 ++ .../outbox/install/templates/outbox.yml | 2 + lib/sbmt/outbox.rb | 1 + lib/sbmt/outbox/cli.rb | 2 +- lib/sbmt/outbox/probes/metrics.rb | 52 +++++++++++++++++++ lib/sbmt/outbox/probes/probe.rb | 10 ++++ lib/sbmt/outbox/version.rb | 2 +- 8 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 lib/sbmt/outbox/probes/metrics.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index a3e20f1..e59fedc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - yyyy-mm-dd +## [6.4.0] - 2024-05-02 + +### Added +- added autostart of the yabeda server via the `enabled` option +- added disabling autorun of probes via the `enabled` option + ## [6.3.1] - 2024-05-01 ### Added diff --git a/README.md b/README.md index 59164f5..1603266 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,11 @@ The `outbox.yml` configuration file is the main configuration for the gem, where default: &default owner: foo-team # optional, used in Yabeda metrics bucket_size: 16 # optional, default 16, see into about the buckets at the #Concurrency section + metrics: + enabled: true # default false, yabeda server autostart with port: 9090 and path: /metrics + port: 9090 # optional, default, used in Yabeda metrics probes: + enabled: false # optional, default true port: 5555 # default, used for Kubernetes probes outbox_items: # outbox items section diff --git a/lib/generators/outbox/install/templates/outbox.yml b/lib/generators/outbox/install/templates/outbox.yml index 00a5e93..78e05d5 100644 --- a/lib/generators/outbox/install/templates/outbox.yml +++ b/lib/generators/outbox/install/templates/outbox.yml @@ -1,6 +1,8 @@ default: &default owner: owner-team bucket_size: 16 + metrics: + enabled: true probes: port: SET-UP-YOUR-HEALTHCHECK-PORT-HERE diff --git a/lib/sbmt/outbox.rb b/lib/sbmt/outbox.rb index 22d62e3..37ace1c 100644 --- a/lib/sbmt/outbox.rb +++ b/lib/sbmt/outbox.rb @@ -38,6 +38,7 @@ require_relative "outbox/middleware/builder" require_relative "outbox/middleware/runner" require_relative "outbox/probes/probe" +require_relative "outbox/probes/metrics" require_relative "outbox/redis_client_factory" module Sbmt diff --git a/lib/sbmt/outbox/cli.rb b/lib/sbmt/outbox/cli.rb index 96c3639..71f2c9d 100644 --- a/lib/sbmt/outbox/cli.rb +++ b/lib/sbmt/outbox/cli.rb @@ -69,8 +69,8 @@ def start $stdout.puts AsciiArt.logo $stdout.puts "Outbox/Inbox worker has been started" $stdout.puts "Version: #{VERSION}" - $stdout.puts "Starting probes..." Sbmt::Outbox::Probes::Probe.run_probes + Sbmt::Outbox::Probes::Metrics.run_metrics worker.start end diff --git a/lib/sbmt/outbox/probes/metrics.rb b/lib/sbmt/outbox/probes/metrics.rb new file mode 100644 index 0000000..a66f1f8 --- /dev/null +++ b/lib/sbmt/outbox/probes/metrics.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module Sbmt + module Outbox + module Probes + class Metrics + DEFAULT_YABEDA_PORT = 9090 + DEFAULT_YABEDA_PATH = "/metrics" + class << self + def run_metrics + return unless autostart_yabeda_server? + + if defined?(Yabeda) + $stdout.puts "Starting metrics http-server..." + + start_webrick( + Yabeda::Prometheus::Mmap::Exporter::NOT_FOUND_HANDLER, + middlewares: {::Yabeda::Prometheus::Exporter => {path: DEFAULT_YABEDA_PATH}}, + port: yabeda_port + ) + end + end + + private + + def yabeda_port + Sbmt::Outbox.yaml_config.dig(:metrics, :port) || DEFAULT_YABEDA_PORT + end + + def start_webrick(app, middlewares:, port:) + Thread.new do + ::Rack::Handler::WEBrick.run( + ::Rack::Builder.new do + middlewares.each do |middleware, options| + use middleware, **options + end + run app + end, + Host: "0.0.0.0", + Port: port + ) + end + end + + def autostart_yabeda_server? + Sbmt::Outbox.yaml_config.dig(:metrics, :enabled) || false + end + end + end + end + end +end diff --git a/lib/sbmt/outbox/probes/probe.rb b/lib/sbmt/outbox/probes/probe.rb index 83e2b72..40b0a95 100644 --- a/lib/sbmt/outbox/probes/probe.rb +++ b/lib/sbmt/outbox/probes/probe.rb @@ -7,6 +7,10 @@ class Probe DEFAULT_PROBE_PORT = 5555 class << self def run_probes + return unless autostart_probe? + + $stdout.puts "Starting probes..." + ::HttpHealthCheck.run_server_async( port: probe_port, rack_app: HttpHealthCheck::RackApp.configure do |c| @@ -31,6 +35,12 @@ def probe_port Sbmt::Outbox.yaml_config.fetch(:probes).fetch(:port) end + + def autostart_probe? + value = Sbmt::Outbox.yaml_config.dig(:probes, :enabled) + value = true if value.nil? + value + end end end end diff --git a/lib/sbmt/outbox/version.rb b/lib/sbmt/outbox/version.rb index 38e631b..141f68f 100644 --- a/lib/sbmt/outbox/version.rb +++ b/lib/sbmt/outbox/version.rb @@ -2,6 +2,6 @@ module Sbmt module Outbox - VERSION = "6.3.1" + VERSION = "6.4.0" end end