2019-05-22 21:16:55 +00:00
package influx
import (
2019-06-14 06:57:13 +00:00
"flag"
2019-05-22 21:16:55 +00:00
"net/http"
"runtime"
"sync"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
2020-02-23 11:35:47 +00:00
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/influx"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
2020-02-23 11:35:47 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/metrics"
)
2019-06-14 06:57:13 +00:00
var (
2020-02-04 13:46:13 +00:00
measurementFieldSeparator = flag . String ( "influxMeasurementFieldSeparator" , "_" , "Separator for '{measurement}{separator}{field_name}' metric name when inserted via Influx line protocol" )
skipSingleField = flag . Bool ( "influxSkipSingleField" , false , "Uses '{measurement}' instead of '{measurement}{separator}{field_name}' for metic name if Influx line contains only a single field" )
2019-06-14 06:57:13 +00:00
)
2019-07-27 10:20:47 +00:00
var (
rowsInserted = metrics . NewCounter ( ` vm_rows_inserted_total { type="influx"} ` )
2020-02-23 11:35:47 +00:00
rowsPerInsert = metrics . NewHistogram ( ` vm_rows_per_insert { type="influx"} ` )
2019-07-27 10:20:47 +00:00
)
2019-05-22 21:16:55 +00:00
// InsertHandler processes remote write for influx line protocol.
//
// See https://github.com/influxdata/influxdb/blob/4cbdc197b8117fee648d62e2e5be75c6575352f0/tsdb/README.md
func InsertHandler ( req * http . Request ) error {
2020-02-23 11:35:47 +00:00
return writeconcurrencylimiter . Do ( func ( ) error {
return parser . ParseStream ( req , insertRows )
2019-05-22 21:16:55 +00:00
} )
}
2020-02-23 11:35:47 +00:00
func insertRows ( db string , rows [ ] parser . Row ) error {
2019-05-22 21:16:55 +00:00
ctx := getPushCtx ( )
defer putPushCtx ( ctx )
rowsLen := 0
for i := range rows {
2019-12-09 18:58:19 +00:00
rowsLen += len ( rows [ i ] . Fields )
2019-05-22 21:16:55 +00:00
}
ic := & ctx . Common
ic . Reset ( rowsLen )
2019-07-27 10:20:47 +00:00
rowsTotal := 0
2019-05-22 21:16:55 +00:00
for i := range rows {
r := & rows [ i ]
ic . Labels = ic . Labels [ : 0 ]
2019-08-24 10:51:51 +00:00
hasDBLabel := false
2019-05-22 21:16:55 +00:00
for j := range r . Tags {
tag := & r . Tags [ j ]
2019-08-24 10:51:51 +00:00
if tag . Key == "db" {
hasDBLabel = true
}
2019-05-22 21:16:55 +00:00
ic . AddLabel ( tag . Key , tag . Value )
}
2019-08-24 10:51:51 +00:00
if len ( db ) > 0 && ! hasDBLabel {
ic . AddLabel ( "db" , db )
}
2019-05-22 21:16:55 +00:00
ctx . metricNameBuf = storage . MarshalMetricNameRaw ( ctx . metricNameBuf [ : 0 ] , ic . Labels )
ctx . metricGroupBuf = append ( ctx . metricGroupBuf [ : 0 ] , r . Measurement ... )
2019-06-14 07:51:57 +00:00
skipFieldKey := len ( r . Fields ) == 1 && * skipSingleField
2019-11-30 19:54:34 +00:00
if len ( ctx . metricGroupBuf ) > 0 && ! skipFieldKey {
2019-06-14 07:51:57 +00:00
ctx . metricGroupBuf = append ( ctx . metricGroupBuf , * measurementFieldSeparator ... )
}
2019-05-22 21:16:55 +00:00
metricGroupPrefixLen := len ( ctx . metricGroupBuf )
for j := range r . Fields {
f := & r . Fields [ j ]
2019-06-14 07:51:57 +00:00
if ! skipFieldKey {
ctx . metricGroupBuf = append ( ctx . metricGroupBuf [ : metricGroupPrefixLen ] , f . Key ... )
}
2019-05-22 21:16:55 +00:00
metricGroup := bytesutil . ToUnsafeString ( ctx . metricGroupBuf )
ic . Labels = ic . Labels [ : 0 ]
ic . AddLabel ( "" , metricGroup )
ic . WriteDataPoint ( ctx . metricNameBuf , ic . Labels [ : 1 ] , r . Timestamp , f . Value )
}
2019-07-27 10:20:47 +00:00
rowsTotal += len ( r . Fields )
2019-05-22 21:16:55 +00:00
}
2019-07-27 10:20:47 +00:00
rowsInserted . Add ( rowsTotal )
rowsPerInsert . Update ( float64 ( rowsTotal ) )
2019-05-22 21:16:55 +00:00
return ic . FlushBufs ( )
}
type pushCtx struct {
2020-02-23 11:35:47 +00:00
Common common . InsertCtx
2019-05-22 21:16:55 +00:00
metricNameBuf [ ] byte
metricGroupBuf [ ] byte
}
func ( ctx * pushCtx ) reset ( ) {
ctx . Common . Reset ( 0 )
ctx . metricNameBuf = ctx . metricNameBuf [ : 0 ]
ctx . metricGroupBuf = ctx . metricGroupBuf [ : 0 ]
}
func getPushCtx ( ) * pushCtx {
select {
case ctx := <- pushCtxPoolCh :
return ctx
default :
if v := pushCtxPool . Get ( ) ; v != nil {
return v . ( * pushCtx )
}
return & pushCtx { }
}
}
func putPushCtx ( ctx * pushCtx ) {
ctx . reset ( )
select {
case pushCtxPoolCh <- ctx :
default :
pushCtxPool . Put ( ctx )
}
}
var pushCtxPool sync . Pool
var pushCtxPoolCh = make ( chan * pushCtx , runtime . GOMAXPROCS ( - 1 ) )