11using System ;
22using System . Collections . Generic ;
33using System . Globalization ;
4+ using System . Linq ;
45using System . Numerics ;
56using System . Text ;
67
@@ -433,13 +434,14 @@ public PointData Copy()
433434 /// Transform to Line Protocol.
434435 /// </summary>
435436 /// <param name="timeUnit">the timestamp precision</param>
437+ /// <param name="defaultTags">Tags added to point</param>
436438 /// <returns>Line Protocol</returns>
437- public string ToLineProtocol ( WritePrecision ? timeUnit = null )
439+ public string ToLineProtocol ( WritePrecision ? timeUnit = null , Dictionary < string , string > ? defaultTags = null )
438440 {
439441 var sb = new StringBuilder ( ) ;
440442
441443 EscapeKey ( sb , _values . GetMeasurement ( ) ! , false ) ;
442- AppendTags ( sb ) ;
444+ AppendTags ( sb , defaultTags ) ;
443445 var appendedFields = AppendFields ( sb ) ;
444446 if ( ! appendedFields )
445447 {
@@ -455,11 +457,23 @@ public string ToLineProtocol(WritePrecision? timeUnit = null)
455457 /// Appends the tags.
456458 /// </summary>
457459 /// <param name="writer">The writer.</param>
458- private void AppendTags ( StringBuilder writer )
460+ /// <param name="defaultTags">Tags added to point</param>
461+ private void AppendTags ( StringBuilder writer , Dictionary < string , string > ? defaultTags = null )
459462 {
460- foreach ( var name in _values . GetTagNames ( ) )
463+ var allNames = defaultTags == null
464+ ? _values . GetTagNames ( )
465+ : _values . GetTagNames ( ) . Concat ( defaultTags . Keys ) . ToArray ( )
466+ ;
467+ Array . Sort ( allNames ) ;
468+ var lastName = "" == allNames . FirstOrDefault ( ) ? "_" : "" ;
469+
470+ foreach ( var name in allNames )
461471 {
462- var value = _values . GetTag ( name ) ;
472+ if ( name == lastName ) continue ;
473+ lastName = name ;
474+
475+ var value = _values . GetTag ( name )
476+ ?? defaultTags . First ( kv => kv . Key == name ) . Value ;
463477
464478 if ( string . IsNullOrEmpty ( name ) || string . IsNullOrEmpty ( value ) )
465479 {
0 commit comments