influxdb/graphite/graphite.go

150 lines
3.9 KiB
Go
Raw Normal View History

package graphite
2014-03-04 01:52:57 +00:00
import (
2014-10-22 05:32:19 +00:00
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/influxdb/influxdb"
2014-03-04 01:52:57 +00:00
)
2015-01-08 19:10:38 +00:00
const (
// DefaultGraphitePort represents the default Graphite (Carbon) plaintext port.
DefaultGraphitePort = 2003
// DefaultGraphiteNameSeparator represents the default Graphite field separator.
DefaultGraphiteNameSeparator = "."
)
2014-10-22 05:32:19 +00:00
var (
// ErrBindAddressRequired is returned when starting the Server
2014-10-22 05:32:19 +00:00
// without a TCP or UDP listening address.
ErrBindAddressRequired = errors.New("bind address required")
2014-03-04 01:52:57 +00:00
// ErrServerClosed return when closing an already closed graphite server.
ErrServerClosed = errors.New("server already closed")
// ErrServerNotSpecified returned when Server is not specified.
ErrServerNotSpecified = errors.New("server not present")
2014-10-23 04:21:48 +00:00
)
2014-10-22 05:32:19 +00:00
2015-01-07 08:10:46 +00:00
// SeriesWriter defines the interface for the destination of the data.
type SeriesWriter interface {
WriteSeries(string, string, []influxdb.Point) (uint64, error)
2015-01-08 00:09:03 +00:00
}
// Server defines the interface all Graphite servers support.
type Server interface {
SetLogOutput(w io.Writer)
ListenAndServe(iface string) error
}
// NewServer return a Graphite server for the given protocol, using the given parser
// series writer, and database.
func NewServer(protocol string, p *Parser, s SeriesWriter, db string) (Server, error) {
if strings.ToLower(protocol) == "tcp" {
return NewTCPServer(p, s, db), nil
} else if strings.ToLower(protocol) == "udp" {
return NewUDPServer(p, s, db), nil
} else {
return nil, fmt.Errorf("unrecognized Graphite Server protocol %s", protocol)
}
}
2015-01-08 19:10:38 +00:00
// Parser encapulates a Graphite Parser.
type Parser struct {
Separator string
LastEnabled bool
}
2015-01-07 08:10:46 +00:00
// NewParser returns a GraphiteParser instance.
func NewParser() *Parser {
2015-01-08 19:10:38 +00:00
return &Parser{Separator: DefaultGraphiteNameSeparator}
2015-01-06 23:38:35 +00:00
}
2015-01-07 08:10:46 +00:00
// Parse performs Graphite parsing of a single line.
func (p *Parser) Parse(line string) (influxdb.Point, error) {
// Break into 3 fields (name, value, timestamp).
fields := strings.Fields(line)
if len(fields) != 3 {
return influxdb.Point{}, fmt.Errorf("received %q which doesn't have three fields", line)
}
2014-10-22 00:20:43 +00:00
// decode the name and tags
name, tags, err := p.DecodeNameAndTags(fields[0])
if err != nil {
return influxdb.Point{}, err
}
2014-11-18 00:23:21 +00:00
// Parse value.
v, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return influxdb.Point{}, err
}
2014-11-18 00:23:21 +00:00
2015-02-23 22:37:10 +00:00
fieldValues := make(map[string]interface{})
// Determine if value is a float or an int.
if i := int64(v); float64(i) == v {
2015-02-23 22:37:10 +00:00
fieldValues[name] = int64(v)
} else {
2015-02-23 22:37:10 +00:00
fieldValues[name] = v
2014-10-22 00:20:43 +00:00
}
2014-10-22 05:32:19 +00:00
// Parse timestamp.
unixTime, err := strconv.ParseInt(fields[2], 10, 64)
if err != nil {
return influxdb.Point{}, err
2014-10-22 00:20:43 +00:00
}
2014-10-22 05:32:19 +00:00
timestamp := time.Unix(0, unixTime*int64(time.Millisecond))
point := influxdb.Point{
Name: name,
Tags: tags,
2015-02-23 22:37:10 +00:00
Fields: fieldValues,
Timestamp: timestamp,
}
2014-10-22 05:32:19 +00:00
return point, nil
}
2014-10-22 05:32:19 +00:00
2015-01-07 08:10:46 +00:00
// DecodeNameAndTags parses the name and tags of a single field of a Graphite datum.
func (p *Parser) DecodeNameAndTags(field string) (string, map[string]string, error) {
var (
name string
tags = make(map[string]string)
)
// decode the name and tags
values := strings.Split(field, p.Separator)
if len(values)%2 != 1 {
// There should always be an odd number of fields to map a point name and tags
// ex: region.us-west.hostname.server01.cpu -> tags -> region: us-west, hostname: server01, point name -> cpu
return name, tags, fmt.Errorf("received %q which doesn't conform to format of key.value.key.value.name or name", field)
}
2014-10-22 05:32:19 +00:00
if p.LastEnabled {
name = values[len(values)-1]
values = values[0 : len(values)-1]
} else {
name = values[0]
2015-02-01 20:33:12 +00:00
values = values[1:]
}
if name == "" {
return name, tags, fmt.Errorf("no name specified for metric. %q", field)
}
2014-11-18 00:23:21 +00:00
// Grab the pairs and throw them in the map
for i := 0; i < len(values); i += 2 {
k := values[i]
v := values[i+1]
tags[k] = v
}
2014-10-22 05:32:19 +00:00
return name, tags, nil
2014-10-22 00:20:43 +00:00
}