# Can use variables for output influxdb db and measurement field?

**URL:** <https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508>\
**Category:** Logstash\
**Created:** [February 2, 2023, 6:50am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508 "2023-02-02T06:50:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 2, 2023, 6:50am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/1 "2023-02-02T06:50:24Z")

</div>

Hi  
I'm wondering if a configuration like this can work or not.

```auto
filter {
  mutate {
    if [topic] == "xxxx" {
      add_field => { "db" => "test2", "measurement" => "access_logs" }
    } else {
      add_field => { "db" => "unknown_topics", "measurement" => "unknown_logs" }
    }
  }
}

output {
  influxdb {
    id => "mydb"
    host => "localhost"
    db => "%{[db]}"
    measurement => "%{[measurement]}"
  }
}

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 2, 2023, 5:41pm UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/2 "2023-02-02T17:41:23Z")

</div>

> [@AlanChan](#):
>
> ```auto
> db => "%{[db]}"
> measurement => "%{[measurement]}"
> 
> ```

The influxdb output calls sprintf for both [db](https://github.com/logstash-plugins/logstash-output-influxdb/blob/75e3908e53d9f5c0efee11fd8c9d7c3d73085b18/lib/logstash/outputs/influxdb.rb#L179) and [measurement](https://github.com/logstash-plugins/logstash-output-influxdb/blob/75e3908e53d9f5c0efee11fd8c9d7c3d73085b18/lib/logstash/outputs/influxdb.rb#L173) options, so I would expect that to work.

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 3, 2023, 11:00am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/3 "2023-02-03T11:00:15Z")

</div>

@Badger  
Thank you and sorry for the late reply.  
Actually, it didn't work as expected. They were just used as normal strings and Logstash was trying to connect to a database named %{[db]} in Influxdb.

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 4, 2023, 12:45am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/4 "2023-02-04T00:45:02Z")

</div>

@Badger  
I found the reason. It's a known bug and the fix has not been merged yet. (2 years ago PR... I don't know what to say... 🙄)

> <https://github.com/logstash-plugins/logstash-output-influxdb/issues/82>
>
> First db variable gets sprintf-ed in line 179.
> https://github.com/logstash-plug…ins/logstash-output-influxdb/blob/288a3b5f8a54f8f9c79629701f2af773569dc87a/lib/logstash/outputs/influxdb.rb#L179-L186
> 
> But then original "@db", rather than sprintf-ed "database" variable is used in line 190.
> https://github.com/logstash-plugins/logstash-output-influxdb/blob/288a3b5f8a54f8f9c79629701f2af773569dc87a/lib/logstash/outputs/influxdb.rb#L188-L198
> 
> Correct code should look like this.
> 
> \`\`\`
> def dowrite(events, database)
> begin
> @influxdbClient.write\_points(events, @time\_precision, @retention\_policy, database )
> rescue InfluxDB::AuthenticationError =\> ae
> @logger.warn("Authentication Error while writing to InfluxDB", :exception =\> ae)
> rescue InfluxDB::ConnectionError =\> ce 
> @logger.warn("Connection Error while writing to InfluxDB", :exception =\> ce)
> rescue Exception =\> e
> @logger.warn("Non recoverable exception while writing to InfluxDB", :exception =\> e)
> end
> end
> \`\`\`

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 4, 2023, 12:58am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/5 "2023-02-04T00:58:50Z")

</div>

Oh yes, I had forgotten about that. (I definitely knew about it once because there is a comment from me on one of the linked issues 🙂 )

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 4, 2023, 1:02am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/6 "2023-02-04T01:02:29Z")

</div>

@Badger  
Yup, thank you~

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 7, 2023, 2:19am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/7 "2023-02-07T02:19:56Z")

</div>

Hi @Badger  
Sorry for pinging you again.  
If we want to use variables as retention\_policy, do you know how to approach this?  
I'm thinking the same way as database, but it seems only database is passed to flush function. So, I'm not sure if we can extend the flush function with more parameters.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 7, 2023, 3:04am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/8 "2023-02-07T03:04:07Z")

</div>

If I had to do it I would probably change [this](https://github.com/logstash-plugins/logstash-output-influxdb/blob/75e3908e53d9f5c0efee11fd8c9d7c3d73085b18/lib/logstash/outputs/influxdb.rb#L190) line to use `event.sprintf(@retention_policy)` instead of just `@retention_policy`.

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 7, 2023, 4:36am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/9 "2023-02-07T04:36:59Z")

</div>

@Badger  
Thank you. I've already tried that but it didn't work.  
I think it's because the passed retention\_policy variable has to be passed to buffer\_receive first.

I also tried to add this to [this line](https://github.com/logstash-plugins/logstash-output-influxdb/blob/75e3908e53d9f5c0efee11fd8c9d7c3d73085b18/lib/logstash/outputs/influxdb.rb#L178).

```auto
@retention_policy = event.sprintf(@retention_policy)

```

But this can work for the first retention policy being passed only. The different values with the following events will use the last value.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 7, 2023, 5:12am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/10 "2023-02-07T05:12:31Z")

</div>

Sorry, I am not a logstash developer and I have never used influxdb. It sounds like your guesses are ahead of mine at this point.

---

<div class="post-metadata">

**Author:** ![AlanChan](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@AlanChan](https://discuss.elastic.co/u/AlanChan)\
**Post date:** [February 7, 2023, 5:27am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/11 "2023-02-07T05:27:16Z")

</div>

No worries. Thank you for your great help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 7, 2023, 5:27am UTC](https://discuss.elastic.co/t/can-use-variables-for-output-influxdb-db-and-measurement-field/324508/12 "2023-03-07T05:27:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
