Project

microphite

0.0
No commit activity in last 3 years
No release in over 3 years
A tiny and fast, asynchronous graphite client
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.7
~> 10.0
~> 2.14.1
 Project Readme

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.

Microphite has been in use in production by a BZ technology client for quite some time without issue.

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

Execute-around for writes and gathers -- Send data unless a block throws. The block's return value is preserved.

client.write(accurate_data: 42) do
  something_that_may_throw()
end
client.gather(accurate_data: 42) do
  something_else_that_throws()
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.