# Using dynamic value in google\_pubsub filter plugin attributes field

**URL:** <https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184>\
**Category:** Logstash\
**Created:** [April 12, 2022, 7:46am UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184 "2022-04-12T07:46:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Altiano\_Gerung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/altiano_gerung/32/38040_2.png) [@Altiano\_Gerung](https://discuss.elastic.co/u/Altiano_Gerung)\
**Post date:** [April 12, 2022, 7:46am UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184/1 "2022-04-12T07:46:04Z")

</div>

Hi all,

I don't know how to give a dynamic value to this `attributes` field in `google_pubsub` output plugin.

Currently this is what I do, which is not DRY

```auto
output {
  if [name] == "userRegistered" {
    google_pubsub {
      project_id => "a"
      topic => "ab"
      json_key_file => "abc"
      attributes => {
        "eventName" => "userRegistered"
      }
    }
  }
}

```

This one tho, won't work, even tho the conditional statement can read from the `name` field

```auto
output {
  if [name] == "userRegistered" {
    google_pubsub {
      project_id => "a"
      topic => "ab"
      json_key_file => "abc"
      attributes => {
        "eventNameA" => "%{[name]}"
        "eventNameB" => "%{name}"
        "eventNameC" => "[name]"
      }
    }
  }
}

```

What am I missing here?

---

<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:** [April 12, 2022, 2:44pm UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184/2 "2022-04-12T14:44:12Z")

</div>

The output [does not sprintf](https://github.com/logstash-plugins/logstash-output-google_pubsub/blob/5dd96d7bac35ef8dfaff35804bab0c0fa1a2e896/lib/logstash/outputs/pubsub/client.rb#L28) the attributes, so you cannot use a sprintf reference.

---

<div class="post-metadata">

**Author:** ![Altiano\_Gerung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/altiano_gerung/32/38040_2.png) [@Altiano\_Gerung](https://discuss.elastic.co/u/Altiano_Gerung)\
**Post date:** [April 13, 2022, 3:19am UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184/3 "2022-04-13T03:19:01Z")

</div>

So there's no way to make it dynamic?

---

<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:** [April 13, 2022, 3:30am UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184/4 "2022-04-13T03:30:56Z")

</div>

With the existing plugin I do not think it can be done. You could change [this](https://github.com/logstash-plugins/logstash-output-google_pubsub/blob/5dd96d7bac35ef8dfaff35804bab0c0fa1a2e896/lib/logstash/outputs/pubsub/client.rb#L35) line from

```
attributes.each { |k, v| builder.putAttributes(k, v) }

```

to

```
attributes.each { |k, v| builder.putAttributes(k, event.sprintf(v)) }

```

and rebuild the plugin from source. Or submit a PR that does that, or open an issue on github requesting that someone else do so 🙂

If doing either of the latter then you might want to consider whether anyone on the planet would consider it useful to sprintf both k _and_ v!

```
attributes.each { |k, v| builder.putAttributes(event.sprintf(k), event.sprintf(v)) }
```

---

<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:** [May 11, 2022, 3:31am UTC](https://discuss.elastic.co/t/using-dynamic-value-in-google-pubsub-filter-plugin-attributes-field/302184/5 "2022-05-11T03:31:24Z")

</div>

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