From 1632fd6591297744e13469f29ee380cf285de687 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 12 Oct 2024 19:44:29 +1300 Subject: [PATCH] Add support for pool tags. --- lib/async/pool/controller.rb | 22 ++++++++++++++++++---- test/async/pool/controller.rb | 8 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/async/pool/controller.rb b/lib/async/pool/controller.rb index 21fe3fc..2304656 100644 --- a/lib/async/pool/controller.rb +++ b/lib/async/pool/controller.rb @@ -13,6 +13,7 @@ require 'traces' require 'metrics' +require 'metrics/tags' module Async module Pool @@ -29,7 +30,7 @@ def self.wrap(**options, &block) # @parameter limit [Integer | Nil] The maximum number of resources that this pool can have at any given time. If nil, the pool can have an unlimited number of resources. # @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource. # @parameter policy [Policy] The pool policy. - def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil) + def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil, tags: nil) @constructor = constructor @limit = limit @@ -39,6 +40,8 @@ def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil) @policy = policy @gardener = nil + @tags = Metrics::Tags.normalize(tags) + # All available resources: @resources = {} @@ -96,6 +99,9 @@ def concurrency= value # @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage. attr :resources + # @attribute [Array(String)] The name of the pool. + attr_accessor :tags + # The number of resources in the pool. def size @resources.size @@ -388,6 +394,7 @@ def create_resource(...) concurrency: @guard.limit, size: @resources.size, limit: @limit, + name: @name, } Traces.trace('async.pool.create', attributes: attributes) {super} @@ -396,6 +403,7 @@ def create_resource(...) def drain(...) attributes = { size: @resources.size, + name: @name, } Traces.trace('async.pool.drain', attributes: attributes) {super} @@ -407,21 +415,27 @@ def drain(...) RELEASE_COUNT = Metrics.metric('async.pool.release', :counter, description: 'Number of times a resource was released.') RETIRE_COUNT = Metrics.metric('async.pool.retire', :counter, description: 'Number of times a resource was retired.') + def metric_tags + if @name + ["name:#{@name}"] + end + end + def acquire(...) - ACQUIRE_COUNT.emit(1) + ACQUIRE_COUNT.emit(1, tags: self.metric_tags) super end def release(...) super.tap do - RELEASE_COUNT.emit(1) + RELEASE_COUNT.emit(1, tags: self.metric_tags) end end def retire(...) super.tap do - RETIRE_COUNT.emit(1) + RETIRE_COUNT.emit(1, tags: self.metric_tags) end end end diff --git a/test/async/pool/controller.rb b/test/async/pool/controller.rb index 6dc6698..6b33d29 100644 --- a/test/async/pool/controller.rb +++ b/test/async/pool/controller.rb @@ -46,6 +46,14 @@ end end + with "tags" do + let(:pool) {subject.new(Async::Pool::Resource, tags: {a: 1, b: 2})} + + it "can assign tags to the pool" do + expect(pool.tags).to be == ["a:1", "b:2"] + end + end + with 'a limited pool' do let(:pool) {subject.new(Async::Pool::Resource, limit: 1)}