13
13
14
14
require 'traces'
15
15
require 'metrics'
16
+ require 'metrics/tags'
16
17
17
18
module Async
18
19
module Pool
@@ -29,7 +30,7 @@ def self.wrap(**options, &block)
29
30
# @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.
30
31
# @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource.
31
32
# @parameter policy [Policy] The pool policy.
32
- def initialize ( constructor , limit : nil , concurrency : ( limit || 1 ) , policy : nil )
33
+ def initialize ( constructor , limit : nil , concurrency : ( limit || 1 ) , policy : nil , tags : nil )
33
34
@constructor = constructor
34
35
@limit = limit
35
36
@@ -39,6 +40,8 @@ def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
39
40
@policy = policy
40
41
@gardener = nil
41
42
43
+ @tags = Metrics ::Tags . normalize ( tags )
44
+
42
45
# All available resources:
43
46
@resources = { }
44
47
@@ -96,6 +99,9 @@ def concurrency= value
96
99
# @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
97
100
attr :resources
98
101
102
+ # @attribute [Array(String)] The name of the pool.
103
+ attr_accessor :tags
104
+
99
105
# The number of resources in the pool.
100
106
def size
101
107
@resources . size
@@ -388,6 +394,7 @@ def create_resource(...)
388
394
concurrency : @guard . limit ,
389
395
size : @resources . size ,
390
396
limit : @limit ,
397
+ name : @name ,
391
398
}
392
399
393
400
Traces . trace ( 'async.pool.create' , attributes : attributes ) { super }
@@ -396,6 +403,7 @@ def create_resource(...)
396
403
def drain ( ...)
397
404
attributes = {
398
405
size : @resources . size ,
406
+ name : @name ,
399
407
}
400
408
401
409
Traces . trace ( 'async.pool.drain' , attributes : attributes ) { super }
@@ -407,21 +415,27 @@ def drain(...)
407
415
RELEASE_COUNT = Metrics . metric ( 'async.pool.release' , :counter , description : 'Number of times a resource was released.' )
408
416
RETIRE_COUNT = Metrics . metric ( 'async.pool.retire' , :counter , description : 'Number of times a resource was retired.' )
409
417
418
+ def metric_tags
419
+ if @name
420
+ [ "name:#{ @name } " ]
421
+ end
422
+ end
423
+
410
424
def acquire ( ...)
411
- ACQUIRE_COUNT . emit ( 1 )
425
+ ACQUIRE_COUNT . emit ( 1 , tags : self . metric_tags )
412
426
413
427
super
414
428
end
415
429
416
430
def release ( ...)
417
431
super . tap do
418
- RELEASE_COUNT . emit ( 1 )
432
+ RELEASE_COUNT . emit ( 1 , tags : self . metric_tags )
419
433
end
420
434
end
421
435
422
436
def retire ( ...)
423
437
super . tap do
424
- RETIRE_COUNT . emit ( 1 )
438
+ RETIRE_COUNT . emit ( 1 , tags : self . metric_tags )
425
439
end
426
440
end
427
441
end
0 commit comments