Class: StatsD::Instrument::CompiledMetric::PrecompiledDatagram

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/instrument/compiled_metric.rb

Overview

A precompiled datagram that can quickly build the final StatsD datagram string using sprintf formatting with cached tag values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_values, datagram_blueprint, sample_rate) ⇒ PrecompiledDatagram



358
359
360
361
362
# File 'lib/statsd/instrument/compiled_metric.rb', line 358

def initialize(tag_values, datagram_blueprint, sample_rate)
  @tag_values = tag_values
  @datagram_blueprint = datagram_blueprint
  @sample_rate = sample_rate
end

Instance Attribute Details

#datagram_blueprintObject (readonly)

Returns the value of attribute datagram_blueprint.



353
354
355
# File 'lib/statsd/instrument/compiled_metric.rb', line 353

def datagram_blueprint
  @datagram_blueprint
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



353
354
355
# File 'lib/statsd/instrument/compiled_metric.rb', line 353

def sample_rate
  @sample_rate
end

#tag_valuesObject (readonly)

Returns the value of attribute tag_values.



353
354
355
# File 'lib/statsd/instrument/compiled_metric.rb', line 353

def tag_values
  @tag_values
end

Instance Method Details

#to_datagram(value) ⇒ String

Builds the final datagram string by substituting values into the blueprint



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/statsd/instrument/compiled_metric.rb', line 367

def to_datagram(value)
  packed_value = if value.is_a?(Array)
    value.join(":")
  else
    value.to_s
  end

  # Fast path: no tag values (static metrics)
  return @datagram_blueprint % packed_value if @tag_values.empty?

  # Sanitize string and symbol values (other types handled by sprintf %s)
  values = @tag_values.map do |arg|
    if arg.is_a?(String)
      /[|,]/.match?(arg) ? arg.tr("|,", "") : arg
    elsif arg.is_a?(Symbol)
      str = arg.to_s
      /[|,]/.match?(str) ? str.tr("|,", "") : str
    else
      arg
    end
  end

  # Prepend the metric value
  values.unshift(packed_value)

  # Use sprintf to build the final datagram
  @datagram_blueprint % values
end