Microphite

Gem Version Build Status Code Climate Coverage Status

Overview

Microphite is a tiny and fast, asynchronous graphite client. It can be called many times per second with minimal overhead. It is synchronized internally and can be shared across threads. Both tcp and udp connections are supported.

Usage

Construct a standard socket client. See the 'Client Options' section below for initializer options.

client = Microphite.client(
    host: 'graphite.host',
    port: 2003,
    transport: :udp,
    prefix: 'app.prefix.')

Construct a client with an error_handler. The client is fault tolerant, but an error_handler is useful for logging connection failures.

handler = Proc.new { |error| Rails.logger.error "Microphite error: #{error.message}" }
client = Microphite.client(host: '...', error_handler: handler)

Construct a no-op/dummy client. This is useful in development. You can leave client API calls in-place and the dummy client will behave appropriately.

# Initializer options are accepted, but no data is written
client = Microphite.noop(host: 'host', ...)

Send data points

client.write('some.key': 300, 'another.key': 25)

Accumulate counters (flushed every :flush_interval seconds)

client.gather('some.counter': 22, 'another.counter': 10)

Time a code block, gathering to timing.task

client.time('timing.task') do
  task
end

Easy prefixing

client.prefix('p1.') do |p1|
  # Write to p1.key
  app.write(key: 42)

  p1.prefix('p2.') do |p2|
    # Write to p1.p2.key
    p2.write(key: 5)
  end
end

Close the client, waiting for data to flush

client.close

Alternatively, wait at most 1 second to flush

flushed = client.close(1)

Client Options

Key Type Description Default
:host String Graphite server host (REQUIRED) nil
:port Integer Graphite port 2003
:transport Symbol Graphite transport to use (:tcp or :udp) :udp
:prefix String/Symbol Global prefix for all keys ''
:flush_interval Numeric How often to flush gathered data (in seconds) 1.0
:limit Integer Limit the write and gather stacks to this size 1000000
:min_delay Numeric Initial delay between retry attempts after failure (in seconds) 2
:max_delay Numeric Maximum delay between retry attempts after failure (in seconds) 60
:error_handler Proc Code block called on exception (takes a single exception param) nil

License

The MIT License (MIT)

Copyright (c) 2013 BZ Technology Services, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.