sender

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2019 License: MIT Imports: 7 Imported by: 4

README

GoDoc CircleCI

A library that wraps InfluxData's line protocol with a batching client and simple metric declaration

Installation

go get github.com/itzg/line-protocol-sender

Documentation

Overview

Package sender provides a client that sends Influx line protocol metrics to a TCP endpoint. The client provides options for batching metrics by size and/or timeout.

A simple implementation of protocol.Metric is also provided to ensure this package is ready-to-use with no Influx specific implementation needed.

Example

The following would send a metric immediately to the telegraf socket_listener input plugin listening on port 8094:

client, err := sender.NewClient(context.Background(), sender.Config{Endpoint: "telegraf:8094"})

metric := sender.NewSimpleMetric("metric_name")
metric.AddTag("tag", "t1")
metric.AddField("intField", 1)
metric.AddField("floatField", 3.14)
client.Send(metric)
Example (Sending)
package main

import (
	"bytes"
	"context"
	"fmt"
	sender "github.com/itzg/line-protocol-sender"
	"io"
	"log"
	"net"
	"time"
)

type ExampleEndpoint struct {
	listener net.Listener
}

func NewExampleEndpoint() *ExampleEndpoint {
	listener, err := net.Listen("tcp", "127.0.0.1:")
	if err != nil {
		log.Fatal(err)
	}
	e := &ExampleEndpoint{listener: listener}
	go e.listen()
	return e
}

func (e *ExampleEndpoint) Addr() string {
	return e.listener.Addr().String()
}

func (e *ExampleEndpoint) listen() {
	conn, err := e.listener.Accept()
	if err != nil {
		log.Fatal(err)
	}
	var buffer bytes.Buffer
	_, err = io.Copy(&buffer, conn)
	if err != nil {
		log.Fatal(err)
	}
	conn.Close()

	fmt.Print(buffer.String())
}

func main() {
	endpoint := NewExampleEndpoint()

	client, _ := sender.NewClient(context.Background(), sender.Config{Endpoint: endpoint.Addr()})

	metric := sender.NewSimpleMetric("metric_name")
	metric.SetTime(time.Unix(3, 1))
	metric.AddTag("tag", "t1")
	metric.AddField("intField", 1)
	metric.AddField("floatField", 3.14)
	client.Send(metric)

	// allow time for listener to receive line
	time.Sleep(10 * time.Millisecond)

}
Output:

metric_name,tag=t1 intField=1i,floatField=3.14 3000000001

Index

Examples

Constants

View Source
const MetricsChanSize = 100

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Send(m protocol.Metric)
	Flush()
}

func NewClient

func NewClient(ctx context.Context, config Config) (Client, error)

type Config

type Config struct {
	Endpoint     string
	BatchSize    int
	BatchTimeout time.Duration
	ErrorListener
}

type ErrorListener

type ErrorListener func(err error)

type SimpleMetric

type SimpleMetric struct {
	// contains filtered or unexported fields
}

func NewSimpleMetric

func NewSimpleMetric(name string) *SimpleMetric

func (*SimpleMetric) AddField

func (m *SimpleMetric) AddField(key string, value interface{})

func (*SimpleMetric) AddTag

func (m *SimpleMetric) AddTag(key, value string)

func (*SimpleMetric) FieldList

func (m *SimpleMetric) FieldList() []*protocol.Field

func (*SimpleMetric) Name

func (m *SimpleMetric) Name() string

func (*SimpleMetric) SetTime

func (m *SimpleMetric) SetTime(t time.Time)

func (*SimpleMetric) TagList

func (m *SimpleMetric) TagList() []*protocol.Tag

func (*SimpleMetric) Time

func (m *SimpleMetric) Time() time.Time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL