danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

Introducing first release of MyInfluxDbClient

Daniel WertheimDaniel Wertheim

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.

Write points

The MyInfluxDbClient makes use of the Line Protocol when writing points.

You write points by calling client.WriteAsync(points). Do this by:

  1. Create an InfluxPoints instance
  2. Add one or more InfluxPoint instances to the collection
  3. Pass the InfluxPoints to client.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

Developer that lives by the mantra "code is meant to be shared".

Comments