k6

InfluxDB information in code

Hi, I am not sure if this was asked before. I tried going through most of the messages.
Is there a way to pass InfluxDB info via options in code? Something like this :

export let options = {
collectors: {
mydb: {
type: “influxdb”,
url: “https://localhost:8086
}
}
};

I found this link mentioning these options - https://github.com/loadimpact/k6/issues/587
Am I specifying it the wrong way?

Hi,

unfortunately, the Collectors/Output documentation is lacking a bit, but currently configuring them via JS script options is not supported.

You can configure them with a JSON config file, environment variables or CLI flags, however.

Here’s a JSON config file example:

{
  "out": ["influxdb"],
  "collectors": {
    "influxdb": {
      "addr": "http://localhost:8086",
      "db": "k6"
    }
  }
}

On Linux/BSD you can save this at ${HOME}/.config/loadimpact/k6/config.json, or specify an alternative path via the --config global option.

You can see other possible InfluxDB options here.

Alternatively you can use environment variables as well: K6_INFLUXDB_ADDR, K6_INFLUXDB_DB, etc. In this case make sure to specify K6_OUT=influxdb as well, otherwise they won’t be picked up.

Thanks for the solution! I am able to use it via the config option :slight_smile:
I have one more question :
If I have 2 testcases and both use different databases, other than specifying the DB via CLI, is there a way to run both testcases with each of them using different databases?

If both tests are part of the same test script, and run as part of the same k6 run command, then they will both use the same database as specified in the environment or CLI flags, since there is no way to alter this at runtime within the test script.

However, you can have different JSON config files for each database and change it with the --config option, or use different K6_INFLUXDB_DB environment variable values for each test run.

Thank you for all the info @imiric :slight_smile: