Splunk Search

Custom event renderer: able to access values in multivalued field?

Jason
Motivator

I'm writing up a custom event renderer to show the differences in two events in a transaction. Naturally, transaction will put in multivalued fields if the values are different between the two events.

I have seen both

<% count = event.fields['_count'] %>

and

<% tags = event.fields.get('_tags','') %>

formations in the search app's discovered.html. Can either of these be used to get values from a multivalued field, or get the count of values in a multivalued field? Or do I have to load up my search with a ton of evals?

0 Karma
1 Solution

Johnvey
Contributor

Yes, multivalue fields are accessible via the Python SDK. Let's use the following working example.

Get a search job that has multivalue fields (I coerce that by using makemv😞

my_job = splunk.search.dispatch('| windbag | makemv delim=" " sample | fields sample, host, source')

and then grab the first result to play around with:

result = my_job.results[0]

result is actually an object that contains all of the multivalue data as well as tags:

>>> result.fields.keys()
['sample', 'host', 'source', '_cd', '_raw', '_time']
>>> result.fields['sample']
Je,peux,manger,du,verre,,ça,ne,me,fait,pas,de,mal.
>>> result.fields['sample'][0]
<splunk.search.ResultFieldValue object at 0xa7dd8d0>
>>> result.fields['sample'][0].value
'Je'
>>> result.fields['sample'][0].tags
[]
>>> len(result.fields['sample'])
12
>>> result.fields['sample'][11].value
'mal.'

From the example above, you'll note that:

  • the base ResultField object will return a convenience string that is a concatenation of all of its values
  • applying an index selector to the ResultField object will pull out each multivalue field individually
  • the multivalue field values are stored in an array
  • the individual ResultFieldValue object contains 'value' and 'tags' members

View solution in original post

Johnvey
Contributor

Yes, multivalue fields are accessible via the Python SDK. Let's use the following working example.

Get a search job that has multivalue fields (I coerce that by using makemv😞

my_job = splunk.search.dispatch('| windbag | makemv delim=" " sample | fields sample, host, source')

and then grab the first result to play around with:

result = my_job.results[0]

result is actually an object that contains all of the multivalue data as well as tags:

>>> result.fields.keys()
['sample', 'host', 'source', '_cd', '_raw', '_time']
>>> result.fields['sample']
Je,peux,manger,du,verre,,ça,ne,me,fait,pas,de,mal.
>>> result.fields['sample'][0]
<splunk.search.ResultFieldValue object at 0xa7dd8d0>
>>> result.fields['sample'][0].value
'Je'
>>> result.fields['sample'][0].tags
[]
>>> len(result.fields['sample'])
12
>>> result.fields['sample'][11].value
'mal.'

From the example above, you'll note that:

  • the base ResultField object will return a convenience string that is a concatenation of all of its values
  • applying an index selector to the ResultField object will pull out each multivalue field individually
  • the multivalue field values are stored in an array
  • the individual ResultFieldValue object contains 'value' and 'tags' members

Jason
Motivator

So, in answer to my immediate question, <% makovar = event.fields['mvfield'][1] %> was able to access my second multivalued field value, and <% fieldcount = len(event.fields['mvfield']) %> got me how many values there were. I never knew this was python - thanks!

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...