2015-03-30 00:42:04 +00:00
|
|
|
// 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 influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-05-20 16:10:29 +00:00
|
|
|
"github.com/prometheus/log"
|
2015-03-30 00:42:04 +00:00
|
|
|
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2015-03-30 00:42:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
writeEndpoint = "/write"
|
|
|
|
contentTypeJSON = "application/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client allows sending batches of Prometheus samples to InfluxDB.
|
|
|
|
type Client struct {
|
2015-06-15 10:49:28 +00:00
|
|
|
url string
|
|
|
|
httpClient *http.Client
|
|
|
|
retentionPolicy string
|
|
|
|
database string
|
2015-03-30 00:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
2015-06-15 10:49:28 +00:00
|
|
|
func NewClient(url string, timeout time.Duration, database, retentionPolicy string) *Client {
|
2015-03-30 00:42:04 +00:00
|
|
|
return &Client{
|
2015-06-15 10:49:28 +00:00
|
|
|
url: url,
|
2015-08-06 16:12:55 +00:00
|
|
|
httpClient: httputil.NewDeadlineClient(timeout, nil),
|
2015-06-15 10:49:28 +00:00
|
|
|
retentionPolicy: retentionPolicy,
|
|
|
|
database: database,
|
2015-03-30 00:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreSamplesRequest is used for building a JSON request for storing samples
|
|
|
|
// in InfluxDB.
|
|
|
|
type StoreSamplesRequest struct {
|
|
|
|
Database string `json:"database"`
|
|
|
|
RetentionPolicy string `json:"retentionPolicy"`
|
|
|
|
Points []point `json:"points"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// point represents a single InfluxDB measurement.
|
|
|
|
type point struct {
|
2015-08-20 15:18:46 +00:00
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
Precision string `json:"precision"`
|
|
|
|
Name model.LabelValue `json:"name"`
|
|
|
|
Tags model.LabelSet `json:"tags"`
|
|
|
|
Fields fields `json:"fields"`
|
2015-03-30 00:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fields represents the fields/columns sent to InfluxDB for a given measurement.
|
|
|
|
type fields struct {
|
2015-08-20 15:18:46 +00:00
|
|
|
Value model.SampleValue `json:"value"`
|
2015-03-30 00:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tagsFromMetric extracts InfluxDB tags from a Prometheus metric.
|
2015-08-20 15:18:46 +00:00
|
|
|
func tagsFromMetric(m model.Metric) model.LabelSet {
|
|
|
|
tags := make(model.LabelSet, len(m)-1)
|
2015-03-30 00:42:04 +00:00
|
|
|
for l, v := range m {
|
2015-08-20 15:18:46 +00:00
|
|
|
if l == model.MetricNameLabel {
|
2015-03-30 00:42:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tags[l] = v
|
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store sends a batch of samples to InfluxDB via its HTTP API.
|
2015-08-20 15:18:46 +00:00
|
|
|
func (c *Client) Store(samples model.Samples) error {
|
2015-03-30 00:42:04 +00:00
|
|
|
points := make([]point, 0, len(samples))
|
|
|
|
for _, s := range samples {
|
|
|
|
v := float64(s.Value)
|
|
|
|
if math.IsNaN(v) || math.IsInf(v, 0) {
|
|
|
|
// TODO(julius): figure out if it's possible to insert special float
|
|
|
|
// values into InfluxDB somehow.
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Warnf("cannot send value %f to InfluxDB, skipping sample %#v", v, s)
|
2015-03-30 00:42:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
metric := s.Metric[model.MetricNameLabel]
|
2015-03-30 00:42:04 +00:00
|
|
|
points = append(points, point{
|
|
|
|
Timestamp: s.Timestamp.UnixNano(),
|
|
|
|
Precision: "n",
|
|
|
|
Name: metric,
|
|
|
|
Tags: tagsFromMetric(s.Metric),
|
|
|
|
Fields: fields{
|
|
|
|
Value: s.Value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(c.url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = writeEndpoint
|
|
|
|
|
|
|
|
req := StoreSamplesRequest{
|
2015-06-15 10:49:28 +00:00
|
|
|
Database: c.database,
|
|
|
|
RetentionPolicy: c.retentionPolicy,
|
2015-03-30 00:42:04 +00:00
|
|
|
Points: points,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Post(
|
|
|
|
u.String(),
|
|
|
|
contentTypeJSON,
|
|
|
|
bytes.NewBuffer(buf),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// API returns status code 200 for successful writes.
|
|
|
|
// http://influxdb.com/docs/v0.9/concepts/reading_and_writing_data.html#response
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// API returns error details in the response content in JSON.
|
|
|
|
buf, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var r map[string]string
|
|
|
|
if err := json.Unmarshal(buf, &r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("failed to write samples into InfluxDB. Error: %s", r["error"])
|
|
|
|
}
|
2015-04-02 18:20:00 +00:00
|
|
|
|
|
|
|
// Name identifies the client as an InfluxDB client.
|
|
|
|
func (c Client) Name() string {
|
|
|
|
return "influxdb"
|
|
|
|
}
|