How to make a new custom global variable?


#1

Hi all,
I need to make a custom global variable (to write inside the kibana.yml) and maybe access to its value from the 'config' service inside my controllers.
Can anyone explain me how kibana deals with settings variable?
Thank you so much in advance!


(Joe Fleming) #2

I'm assuming that when you say "inside my controllers" that you're talking about inside a Kibana plugin you're developing. In that case, adding variable that will live inside kibana.yml is pretty easy.

In your plugin configuration, you set up something like this (just the important bits here):

return new kibana.Plugin({
  id: 'myplugin',

  //optionally override the setting prefix
  // configPrefix: 'custom.myplugin', 
  
  config: function (Joi) {
    return Joi.object({
      enabled: Joi.boolean().default(true),
      firstsetting: Joi.string(),
      deepsetting: Joi.object({
        propone: Joi.string().default('some value'),
        proptwo: Joi.number().integer().default(100),
        propthree: Joi.string(),
      }).default(),
    });
  },
  
  // ...
});

The config parameter is the important part here. It uses Joi, which is passed in as the first parameter. Any settings you define in here will be prefixed with the name of your app by default, but you can override that using configPrefix (note that it doesn't have to use a . in its name, but it can).

So, say you wanted to set firstsetting and override the default of deepsetting.propone, you would add something like this to your kibana.yml.

myplugin.firstsetting: 'hello'
myplugin.deepsetting.propone: 'another string'

The values of these settings in the yml file are validated with Joi, and if they are wrong, Kibana will not start. The resulting value after resolving it with the yml file is now available using the config service on the server object. As an example, you could use this value in an init function like so:

return new kibana.Plugin({
  id: 'myplugin',

  // ...
  
  init: function (server) {
    const config = server.config();
    const propone = config.get('myplugin.deepsetting.propone'); // another string
    const proptwo = config.get('myplugin.deepsetting.proptwo'); // 100
  }

  // ...
});

#3

Hi Joe, perfect explanation. Thank you so much!!! Now it's all clear! :+1:


(system) #4

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