@@ -65,8 +65,12 @@ const Client = function (host, port, prefix, suffix, globalize, cacheDns, mock,
65
65
this . prefix = options . prefix || '' ;
66
66
this . suffix = options . suffix || '' ;
67
67
this . tagPrefix = options . tagPrefix || '#' ;
68
- this . tagSeparator = options . tagSeparator || ',' ;
68
+ this . telegraf = options . telegraf || false ;
69
+ this . tagSeparator = this . telegraf ? ',' : options . tagSeparator || ',' ;
69
70
this . mock = options . mock ;
71
+ /**
72
+ * this is always an array. It's always populated.
73
+ */
70
74
this . globalTags = typeof options . globalTags === 'object' ?
71
75
helpers . formatTags ( options . globalTags , options . telegraf ) : [ ] ;
72
76
const availableDDEnvs = Object . keys ( DD_ENV_GLOBAL_TAGS_MAPPING ) . filter ( key => process . env [ key ] ) ;
@@ -75,7 +79,21 @@ const Client = function (host, port, prefix, suffix, globalize, cacheDns, mock,
75
79
filter ( ( item ) => ! availableDDEnvs . some ( env => item . startsWith ( `${ DD_ENV_GLOBAL_TAGS_MAPPING [ env ] } :` ) ) ) .
76
80
concat ( availableDDEnvs . map ( env => `${ DD_ENV_GLOBAL_TAGS_MAPPING [ env ] } :${ helpers . sanitizeTags ( process . env [ env ] ) } ` ) ) ;
77
81
}
78
- this . telegraf = options . telegraf || false ;
82
+ /**
83
+ * This is an object or `null`. It's populated if the original input is an object.
84
+ * We can't necessarily convert an array to an object, since array supports keys without values
85
+ */
86
+ this . globalTagsObjectSanitized = null ;
87
+ if ( typeof options . globalTags === 'object' && ! Array . isArray ( options . globalTags ) ) {
88
+ this . globalTagsObjectSanitized = { } ;
89
+ for ( const key of Object . keys ( options . globalTags ) ) {
90
+ const value = options . globalTags [ key ] ;
91
+ const sanitizedKey = helpers . sanitizeTags ( key ) ;
92
+ const sanitizedVal = helpers . sanitizeTags ( value ) ;
93
+ this . globalTagsObjectSanitized [ sanitizedKey ] = sanitizedVal ;
94
+ }
95
+ }
96
+ this . globalTagsMemo = helpers . overrideTagsToStringUnoptimized ( this . globalTags , [ ] , this . telegraf , this . tagSeparator ) ;
79
97
this . maxBufferSize = options . maxBufferSize || 0 ;
80
98
this . sampleRate = options . sampleRate || 1 ;
81
99
this . bufferFlushInterval = options . bufferFlushInterval || 1000 ;
@@ -242,22 +260,30 @@ Client.prototype.sendStat = function (stat, value, type, sampleRate, tags, callb
242
260
* @param callback {Function=} Callback when message is done being delivered (only if maxBufferSize == 0). Optional.
243
261
*/
244
262
Client . prototype . send = function ( message , tags , callback ) {
245
- let mergedTags = this . globalTags ;
246
- if ( tags && typeof tags === 'object' ) {
247
- mergedTags = helpers . overrideTags ( mergedTags , tags , this . telegraf ) ;
248
- }
249
- if ( mergedTags . length > 0 ) {
263
+ const tagsString = getTagString ( this . globalTags , this . globalTagsObjectSanitized , this . globalTagsMemo , tags , this . telegraf , this . tagSeparator ) ;
264
+ if ( tagsString . length > 0 ) {
250
265
if ( this . telegraf ) {
251
266
message = message . split ( ':' ) ;
252
- message = `${ message [ 0 ] } ,${ mergedTags . join ( ',' ) . replace ( / : / g, '=' ) } :${ message . slice ( 1 ) . join ( ':' ) } ` ;
267
+ message = `${ message [ 0 ] } ,${ tagsString . replace ( / : / g, '=' ) } :${ message . slice ( 1 ) . join ( ':' ) } ` ;
253
268
} else {
254
- message += `|${ this . tagPrefix } ${ mergedTags . join ( this . tagSeparator ) } ` ;
269
+ message += `|${ this . tagPrefix } ${ tagsString } ` ;
255
270
}
256
271
}
257
272
258
273
this . _send ( message , callback ) ;
259
274
} ;
260
275
276
+ // eslint-disable-next-line require-jsdoc
277
+ function getTagString ( globalTags , globalTagsObject , globalTagsMemo , tags , telegraf , tagSeparator ) {
278
+ if ( ! ( tags && typeof tags === 'object' ) ) {
279
+ return globalTagsMemo ;
280
+ }
281
+ if ( globalTagsObject !== null && tags && ! Array . isArray ( tags ) ) {
282
+ return helpers . overrideTags2 ( globalTagsObject , globalTagsMemo , tags , telegraf , tagSeparator ) ;
283
+ }
284
+ return helpers . overrideTagsToStringUnoptimized ( globalTags , tags , telegraf , tagSeparator ) ;
285
+ }
286
+
261
287
/**
262
288
* Send a stat or event across the wire
263
289
* @param message {String} The constructed message without tags
@@ -505,7 +531,7 @@ const ChildClient = function (parent, options) {
505
531
mock : parent . mock ,
506
532
// Append child's tags to parent's tags
507
533
globalTags : typeof options . globalTags === 'object' ?
508
- helpers . overrideTags ( parent . globalTags , options . globalTags , parent . telegraf ) : parent . globalTags ,
534
+ helpers . overrideTagsUnoptimized ( parent . globalTags , options . globalTags , parent . telegraf ) : parent . globalTags ,
509
535
maxBufferSize : parent . maxBufferSize ,
510
536
bufferFlushInterval : parent . bufferFlushInterval ,
511
537
telegraf : parent . telegraf ,
0 commit comments