Class: SemanticLogger::Formatters::Signalfx
- Defined in:
- lib/semantic_logger/formatters/signalfx.rb
Constant Summary
Constants inherited from Base
Base::CONTROL_CHARS, Base::CONTROL_CHAR_ESCAPES, Base::PRECISION, Base::UNICODE_CONTROL_CHARS
Instance Attribute Summary collapse
-
#counter_name ⇒ Object
Returns the value of attribute counter_name.
-
#dimensions ⇒ Object
Returns the value of attribute dimensions.
-
#gauge_name ⇒ Object
Returns the value of attribute gauge_name.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#token ⇒ Object
Returns the value of attribute token.
Attributes inherited from Base
#backtrace_cleaner, #escape_control_chars, #log, #log_application, #log_environment, #log_exception_details, #log_host, #logger, #precision, #time_format
Instance Method Summary collapse
-
#batch(logs, logger) ⇒ Object
Returns [Hash] a batch of log messages.
-
#call(log, logger) ⇒ Object
Returns [Hash] log message in Signalfx format.
-
#format_dimensions ⇒ Object
Dimensions for this metric: those of the log entry that are not blank, or when it has none, the named tags listed in the dimensions option whose values are not blank (see #blank_value?).
-
#initialize(token:, dimensions: nil, gauge_name: "Application.average", counter_name: "Application.counter", time_format: :ms, **args) ⇒ Signalfx
constructor
A new instance of Signalfx.
-
#log=(log) ⇒ Object
The log entry to format.
-
#metric ⇒ Object
Create SignalFx friendly metric.
-
#time ⇒ Object
Date & time.
-
#value ⇒ Object
Value of this metric.
Methods inherited from Base
Constructor Details
#initialize(token:, dimensions: nil, gauge_name: "Application.average", counter_name: "Application.counter", time_format: :ms, **args) ⇒ Signalfx
Returns a new instance of Signalfx.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 11 def initialize(token:, dimensions: nil, gauge_name: "Application.average", counter_name: "Application.counter", time_format: :ms, **args) @token = token @dimensions = dimensions.map(&:to_sym) if dimensions @gauge_name = gauge_name @counter_name = counter_name super(time_format: time_format, **args) end |
Instance Attribute Details
#counter_name ⇒ Object
Returns the value of attribute counter_name.
9 10 11 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 9 def counter_name @counter_name end |
#dimensions ⇒ Object
Returns the value of attribute dimensions.
9 10 11 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 9 def dimensions @dimensions end |
#gauge_name ⇒ Object
Returns the value of attribute gauge_name.
9 10 11 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 9 def gauge_name @gauge_name end |
#hash ⇒ Object
Returns the value of attribute hash.
9 10 11 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 9 def hash @hash end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 9 def token @token end |
Instance Method Details
#batch(logs, logger) ⇒ Object
Returns [Hash] a batch of log messages. Signalfx has a minimum resolution of 1 second. Metrics of the same type, time (second), and dimensions can be aggregated together.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 117 def batch(logs, logger) self.logger = logger data = {} logs.each do |log| self.hash = {} self.log = log metric time value format_dimensions if log.duration gauges = (data[:gauge] ||= []) add_gauge(gauges, hash) # Also send a count metric whenever it is a gauge so that it can be counted. unless metric_dimensions count_hash = hash.dup count_hash[:value] = log.metric_amount || 1 count_hash[:metric] = counter_name counters = (data[:counter] ||= []) add_counter(counters, count_hash) end else counters = (data[:counter] ||= []) add_counter(counters, hash) end end Utils.to_json(data) end |
#call(log, logger) ⇒ Object
Returns [Hash] log message in Signalfx format.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 86 def call(log, logger) self.hash = {} self.log = log self.logger = logger metric time value format_dimensions # gauge, counter, or cumulative_counter data = {} if log.duration data[:gauge] = [hash] # Also send a count metric whenever it is a gauge so that it can be counted. unless metric_dimensions count_hash = hash.dup count_hash[:value] = log.metric_amount || 1 count_hash[:metric] = counter_name data[:counter] = [count_hash] end else data[:counter] = [hash] end Utils.to_json(data) end |
#format_dimensions ⇒ Object
Dimensions for this metric: those of the log entry that are not blank, or when it has none, the named tags listed in the dimensions option whose values are not blank (see #blank_value?).
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 67 def format_dimensions h = (hash[:dimensions] ||= {}) if metric_dimensions h.merge!(metric_dimensions) elsif dimensions log..each_pair do |name, value| name = name.to_sym next unless dimensions.include?(name) value = value.to_s h[name] = value unless blank_value?(value) end end h[:host] = logger.host if log_host && logger.host h[:application] = logger.application if log_application && logger.application h[:environment] = logger.environment if log_environment && logger.environment end |
#log=(log) ⇒ Object
The log entry to format. Also keeps the dimensions of its metric that are sent (see #present_dimensions), which formatting the entry asks for several times.
153 154 155 156 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 153 def log=(log) super @metric_dimensions = log && present_dimensions end |
#metric ⇒ Object
Create SignalFx friendly metric. Strip leading '/' Convert remaining '/' to '.' A name that is not valid UTF-8, such as one with invalid bytes or in UTF-16, which matching it raises for, is converted to valid UTF-8 first (see Utils.encode_utf8_string).
A metric with dimensions that are not blank (see #present_dimensions) is sent under its own name. Any other is sent as the gauge or the counter, with the class and action taken from its name.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 33 def metric name = Utils.encode_utf8_string(log.metric.to_s).sub(%r{\A/+}, "") if metric_dimensions name.tr!("/", ".") hash[:metric] = name else # Extract class and action from metric name names = name.split("/") h = (hash[:dimensions] ||= {}) if names.size > 1 h[:action] = names.pop h[:class] = names.join("::") else h[:class] = "Unknown" h[:action] = names.first || log.metric end hash[:metric] = log.duration ? gauge_name : counter_name end end |
#time ⇒ Object
Date & time
55 56 57 58 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 55 def time # 1 second resolution, represented as ms. hash[:timestamp] = log.time.to_i * 1000 end |
#value ⇒ Object
Value of this metric
61 62 63 |
# File 'lib/semantic_logger/formatters/signalfx.rb', line 61 def value hash[:value] = log.metric_amount || log.duration || 1 end |