@@ -405,22 +405,31 @@ public String toString() {
405
405
}
406
406
407
407
/**
408
- * calculate the lineprotocol entry for a single Point.
408
+ * Calculate the lineprotocol entry for a single Point.
409
+ * <p>
410
+ * NaN and infinity values are silently dropped as they are unsupported:
411
+ * https://github.com/influxdata/influxdb/issues/4089
409
412
*
410
- * Documentation is WIP : https://github.com/influxdb/influxdb/pull/2997
413
+ * @see <a href="https://docs.influxdata.com/influxdb/v1.7/write_protocols/line_protocol_reference/">
414
+ * InfluxDB line protocol reference</a>
411
415
*
412
- * https://github.com/influxdb/influxdb/blob/master/tsdb/README.md
413
- *
414
- * @return the String without newLine.
416
+ * @return the String without newLine, empty when there are no fields to write
415
417
*/
416
418
public String lineProtocol () {
417
419
return lineProtocol (null );
418
420
}
419
421
420
422
/**
421
423
* Calculate the lineprotocol entry for a single point, using a specific {@link TimeUnit} for the timestamp.
424
+ * <p>
425
+ * NaN and infinity values are silently dropped as they are unsupported:
426
+ * https://github.com/influxdata/influxdb/issues/4089
427
+ *
428
+ * @see <a href="https://docs.influxdata.com/influxdb/v1.7/write_protocols/line_protocol_reference/">
429
+ * InfluxDB line protocol reference</a>
430
+ *
422
431
* @param precision the time precision unit for this point
423
- * @return the String without newLine
432
+ * @return the String without newLine, empty when there are no fields to write
424
433
*/
425
434
public String lineProtocol (final TimeUnit precision ) {
426
435
@@ -431,7 +440,10 @@ public String lineProtocol(final TimeUnit precision) {
431
440
432
441
escapeKey (sb , measurement );
433
442
concatenatedTags (sb );
434
- concatenatedFields (sb );
443
+ int writtenFields = concatenatedFields (sb );
444
+ if (writtenFields == 0 ) {
445
+ return "" ;
446
+ }
435
447
formatedTime (sb , precision );
436
448
437
449
return sb .toString ();
@@ -447,10 +459,11 @@ private void concatenatedTags(final StringBuilder sb) {
447
459
sb .append (' ' );
448
460
}
449
461
450
- private void concatenatedFields (final StringBuilder sb ) {
462
+ private int concatenatedFields (final StringBuilder sb ) {
463
+ int fieldCount = 0 ;
451
464
for (Entry <String , Object > field : this .fields .entrySet ()) {
452
465
Object value = field .getValue ();
453
- if (value == null ) {
466
+ if (value == null || isNotFinite ( value ) ) {
454
467
continue ;
455
468
}
456
469
escapeKey (sb , field .getKey ());
@@ -471,13 +484,17 @@ private void concatenatedFields(final StringBuilder sb) {
471
484
}
472
485
473
486
sb .append (',' );
487
+
488
+ fieldCount ++;
474
489
}
475
490
476
491
// efficiently chop off the trailing comma
477
492
int lengthMinusOne = sb .length () - 1 ;
478
493
if (sb .charAt (lengthMinusOne ) == ',' ) {
479
494
sb .setLength (lengthMinusOne );
480
495
}
496
+
497
+ return fieldCount ;
481
498
}
482
499
483
500
static void escapeKey (final StringBuilder sb , final String key ) {
@@ -505,6 +522,11 @@ static void escapeField(final StringBuilder sb, final String field) {
505
522
}
506
523
}
507
524
525
+ private static boolean isNotFinite (final Object value ) {
526
+ return value instanceof Double && !Double .isFinite ((Double ) value )
527
+ || value instanceof Float && !Float .isFinite ((Float ) value );
528
+ }
529
+
508
530
private void formatedTime (final StringBuilder sb , final TimeUnit precision ) {
509
531
if (this .time == null ) {
510
532
return ;
0 commit comments