MyInfluxDbClient is a simple async client for interacting with InfluxDB. It’s under continuous development, but can already be used to perform various database operations and write points, that you then e.g. can use in Grafana dashboards.
Roadmap
Track all features etc. via the Issues & Milestones.
The version will be pre v1.0.0
until the milestones: Gecko and Schema exploration; are done. Potentially also Digger
Getting setup
All operations are currently located on InfluxDbClient
which extends the interface IInfluxDbClient
. Internally it makes use of Requester to perform all HTTP-requests.
Install the NuGet package:
install-package MyInfluxDbClient
then create a client:
using (var client = new InfluxDbClient("http://192.168.1.176:8086"))
{
//consume client
}
you are now ready to start consuming it. See below for available operations.
Exceptions
Simple argument validation will throw either ArgumentException
or a derivate of it. This could be thrown if you e.g. pass null
for an operation that requires a databaseName
to be passed.
If, for some reason the call against InfluxDB fails, an InfluxDbClientException
will be thrown. For more info of why it failed, you can inspect the members:
public class InfluxDbClientException : Exception
{
public HttpMethod HttpMethod { get; }
public HttpStatusCode HttpStatus { get; }
public Uri Uri { get; }
public string Reason { get; }
public string Content { get; }
...
...
}
Database operations
Currently, the ops throws if InfluxDB returns failures. There will be additional, complementary operations e.g. EnsureDatabaseExistsAsync(dbName)
that will not throw if a database already exists.
client.GetDatabaseNamesAsync():Task
– returns an array of database name. Note! All databases are returned, even system databases.client.DatabaseExistsAsync(dbName):Task
– checks if a database exists. Note. Makes use ofGetDatabaseNamesAsync
to compare.client.CreateDatabaseAsync(dbName):Task
– create a database. Note! Throws if the database already exists.client.DropDatabaseAsync(dbName):Task
– drop an existing database. Note! Throws if the database does not exist.
Write points
The MyInfluxDbClient
makes use of the Line Protocol when writing points.
You write points by calling client.WriteAsync(points)
. Do this by:
- Create an
InfluxPoints
instance - Add one or more
InfluxPoint
instances to the collection - Pass the
InfluxPoints
toclient.WriteAsync
and specify the database to write to.
var points = new InfluxPoints()
.Add(new InfluxPoint("ticketsold")
.AddTag("seller", "1")
.AddTag("cur", "SEK")
.AddField("amt", 150.0)
.AddField("fee", 50.0)) //TimeStamp is assigned in Influx
.Add(new InfluxPoint("ticketsold")
.AddTag("seller", "1")
.AddTag("cur", "USD")
.AddField("amt", 10.0)
.AddField("fee", 2.5).AddTimeStamp())
.Add(new InfluxPoint("ticketsold")
.AddTag("seller", "2")
.AddTag("cur", "USD")
.AddField("amt", 10.0)
.AddField("fee", 2.5)
.AddTimeStamp(DateTime.Now)) //Conv. to nanoseconds since Epoch (UTC)
.Add(new InfluxPoint("ticketsold")
.AddTag("seller", "2")
.AddTag("cur", "EUR")
.AddField("amt", 51.8)
.AddField("fee", 5.0)
.AddTimeStamp(TimeStampResolutions.Minutes)) //Just keep timestamps to minutes. You can also pass a DateTime.
await client.WriteAsync("mydb", points);
WriteOptions
There are some WriteOptions
that you can configure either per-client or per call to WriteAsync
.
Note These are just formatted according to documentation.
WriteOptions per client (optional)
//Configure write options on client (if you want)
client.DefaultWriteOptions
.SetRetentionPolicy("test")
.SetTimeStampPrecision(TimeStampPrecision.Minutes)
.SetConsistency(Consistency.Quorum);
Tip If you want to reuse on all clients, then create a factory for creating your clients (or configure in your IoC container like Ninject).
WriteOptions per WriteAsync call (optional)
//Specific options can be passed if you want
var writeOptions = new WriteOptions()
.SetConsistency(Consistency.Any);
await client.WriteAsync("mydb", points, writeOptions);
The end
Keep track of upcoming changes by having a look at the milestones in the GitHub repo.
//Daniel