Getting Data In

How to set the source in a collect command to a variable?

creiglow
Explorer

I am working with the collect command an want to set the source to a variable, not a string.

| eval myDynamicSource=index."-".sourcetype
| collect index=garbage source=myDynamicSource

However, the source in the collect is always just the name of the variable even if I use no quotes, double quotes or single quotes.

Any idea what I am missing?

Thanks,
--Keith

0 Karma
1 Solution

arjunpkishore5
Motivator

To my knowledge, collect operates on the entire Input, as in, it's not a streaming command. This is why if you cancel the search(with a collect statement at the end) before it has completed, none of the records are saved. So you cannot dynamically change the metadata directly on the collect, but you can do a workaround using props and transforms. here is how.

your collect remains as is | collect index=garbage source=myDynamicSource

In transforms.conf, you have this. In this, you're extracting the value of myDynamicSource and assigning it to the metadata field source

[setDynamicSource]
REGEX = (myDynamicSource)=(.+)
FORMAT = source::$2
DEST_KEY= MetaData:Source

In props.conf, you just apply the above transform to the source myDynamicSource

[source::myDynamicSource]
TRANSFORMS-set_source = setDynamicSource

Hope this helps.

Cheers

View solution in original post

0 Karma

arjunpkishore5
Motivator

To my knowledge, collect operates on the entire Input, as in, it's not a streaming command. This is why if you cancel the search(with a collect statement at the end) before it has completed, none of the records are saved. So you cannot dynamically change the metadata directly on the collect, but you can do a workaround using props and transforms. here is how.

your collect remains as is | collect index=garbage source=myDynamicSource

In transforms.conf, you have this. In this, you're extracting the value of myDynamicSource and assigning it to the metadata field source

[setDynamicSource]
REGEX = (myDynamicSource)=(.+)
FORMAT = source::$2
DEST_KEY= MetaData:Source

In props.conf, you just apply the above transform to the source myDynamicSource

[source::myDynamicSource]
TRANSFORMS-set_source = setDynamicSource

Hope this helps.

Cheers

0 Karma

frankfowler
Explorer

I dont see this as working :

I have transforms.conf and props.conf set as you stated above in my etc/system/local area.

my search is as follows :

index=xxx | eval myDynamicSource=source | collect index=xxx source=myDynamicSource

 

i see the source still set to myDynamicSource.

 

what am i missing ?

0 Karma
Get Updates on the Splunk Community!

Introduction to Splunk Observability Cloud - Building a Resilient Hybrid Cloud

Introduction to Splunk Observability Cloud - Building a Resilient Hybrid Cloud  In today’s fast-paced digital ...

Observability protocols to know about

Observability protocols define the specifications or formats for collecting, encoding, transporting, and ...

Take Your Breath Away with Splunk Risk-Based Alerting (RBA)

WATCH NOW!The Splunk Guide to Risk-Based Alerting is here to empower your SOC like never before. Join Haylee ...