diff options
author | Vladimir Kobal <vlad@prokk.net> | 2022-01-10 15:19:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-10 15:19:55 +0200 |
commit | fce052ee7986d7c2c562093f6c44728dcb077d33 (patch) | |
tree | 40bb4a2c5b642431d40d4ca73c8a35ad936a3698 /exporting/prometheus | |
parent | 3ba9dc6cf0905115f8e5046bd4e50bb866c15ded (diff) |
Fix unit tests for Prometheus remote write exporting connector (#11883)
Diffstat (limited to 'exporting/prometheus')
-rw-r--r-- | exporting/prometheus/remote_write/remote_write_request.cc | 40 | ||||
-rw-r--r-- | exporting/prometheus/remote_write/remote_write_request.h | 6 |
2 files changed, 46 insertions, 0 deletions
diff --git a/exporting/prometheus/remote_write/remote_write_request.cc b/exporting/prometheus/remote_write/remote_write_request.cc index 48d19efd62..cfd61271e6 100644 --- a/exporting/prometheus/remote_write/remote_write_request.cc +++ b/exporting/prometheus/remote_write/remote_write_request.cc @@ -178,6 +178,46 @@ int pack_and_clear_write_request(void *write_request_p, char *buffer, size_t *si } /** + * Writes an unpacked write request into a text buffer + * + * @param write_request_p the write request + * @param buffer a buffer, where text is written + * @param size the size of the buffer + * @return Returns 0 on success, 1 on failure + */ +int convert_write_request_to_string( + const char *compressed_write_request, + size_t compressed_size, + char *buffer, + size_t size) +{ + size_t uncompressed_size = 0; + + snappy::GetUncompressedLength(compressed_write_request, compressed_size, &uncompressed_size); + if (size < uncompressed_size) + return 1; + char *uncompressed_write_request = (char *)malloc(size); + + if (snappy::RawUncompress(compressed_write_request, compressed_size, uncompressed_write_request) == false) { + free(uncompressed_write_request); + return 1; + } + + WriteRequest *write_request = google::protobuf::Arena::CreateMessage<WriteRequest>(&arena); + if (write_request->ParseFromString(std::string(uncompressed_write_request, uncompressed_size)) == false) { + free(uncompressed_write_request); + return 1; + } + + std::string text_write_request(write_request->DebugString()); + text_write_request.copy(buffer, size); + + free(uncompressed_write_request); + + return 0; +} + +/** * Shuts down the Protobuf library */ void protocol_buffers_shutdown() diff --git a/exporting/prometheus/remote_write/remote_write_request.h b/exporting/prometheus/remote_write/remote_write_request.h index e1dfacaf86..5f242b9418 100644 --- a/exporting/prometheus/remote_write/remote_write_request.h +++ b/exporting/prometheus/remote_write/remote_write_request.h @@ -24,6 +24,12 @@ size_t get_write_request_size(void *write_request_p); int pack_and_clear_write_request(void *write_request_p, char *buffer, size_t *size); +int convert_write_request_to_string( + const char *compressed_write_request, + size_t compressed_size, + char *buffer, + size_t size); + void protocol_buffers_shutdown(); #ifdef __cplusplus |