Coder Social home page Coder Social logo

lapis-stats's Introduction

lapis-stats

Statsd and Influxdb support for Lua, OpenResty & Lapis

statsd

The lapis.statsd module lets you send metrics to statsd with a UDP socket. Inside of OpenResty the non-blocking co-socket API is used. Otherwise, LuaSocket is used.

You must configure your statsd location in your Lapis config:

-- config.lua

local config = require("lapis.config")

config("development", function()
  statsd {
    host = "127.0.0.1",
    port = 8125,
    -- debug: true,
  }
end)

Include the module from lapis.statsd

local statsd = require("lapis.statsd")

app:get("/hello", function(self)
  statsd.counter("my_counter", 5)
  statsd.timer("my_counter", 100)
  statsd.value("hello", 9)
  statsd.guage("some_guage", -1)
end)

If you're sending many metrics at once then you can take advantage of the Pipeline interface:

app:get("/hello", function(self)
  p = statsd.Pipeline()
  p:counter("my_counter", 5)
  p:timer("my_counter", 100)
  p:value("hello", 9)
  p:guage("some_guage", -1)
  p:flush()
end)

Reference

  • timer(key, value)
  • counter(key, value)
  • guage(key, value)
  • value(key, value)

The Pipeline instance exposes all of the same functions, but as methods. (So you should call them using :)

influxdb

The lapis.influxdb module provides a way to configure and send data to InfluxDB over the HTTP API.

TODO: only LuaSocket is used right now

You must configure your InfluxDB server in your Lapis config:

-- config.lua

local config = require("lapis.config")

config("development", function()
  influxdb {
    -- host = "127.0.0.1", -- default
    -- port = 8086, -- default
    username = "influx",
    password = "my-password",
    database = "my-db",
  }
end)

You can then use the module to query data:

local influxdb = require("lapis.influxdb")

local res = influxdb.query([[
  select * from "counts.users" where time > now() - 1d group by time(1h)
]])

Or write data points:

local res = influxdb.write {
  "count.users value=4",
  "summary.ip count=32 tag=US"
}

Reference

get_client()

Get the current instance of the InfluxDB client from the Lapis configuration.

query(query, values...)

Send a query to the current connection. The values are interpolated into the query escaped where the character ? appears.

local res = influxdb.query([[
  select * from "counts.users" where tag = ?
]], "hello world")

The response is returned as an array table of results.

write(points)

Write measurements to the database. points is an array table with all the measurements to write as a string. It uses the same text syntax documented in the InfluxDB manual.

local res = influxdb.write {
  "count.users value=4",
}

Writing an InfluxDB sink for Statsd

Using this library you can write a command line script to use as a sink to handle your statsd flush to InfluxDB.

Note: I recommend using statsite over statsd

You might write something like this:

-- influxdb_sink.lua
local influxdb = require("lapis.influxdb").get_client()

local points = {}

for line in io.stdin:lines() do
  local name, val, time = line:match("^([^|]+)|([^|]+)|([^|]+)$")
  if name then
     table.insert(points, name .. " value=" .. val)
  end
end

if not next(points) then
  return
end

influxdb:write(points)

lapis-stats's People

Contributors

leafo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

gaecom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.