Skip to content

Commit 288ed7f

Browse files
committed
Merge branch 'metrics_mqtt3' of github.com:awslabs/aws-c-mqtt into metrics_mqtt5
2 parents 8a4a274 + 23302f1 commit 288ed7f

7 files changed

Lines changed: 215 additions & 203 deletions

File tree

include/aws/mqtt/private/client_impl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ struct aws_mqtt_client_connection_311_impl {
369369
/**
370370
* IoT SDK metrics configuration
371371
*/
372-
struct aws_mqtt_iot_sdk_metrics_storage *metrics;
373-
struct aws_byte_buf username_with_metrics_buf;
372+
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage;
374373
};
375374

376375
struct aws_channel_handler_vtable *aws_mqtt_get_client_channel_vtable(void);

include/aws/mqtt/private/client_impl_shared.h

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,6 @@ struct aws_mqtt_client_connection {
134134
void *impl;
135135
};
136136

137-
struct aws_mqtt_iot_sdk_metrics_storage {
138-
struct aws_allocator *allocator;
139-
140-
struct aws_mqtt_iot_sdk_metrics storage_view;
141-
142-
struct aws_array_list metadata_entries;
143-
144-
struct aws_byte_cursor library_name;
145-
146-
struct aws_byte_buf storage;
147-
};
148-
149-
/* IoT SDK Metrics */
150-
AWS_MQTT_API struct aws_mqtt_iot_sdk_metrics_storage *aws_mqtt_iot_sdk_metrics_storage_new(
151-
struct aws_allocator *allocator,
152-
const struct aws_mqtt_iot_sdk_metrics *metrics_options);
153-
154-
AWS_MQTT_API size_t aws_mqtt_iot_sdk_metrics_compute_storage_size(const struct aws_mqtt_iot_sdk_metrics *metrics);
155-
156-
AWS_MQTT_API void aws_mqtt_iot_sdk_metrics_storage_destroy(struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage);
157-
158137
AWS_MQTT_API enum aws_mqtt311_impl_type aws_mqtt_client_connection_get_impl_type(
159138
const struct aws_mqtt_client_connection *connection);
160139

