Sha256: d1a9f006f9e82ce5ce3ad86b047eff56a8d3be3f09129da027089e4d5393b109

Contents?: true

Size: 932 Bytes

Versions: 5

Compression:

Stored size: 932 Bytes

Contents

# frozen_string_literal: true

class Async::App::Metrics::Server
  include Async::Logger

  PATHS = ["/metrics", "/metrics/"].freeze

  def initialize(prefix:, port: 8080)
    @prefix = prefix
    @port = port
  end

  def run
    Async::App::Metrics::RubyRuntimeMonitor.new.run { update_metrics(_1) }

    endpoint = Async::HTTP::Endpoint.parse("http://0.0.0.0:#{@port}")
    Async { Async::HTTP::Server.new(self, endpoint).run }
    info { "Started on #{endpoint.url}" }
  end

  def call(request)
    return Protocol::HTTP::Response[404, {}, ["Not found"]] unless PATHS.include?(request.path)

    Protocol::HTTP::Response[200, {}, serializer.serialize(metrics_store)]
  end

  def update_metrics(metrics) = metrics.each { metrics_store.set(_1, **_2) }

  private

  def metrics_store = @metrics_store ||= Async::App::Metrics::Store.new
  def serializer = @serializer ||= Async::App::Metrics::Serializer.new(prefix: @prefix)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
async-tools-0.2.7 lib/async/app/metrics/server.rb
async-tools-0.2.6 lib/async/app/metrics/server.rb
async-tools-0.2.5 lib/async/app/metrics/server.rb
async-tools-0.2.4 lib/async/app/metrics/server.rb
async-tools-0.2.2 lib/async/app/metrics/server.rb