# Dynamic add\_field in metrics

**URL:** https://discuss.elastic.co/t/dynamic-add-field-in-metrics/94926
**Category:** Logstash
**Created:** [July 28, 2017, 9:32am UTC](https://discuss.elastic.co/t/dynamic-add-field-in-metrics/94926 "2017-07-28T09:32:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![costef](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@costef](https://discuss.elastic.co/u/costef)
#### Post date: [July 28, 2017, 9:32am UTC](https://discuss.elastic.co/t/dynamic-add-field-in-metrics/94926/1 "2017-07-28T09:32:05Z")

</div>

I've a logstash conf file like the following:  
input {  
generator {  
type =\> "generated"  
}  
}

filter {  
metrics {  
type =\> "generated"  
meter =\> "foo"  
add\_tag =\> "metric"  
**add\_field =\> ["foo", "Hello world, from %{host}"]**  
}  
}

output {  
if [type] != "generated" {  
stdout {  
debug =\> true  
}  
}  
}

that produces the following output:

{  
"@timestamp" =\> "2017-07-28T11:00:01.123Z",  
"@version" =\> "1",  
"message" =\> "27b0c651-0dc0-4910-8c6f-98778e76f2b3",  
"foo.count" =\> 1,  
"foo.rate\_1m" =\> 0.0,  
"foo.rate\_5m" =\> 0.0,  
"foo.rate\_15m" =\> 0.0,  
**"foo" =\> "Hello world, from %{host}",**  
"tags" =\> [  
[0] "metric"  
]  
}

Instead of having the string "%{host}" I would like to have the resolved host name.  
Unfortunately this doesn't happen. Not resolving the dynamic field in metrics seems to be an old bug of logstash (isn't it ?).

Unfortunately I don't have any clue about how to resolve it.  
Is there anybody that could provide me a solution ? A workaround is also very well accepted.

Thanks a lot in advance

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [August 5, 2017, 7:58pm UTC](https://discuss.elastic.co/t/dynamic-add-field-in-metrics/94926/2 "2017-08-05T19:58:41Z")

</div>

> Instead of having the string “%{host}” I would like to have the resolved host name.

`%{host}` doesn't expand because the event doesn't have a `host` field.

- If you store the hostname in an environment variable you can replace `%{host}` with `${HOSTNAME}` (or whatever the variable is named).
- You can use a ruby filter to obtain the hostname and add it as a field.
- What input plugin are you _really_ planning on using as presumably the generator input is just for testing? Many real input plugins add a `host` field.

---

<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: [September 2, 2017, 7:58pm UTC](https://discuss.elastic.co/t/dynamic-add-field-in-metrics/94926/3 "2017-09-02T19:58:48Z")

</div>

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