Module: TsdMetrics

Defined in:
lib/tsd_metrics/units.rb,
lib/tsd_metrics.rb,
lib/tsd_metrics/timer.rb,
lib/tsd_metrics/counter.rb,
lib/tsd_metrics/version.rb,
lib/tsd_metrics/exceptions.rb,
lib/tsd_metrics/tsd_metric.rb,
lib/tsd_metrics/queue_writer.rb,
lib/tsd_metrics/timer_sample.rb,
lib/tsd_metrics/counter_sample.rb,
lib/tsd_metrics/async_queue_writer.rb,
lib/tsd_metrics/json_formatting_sink.rb,
lib/tsd_metrics/metric_builder_for_single_struct_receiver.rb

Overview

Copyright 2014 Groupon.com

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Defined Under Namespace

Classes: AsyncQueueWriter, Counter, CounterSample, Error, JsonFormattingSink, MetricBuilderForSingleStructReceiver, MetricClosedError, OutputThreadError, QueueWriter, Timer, TimerSample, TsdMetric, UnitsUtils

Constant Summary collapse

UNITS =
Set.new([:nanosecond, :microsecond, :millisecond, :second, :minute, :hour, :day, :week, :bit, :byte, :kilobit, :kilobyte, :megabit, :megabyte, :gigabit, :gigabyte, :terabyte, :petabyte])
VERSION =
"0.2.9"
@@errorLogger =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#errorLoggerObject (readonly)

Returns the value of attribute errorLogger.



24
25
26
# File 'lib/tsd_metrics.rb', line 24

def errorLogger
  @errorLogger
end

Class Method Details

.buildMetricObject



75
76
77
78
# File 'lib/tsd_metrics.rb', line 75

def self.buildMetric
  init() if @metricBuilder == nil
  @metricBuilder.build
end

.init(providedOpts = {}) ⇒ Object

filename

Optional: the relative or absolute path to output metrics. Default: ‘query.log’ in the working directory

errorLogger

Optional: a place to which mis-uses of the library can be logged.

Expects methods +info+, +warn+, +error+
rollLogs

Optional: Have the library do log rolling; not thread-safe! Setting to false means the library-user should have external log-rolling in place. Default: true



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tsd_metrics.rb', line 32

def self.init(providedOpts={})
  defaultOpts = {filename: "query.log", rollLogs: true}
  opts = defaultOpts.merge providedOpts
  @errorLogger = opts[:errorLogger] || Logger.new(STDOUT)

  outputFileQueue = Queue.new

  # JSON to queue
  writer = QueueWriter.new(outputFileQueue)

  # Metric to JSON
  formatterStructReveiver = JsonFormattingSink.new(writer)
  @metricBuilder = MetricBuilderForSingleStructReceiver.new(formatterStructReveiver)

  loggerOptions = [opts[:filename]]
  # Set the ':daily' option on the logger if we want to roll the logs
  loggerOptions.push(:daily) if opts[:rollLogs]
  # TODO(mhayter): Switch to hourly rolling (will need different log lib)
  # [MAI-173]
  # File writer
  toFileLogger = createHeaderlessLogger(*loggerOptions)

  # Queue to file
  @queueToFileWriter = AsyncQueueWriter.new(outputFileQueue, toFileLogger)
  @queueToFileWriter.start

  # Phusion Passenger loses all threads on a fork, this is added
  # so that the writing thread is recreated.
  # This code was found at
  # https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#_smart_spawning_caveat_2_the_need_to_revive_threads
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      if forked
        outputFileQueue.clear
        @queueToFileWriter.start
      else
        # We're in direct spawning mode. We don't need to do anything.
      end
    end
  end

end