Skip to content

Commit 623b5d7

Browse files
committed
Merge branch 'metrics_mqtt5' of github.com:awslabs/aws-c-mqtt into metrics_adapter
2 parents 46860a4 + 42a7372 commit 623b5d7

6 files changed

Lines changed: 44 additions & 40 deletions

File tree

include/aws/mqtt/private/mqtt_iot_sdk_metrics.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
struct aws_mqtt_iot_sdk_metrics_storage {
1111
struct aws_allocator *allocator;
1212

13+
struct aws_mqtt_iot_sdk_metrics storage_view;
14+
1315
struct aws_array_list metadata_entries;
1416

1517
struct aws_byte_cursor library_name;
@@ -27,7 +29,7 @@ AWS_MQTT_API void aws_mqtt_iot_sdk_metrics_storage_destroy(struct aws_mqtt_iot_s
2729
* Appends SDK metrics to the username
2830
*
2931
* @param original_username The original username
30-
* @param metrics_storage The metrics configuration
32+
* @param metrics The metrics configuration
3133
* @param output_username Buffer to store the modified username. If the function succeed, caller is responsible to
3234
* release the memory for output_username.
3335
* @param out_full_username_size If not NULL, will be set to the full size of the username with metrics appended
@@ -38,7 +40,7 @@ AWS_MQTT_API
3840
int aws_mqtt_append_sdk_metrics_to_username(
3941
struct aws_allocator *allocator,
4042
const struct aws_byte_cursor *original_username,
41-
const struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage,
43+
const struct aws_mqtt_iot_sdk_metrics *metrics,
4244
struct aws_byte_buf *output_username,
4345
size_t *out_full_username_size);
4446

source/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static void s_mqtt_client_init(
637637
if (aws_mqtt_append_sdk_metrics_to_username(
638638
connection->allocator,
639639
&username_cur,
640-
connection->metrics_storage ? connection->metrics_storage : NULL,
640+
connection->metrics_storage ? &connection->metrics_storage->storage_view : NULL,
641641
&username_with_metrics_buf,
642642
NULL) == AWS_OP_SUCCESS) {
643643
username_cur = aws_byte_cursor_from_buf(&username_with_metrics_buf);

source/mqtt_iot_sdk_metrics.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int s_build_username_query(
7878
int aws_mqtt_append_sdk_metrics_to_username(
7979
struct aws_allocator *allocator,
8080
const struct aws_byte_cursor *original_username,
81-
const struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage,
81+
const struct aws_mqtt_iot_sdk_metrics *metrics,
8282
struct aws_byte_buf *output_username,
8383
size_t *out_full_username_size) {
8484

@@ -88,7 +88,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
8888
const struct aws_byte_cursor local_original_username =
8989
original_username == NULL ? aws_byte_cursor_from_c_str("") : *original_username;
9090

91-
if (!metrics_storage) {
91+
if (!metrics) {
9292
if (out_full_username_size) {
9393
*out_full_username_size = local_original_username.len;
9494
}
@@ -100,6 +100,10 @@ int aws_mqtt_append_sdk_metrics_to_username(
100100
return AWS_OP_SUCCESS;
101101
}
102102

103+
if (aws_mqtt_validate_iot_sdk_metrics(metrics)) {
104+
return AWS_OP_ERR;
105+
}
106+
103107
int result = AWS_OP_ERR;
104108
// The length of the base username part not including query parameters
105109
size_t base_username_length = 0;
@@ -149,8 +153,8 @@ int aws_mqtt_append_sdk_metrics_to_username(
149153
if (!found_sdk) {
150154
struct aws_uri_param sdk_params = {
151155
.key = sdk_str,
152-
.value = metrics_storage->library_name.len > 0 ? metrics_storage->library_name
153-
: aws_byte_cursor_from_c_str("IoTDeviceSDK/C"),
156+
.value =
157+
metrics->library_name.len > 0 ? metrics->library_name : aws_byte_cursor_from_c_str("IoTDeviceSDK/C"),
154158
};
155159
aws_array_list_push_back(&params_list, &sdk_params);
156160
}
@@ -236,11 +240,14 @@ struct aws_mqtt_iot_sdk_metrics_storage *aws_mqtt_iot_sdk_metrics_storage_new(
236240

237241
metrics_storage->allocator = allocator;
238242

243+
struct aws_mqtt_iot_sdk_metrics *storage_view = &metrics_storage->storage_view;
244+
239245
if (metrics_options->library_name.len > 0) {
240246
metrics_storage->library_name = metrics_options->library_name;
241247
if (aws_byte_buf_append_and_update(&metrics_storage->storage, &metrics_storage->library_name)) {
242248
goto cleanup_storage;
243249
}
250+
storage_view->library_name = metrics_storage->library_name;
244251
}
245252

246253
return metrics_storage;

source/v5/mqtt5_options_storage.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ static size_t s_aws_mqtt5_packet_connect_compute_storage_size(
649649
if (options) {
650650
size_t username_size = 0;
651651
aws_mqtt_append_sdk_metrics_to_username(
652-
aws_default_allocator(), view->username, options->metrics_storage, NULL, &username_size);
652+
aws_default_allocator(), view->username, &options->metrics_storage->storage_view, NULL, &username_size);
653653
storage_size += username_size;
654654
} else {
655655
storage_size += view->username->len;
@@ -704,7 +704,11 @@ int aws_mqtt5_packet_connect_storage_init(
704704
if (client_storage) {
705705
struct aws_byte_cursor username_cur = storage->username;
706706
if (aws_mqtt_append_sdk_metrics_to_username(
707-
allocator, &username_cur, client_storage->metrics_storage, &metrics_username_buf, NULL)) {
707+
allocator,
708+
&username_cur,
709+
&client_storage->metrics_storage->storage_view,
710+
&metrics_username_buf,
711+
NULL)) {
708712
return AWS_OP_ERR;
709713
}
710714
storage->username = aws_byte_cursor_from_buf(&metrics_username_buf);

tests/shared_utils_tests.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,11 @@ static int s_test_mqtt_append_sdk_metrics_empty(struct aws_allocator *allocator,
163163
(void)ctx;
164164

165165
struct aws_mqtt_iot_sdk_metrics metrics = {{0}};
166-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
167-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &metrics);
168166

169167
struct aws_byte_buf output_username;
170168
AWS_ZERO_STRUCT(output_username);
171169

172-
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(allocator, NULL, metrics_storage, &output_username, NULL));
173-
aws_mqtt_iot_sdk_metrics_storage_destroy(metrics_storage);
170+
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(allocator, NULL, &metrics, &output_username, NULL));
174171

175172
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username);
176173

@@ -199,17 +196,14 @@ static int s_test_mqtt_append_sdk_metrics_basic(struct aws_allocator *allocator,
199196
// .metadata_entries = NULL,
200197
// .metadata_count = 0,
201198
};
202-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
203-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &metrics);
204199

205200
struct aws_byte_buf output_username;
206201
AWS_ZERO_STRUCT(output_username);
207202

208203
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("TEST_USERNAME");
209204

210-
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(
211-
allocator, &original_username, metrics_storage, &output_username, NULL));
212-
aws_mqtt_iot_sdk_metrics_storage_destroy(metrics_storage);
205+
ASSERT_SUCCESS(
206+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL));
213207

214208
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username);
215209

@@ -238,18 +232,15 @@ static int s_test_mqtt_append_sdk_metrics_existing_attributes(struct aws_allocat
238232
// .metadata_entries = NULL,
239233
// .metadata_count = 0,
240234
};
241-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
242-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &metrics);
243235

244236
struct aws_byte_buf output_username;
245237
AWS_ZERO_STRUCT(output_username);
246238

247239
struct aws_byte_cursor original_username =
248240
aws_byte_cursor_from_c_str("user?SDK=ExistingSDK&Platform=ExistingPlatform");
249241

250-
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(
251-
allocator, &original_username, metrics_storage, &output_username, NULL));
252-
aws_mqtt_iot_sdk_metrics_storage_destroy(metrics_storage);
242+
ASSERT_SUCCESS(
243+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL));
253244

254245
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username);
255246

@@ -266,17 +257,14 @@ static int s_test_mqtt_append_sdk_metrics_special_chars(struct aws_allocator *al
266257
(void)ctx;
267258

268259
struct aws_mqtt_iot_sdk_metrics metrics = {.library_name = aws_byte_cursor_from_c_str("SDK/Test-1.0")};
269-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
270-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &metrics);
271260

272261
struct aws_byte_buf output_username;
273262
AWS_ZERO_STRUCT(output_username);
274263

275264
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("user@domain.com");
276265

277-
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(
278-
allocator, &original_username, metrics_storage, &output_username, NULL));
279-
aws_mqtt_iot_sdk_metrics_storage_destroy(metrics_storage);
266+
ASSERT_SUCCESS(
267+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL));
280268

