Class: SemanticLogger::Metric::Statsd
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Metric::Statsd
- Defined in:
- lib/semantic_logger/metric/statsd.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from Subscriber
#application, #environment, #formatter, #host, #logger, #metrics
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(url: "udp://localhost:8125") ⇒ Statsd
constructor
Create Statsd metrics subscriber.
- #log(log) ⇒ Object
- #reopen ⇒ Object
-
#should_log?(log) ⇒ Boolean
Only forward log entries that contain metrics.
Methods inherited from Subscriber
#close, #console_output?, #default_formatter, #flush, #level
Methods inherited from Base
#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #silence, #tagged, #tags
Constructor Details
#initialize(url: "udp://localhost:8125") ⇒ Statsd
Create Statsd metrics subscriber
Parameters:
url: [String]
Valid URL to post to.
Example:
udp://localhost:8125
Example, send all metrics to a particular namespace:
udp://localhost:8125/namespace
Default: udp://localhost:8125
Example:
SemanticLogger.add_appender(
metric: :statsd,
url: 'localhost:8125'
)
29 30 31 32 |
# File 'lib/semantic_logger/metric/statsd.rb', line 29 def initialize(url: "udp://localhost:8125") @url = url super() end |
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
11 12 13 |
# File 'lib/semantic_logger/metric/statsd.rb', line 11 def url @url end |
Instance Method Details
#log(log) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/semantic_logger/metric/statsd.rb', line 43 def log(log) metric = log.metric if (duration = log.duration) @statsd.timing(metric, duration) else amount = (log.metric_amount || 1).round if amount.negative? amount.times { @statsd.decrement(metric) } else amount.times { @statsd.increment(metric) } end end end |
#reopen ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/semantic_logger/metric/statsd.rb', line 34 def reopen uri = URI.parse(@url) raise('Statsd only supports udp. Example: "udp://localhost:8125"') if uri.scheme != "udp" @statsd = ::Statsd.new(uri.host, uri.port) path = uri.path.chomp("/") @statsd.namespace = path.sub("/", "") if path != "" end |
#should_log?(log) ⇒ Boolean
Only forward log entries that contain metrics.
58 59 60 61 |
# File 'lib/semantic_logger/metric/statsd.rb', line 58 def should_log?(log) # Does not support metrics with dimensions. log.metric && !log.dimensions && meets_log_level?(log) && !filtered?(log) end |