@@ -226,8 +226,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
226226{
227227 int i ;
228228 int n ;
229- int count = 0 ;
229+ int static_count = 0 ;
230230 int static_labels = 0 ;
231+ int has_namespace = CMT_FALSE ;
231232 struct cmt_map_label * label_k ;
232233 struct cmt_map_label * label_v ;
233234 struct cfl_list * head ;
@@ -241,19 +242,26 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
241242 opts = map -> opts ;
242243
243244 /* Measurement */
244- cfl_sds_cat_safe (buf , opts -> ns , cfl_sds_len (opts -> ns ));
245-
246- if (cfl_sds_len (opts -> subsystem ) > 0 ) {
247- cfl_sds_cat_safe (buf , "_" , 1 );
248- cfl_sds_cat_safe (buf , opts -> subsystem , cfl_sds_len (opts -> subsystem ));
245+ if (cfl_sds_len (opts -> ns ) > 0 ) {
246+ cfl_sds_cat_safe (buf , opts -> ns , cfl_sds_len (opts -> ns ));
247+ if (cfl_sds_len (opts -> subsystem ) > 0 ) {
248+ cfl_sds_cat_safe (buf , "_" , 1 );
249+ cfl_sds_cat_safe (buf , opts -> subsystem , cfl_sds_len (opts -> subsystem ));
250+ }
251+ has_namespace = CMT_TRUE ;
252+ }
253+ else {
254+ has_namespace = CMT_FALSE ;
249255 }
250256
251257 /* Static labels (tags) */
252258 static_labels = cmt_labels_count (cmt -> static_labels );
253259 if (static_labels > 0 ) {
254- cfl_sds_cat_safe (buf , "," , 1 );
260+ if (has_namespace == CMT_TRUE ) {
261+ cfl_sds_cat_safe (buf , "," , 1 );
262+ }
255263 cfl_list_foreach (head , & cmt -> static_labels -> list ) {
256- count ++ ;
264+ static_count ++ ;
257265 slabel = cfl_list_entry (head , struct cmt_label , _head );
258266
259267 /* key */
@@ -265,7 +273,7 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
265273 /* val */
266274 append_string (buf , slabel -> val );
267275
268- if (count < static_labels ) {
276+ if (static_count < static_labels ) {
269277 cfl_sds_cat_safe (buf , "," , 1 );
270278 }
271279 }
@@ -274,7 +282,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
274282 /* Labels / Tags */
275283 n = cfl_list_size (& metric -> labels );
276284 if (n > 0 ) {
277- cfl_sds_cat_safe (buf , "," , 1 );
285+ if (static_labels > 0 || has_namespace == CMT_TRUE ) {
286+ cfl_sds_cat_safe (buf , "," , 1 );
287+ }
278288
279289 label_k = cfl_list_entry_first (& map -> label_keys , struct cmt_map_label , _head );
280290
@@ -297,7 +307,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
297307 }
298308 }
299309
300- cfl_sds_cat_safe (buf , " " , 1 );
310+ if (has_namespace == CMT_TRUE || static_labels > 0 || n > 0 ) {
311+ cfl_sds_cat_safe (buf , " " , 1 );
312+ }
301313 append_metric_value (map , buf , metric );
302314}
303315
0 commit comments