remote_write_request.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <snappy.h>
  3. #include "remote_write.pb.h"
  4. #include "remote_write_request.h"
  5. using namespace prometheus;
  6. google::protobuf::Arena arena;
  7. /**
  8. * Initialize a write request
  9. *
  10. * @return Returns a new write request
  11. */
  12. void *init_write_request()
  13. {
  14. GOOGLE_PROTOBUF_VERIFY_VERSION;
  15. WriteRequest *write_request = google::protobuf::Arena::CreateMessage<WriteRequest>(&arena);
  16. return (void *)write_request;
  17. }
  18. /**
  19. * Adds information about a host to a write request
  20. *
  21. * @param write_request_p the write request
  22. * @param name the name of a metric which is used for providing the host information
  23. * @param instance the name of the host itself
  24. * @param application the name of a program which sends the information
  25. * @param version the version of the program
  26. * @param timestamp the timestamp for the metric in milliseconds
  27. */
  28. void add_host_info(
  29. void *write_request_p,
  30. const char *name, const char *instance, const char *application, const char *version, const int64_t timestamp)
  31. {
  32. WriteRequest *write_request = (WriteRequest *)write_request_p;
  33. TimeSeries *timeseries;
  34. Sample *sample;
  35. Label *label;
  36. timeseries = write_request->add_timeseries();
  37. label = timeseries->add_labels();
  38. label->set_name("__name__");
  39. label->set_value(name);
  40. label = timeseries->add_labels();
  41. label->set_name("instance");
  42. label->set_value(instance);
  43. if (application) {
  44. label = timeseries->add_labels();
  45. label->set_name("application");
  46. label->set_value(application);
  47. }
  48. if (version) {
  49. label = timeseries->add_labels();
  50. label->set_name("version");
  51. label->set_value(version);
  52. }
  53. sample = timeseries->add_samples();
  54. sample->set_value(1);
  55. sample->set_timestamp(timestamp);
  56. }
  57. /**
  58. * Adds a label to the last created timeseries
  59. *
  60. * @param write_request_p the write request with the timeseries
  61. * @param key the key of the label
  62. * @param value the value of the label
  63. */
  64. void add_label(void *write_request_p, char *key, char *value)
  65. {
  66. WriteRequest *write_request = (WriteRequest *)write_request_p;
  67. TimeSeries *timeseries;
  68. Label *label;
  69. timeseries = write_request->mutable_timeseries(write_request->timeseries_size() - 1);
  70. label = timeseries->add_labels();
  71. label->set_name(key);
  72. label->set_value(value);
  73. }
  74. /**
  75. * Adds a metric to a write request
  76. *
  77. * @param write_request_p the write request
  78. * @param name the name of the metric
  79. * @param chart the chart, the metric belongs to
  80. * @param family the family, the metric belongs to
  81. * @param dimension the dimension, the metric belongs to
  82. * @param instance the name of the host, the metric belongs to
  83. * @param value the value of the metric
  84. * @param timestamp the timestamp for the metric in milliseconds
  85. */
  86. void add_metric(
  87. void *write_request_p,
  88. const char *name, const char *chart, const char *family, const char *dimension, const char *instance,
  89. const double value, const int64_t timestamp)
  90. {
  91. WriteRequest *write_request = (WriteRequest *)write_request_p;
  92. TimeSeries *timeseries;
  93. Sample *sample;
  94. Label *label;
  95. timeseries = write_request->add_timeseries();
  96. label = timeseries->add_labels();
  97. label->set_name("__name__");
  98. label->set_value(name);
  99. label = timeseries->add_labels();
  100. label->set_name("chart");
  101. label->set_value(chart);
  102. label = timeseries->add_labels();
  103. label->set_name("family");
  104. label->set_value(family);
  105. if (dimension) {
  106. label = timeseries->add_labels();
  107. label->set_name("dimension");
  108. label->set_value(dimension);
  109. }
  110. label = timeseries->add_labels();
  111. label->set_name("instance");
  112. label->set_value(instance);
  113. sample = timeseries->add_samples();
  114. sample->set_value(value);
  115. sample->set_timestamp(timestamp);
  116. }
  117. /**
  118. * Gets the size of a write request
  119. *
  120. * @param write_request_p the write request
  121. * @return Returns the size of the write request
  122. */
  123. size_t get_write_request_size(void *write_request_p)
  124. {
  125. WriteRequest *write_request = (WriteRequest *)write_request_p;
  126. #if GOOGLE_PROTOBUF_VERSION < 3001000
  127. size_t size = (size_t)snappy::MaxCompressedLength(write_request->ByteSize());
  128. #else
  129. size_t size = (size_t)snappy::MaxCompressedLength(write_request->ByteSizeLong());
  130. #endif
  131. return (size < INT_MAX) ? size : 0;
  132. }
  133. /**
  134. * Packs a write request into a buffer and clears the request
  135. *
  136. * @param write_request_p the write request
  137. * @param buffer a buffer, where compressed data is written
  138. * @param size gets the size of the write request, returns the size of the compressed data
  139. * @return Returns 0 on success, 1 on failure
  140. */
  141. int pack_and_clear_write_request(void *write_request_p, char *buffer, size_t *size)
  142. {
  143. WriteRequest *write_request = (WriteRequest *)write_request_p;
  144. std::string uncompressed_write_request;
  145. if (write_request->SerializeToString(&uncompressed_write_request) == false)
  146. return 1;
  147. write_request->clear_timeseries();
  148. snappy::RawCompress(uncompressed_write_request.data(), uncompressed_write_request.size(), buffer, size);
  149. return 0;
  150. }
  151. /**
  152. * Writes an unpacked write request into a text buffer
  153. *
  154. * @param write_request_p the write request
  155. * @param buffer a buffer, where text is written
  156. * @param size the size of the buffer
  157. * @return Returns 0 on success, 1 on failure
  158. */
  159. int convert_write_request_to_string(
  160. const char *compressed_write_request,
  161. size_t compressed_size,
  162. char *buffer,
  163. size_t size)
  164. {
  165. size_t uncompressed_size = 0;
  166. snappy::GetUncompressedLength(compressed_write_request, compressed_size, &uncompressed_size);
  167. if (size < uncompressed_size)
  168. return 1;
  169. char *uncompressed_write_request = (char *)malloc(size);
  170. if (snappy::RawUncompress(compressed_write_request, compressed_size, uncompressed_write_request) == false) {
  171. free(uncompressed_write_request);
  172. return 1;
  173. }
  174. WriteRequest *write_request = google::protobuf::Arena::CreateMessage<WriteRequest>(&arena);
  175. if (write_request->ParseFromString(std::string(uncompressed_write_request, uncompressed_size)) == false) {
  176. free(uncompressed_write_request);
  177. return 1;
  178. }
  179. std::string text_write_request(write_request->DebugString());
  180. text_write_request.copy(buffer, size);
  181. free(uncompressed_write_request);
  182. return 0;
  183. }
  184. /**
  185. * Shuts down the Protobuf library
  186. */
  187. void protocol_buffers_shutdown()
  188. {
  189. google::protobuf::ShutdownProtobufLibrary();
  190. }