Class: StatsD::Instrument::BatchedSink

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/statsd/instrument/batched_sink.rb

Defined Under Namespace

Classes: Buffer, Dispatcher, DispatcherStats

Constant Summary collapse

DEFAULT_THREAD_PRIORITY =
100
DEFAULT_BUFFER_CAPACITY =
5_000
DEFAULT_MAX_PACKET_SIZE =
1472
DEFAULT_STATISTICS_INTERVAL =

in seconds, and 0 implies disabled-by-default.

0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sink, thread_priority: DEFAULT_THREAD_PRIORITY, buffer_capacity: DEFAULT_BUFFER_CAPACITY, max_packet_size: DEFAULT_MAX_PACKET_SIZE, statistics_interval: DEFAULT_STATISTICS_INTERVAL) ⇒ BatchedSink

Returns a new instance of BatchedSink.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/statsd/instrument/batched_sink.rb', line 35

def initialize(
  sink,
  thread_priority: DEFAULT_THREAD_PRIORITY,
  buffer_capacity: DEFAULT_BUFFER_CAPACITY,
  max_packet_size: DEFAULT_MAX_PACKET_SIZE,
  statistics_interval: DEFAULT_STATISTICS_INTERVAL
)
  @sink = sink
  @dispatcher = Dispatcher.new(
    @sink,
    buffer_capacity,
    thread_priority,
    max_packet_size,
    statistics_interval,
  )
  ObjectSpace.define_finalizer(self, self.class.finalize(@dispatcher))
end

Class Method Details

.finalize(dispatcher) ⇒ Object



30
31
32
# File 'lib/statsd/instrument/batched_sink.rb', line 30

def finalize(dispatcher)
  proc { dispatcher.shutdown }
end

.for_addr(addr, **kwargs) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/statsd/instrument/batched_sink.rb', line 20

def for_addr(addr, **kwargs)
  if addr.include?(":")
    sink = StatsD::Instrument::Sink.for_addr(addr)
    new(sink, **kwargs)
  else
    connection = UdsConnection.new(addr)
    new(connection, **kwargs)
  end
end

Instance Method Details

#<<(datagram) ⇒ Object



57
58
59
60
# File 'lib/statsd/instrument/batched_sink.rb', line 57

def <<(datagram)
  @dispatcher << datagram
  self
end

#connectionObject



70
71
72
# File 'lib/statsd/instrument/batched_sink.rb', line 70

def connection
  @sink.connection
end

#flush(blocking:) ⇒ Object



66
67
68
# File 'lib/statsd/instrument/batched_sink.rb', line 66

def flush(blocking:)
  @dispatcher.flush(blocking: blocking)
end

#sample?(sample_rate) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/statsd/instrument/batched_sink.rb', line 53

def sample?(sample_rate)
  sample_rate == 1.0 || rand < sample_rate
end

#shutdown(*args) ⇒ Object



62
63
64
# File 'lib/statsd/instrument/batched_sink.rb', line 62

def shutdown(*args)
  @dispatcher.shutdown(*args)
end