2019-08-22 11:27:18 +02:00
|
|
|
package opentsdbhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/opentsdbhttp"
|
2019-08-22 11:27:18 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
2019-08-22 11:27:18 +02:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = tenantmetrics.NewCounterMap(`vm_rows_inserted_total{type="opentsdb-http"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="opentsdbhttp"}`)
|
2019-08-22 11:27:18 +02:00
|
|
|
)
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
// InsertHandler processes HTTP OpenTSDB put requests.
|
2019-08-22 11:27:18 +02:00
|
|
|
// See http://opentsdb.net/docs/build/html/api_http/put.html
|
2020-02-23 12:35:47 +01:00
|
|
|
func InsertHandler(req *http.Request) error {
|
|
|
|
path := req.URL.Path
|
|
|
|
p, err := httpserver.ParsePath(path)
|
2019-08-22 11:27:18 +02:00
|
|
|
if err != nil {
|
2020-02-23 12:35:47 +01:00
|
|
|
return fmt.Errorf("cannot parse path %q: %s", path, err)
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
if p.Prefix != "insert" {
|
|
|
|
// This is not our link.
|
|
|
|
return fmt.Errorf("unexpected path requested on HTTP OpenTSDB server: %q", path)
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
at, err := auth.NewToken(p.AuthToken)
|
2019-08-22 11:27:18 +02:00
|
|
|
if err != nil {
|
2020-02-23 12:35:47 +01:00
|
|
|
return fmt.Errorf("auth error: %s", err)
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
switch p.Suffix {
|
|
|
|
case "api/put", "opentsdb/api/put":
|
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
|
|
|
return parser.ParseStream(req, func(rows []parser.Row) error {
|
|
|
|
return insertRows(at, rows)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected path requested on HTTP OpenTSDB server: %q", path)
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2019-08-22 11:27:18 +02:00
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
func insertRows(at *auth.Token, rows []parser.Row) error {
|
|
|
|
ctx := netstorage.GetInsertCtx()
|
|
|
|
defer netstorage.PutInsertCtx(ctx)
|
2019-08-22 11:27:18 +02:00
|
|
|
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
2020-02-23 12:35:47 +01:00
|
|
|
ctx.Labels = ctx.Labels[:0]
|
|
|
|
ctx.AddLabel("", r.Metric)
|
2019-08-22 11:27:18 +02:00
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
2020-02-23 12:35:47 +01:00
|
|
|
ctx.AddLabel(tag.Key, tag.Value)
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
if err := ctx.WriteDataPoint(at, ctx.Labels, r.Timestamp, r.Value); err != nil {
|
2019-08-23 08:46:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|
|
|
|
rowsInserted.Get(at).Add(len(rows))
|
|
|
|
rowsPerInsert.Update(float64(len(rows)))
|
2020-02-23 12:35:47 +01:00
|
|
|
return ctx.FlushBufs()
|
2019-08-22 11:27:18 +02:00
|
|
|
}
|