Class: PromMultiProc::Writer
- Inherits:
-
Object
- Object
- PromMultiProc::Writer
- Defined in:
- lib/prom_multi_proc/writer.rb
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(socket:, batch_size: 1, validate: false) ⇒ Writer
constructor
A new instance of Writer.
- #socket? ⇒ Boolean
- #validate? ⇒ Boolean
- #write(metric, method, value, labels) ⇒ Object
-
#write_multi(metrics) ⇒ Object
array of arrays where inner array is length 4 matching arguments for signature of #write.
Constructor Details
#initialize(socket:, batch_size: 1, validate: false) ⇒ Writer
Returns a new instance of Writer.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/prom_multi_proc/writer.rb', line 5 def initialize(socket:, batch_size: 1, validate: false) if !batch_size.is_a?(Integer) || batch_size <= 0 raise PromMultiProcError.new("Invalid batch size: #{batch_size}") end @batch_size = batch_size @validate = !!validate @lock = Mutex.new = [] @socket = socket end |
Instance Attribute Details
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
3 4 5 |
# File 'lib/prom_multi_proc/writer.rb', line 3 def batch_size @batch_size end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
3 4 5 |
# File 'lib/prom_multi_proc/writer.rb', line 3 def socket @socket end |
Instance Method Details
#flush ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/prom_multi_proc/writer.rb', line 49 def flush @lock.synchronize do if .length >= batch_size begin write_socket(JSON.generate()) ensure .clear end else true end end end |
#socket? ⇒ Boolean
63 64 65 |
# File 'lib/prom_multi_proc/writer.rb', line 63 def socket? !!write_socket("\n") end |
#validate? ⇒ Boolean
18 19 20 |
# File 'lib/prom_multi_proc/writer.rb', line 18 def validate? @validate end |
#write(metric, method, value, labels) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/prom_multi_proc/writer.rb', line 22 def write(metric, method, value, labels) @lock.synchronize do metric.validate!(method, value, labels) if validate? << metric.to_msg(method, value, labels) end flush end |
#write_multi(metrics) ⇒ Object
array of arrays where inner array is length 4 matching arguments for signature of #write
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/prom_multi_proc/writer.rb', line 33 def write_multi(metrics) @lock.synchronize do if validate? metrics.each do |m, method, value, labels| m.validate!(method, value, labels) end end metrics.each do |m, method, value, labels| << m.to_msg(method, value, labels) end end flush end |