Module: PMetric

Defined in:
lib/pmetric.rb,
lib/pmetric/testing.rb,
lib/pmetric/version.rb,
lib/pmetric/collector.rb,
lib/pmetric/configuration.rb,
lib/pmetric/collector/base.rb,
lib/pmetric/collector/noop.rb,
lib/pmetric/collector/influx.rb

Defined Under Namespace

Modules: Collector Classes: Configuration

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.collectorPMetric::Collector::Base

Returns and caches the global collector.



21
22
23
# File 'lib/pmetric.rb', line 21

def self.collector
  Thread.main[:pmetric_collector] ||= Collector.build(config)
end

.configPMetric::Configuration

Returns the global configuration.

Do not set values on this object. Use ‘#configure` instead.



30
31
32
# File 'lib/pmetric.rb', line 30

def self.config
  Thread.main[:pmetric_config] ||= Configuration.new
end

.configure {|PMetric::Configuration| ... } ⇒ Object

Yields the configuration for the current thread.



37
38
39
# File 'lib/pmetric.rb', line 37

def self.configure
  CONFIGURE_MUTEX.synchronize { yield config }
end

.disable!Object

Disables metric collection by setting the collector to ‘:noop`



14
15
16
# File 'lib/pmetric.rb', line 14

def self.disable!
  configure { |c| c.collector = :noop }
end

.enable!Object

Enables metric collection by setting the collector to ‘:influx`



9
10
11
# File 'lib/pmetric.rb', line 9

def self.enable!
  configure { |c| c.collector = :influx }
end

.increment(metric, fields: {}, tags: {}) ⇒ Object

Delegates to collector. Collectors will add the ‘value: 1` field on their own, so passing it here will be overridden later.

Parameters:

  • metric (String)

    the item/event being measured

  • fields (Hash) (defaults to: {})

    additional field values to be recorded

  • tags (Hash) (defaults to: {})

    additional tags for this metric



47
48
49
# File 'lib/pmetric.rb', line 47

def self.increment(metric, fields: {}, tags: {})
  collector.increment(metric, fields: fields, tags: tags)
end

.load_config_for_env(path, env) ⇒ Object

Loads a YAML configuration for the given file/environment.

Parameters:

  • path (String)

    The path to the configuration file

  • env (String)

    The environment to load the configs for.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pmetric.rb', line 64

def self.load_config_for_env(path, env)
  if !File.exists?(path)
    warn "[#{self.name}] Configuration file does not exist: #{path}"
    return
  end

  yaml = Hash(YAML.load_file(path))[env]

  if !yaml
    warn "[#{self.name}] Environment not configured at #{path}: #{env}"
    return
  end

  enable! if yaml['enabled']
  Hash(yaml['config']).each { |k, v| config.send("#{k}=", v) }
end

.with_clean_envObject



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/pmetric/testing.rb', line 2

def self.with_clean_env
  old_config = Thread.main[:pmetric_config]
  Thread.main[:pmetric_config] = nil

  old_collector = Thread.main[:pmetric_collector]
  Thread.main[:pmetric_collector] = nil

  yield

  Thread.main[:pmetric_config] = old_config
  Thread.main[:pmetric_collector] = old_collector
end

.write(stat, fields:, tags: {}) ⇒ Object

Delegates to collector.

Parameters:

  • metric (String)

    the item/event being measured

  • fields (Hash)

    additional field values to be recorded

  • tags (Hash) (defaults to: {})

    additional tags for this metric



56
57
58
# File 'lib/pmetric.rb', line 56

def self.write(stat, fields:, tags: {})
  collector.write(stat, fields: fields, tags: tags)
end