Class: StatsD::Instrument::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/instrument/environment.rb

Overview

The environment module is used to detect, and initialize the environment in which this library is active. It will use different default values based on the environment.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Environment

Returns a new instance of Environment.



39
40
41
# File 'lib/statsd/instrument/environment.rb', line 39

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



37
38
39
# File 'lib/statsd/instrument/environment.rb', line 37

def env
  @env
end

Class Method Details

.currentObject



7
8
9
# File 'lib/statsd/instrument/environment.rb', line 7

def current
  @current ||= StatsD::Instrument::Environment.new(ENV)
end

.environmentString

Detects the current environment, either by asking Rails, or by inspecting environment variables.

  • Within a Rails application, Rails.env is used.
  • It will check the following environment variables in order: RAILS_ENV, RACK_ENV, ENV.
  • If none of these are set, it will return development

Returns:

  • (String)

    The detected environment.



18
19
20
# File 'lib/statsd/instrument/environment.rb', line 18

def environment
  current.environment
end

.setupvoid

This method returns an undefined value.

Sets default values for sample rate and logger.

  • Default sample rate is set to the value in the STATSD_SAMPLE_RATE environment variable, or 1.0 otherwise. See StatsD#default_sample_rate
  • StatsD#logger is set to a logger that send output to stderr.

If you are including this library inside a Rails environment, additional initialization will be done as part of the Railtie.



32
33
34
# File 'lib/statsd/instrument/environment.rb', line 32

def setup
  StatsD.logger = Logger.new($stderr)
end

Instance Method Details

#clientObject



80
81
82
# File 'lib/statsd/instrument/environment.rb', line 80

def client
  StatsD::Instrument::Client.from_env(self)
end

#default_sink_for_environmentObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/statsd/instrument/environment.rb', line 84

def default_sink_for_environment
  case environment
  when 'production', 'staging'
    StatsD::Instrument::UDPSink.for_addr(statsd_addr)
  when 'test'
    StatsD::Instrument::NullSink.new
  else
    StatsD::Instrument::LogSink.new(StatsD.logger)
  end
end

#environmentString

Detects the current environment, either by asking Rails, or by inspecting environment variables.

  • Within a Rails application, Rails.env is used.
  • It will check the following environment variables in order: RAILS_ENV, RACK_ENV, ENV.
  • If none of these are set, it will return development

Returns:

  • (String)

    The detected environment.



50
51
52
53
54
55
56
57
58
# File 'lib/statsd/instrument/environment.rb', line 50

def environment
  if env['STATSD_ENV']
    env['STATSD_ENV']
  elsif defined?(Rails) && Rails.respond_to?(:env)
    Rails.env.to_s
  else
    env['RAILS_ENV'] || env['RACK_ENV'] || env['ENV'] || 'development'
  end
end

#statsd_addrObject



72
73
74
# File 'lib/statsd/instrument/environment.rb', line 72

def statsd_addr
  env.fetch('STATSD_ADDR', 'localhost:8125')
end

#statsd_default_tagsObject



76
77
78
# File 'lib/statsd/instrument/environment.rb', line 76

def statsd_default_tags
  env.key?('STATSD_DEFAULT_TAGS') ? env.fetch('STATSD_DEFAULT_TAGS').split(',') : nil
end

#statsd_implementationObject



60
61
62
# File 'lib/statsd/instrument/environment.rb', line 60

def statsd_implementation
  env.fetch('STATSD_IMPLEMENTATION', 'datadog')
end

#statsd_prefixObject



68
69
70
# File 'lib/statsd/instrument/environment.rb', line 68

def statsd_prefix
  env.fetch('STATSD_PREFIX', nil)
end

#statsd_sample_rateObject



64
65
66
# File 'lib/statsd/instrument/environment.rb', line 64

def statsd_sample_rate
  env.fetch('STATSD_SAMPLE_RATE', 1.0).to_f
end