mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-01 08:57:26 -08:00
a2e4439086
Allows to use graphite over tcp or udp. Metrics labels and values are used to construct a valid Graphite path in a way that will allow us to eventually read them back and reconstruct the metrics. For example, this metric: model.Metric{ model.MetricNameLabel: "test:metric", "testlabel": "test:value", "testlabel2": "test:value", ) Will become: test:metric.testlabel=test:value.testlabel2=test:value escape.go takes care of escaping values to match Graphite character set, it basically uses percent-encoding as a fallback wich will work pretty will in the graphite/grafana world. The remote storage module also has an optional 'prefix' parameter to prefix all metrics with a path (for example, 'prometheus.'). Graphite URLs are simply in the form tcp://host:port or udp://host:port.
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
// Copyright 2015 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package graphite
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"math"
|
|
"net"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/prometheus/common/log"
|
|
"github.com/prometheus/common/model"
|
|
)
|
|
|
|
// Client allows sending batches of Prometheus samples to Graphite.
|
|
type Client struct {
|
|
address string
|
|
transport string
|
|
timeout time.Duration
|
|
prefix string
|
|
}
|
|
|
|
// NewClient creates a new Client.
|
|
func NewClient(address string, transport string, timeout time.Duration, prefix string) *Client {
|
|
return &Client{
|
|
address: address,
|
|
transport: transport,
|
|
timeout: timeout,
|
|
prefix: prefix,
|
|
}
|
|
}
|
|
|
|
func pathFromMetric(m model.Metric, prefix string) string {
|
|
var buffer bytes.Buffer
|
|
|
|
buffer.WriteString(prefix)
|
|
buffer.WriteString(escape(m[model.MetricNameLabel]))
|
|
|
|
// We want to sort the labels.
|
|
labels := make(model.LabelNames, 0, len(m))
|
|
for l, _ := range m {
|
|
labels = append(labels, l)
|
|
}
|
|
sort.Sort(labels)
|
|
|
|
// For each label, in order, add ".<label>=<value>".
|
|
for _, l := range labels {
|
|
v := m[l]
|
|
|
|
if l == model.MetricNameLabel || len(l) == 0 {
|
|
continue
|
|
}
|
|
// Here we use '=' instead of '.' to be able
|
|
// to later read back the value correctly. Using '.' would
|
|
// not allow to distinguish labels from values.
|
|
buffer.WriteString(fmt.Sprintf(
|
|
".%s=%s", string(l), escape(v)))
|
|
}
|
|
return buffer.String()
|
|
}
|
|
|
|
// Store sends a batch of samples to Graphite.
|
|
func (c *Client) Store(samples model.Samples) error {
|
|
conn, err := net.DialTimeout(c.transport, c.address, c.timeout)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
|
|
var buf bytes.Buffer
|
|
for _, s := range samples {
|
|
k := pathFromMetric(s.Metric, c.prefix)
|
|
t := float64(s.Timestamp.UnixNano()) / 1e9
|
|
v := float64(s.Value)
|
|
if math.IsNaN(v) || math.IsInf(v, 0) {
|
|
log.Warnf("cannot send value %f to Graphite,"+
|
|
"skipping sample %#v", v, s)
|
|
continue
|
|
}
|
|
fmt.Fprintf(&buf, "%s %f %f\n", k, v, t)
|
|
}
|
|
|
|
_, err = conn.Write(buf.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Name identifies the client as a Graphite client.
|
|
func (c Client) Name() string {
|
|
return "graphite"
|
|
}
|