localhots.xyz influxdb_buffer / master

Tree @master (Download .tar.gz)

InfluxDB aggregated buffer

In order to achieve maximum writing performance to InfluxDB an application needs to write series with as many points per series, as many series per batch as possible.

This buffer does exactly that: it aggregates multiple points into single series and multiple series into single batch. When buffer gets full it flushes series into database.


package main

import (

    influxdb "github.com/influxdb/influxdb/client"

func handleMetrics(rw http.ResponseWriter, req *http.Request) {
    ua := parseUserAgent(req.UserAgent())
    geo := fetchGeoInfo(req.RemoteAddr)

            Name:    "app.browsers",
            Columns: []string{"vendor", "version", "mobile"},
            Points:  [][]interface{}{{ua.Vendor, ua.Version, ua.IsMobile}},
            Name:    "app.visitors.geo",
            Columns: []string{"ip", "country", "city", "isp"},
            Points:  [][]interface{}{{ip, geo.Country, geo.City, geo.Isp}},

func writeMetrics(series []*influxdb.Series) {
    influxdb.WriteSeriesWithTimePrecision(series, "s")

var (
    buf *buffer.Buffer

func main() {
    buf = buffer.New(100, writeMetrics)
    defer buf.Close()

    http.HandleFunc("/metrics", handleMetrics)
    http.ListenAndServe(":8080", nil)