@@ -167,32 +146,4 @@ AWS_MQTT_API bool aws_mqtt_byte_cursor_hash_equality(const void *a, const void *
167146
AWS_MQTT_API struct aws_event_loop *aws_mqtt_client_connection_get_event_loop(
168147
const struct aws_mqtt_client_connection *connection);
169148

170-
/**
171-
* Appends SDK metrics to the username field
172-
*
173-
* @param original_username The original username
174-
* @param metrics The metrics configuration
175-
* @param output_username Buffer to store the modified username. If the function succeed, caller is responsible to
176-
* release the memory for output_username.
177-
* @param out_full_username_size If not NULL, will be set to the full size of the username with metrics appended
178-
*
179-
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
180-
*/
181-
AWS_MQTT_API
182-
int aws_mqtt_append_sdk_metrics_to_username(
183-
struct aws_allocator *allocator,
184-
const struct aws_byte_cursor *original_username,
185-
const struct aws_mqtt_iot_sdk_metrics metrics,
186-
struct aws_byte_buf *output_username,
187-
size_t *out_full_username_size);
188-
189-
/**
190-
* Validates that all string fields in aws_mqtt_iot_sdk_metrics are valid UTF-8
191-
*
192-
* @param metrics The metrics structure to validate
193-
* @return AWS_OP_SUCCESS if all strings are valid UTF-8, AWS_OP_ERR otherwise
194-
*/
195-
AWS_MQTT_API
196-
int aws_mqtt_validate_iot_sdk_metrics_utf8(const struct aws_mqtt_iot_sdk_metrics *metrics);
197-
198149
#endif /* AWS_MQTT_PRIVATE_CLIENT_IMPL_SHARED_H */
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#ifndef AWS_MQTT_IOT_SDK_METRICS_H
7+
#define AWS_MQTT_IOT_SDK_METRICS_H
8+
9+
/* IoT SDK Metrics */
10+
struct aws_mqtt_iot_sdk_metrics_storage {
11+
struct aws_allocator *allocator;
12+
13+
struct aws_mqtt_iot_sdk_metrics storage_view;
14+
15+
struct aws_array_list metadata_entries;
16+
17+
struct aws_byte_cursor library_name;
18+
19+
struct aws_byte_buf storage;
20+
};
21+
22+
AWS_MQTT_API struct aws_mqtt_iot_sdk_metrics_storage *aws_mqtt_iot_sdk_metrics_storage_new(
23+
struct aws_allocator *allocator,
24+
const struct aws_mqtt_iot_sdk_metrics *metrics_options);
25+
26+
AWS_MQTT_API size_t aws_mqtt_iot_sdk_metrics_compute_storage_size(const struct aws_mqtt_iot_sdk_metrics *metrics);
27+
28+
AWS_MQTT_API void aws_mqtt_iot_sdk_metrics_storage_destroy(struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage);
29+
30+
/**
31+
* Appends SDK metrics to the username field 1Code has comments. Press enter to view.
32+
*
33+
* @param original_username The original username
34+
* @param metrics The metrics configuration
35+
* @param output_username Buffer to store the modified username. If the function succeed, caller is responsible to
36+
* release the memory for output_username.
37+
* @param out_full_username_size If not NULL, will be set to the full size of the username with metrics appended
38+
*
39+
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
40+
*/
41+
AWS_MQTT_API
42+
int aws_mqtt_append_sdk_metrics_to_username(
43+
struct aws_allocator *allocator,
44+
const struct aws_byte_cursor *original_username,
45+
const struct aws_mqtt_iot_sdk_metrics metrics,
46+
struct aws_byte_buf *output_username,
47+
size_t *out_full_username_size);
48+
49+
/**
50+
* Validates that all string fields in aws_mqtt_iot_sdk_metrics are valid UTF-8
51+
*
52+
* @param metrics The metrics structure to validate
53+
* @return AWS_OP_SUCCESS if all strings are valid UTF-8, AWS_OP_ERR otherwise
54+
*/
55+
AWS_MQTT_API
56+
int aws_mqtt_validate_iot_sdk_metrics_utf8(const struct aws_mqtt_iot_sdk_metrics *metrics);
57+
58+
#endif /* AWS_MQTT_IOT_SDK_METRICS_H */

source/client.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <aws/mqtt/client.h>
66

77
#include <aws/mqtt/private/client_impl.h>
8-
#include <aws/mqtt/private/client_impl_shared.h>
98
#include <aws/mqtt/private/mqtt_client_test_helper.h>
9+
#include <aws/mqtt/private/mqtt_iot_sdk_metrics.h>
1010
#include <aws/mqtt/private/packets.h>
1111
#include <aws/mqtt/private/shared.h>
1212
#include <aws/mqtt/private/topic_tree.h>
@@ -504,6 +504,8 @@ static void s_mqtt_client_init(
504504
AWS_FATAL_ASSERT((error_code != 0) == (channel == NULL));
505505

506506
struct aws_mqtt_client_connection_311_impl *connection = user_data;
507+
struct aws_byte_buf username_with_metrics_buf;
508+
aws_byte_buf_init(&username_with_metrics_buf, connection->allocator, 0);
507509

508510
if (error_code != AWS_OP_SUCCESS) {
509511
/* client shutdown already handles this case, so just call that. */
@@ -624,7 +626,7 @@ static void s_mqtt_client_init(
624626
&connect, topic_cur, connection->will.qos, connection->will.retain, payload_cur);
625627
}
626628

627-
if (connection->username || connection->metrics) {
629+
if (connection->username || connection->metrics_storage) {
628630
struct aws_byte_cursor username_cur;
629631
if (connection->username) {
630632
username_cur = aws_byte_cursor_from_string(connection->username);
@@ -633,17 +635,15 @@ static void s_mqtt_client_init(
633635
username_cur = aws_byte_cursor_from_c_str("");
634636
}
635637

636-
aws_byte_buf_clean_up(&connection->username_with_metrics_buf);
637-
638638
/* Apply metrics to username if configured */
639-
if (connection->metrics) {
639+
if (connection->metrics_storage) {
640640
if (aws_mqtt_append_sdk_metrics_to_username(
641641
connection->allocator,
642642
&username_cur,
643-
connection->metrics->storage_view,
644-
&connection->username_with_metrics_buf,
643+
connection->metrics_storage->storage_view,
644+
&username_with_metrics_buf,
645645
NULL) == AWS_OP_SUCCESS) {
646-
username_cur = aws_byte_cursor_from_buf(&connection->username_with_metrics_buf);
646+
username_cur = aws_byte_cursor_from_buf(&username_with_metrics_buf);
647647
} else {
648648
AWS_LOGF_WARN(
649649
AWS_LS_MQTT_CLIENT,
@@ -689,13 +689,21 @@ static void s_mqtt_client_init(
689689
goto handle_error;
690690
}
691691

692+
if (aws_byte_buf_is_valid(&username_with_metrics_buf)) {
693+
aws_byte_buf_clean_up(&username_with_metrics_buf);
694+
}
695+
692696
return;
693697

694698
handle_error:
695699
MQTT_CLIENT_CALL_CALLBACK_ARGS(connection, on_connection_complete, aws_last_error(), 0, false);
696700
MQTT_CLIENT_CALL_CALLBACK_ARGS(connection, on_connection_failure, aws_last_error());
697701
aws_channel_shutdown(channel, aws_last_error());
698702

703+
if (aws_byte_buf_is_valid(&username_with_metrics_buf)) {
704+
aws_byte_buf_clean_up(&username_with_metrics_buf);
705+
}
706+
699707
if (message) {
700708
aws_mem_release(message->allocator, message);
701709
}
@@ -871,13 +879,11 @@ static void s_mqtt_client_connection_destroy_final(struct aws_mqtt_client_connec
871879
}
872880

873881
/* Clean up metrics */
874-
if (connection->metrics) {
875-
aws_mqtt_iot_sdk_metrics_storage_destroy(connection->metrics);
876-
connection->metrics = NULL;
882+
if (connection->metrics_storage) {
883+
aws_mqtt_iot_sdk_metrics_storage_destroy(connection->metrics_storage);
884+
connection->metrics_storage = NULL;
877885
}
878886

879-
aws_byte_buf_clean_up(&connection->username_with_metrics_buf);
880-
881887
aws_mqtt_client_release(connection->client);
882888

883889
/* Frees all allocated memory */
@@ -3395,14 +3401,14 @@ static int s_aws_mqtt_client_connection_311_set_metrics(void *impl, const struct
33953401
AWS_LOGF_TRACE(AWS_LS_MQTT_CLIENT, "id=%p: Setting IoT SDK metrics", (void *)connection);
33963402

33973403
/* Clean up existing metrics if any */
3398-
if (connection->metrics) {
3399-
aws_mqtt_iot_sdk_metrics_storage_destroy(connection->metrics);
3400-
connection->metrics = NULL;
3404+
if (connection->metrics_storage) {
3405+
aws_mqtt_iot_sdk_metrics_storage_destroy(connection->metrics_storage);
3406+
connection->metrics_storage = NULL;
34013407
}
34023408

34033409
if (metrics) {
34043410
/* Initialize metrics storage */
3405-
connection->metrics = aws_mqtt_iot_sdk_metrics_storage_new(connection->allocator, metrics);
3411+
connection->metrics_storage = aws_mqtt_iot_sdk_metrics_storage_new(connection->allocator, metrics);
34063412
}
34073413

34083414
return AWS_OP_SUCCESS;
@@ -3561,8 +3567,6 @@ struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqt
35613567
connection->handler.vtable = aws_mqtt_get_client_channel_vtable();
35623568
connection->handler.impl = connection;
35633569

3564-
aws_byte_buf_init(&connection->username_with_metrics_buf, connection->allocator, 0);
3565-
35663570
aws_mqtt311_callback_set_manager_init(&connection->callback_manager, connection->allocator, &connection->base);
35673571

35683572
return &connection->base;

source/client_impl_shared.c

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -227,134 +227,3 @@ bool aws_mqtt_byte_cursor_hash_equality(const void *a, const void *b) {
227227
struct aws_event_loop *aws_mqtt_client_connection_get_event_loop(const struct aws_mqtt_client_connection *connection) {
228228
return (*connection->vtable->get_event_loop)(connection->impl);
229229
}
230-
231-
/*********************************************************************************************************************
232-
* IoT SDK Metrics
233-
********************************************************************************************************************/
234-
235-
size_t aws_mqtt_iot_sdk_metrics_compute_storage_size(const struct aws_mqtt_iot_sdk_metrics *metrics) {
236-
if (metrics == NULL) {
237-
return 0;
238-
}
239-
240-
size_t storage_size = 0;
241-
242-
storage_size += metrics->library_name.len;
243-
244-
// TODO: add metadata entries when enabled
245-
// for (size_t i = 0; i < metrics->metadata_count; ++i) {
246-
// storage_size += metrics->metadata_entries[i].key.len;
247-
// storage_size += metrics->metadata_entries[i].value.len;
248-
// }
249-
250-
return storage_size;
251-
}
252-
253-
struct aws_mqtt_iot_sdk_metrics_storage *aws_mqtt_iot_sdk_metrics_storage_new(
254-
struct aws_allocator *allocator,
255-
const struct aws_mqtt_iot_sdk_metrics *metrics_options) {
256-
257-
if (allocator == NULL || metrics_options == NULL) {
258-
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
259-
return NULL;
260-
}
261-
262-
struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage =
263-
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_iot_sdk_metrics_storage));
264-
265-
size_t storage_capacity = aws_mqtt_iot_sdk_metrics_compute_storage_size(metrics_options);
266-
if (aws_byte_buf_init(&metrics_storage->storage, allocator, storage_capacity)) {
267-
goto cleanup_storage;
268-
}
269-
270-
metrics_storage->allocator = allocator;
271-
272-
struct aws_mqtt_iot_sdk_metrics *storage_view = &metrics_storage->storage_view;
273-
274-
// TODO Future Work: add metadata entries once we implemented the metadata feature
275-
//
276-
// if (aws_array_list_init_dynamic(
277-
// &metrics_storage->metadata_entries,
278-
// allocator,
279-
// metrics_options->metadata_count,
280-
// sizeof(struct aws_mqtt_metadata_entry))) {
281-
// goto metrics_storage_error;
282-
// }
283-
284-
// for (size_t i = 0; i < metrics_options->metadata_count; ++i) {
285-
// struct aws_mqtt_metadata_entry entry = metrics_options->metadata_entries[i];
286-
287-
// if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.key)) {
288-
// goto metrics_storage_error;
289-
// }
290-
291-
// if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.value)) {
292-
// goto metrics_storage_error;
293-
// }
294-
295-
// if (aws_array_list_push_back(&metrics_storage->metadata_entries, &entry)) {
296-
// goto metrics_storage_error;
297-
// }
298-
// }
299-
300-
// storage_view->metadata_entries = metrics_storage->metadata_entries.data;
301-
// storage_view->metadata_count = aws_array_list_length(&metrics_storage->metadata_entries);
302-
303-
if (metrics_options->library_name.len > 0) {
304-
metrics_storage->library_name = metrics_options->library_name;
305-
if (aws_byte_buf_append_and_update(&metrics_storage->storage, &metrics_storage->library_name)) {
306-
goto cleanup_storage;
307-
}
308-
storage_view->library_name = metrics_storage->library_name;
309-
}
310-
311-
return metrics_storage;
312-
313-
cleanup_storage:
314-
// TODO Future Work: add metadata entries once we implemented the metadata feature
315-
// if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) {
316-
// aws_array_list_clean_up(&metrics_storage->metadata_entries);
317-
// }
318-
319-
if (aws_byte_buf_is_valid(&metrics_storage->storage)) {
320-
aws_byte_buf_clean_up(&metrics_storage->storage);
321-
}
322-
if (metrics_options != NULL) {
323-
aws_mem_release(allocator, metrics_storage);
324-
}
325-
326-
return NULL;
327-
}
328-
329-
void aws_mqtt_iot_sdk_metrics_storage_destroy(struct aws_mqtt_iot_sdk_metrics_storage *metrics_storage) {
330-
if (metrics_storage == NULL) {
331-
return;
332-
}
333-
334-
if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) {
335-
aws_array_list_clean_up(&metrics_storage->metadata_entries);
336-
}
337-
aws_byte_buf_clean_up(&metrics_storage->storage);
338-
339-
aws_mem_release(metrics_storage->allocator, metrics_storage);
340-
}
341-
342-
int aws_mqtt_validate_iot_sdk_metrics_utf8(const struct aws_mqtt_iot_sdk_metrics *metrics) {
343-
if (metrics == NULL) {
344-
return AWS_OP_SUCCESS;
345-
}
346-
347-
if (aws_mqtt_validate_utf8_text(metrics->library_name)) {
348-
return AWS_OP_ERR;
349-
}
350-
351-
// TODO: add metadata entries when enabled
352-
// for (size_t i = 0; i < metrics->metadata_count; ++i) {
353-
// if (aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].key) ||
354-
// aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].value)) {
355-
// return AWS_OP_ERR;
356-
// }
357-
// }
358-
359-
return AWS_OP_SUCCESS;
360-
}

0 commit comments

Comments
 (0)