Module: Yabeda::DSL::ClassMethods

Defined in:
lib/yabeda/dsl/class_methods.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#adapter(*adapter_names, group: @group) ⇒ Object

Limit all group metrics to specific adapters only

Parameters:

  • adapter_names (Array<Symbol>)

    Names of adapters to use



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/yabeda/dsl/class_methods.rb', line 99

def adapter(*adapter_names, group: @group)
  raise ConfigurationError, "Adapter limitation can't be defined without adapter_names" if adapter_names.empty?

  @adapter_names = adapter_names
  if group
    include_group(group)
  else
    return yield if block_given?

    raise ConfigurationError, "Yabeda.adapter should be called either inside group declaration " \
      "or should have block provided with a call to include_group. No metric group provided."
  end
ensure @adapter_names = nil
end

#collect(&block) ⇒ Object

Define the actions that should be performed



25
26
27
# File 'lib/yabeda/dsl/class_methods.rb', line 25

def collect(&block)
  ::Yabeda.collectors.push(block)
end

#configure(&block) ⇒ Object

Block for grouping and simplifying configuration of related metrics



18
19
20
21
22
# File 'lib/yabeda/dsl/class_methods.rb', line 18

def configure(&block)
  Yabeda.configurators.push([@group, block])
  class_exec(&block) if Yabeda.configured?
  @group = nil
end

#counter(*args, **kwargs, &block) ⇒ Object

Register a growing-only counter



43
44
45
46
# File 'lib/yabeda/dsl/class_methods.rb', line 43

def counter(*args, **kwargs, &block)
  metric = MetricBuilder.new(Counter).build(args, kwargs, @group, &block)
  register_metric(metric)
end

#default_tag(name, value, group: @group) ⇒ Object

Add default tag for all metric

Parameters:

  • name (Symbol)

    Name of default tag

  • value (String)

    Value of default tag



70
71
72
73
74
75
76
77
# File 'lib/yabeda/dsl/class_methods.rb', line 70

def default_tag(name, value, group: @group)
  if group
    Yabeda.groups[group] ||= Yabeda::Group.new(group)
    Yabeda.groups[group].default_tag(name, value)
  else
    Yabeda.default_tags[name] = value
  end
end

#except(*metric_names, group: @group) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/yabeda/dsl/class_methods.rb', line 124

def except(*metric_names, group: @group)
  if group
    Yabeda.groups[group] ||= Yabeda::Group.new(group)
    Yabeda.groups[group].except(*metric_names)
  else
    raise ConfigurationError, "Yabeda.except should be called either inside group declaration " \
      "or should have group name provided. No metric group provided."
  end
end

#gauge(*args, **kwargs, &block) ⇒ Object

Register a gauge



49
50
51
52
# File 'lib/yabeda/dsl/class_methods.rb', line 49

def gauge(*args, **kwargs, &block)
  metric = MetricBuilder.new(Gauge).build(args, kwargs, @group, &block)
  register_metric(metric)
end

#group(group_name) ⇒ Object

Specify metric category or group for all consecutive metrics in this configure block. On most adapters it is only adds prefix to the metric name but on some (like NewRelic) it is treated individually and has a special meaning.



33
34
35
36
37
38
39
40
# File 'lib/yabeda/dsl/class_methods.rb', line 33

def group(group_name)
  @group = group_name
  Yabeda.groups[@group] ||= Yabeda::Group.new(@group)
  return unless block_given?

  yield
  @group = nil
end

#histogram(*args, **kwargs, &block) ⇒ Object

Register a histogram



55
56
57
58
# File 'lib/yabeda/dsl/class_methods.rb', line 55

def histogram(*args, **kwargs, &block)
  metric = MetricBuilder.new(Histogram).build(args, kwargs, @group, &block)
  register_metric(metric)
end

#include_group(group) ⇒ Object

Raises:



134
135
136
137
138
139
# File 'lib/yabeda/dsl/class_methods.rb', line 134

def include_group(group)
  raise ConfigurationError, "Adapter limitation can't be defined without of group name" unless group

  Yabeda.groups[group] ||= Yabeda::Group.new(group)
  Yabeda.groups[group].adapter(*@adapter_names)
end

#only(*metric_names, group: @group) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/yabeda/dsl/class_methods.rb', line 114

def only(*metric_names, group: @group)
  if group
    Yabeda.groups[group] ||= Yabeda::Group.new(group)
    Yabeda.groups[group].only(*metric_names)
  else
    raise ConfigurationError, "Yabeda.only should be called either inside group declaration " \
      "or should have group name provided. No metric group provided."
  end
end

#summary(*args, **kwargs, &block) ⇒ Object

Register a summary



61
62
63
64
# File 'lib/yabeda/dsl/class_methods.rb', line 61

def summary(*args, **kwargs, &block)
  metric = MetricBuilder.new(Summary).build(args, kwargs, @group, &block)
  register_metric(metric)
end

#temporary_tagsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get tags set by with_tags

Returns:

  • Hash



92
93
94
# File 'lib/yabeda/dsl/class_methods.rb', line 92

def temporary_tags
  Thread.current[:yabeda_temporary_tags] ||= {}
end

#with_tags(**tags) ⇒ Object

Redefine default tags for a limited amount of time

Parameters:

  • tags

    HashSymbol=>#to_s



81
82
83
84
85
86
87
# File 'lib/yabeda/dsl/class_methods.rb', line 81

def with_tags(**tags)
  previous_temp_tags = temporary_tags
  Thread.current[:yabeda_temporary_tags] = Thread.current[:yabeda_temporary_tags].merge(tags)
  yield
ensure
  Thread.current[:yabeda_temporary_tags] = previous_temp_tags
end