Class: Sidekiq::Datadog::TagBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/datadog/tag_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(custom_tags = [], skip_tags = [], custom_hostname = nil) ⇒ TagBuilder

Returns a new instance of TagBuilder.



4
5
6
7
8
9
10
11
# File 'lib/sidekiq/datadog/tag_builder.rb', line 4

def initialize(custom_tags = [], skip_tags = [], custom_hostname = nil)
  @tags = Array(custom_tags)
  @skip_tags = Array(skip_tags).map(&:to_s)

  env  = Sidekiq.options[:environment] || ENV['RACK_ENV']
  host = custom_hostname || ENV['INSTRUMENTATION_HOSTNAME'] || Socket.gethostname
  setup_defaults(host: host, env: env)
end

Instance Method Details

#build_tags(worker_or_worker_class, job, queue = nil, error = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sidekiq/datadog/tag_builder.rb', line 13

def build_tags(worker_or_worker_class, job, queue = nil, error = nil)
  tags = @tags.flat_map do |tag|
    case tag
    when String then tag
    when Proc then tag.call(worker_or_worker_class, job, queue, error)
    end
  end
  tags.compact!

  tags.push "name:#{job_name(worker_or_worker_class, job)}" if include_tag?('name')
  tags.push "queue:#{queue}" if queue && include_tag?('queue')

  if error.nil?
    tags.push 'status:ok' if include_tag?('status')
  else
    kind = underscore(error.class.name.sub(/Error$/, ''))
    tags.push 'status:error' if include_tag?('status')
    tags.push "error:#{kind}" if include_tag?('error')
  end

  tags
end