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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...