281269
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username);
282270

@@ -309,27 +297,20 @@ static int s_test_mqtt_append_sdk_metrics_long_strings(struct aws_allocator *all
309297
long_sdk_name[sizeof(long_sdk_name) - 1] = '\0';
310298

311299
struct aws_mqtt_iot_sdk_metrics metrics = {.library_name = aws_byte_cursor_from_c_str(long_sdk_name)};
312-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
313-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &metrics);
314300

315301
struct aws_mqtt_iot_sdk_metrics short_metrics = {.library_name = aws_byte_cursor_from_c_str("SHORT_SDK_NAME")};
316-
struct aws_mqtt_iot_sdk_metrics_storage *short_metrics_storage =
317-
aws_mqtt_iot_sdk_metrics_storage_new(allocator, &short_metrics);
318302

319303
struct aws_byte_buf output_username;
320304
AWS_ZERO_STRUCT(output_username);
321305

322306
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str(long_username);
323307

324308
// aws_mqtt_append_sdk_metrics_to_username does not valid original username length
325-
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(
326-
allocator, &original_username, short_metrics_storage, &output_username, NULL));
327-
aws_mqtt_iot_sdk_metrics_storage_destroy(short_metrics_storage);
328-
309+
ASSERT_SUCCESS(
310+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &short_metrics, &output_username, NULL));
329311
// aws_mqtt_append_sdk_metrics_to_username fails when the extra metrics string exceeds buffer limit
330-
ASSERT_FAILS(aws_mqtt_append_sdk_metrics_to_username(
331-
allocator, &original_username, metrics_storage, &output_username, NULL));
332-
aws_mqtt_iot_sdk_metrics_storage_destroy(metrics_storage);
312+
ASSERT_FAILS(
313+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL));
333314

