Search code examples
c#influxdb-2

InfluxDB 2 introductory sample does not insert any data into the bucket


Once you install InfluxDB, 2 it surfaces a website that includes sample code for various languages. Having created a bucket and a token with RW permissions and selected them, snippets of code with appropriate magic strings are available. Putting them together I have this:

using System;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
namespace gen
{
  class Program
  {
    static async Task Main(string[] args)
    {
      // init
      const string token = "uaKktnduBm_ranBVaG3y8vU-AAN ... w==";
      const string bucket = "SystemMonitor";
      const string org = "pdconsec";

      var client = InfluxDBClientFactory.Create("http://10.1.1.182:8086", token.ToCharArray());
      // write using data point (doesn't require model class)
      var point = PointData
      .Measurement("mem")
      .Tag("host", "host1")
      .Field("used_percent", 23.43234543)
      .Timestamp(DateTime.UtcNow, WritePrecision.Ns);
      using (var writeApi = client.GetWriteApi())
      {
        writeApi.WritePoint(bucket, org, point);
      }
      // Flux query
      var query = $"from(bucket: \"{bucket}\") |> range(start: -1h)";
      var tables = await client.GetQueryApi().QueryAsync(query, org);
    }
  }
}

The snippets demonstrate three different ways to write the same datum. All three execute without incident, but without data appearing in the bucket, so I have simplified the code here to just one write method. It runs without incident, but nothing appears in the bucket. Stepping through execution reveals that the Flux query executes returning an empty list of tables.

  • Do I need to create something inside a bucket or somehow assign it a structure corresponding to the shape of the data point?
  • Is there some kind of save, flush or commit that I have omitted?
  • That query looks to me like it means "everything from the named bucket that was logged in the last hour", is that right?

Solution

  • On the debug console an error message appears. You have to scroll back to see this message. It is buried in the usual avalanche of assembly load information when the application loads.

    The batch item wasn't processed successfully because: InfluxDB.Client.Core.Exceptions.ForbiddenException: insufficient permissions for write
       at InfluxDB.Client.WriteApi.<>c__DisplayClass9_2.<.ctor>b__21(RetryAttempt attempt)
       at System.Reactive.Linq.ObservableImpl.SelectMany`2.ObservableSelector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/SelectMany.cs:line 869
    

    So why doesn't my token have write permission? I thought I specified RW. Revisiting token creation, it appears one must click the write permission to highlight it in order to assign it to the token being created.

    In this example,

    Creating a write only token

    the token would be created with only write permission for the SystemMonitor bucket, because that bucket is highlighted only in the write column.

    The way this UI works isn't really clear until you have more than one bucket in it, and then it becomes more obvious that the highlighted buckets in the Write (or read) column are the ones for which the token will have write (or read) permission.