# Get current time using ruby filter in logstash

**URL:** https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084
**Category:** Logstash
**Created:** [June 20, 2017, 11:00am UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084 "2017-06-20T11:00:04Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![sachintanpure85](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sachintanpure85](https://discuss.elastic.co/u/sachintanpure85)
#### Post date: [June 20, 2017, 11:00am UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/1 "2017-06-20T11:00:04Z")

</div>

I need to add the filed value for my index as current time. like Updated\_date column value as current time.  
As reference of post :[Add field timestamp with current time](https://discuss.elastic.co/t/add-field-timestamp-with-current-time/59862)  
we need ruby filter to create a field with the current time.

I am using :  
ruby {  
code =\> "event.set('Updated\_date',event.get('@timestamp'));  
}  
but I am getting the result as the timeof data inserted into elasticsearch first time, not after updating the data .  
how to create ruby filter to get the current time ?

---

<div class="post-metadata">

### Author: ![jsvd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsvd/32/6203_2.png) [@jsvd](https://discuss.elastic.co/u/jsvd)
#### Post date: [June 20, 2017, 11:07am UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/2 "2017-06-20T11:07:51Z")

</div>

so, you have the current timestamp in `@timestamp` and you want to use that time value to name your indices? I'm not sure I understand

Your example takes the value from `@timestamp` and places it in a field called `Updated_date`. What does your elasticsearch output look like?

---

<div class="post-metadata">

### Author: ![sachintanpure85](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sachintanpure85](https://discuss.elastic.co/u/sachintanpure85)
#### Post date: [June 20, 2017, 11:12am UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/3 "2017-06-20T11:12:35Z")

</div>

I have another column in my index as ''amount'' , I am updating the amount value once or a twice a day . so the "Updated\_date " column is for getting the last updated time for "amount".  
the output of my script is a timestamp : 2017-06-15T09:46:56.415Z

---

<div class="post-metadata">

### Author: ![jsvd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsvd/32/6203_2.png) [@jsvd](https://discuss.elastic.co/u/jsvd)
#### Post date: [June 20, 2017, 11:42am UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/4 "2017-06-20T11:42:42Z")

</div>

can you show me the elasticsearch output portion of the pipeline?

---

<div class="post-metadata">

### Author: ![sachintanpure85](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sachintanpure85](https://discuss.elastic.co/u/sachintanpure85)
#### Post date: [June 20, 2017, 12:18pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/5 "2017-06-20T12:18:31Z")

</div>

I am just printing the required columns in the output section after updating the amount values:  
output{  
stdout{  
codec =\> line { format =\> "%{amount}|%{updated\_date}"}  
}  
elasticsearch{  
hosts =\> ["localhost:9200"]  
user =\> "elastic"  
password =\> "changeme"  
index =\> "thresholds"  
document\_id =\> "%{issuer\_id}"  
doc\_as\_upsert =\> "true"  
}  
}

---

<div class="post-metadata">

### Author: ![jsvd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsvd/32/6203_2.png) [@jsvd](https://discuss.elastic.co/u/jsvd)
#### Post date: [June 20, 2017, 1:14pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/6 "2017-06-20T13:14:04Z")

</div>

have you tried with `action => "update"` ?

---

<div class="post-metadata">

### Author: ![sachintanpure85](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sachintanpure85](https://discuss.elastic.co/u/sachintanpure85)
#### Post date: [June 20, 2017, 1:27pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/7 "2017-06-20T13:27:19Z")

</div>

No , I haven't tried. will action =\> "update" ? provide me the current timestamp(after updation) of the record?

---

<div class="post-metadata">

### Author: ![jsvd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsvd/32/6203_2.png) [@jsvd](https://discuss.elastic.co/u/jsvd)
#### Post date: [June 20, 2017, 2:10pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/8 "2017-06-20T14:10:01Z")

</div>

I was only able to have a similar feature as what you wanted by creating explicit created\_at and updated\_at fields:

```auto
input { stdin { codec => json } }
filter {
  ruby {
    code => "event.set('updated_date', event.get('@timestamp'))"
  }
}
output {
  elasticsearch {
    action => update
    document_id => "%{my_id}"
    upsert => '{ "document_id": "%{my_id}", "value": "%{value}", "created_at": "%{@timestamp}", "updated_at": "%{@timestamp}" }'
  }
}

```

---

<div class="post-metadata">

### Author: ![jsvd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsvd/32/6203_2.png) [@jsvd](https://discuss.elastic.co/u/jsvd)
#### Post date: [June 20, 2017, 2:10pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/9 "2017-06-20T14:10:46Z")

</div>

This gives me as:

```auto
{
  "_index": "logstash-2017.06.20",
  "_type": "logs",
  "_id": "hey",
  "_version": 2,
  "found": true,
  "_source": {
    "document_id": "hey",
    "value": 2,
    "created_at": "2017-06-20T14:07:45.868Z",
    "@timestamp": "2017-06-20T14:08:23.229Z",
    "my_id": "hey",
    "@version": "1",
    "host": "Joaos-MBP-5",
    "updated_date": "2017-06-20T14:08:23.229Z"
  }
}

```

after sending these two events:

```auto
{ "my_id": "hey", "value": 1 }
{ "my_id": "hey", "value": 2 }

```

---

<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: [July 18, 2017, 2:10pm UTC](https://discuss.elastic.co/t/get-current-time-using-ruby-filter-in-logstash/90084/10 "2017-07-18T14:10:58Z")

</div>

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