Skip to main content

InfluxDB

Using Reporting Sinks, you can store NBomber metrics in InfluxDB and analyze your performance results with InfluxDB UI or Grafana.

info

To start working with NBomber.Sinks.InfluxDB package you should install it:

dotnet add package NBomber.Sinks.InfluxDB

Also, the source code is available on Github.

Integrating with InfluxDB

info

The simple way to run InfluxDB is via Docker. By this link, you can find a docker-compose.yaml to run it.

Configuring InfluxDB Sink via JSON Config

To configure InfluxDB Sink we will use JSON Infrastracture Config file

infra-config.json
{
"InfluxDBSink": {
"Url": "http://localhost:8086",
"Database": "nbomber",
"UserName": "username",
"Password": "password",
"CustomTags": [{"Key": "environment", "Value": "linux"}]
}
}

Now you should load this "infra-config.json" file.

var influxDbSink = new InfluxDBSink();

var scenario = Scenario.Create("scenario", async context => { ... });

NBomberRunner
.RegisterScenarios(scenario)
.WithReportingInterval(TimeSpan.FromSeconds(5))
.WithReportingSinks(influxDbSink)
.LoadInfraConfig("infra-config.json");

You can find the complete example by this link.

Saving custom metrics to InfluxDB

There could be cases where you want to write your custom raw metrics to InfluxDB. Here is an example of how you can use InfluxDB sink to write your custom data.

var influxDbSink = new InfluxDBSink();

var scenario = Scenario.Create("scenario", async context =>
{
var writeApi = influxDbSink.InfluxClient.GetWriteApiAsync();

var point = PointData
.Measurement("nbomber")
.Field("my_custom_counter", 1);

await writeApi.WritePointAsync(point);

return Response.Ok();
});

By following this link, you can get more info about InfluxClient.

Working with InfluxDB v1 and v2

For InfluxDB v1:

infra-config.json
{
"InfluxDBSink": {
"Url": "http://localhost:8086",
"Database": "database",
"UserName": "username",
"Password": "password",
"CustomTags": [{"Key": "environment", "Value": "linux"}]
}
}

For InfluxDB v2:

infra-config.json
{
"InfluxDBSink": {
"Url": "http://localhost:8086",
"Token": "Token",
"Org": "Org",
"Bucket": "nbomber",
"CustomTags": [{"Key": "environment", "Value": "linux"}]
}
}

Connecting to InfluxDB via code

You might have a situation that requires you to connect to InfluxDB via code. For this, you can inject an instance of InfluxDBClient.

For InfluxDB v1:

var influxDbSink = new InfluxDBSink(
new InfluxDBClient("http://localhost:8086", "username", "password", "database", retentionPolicy: "autogen")
);

For InfluxDB v2:

var influxOpt = new InfluxDBClientOptions("http://localhost:8086");
influxOpt.Org = "Org";
influxOpt.Bucket = "nbomber";
influxOpt.Token = "Token"

var influxDbSink = new InfluxDBSink(
new InfluxDBClient(influxOpt)
);