module InstJobsStatsd::Naming

Constants

BASENAME

Public Class Methods

basename() click to toggle source

The root prefix for all stat names TODO: Make this configurable

# File lib/inst_jobs_statsd/naming.rb, line 7
def self.basename
  BASENAME
end
configure(strand_filter: nil) click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 11
def self.configure(strand_filter: nil)
  @strand_filter = strand_filter
end
custom_tags(job, tags) click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 51
def self.custom_tags(job, tags)
  tags[:jobshard] = job.shard.id if job.respond_to?(:shard)
  tags[:strand] = job.strand if job&.strand && @strand_filter&.call(job)
  tags
end
dd_job_tags(job) click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 37
def self.dd_job_tags(job)
  tags = dd_region_tags
  return tags unless job
  tags = custom_tags(job, tags)
  return tags unless job.tag
  return tags if job.tag =~ /Class:0x/

  method_tag, obj_tag = split_to_tag(job)
  tag = obj_tag
  tag = [obj_tag, method_tag].join('.') if method_tag.present?
  tags[:tag] = tag
  tags
end
dd_region_tags() click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 83
def self.dd_region_tags
  return {} unless ENV['INST_JOBS_STATSD_NAMESPACE']
  { namespace: ENV['INST_JOBS_STATSD_NAMESPACE'] }
end
job_tags(job) click to toggle source

this converts Foo#bar“ or ”Foo.bar“ into ”Foo and “bar”, and makes sure the values are valid to be used for statsd names

# File lib/inst_jobs_statsd/naming.rb, line 59
def self.job_tags(job)
  return unless job
  return unless job.tag
  return if job.tag =~ /Class:0x/

  method_tag, obj_tag = split_to_tag(job)
  tags = [obj_tag]
  tags << method_tag if method_tag.present?
  tags
end
qualified_names(stat_name, job) click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 15
def self.qualified_names(stat_name, job)
  names = ["#{basename}.#{stat_name}"]
  tagged = tagged_stat(names[0], job)
  names << tagged if tagged.present?
  names << region_tags(names)
  names.flatten.compact
end
region_tags(stat_names) click to toggle source

We are using all existing stat names here because we do not want to break existing dependencies on the non-regioned data

# File lib/inst_jobs_statsd/naming.rb, line 72
def self.region_tags(stat_names)
  return unless ENV['INST_JOBS_STATSD_NAMESPACE']

  stat_names.map do |name|
    name
      .split('.')
      .insert(2, ENV['INST_JOBS_STATSD_NAMESPACE'])
      .join('.')
  end
end
tagged_stat(stat_name, job) click to toggle source

Given a stat name, add a suffix to it to make it unique per job type – using the job's class name and method name as appropriate

# File lib/inst_jobs_statsd/naming.rb, line 26
def self.tagged_stat(stat_name, job)
  return unless job

  obj_tag, method_tag = job_tags(job)
  return if obj_tag.blank?

  tagged = "#{stat_name}.tag.#{obj_tag}"
  tagged += ".#{method_tag}" if method_tag.present?
  tagged
end

Private Class Methods

split_to_tag(job) click to toggle source
# File lib/inst_jobs_statsd/naming.rb, line 90
def self.split_to_tag(job)
  obj_tag, method_tag = job.tag.split(/[\.#]/, 2).map do |v|
    InstStatsd::Statsd.escape(v).gsub('::', '-')
  end
  [method_tag, obj_tag]
end