Add option to save retention policy as tag in influxdb_listener (#7356)
This commit is contained in:
parent
b2ec7973df
commit
702946b5cf
|
@ -54,6 +54,10 @@ submits data to InfluxDB determines the destination database.
|
||||||
## the tag will be overwritten with the database supplied.
|
## the tag will be overwritten with the database supplied.
|
||||||
# database_tag = ""
|
# database_tag = ""
|
||||||
|
|
||||||
|
## If set the retention policy specified in the write query will be added as
|
||||||
|
## the value of this tag name.
|
||||||
|
# retention_policy_tag = ""
|
||||||
|
|
||||||
## Optional username and password to accept for HTTP basic authentication.
|
## Optional username and password to accept for HTTP basic authentication.
|
||||||
## You probably want to make sure you have TLS configured above for this.
|
## You probably want to make sure you have TLS configured above for this.
|
||||||
# basic_username = "foobar"
|
# basic_username = "foobar"
|
||||||
|
|
|
@ -36,6 +36,7 @@ type InfluxDBListener struct {
|
||||||
BasicUsername string `toml:"basic_username"`
|
BasicUsername string `toml:"basic_username"`
|
||||||
BasicPassword string `toml:"basic_password"`
|
BasicPassword string `toml:"basic_password"`
|
||||||
DatabaseTag string `toml:"database_tag"`
|
DatabaseTag string `toml:"database_tag"`
|
||||||
|
RetentionPolicyTag string `toml:"retention_policy_tag"`
|
||||||
|
|
||||||
timeFunc influx.TimeFunc
|
timeFunc influx.TimeFunc
|
||||||
|
|
||||||
|
@ -78,6 +79,10 @@ const sampleConfig = `
|
||||||
## The default value of nothing means it will be off and the database will not be recorded.
|
## The default value of nothing means it will be off and the database will not be recorded.
|
||||||
# database_tag = ""
|
# database_tag = ""
|
||||||
|
|
||||||
|
## If set the retention policy specified in the write query will be added as
|
||||||
|
## the value of this tag name.
|
||||||
|
# retention_policy_tag = ""
|
||||||
|
|
||||||
## Set one or more allowed client CA certificate file names to
|
## Set one or more allowed client CA certificate file names to
|
||||||
## enable mutually authenticated TLS connections
|
## enable mutually authenticated TLS connections
|
||||||
tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
|
tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
|
||||||
|
@ -255,6 +260,7 @@ func (h *InfluxDBListener) handleWrite() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
db := req.URL.Query().Get("db")
|
db := req.URL.Query().Get("db")
|
||||||
|
rp := req.URL.Query().Get("rp")
|
||||||
|
|
||||||
body := req.Body
|
body := req.Body
|
||||||
body = http.MaxBytesReader(res, body, h.MaxBodySize.Size)
|
body = http.MaxBytesReader(res, body, h.MaxBodySize.Size)
|
||||||
|
@ -316,6 +322,10 @@ func (h *InfluxDBListener) handleWrite() http.HandlerFunc {
|
||||||
m.AddTag(h.DatabaseTag, db)
|
m.AddTag(h.DatabaseTag, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if h.RetentionPolicyTag != "" && rp != "" {
|
||||||
|
m.AddTag(h.RetentionPolicyTag, rp)
|
||||||
|
}
|
||||||
|
|
||||||
h.acc.AddMetric(m)
|
h.acc.AddMetric(m)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/internal"
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -207,6 +207,37 @@ func TestWriteKeepDatabase(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteRetentionPolicyTag(t *testing.T) {
|
||||||
|
listener := newTestListener()
|
||||||
|
listener.RetentionPolicyTag = "rp"
|
||||||
|
|
||||||
|
acc := &testutil.Accumulator{}
|
||||||
|
require.NoError(t, listener.Init())
|
||||||
|
require.NoError(t, listener.Start(acc))
|
||||||
|
defer listener.Stop()
|
||||||
|
|
||||||
|
resp, err := http.Post(createURL(listener, "http", "/write", "rp=myrp"), "", bytes.NewBuffer([]byte("cpu time_idle=42")))
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp.Body.Close()
|
||||||
|
require.Equal(t, 204, resp.StatusCode)
|
||||||
|
|
||||||
|
expected := []telegraf.Metric{
|
||||||
|
testutil.MustMetric(
|
||||||
|
"cpu",
|
||||||
|
map[string]string{
|
||||||
|
"rp": "myrp",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"time_idle": 42.0,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
acc.Wait(1)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
||||||
|
}
|
||||||
|
|
||||||
// http listener should add a newline at the end of the buffer if it's not there
|
// http listener should add a newline at the end of the buffer if it's not there
|
||||||
func TestWriteNoNewline(t *testing.T) {
|
func TestWriteNoNewline(t *testing.T) {
|
||||||
listener := newTestListener()
|
listener := newTestListener()
|
||||||
|
|
Loading…
Reference in New Issue