Class: ActiveMeasure::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_measure/client.rb

Constant Summary collapse

DEFAULT_CONNECTION_STRING =
'udp://localhost:8125'
METRIC_METHODS =

forward all methods to transport

%i(
increment
decrement
count
gauge
timing
time
histogram
set
distribution
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_string = nil, logger: nil, tags: {}, namespace: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
# File 'lib/active_measure/client.rb', line 13

def initialize(connection_string = nil, logger: nil, tags: {}, namespace: nil)
  @connection_string = parse_connection_string(connection_string)
  @connection_string[:client] = self
  @transport = ActiveMeasure::Adapters.for(@connection_string[:adapter]).new(@connection_string)
  @logger = logger || ActiveMeasure.logger
  @tags = filter_tags(tags)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/active_measure/client.rb', line 10

def logger
  @logger
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/active_measure/client.rb', line 11

def namespace
  @namespace
end

#tagsObject (readonly)

Returns the value of attribute tags.



10
11
12
# File 'lib/active_measure/client.rb', line 10

def tags
  @tags
end

#transportObject (readonly)

Returns the value of attribute transport.



11
12
13
# File 'lib/active_measure/client.rb', line 11

def transport
  @transport
end

Instance Method Details

#filter_tags(tags) ⇒ Object



63
64
65
# File 'lib/active_measure/client.rb', line 63

def filter_tags(tags)
  (ActiveMeasure.tags || {}).merge(tags || {}).reject { |_, value| value.nil? || value.empty? }
end

#parse_connection_string(connection) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_measure/client.rb', line 38

def parse_connection_string(connection)
  connection ||= ENV['ActiveMeasure_URL']
  default_connection = {
    scheme: 'statsd',
    host: 'localhost',
    port: 8125,
    adapter: 'statsd'
  }

  return default_connection if connection.nil?

  if connection.is_a?(Hash)
    connection[:port] ||= default_connection[:port]
    connection
  else
    uri = URI.parse(connection)
    {
      scheme: uri.scheme,
      host: uri.host,
      port: uri.port || default_connection[:port],
      adapter: determine_adapter(uri.scheme)
    }
  end
end