Module: InstJobsStatsd::Naming

Defined in:
lib/inst_jobs_statsd/naming.rb

Constant Summary collapse

BASENAME =
'delayedjob'.freeze

Class Method Summary collapse

Class Method Details

.basenameObject

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



7
8
9
# File 'lib/inst_jobs_statsd/naming.rb', line 7

def self.basename
  BASENAME
end

.configure(strand_filter: nil) ⇒ Object



11
12
13
# File 'lib/inst_jobs_statsd/naming.rb', line 11

def self.configure(strand_filter: nil)
  @strand_filter = strand_filter
end

.custom_tags(job, tags) ⇒ Object



51
52
53
54
55
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_tagsObject



83
84
85
86
# 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) ⇒ 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



59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



15
16
17
18
19
20
21
# 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) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
# 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) ⇒ 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



26
27
28
29
30
31
32
33
34
35
# 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