@@ -54,8 +54,6 @@ int aws_mqtt_append_sdk_metrics_to_username(
5454 }
5555 }
5656
57- bool metrics_added = false;
58-
5957 /* Add SDK if not present */
6058 if (!has_sdk ) {
6159 if (aws_byte_buf_append (& metrics_string , & sdk_str )) {
@@ -67,15 +65,13 @@ int aws_mqtt_append_sdk_metrics_to_username(
6765 if (aws_byte_buf_append (& metrics_string , & sdk_attr_value )) {
6866 goto error ;
6967 }
70-
71- metrics_added = true;
7268 }
7369
7470 /* Add Platform if not present */
7571 if (!has_platform ) {
7672 struct aws_byte_cursor platform_cursor = aws_get_platform_build_os_string ();
7773 if (platform_cursor .len > 0 ) {
78- if (metrics_added ) {
74+ if (metrics_string . len > 0 ) {
7975 if (aws_byte_buf_append (& metrics_string , & amp )) {
8076 goto error ;
8177 }
@@ -85,7 +81,6 @@ int aws_mqtt_append_sdk_metrics_to_username(
8581 aws_byte_buf_append (& metrics_string , & platform_cursor )) {
8682 goto error ;
8783 }
88- metrics_added = true;
8984 }
9085 }
9186
@@ -131,3 +126,60 @@ int aws_mqtt_append_sdk_metrics_to_username(
131126 aws_byte_buf_clean_up (& metrics_string );
132127 return AWS_OP_ERR ;
133128}
129+
130+ size_t aws_mqtt_append_sdk_metrics_to_username_size (
131+ const struct aws_byte_cursor * original_username ,
132+ const struct aws_mqtt_iot_sdk_metrics metrics ) {
133+
134+ /* Build metrics string */
135+ size_t metrics_string_size = 0 ;
136+
137+ /* Check if the attributes already exists in username */
138+ bool has_sdk = false;
139+ bool has_platform = false;
140+ bool has_query = false;
141+
142+ struct aws_byte_cursor sdk_str = aws_byte_cursor_from_c_str ("SDK=" );
143+ struct aws_byte_cursor platform_str = aws_byte_cursor_from_c_str ("Platform=" );
144+ struct aws_byte_cursor question_mark_str = aws_byte_cursor_from_c_str ("?" );
145+
146+ // TODO: we ignored the metadata field for now, need to add them in the future
147+
148+ if (original_username && original_username -> len > 0 ) {
149+ struct aws_byte_cursor question_mark_find ;
150+ has_query =
151+ AWS_OP_SUCCESS == aws_byte_cursor_find_exact (original_username , & question_mark_str , & question_mark_find );
152+ if (has_query ) {
153+ struct aws_byte_cursor temp_cursor ;
154+ has_sdk = AWS_OP_SUCCESS == aws_byte_cursor_find_exact (& question_mark_find , & sdk_str , & temp_cursor );
155+ has_platform =
156+ AWS_OP_SUCCESS == aws_byte_cursor_find_exact (& question_mark_find , & platform_str , & temp_cursor );
157+ }
158+ }
159+
160+ /* Add SDK if not present */
161+ if (!has_sdk ) {
162+ metrics_string_size += sdk_str .len ;
163+
164+ struct aws_byte_cursor sdk_attr_value =
165+ metrics .library_name .len > 0 ? metrics .library_name : aws_byte_cursor_from_c_str ("IoTDeviceSDK/C" );
166+
167+ metrics_string_size += sdk_attr_value .len ;
168+ }
169+
170+ /* Add Platform if not present */
171+ if (!has_platform ) {
172+ struct aws_byte_cursor platform_cursor = aws_get_platform_build_os_string ();
173+ if (platform_cursor .len > 0 ) {
174+ // Add amp sign if metrics is not empty
175+ metrics_string_size += metrics_string_size + metrics_string_size > 0 ? 1 : 0 ;
176+
177+ metrics_string_size += platform_str .len + platform_cursor .len ;
178+ }
179+ }
180+
181+ /* Build final output */
182+ /* original_username + 1 character of "?" or "&" + metrics string*/
183+ size_t total_size = (original_username ? original_username -> len : 0 ) + 1 + metrics_string_size ;
184+ return total_size ;
185+ }
0 commit comments