app/vminsert/influx: add -influxSkipSingleField flag for using {measurement} instead of {measurement}{separator}{field_name} for Influx lines with a single field

Fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/66
This commit is contained in:
Aliaksandr Valialkin 2019-06-14 10:51:57 +03:00
parent 269285848f
commit 86d3d907a5

View file

@ -19,6 +19,7 @@ import (
var ( var (
measurementFieldSeparator = flag.String("influxMeasurementFieldSeparator", ".", "Separator for `{measurement}{separator}{field_name}` metric name when inserted via Influx line protocol") 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")
) )
var rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="influx"}`) var rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="influx"}`)
@ -93,11 +94,16 @@ func (ctx *pushCtx) InsertRows(db string) error {
} }
ctx.metricNameBuf = storage.MarshalMetricNameRaw(ctx.metricNameBuf[:0], ic.Labels) ctx.metricNameBuf = storage.MarshalMetricNameRaw(ctx.metricNameBuf[:0], ic.Labels)
ctx.metricGroupBuf = append(ctx.metricGroupBuf[:0], r.Measurement...) ctx.metricGroupBuf = append(ctx.metricGroupBuf[:0], r.Measurement...)
skipFieldKey := len(r.Fields) == 1 && *skipSingleField
if !skipFieldKey {
ctx.metricGroupBuf = append(ctx.metricGroupBuf, *measurementFieldSeparator...) ctx.metricGroupBuf = append(ctx.metricGroupBuf, *measurementFieldSeparator...)
}
metricGroupPrefixLen := len(ctx.metricGroupBuf) metricGroupPrefixLen := len(ctx.metricGroupBuf)
for j := range r.Fields { for j := range r.Fields {
f := &r.Fields[j] f := &r.Fields[j]
if !skipFieldKey {
ctx.metricGroupBuf = append(ctx.metricGroupBuf[:metricGroupPrefixLen], f.Key...) ctx.metricGroupBuf = append(ctx.metricGroupBuf[:metricGroupPrefixLen], f.Key...)
}
metricGroup := bytesutil.ToUnsafeString(ctx.metricGroupBuf) metricGroup := bytesutil.ToUnsafeString(ctx.metricGroupBuf)
ic.Labels = ic.Labels[:0] ic.Labels = ic.Labels[:0]
ic.AddLabel("", metricGroup) ic.AddLabel("", metricGroup)