334315
aws_byte_buf_clean_up(&output_username);
335316

@@ -347,8 +328,17 @@ static int s_test_mqtt_append_sdk_metrics_invalid_utf8(struct aws_allocator *all
347328

348329
struct aws_mqtt_iot_sdk_metrics metrics = {.library_name = invalid_utf8_library};
349330

331+
struct aws_byte_buf output_username;
332+
AWS_ZERO_STRUCT(output_username);
333+
334+
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("testuser");
335+
350336
/* Should fail due to invalid UTF-8 */
351-
ASSERT_FAILS(aws_mqtt_validate_iot_sdk_metrics(&metrics));
337+
ASSERT_FAILS(
338+
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL));
339+
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8);
340+
341+
aws_byte_buf_clean_up(&output_username);
352342

353343
return AWS_OP_SUCCESS;
354344
}

tests/v5/mqtt5_operation_and_storage_tests.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ static int s_mqtt5_connect_storage_new_set_all_fn(struct aws_allocator *allocato
729729
expected_username.buffer, expected_username.len, connect_storage.username.ptr, connect_storage.username.len);
730730
aws_byte_buf_clean_up(&expected_username);
731731

732+
AWS_VERIFY_VIEW_STORAGE_RELATIONSHIP_NULLABLE_CURSOR(&connect_storage, &connect_options, username);
732733
AWS_VERIFY_VIEW_STORAGE_RELATIONSHIP_NULLABLE_CURSOR(&connect_storage, &connect_options, password);
733734
AWS_VERIFY_VIEW_STORAGE_RELATIONSHIP_NULLABLE_UINT(
734735
&connect_storage, &connect_options, session_expiry_interval_seconds);

0 commit comments

Comments
 (0)