2

In my nodejs application, I am using influxdb (time series database) to save my log data. I used node-influx to write points in influxdb. But in my case, there are multiple log data which have same timestamp. In that case, only the last data is saved in the db.

I debugged the scripts and found javascript Date only contains milliseconds So in my case, multiple data have same timestamp as they vary on microseconds. So I need a Date format which give me the current date time with microseconds. Or is there any proper way to write multiple points having same timestamp in influxdb?

fyasir
  • 2,924
  • 2
  • 23
  • 36

2 Answers2

4

Influxdb is designed to hold only one point when the conditions(timestamps,measurement,tags) are identical, so you can accomplish this in two different ways:

  1. insert points by different timestamps in js with microsecond(stackoverflow)

  2. try to use different tags when you add multi points with same timestamps (influxdata_writing_data),such as

    api_info,tag=tag1 elapse=0.234 1467085047286(timestamp is optional)
    api_info,tag=tag2 elapse=0.478 1467085047286(timestamp is optional)

Community
  • 1
  • 1
TommyLike
  • 1,010
  • 11
  • 20
1

According to the InfluxDB documentation, point data timestamp can be as fine grain as in nanoseconds.

Writing data using the HTTP API

The HTTP API is the primary means of putting data into InfluxDB. To write data send a POST request to the /write endpoint. The example below writes a single point to the mydb database. The data consist of the measurement cpu_load_short, the tag keys host and region with the tag values server01 and us-west, the field key value with a field value of 0.64, and the timestamp 1434055562000000000.

Note: timestamp 1434055562000000000.

As for writing point in nanoseconds using the npm node-influx module, you can use set the precision as 'ns' (nano-seconds). That is, precision: 'ns'. To write points through the influx node module, it does not require you to have date objects thou, so if you know the exact date timestamp value since epoch, you can just pass in as 64 bit integer values for it to write into influx.

See example code here.

var influx = require('influx');

var databaseWriter = influx({
    host: 'XXX',
    port: 'XXX',
    protocol: 'XXX',
    username: 'XXX',
    password: 'XXX',
    database: 'XXX'
});

this.databaseWriter.writePoints(
    'influx_stackoverflow_solution',
    [
        // point #1
        [
            {
                "value": 999,
                "time" : 1422568543702900257
            },
            {
                'tag1' : 'value_in_nanoseconds'
            }
        ],
        // point #2
        [
            {
                "value": 8888,
                "time" : 1422568543702900600
            },
            {
                'tag1' : 'value_in_nanoseconds'
            }
        ]
    ],
    { precision: 'ns' },
    function(errmsg, returnValue) {
        if (errmsg) return callback(new Error(errmsg));
        callback(null, returnValue);
    }
);

Output:
enter image description here

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • how can I get current date time with nanosecond? – fyasir Jun 28 '16 at 06:02
  • From what I know is that Javascript's date time object only supports up to milliseconds. Hence that is not possible. In your post you mentioned the data you got are vary between microseconds, so I assume you already know the date timestamp of the point already. is that correct? Care to share what kind of data are you logging here? – Samuel Toh Jun 28 '16 at 06:09
  • I can't get the microsecond. That's why I am looking for any options to write them to into influx. – fyasir Jun 28 '16 at 06:13
  • So how do you know your data is differentiated by microseconds? Also, looking at @TommyLike's solution, the tag `elaps`e requires you to have the actual microseconds differentiation value to make each point unique. How are you working the value out then? – Samuel Toh Jun 28 '16 at 06:25
  • 1
    I need to save the data even if it shows the same timestamp. So I have taken @TommyLike's second option.Creating unique tag resolves my issue. But if there is any option that give me the current date time with nanoseconds that would be better. – fyasir Jun 28 '16 at 06:28
  • I see, if that is your use case then I guess it is fine. I think the only disadvantage you got here is that you are actually losing data because you will not be able to tell at what specific time (up to micro or nanosecond) was the point value recorded. – Samuel Toh Jun 28 '16 at 07:30