Class: StatsD::Instrument::CompiledMetric::DatagramBlueprintBuilder

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

Overview

Helper class to build datagram blueprints at definition time. Handles prefix building, tag compilation, and blueprint construction.

Class Method Summary collapse

Class Method Details

.build(name:, type:, client_prefix:, no_prefix:, default_tags:, static_tags:, dynamic_tags:, sample_rate:) ⇒ String

Builds a datagram blueprint string

Parameters:

  • name (String)

    The metric name

  • type (String)

    The metric type (e.g., "c" for counter)

  • client_prefix (String, nil)

    The client's prefix

  • value_format (String)

    The sprintf format for the value (e.g., "%d", "%f")

  • no_prefix (Boolean)

    Whether to skip the prefix

  • default_tags (String, Hash, Array, nil)

    The client's default tags

  • static_tags (Hash)

    Static tags with fixed values

  • dynamic_tags (Hash)

    Dynamic tags with type specifications

  • sample_rate (Float, nil)

    The sample rate (0.0-1.0), nil for no sampling

  • enable_aggregation (Boolean)

    Whether aggregation is enabled

Returns:

  • (String)

    The datagram blueprint with sprintf placeholders



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/statsd/instrument/compiled_metric.rb', line 273

def build(name:, type:, client_prefix:, no_prefix:, default_tags:, static_tags:, dynamic_tags:, sample_rate:)
  # Normalize and build prefix
  normalized_name = normalize_name(name)
  prefix = build_prefix(client_prefix, no_prefix)

  # Compile all tags (default, static, dynamic)
  all_tags = compile_all_tags(default_tags, static_tags, dynamic_tags)

  # Build the datagram blueprint
  # Format: "<prefix><name>:<value_format>|<type>|@<sample_rate>|#<tags>"
  # Note: When aggregation is enabled, sample_rate is applied before aggregation
  if sample_rate && sample_rate < 1
    # Include sample_rate in the blueprint (only when not aggregating)
    if all_tags.empty?
      "#{prefix}#{normalized_name}:%s|#{type}|@#{sample_rate}"
    else
      "#{prefix}#{normalized_name}:%s|#{type}|@#{sample_rate}|##{all_tags}"
    end
  elsif all_tags.empty?
    "#{prefix}#{normalized_name}:%s|#{type}"
  else
    "#{prefix}#{normalized_name}:%s|#{type}|##{all_tags}"
  end
end

.normalize_name(name) ⇒ String

Normalizes metric names by replacing special characters

Parameters:

  • name (String)

    The metric name

Returns:

  • (String)

    The normalized metric name



301
302
303
# File 'lib/statsd/instrument/compiled_metric.rb', line 301

def normalize_name(name)
  name.tr(":|@", "_")
end