Module: InstJobsStatsd::Naming

Defined in:
lib/inst_jobs_statsd/naming.rb

Constant Summary collapse

BASENAME =
"delayedjob"

Class Method Summary collapse

Class Method Details

.basenameObject

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



9
10
11
# File 'lib/inst_jobs_statsd/naming.rb', line 9

def self.basename
  BASENAME
end

.dd_job_tags(job) ⇒ Object



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

def self.dd_job_tags(job)
  tags = dd_region_tags
  return tags unless job

  tags[:cluster] = job.current_shard&.database_server&.id if job.respond_to?(:current_shard)
  tags[:priority] = job.priority
  tags.compact!

  return tags unless job.tag
  return tags if job.tag.include?("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_tagsObject



79
80
81
82
83
# File 'lib/inst_jobs_statsd/naming.rb', line 79

def self.dd_region_tags
  return {} unless ENV["INST_JOBS_STATSD_NAMESPACE"]

  { namespace: ENV["INST_JOBS_STATSD_NAMESPACE"] }
end

.job_tags(job) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
# File 'lib/inst_jobs_statsd/naming.rb', line 55

def self.job_tags(job)
  return unless job
  return unless job.tag
  return if job.tag.include?("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) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/inst_jobs_statsd/naming.rb', line 13

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) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/inst_jobs_statsd/naming.rb', line 68

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

.split_to_tag(job) ⇒ Object



85
86
87
88
89
90
# File 'lib/inst_jobs_statsd/naming.rb', line 85

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

.tagged_stat(stat_name, job) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/inst_jobs_statsd/naming.rb', line 24

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