@@ -37,17 +37,18 @@ const (
3737type TimeFunc func () time.Time
3838
3939type HTTPListener struct {
40- ServiceAddress string
41- ReadTimeout internal.Duration
42- WriteTimeout internal.Duration
43- MaxBodySize internal.Size
44- MaxLineSize internal.Size
45- Port int
46-
40+ ServiceAddress string `toml:"service_address"`
41+ // Port gets pulled out of ServiceAddress
42+ Port int
4743 tlsint.ServerConfig
4844
49- BasicUsername string
50- BasicPassword string
45+ ReadTimeout internal.Duration `toml:"read_timeout"`
46+ WriteTimeout internal.Duration `toml:"write_timeout"`
47+ MaxBodySize internal.Size `toml:"max_body_size"`
48+ MaxLineSize internal.Size `toml:"max_line_size"`
49+ BasicUsername string `toml:"basic_username"`
50+ BasicPassword string `toml:"basic_password"`
51+ DatabaseTag string `toml:"database_tag"`
5152
5253 TimeFunc
5354
@@ -93,6 +94,13 @@ const sampleConfig = `
9394 ## Maximum line size allowed to be sent in bytes.
9495 ## 0 means to use the default of 65536 bytes (64 kibibytes)
9596 max_line_size = "64KiB"
97+
98+
99+ ## Optional tag name used to store the database.
100+ ## If the write has a database in the query string then it will be kept in this tag name.
101+ ## This tag can be used in downstream outputs.
102+ ## The default value of nothing means it will be off and the database will not be recorded.
103+ # database_tag = ""
96104
97105 ## Set one or more allowed client CA certificate file names to
98106 ## enable mutually authenticated TLS connections
@@ -258,6 +266,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
258266 now := h .TimeFunc ()
259267
260268 precision := req .URL .Query ().Get ("precision" )
269+ db := req .URL .Query ().Get ("db" )
261270
262271 // Handle gzip request bodies
263272 body := req .Body
@@ -315,7 +324,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
315324
316325 if err == io .ErrUnexpectedEOF {
317326 // finished reading the request body
318- err = h .parse (buf [:n + bufStart ], now , precision )
327+ err = h .parse (buf [:n + bufStart ], now , precision , db )
319328 if err != nil {
320329 log .Println ("D! " + err .Error (), bufStart + n )
321330 return400 = true
@@ -346,7 +355,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
346355 bufStart = 0
347356 continue
348357 }
349- if err := h .parse (buf [:i + 1 ], now , precision ); err != nil {
358+ if err := h .parse (buf [:i + 1 ], now , precision , db ); err != nil {
350359 log .Println ("D! " + err .Error ())
351360 return400 = true
352361 }
@@ -359,7 +368,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
359368 }
360369}
361370
362- func (h * HTTPListener ) parse (b []byte , t time.Time , precision string ) error {
371+ func (h * HTTPListener ) parse (b []byte , t time.Time , precision , db string ) error {
363372 h .mu .Lock ()
364373 defer h .mu .Unlock ()
365374
@@ -371,6 +380,13 @@ func (h *HTTPListener) parse(b []byte, t time.Time, precision string) error {
371380 }
372381
373382 for _ , m := range metrics {
383+ // Do we need to keep the database name in the query string.
384+ // If a tag has been supplied to put the db in and we actually got a db query,
385+ // then we write it in. This overwrites the database tag if one was sent.
386+ // This makes it behave like the influx endpoint.
387+ if h .DatabaseTag != "" && db != "" {
388+ m .AddTag (h .DatabaseTag , db )
389+ }
374390 h .acc .AddFields (m .Name (), m .Fields (), m .Tags (), m .Time ())
375391 }
376392
0 commit comments