Dashboards & Visualizations

How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

rjthibod
Champion

Splunk 6.5 added global environment tokens that are accessible in SimpleXML (http://docs.splunk.com/Documentation/Splunk/6.5.0/Viz/tokens#Use_global_tokens_to_access_environment...).

My question is how does one obtain these token values from SplunkJS such as in a JavaScript extension to a SimpleXML dashboard?

Trying to obtain the values from defaultTokenModel or submittedTokenModel returns no result, and exploring those objects in the Chrome debugger does not indicate they would be defined in those objects.

I understand some of these values are available via rest API calls. I am specifically interested in getting them via SplunkJS.

1 Solution

ziegfried
Influencer

Here's an example:

require(['splunkjs/mvc', 'splunkjs/mvc/simplexml/ready!'], function(mvc) {

    var envTokenModel = mvc.Components.get('env');

    // Grab a specific env token
    console.log(envTokenModel.get('app'));

    // Log all env tokens
    console.log(envTokenModel.toJSON());

    // React to env token changes:
    envTokenModel.on('change', function() {
        console.log(arguments);
    });

});

View solution in original post

ziegfried
Influencer

Here's an example:

require(['splunkjs/mvc', 'splunkjs/mvc/simplexml/ready!'], function(mvc) {

    var envTokenModel = mvc.Components.get('env');

    // Grab a specific env token
    console.log(envTokenModel.get('app'));

    // Log all env tokens
    console.log(envTokenModel.toJSON());

    // React to env token changes:
    envTokenModel.on('change', function() {
        console.log(arguments);
    });

});

helge
Builder

It should REALLY be documented that you can access the env tokens (e.g. env:version) by specifying "env" as token model.

0 Karma

SK8
Explorer

Hello,
this solution run only for Simple-XML Dashboard. Exist a solution for a HTML-Dashboard to get the environment variables?

0 Karma

rjthibod
Champion

It should be very similar. I haven't tested this, but basing it off of some old HTML code I have.

...

var envTokenModel = mvc.Components.getInstance('env', {create: true});
var app_val = envTokenModel.get("app")
console.log(envTokenModel.get('app'));

...
0 Karma

rjthibod
Champion

Thanks @ziegfried. If you are still at Splunk, would you mind passing on a note to the docs team to incorporate this on the Dev portal at http://dev.splunk.com/view/webframework-developapps/SP-CAAAEW2?

0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...