remote_write_request.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if (application) {
  41. label = timeseries->add_labels();
  42. label->set_name("application");
  43. label->set_value(application);
  44. }
  45. label = timeseries->add_labels();
  46. label->set_name("instance");
  47. label->set_value(instance);
  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. if (dimension) {
  103. label = timeseries->add_labels();
  104. label->set_name("dimension");
  105. label->set_value(dimension);
  106. }
  107. label = timeseries->add_labels();
  108. label->set_name("family");
  109. label->set_value(family);
  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. * Adds a metric to a write request
  119. *
  120. * @param write_request_p the write request
  121. * @param name the name of the metric
  122. * @param instance the name of the host, the metric belongs to
  123. * @param value the value of the metric
  124. * @param timestamp the timestamp for the metric in milliseconds
  125. */
  126. void add_variable(
  127. void *write_request_p, const char *name, const char *instance, const double value, const int64_t timestamp)
  128. {
  129. WriteRequest *write_request = (WriteRequest *)write_request_p;
  130. TimeSeries *timeseries;
  131. Sample *sample;
  132. Label *label;
  133. timeseries = write_request->add_timeseries();
  134. label = timeseries->add_labels();
  135. label->set_name("__name__");
  136. label->set_value(name);
  137. label = timeseries->add_labels();
  138. label->set_name("instance");
  139. label->set_value(instance);
  140. sample = timeseries->add_samples();
  141. sample->set_value(value);
  142. sample->set_timestamp(timestamp);
  143. }
  144. /**
  145. * Gets the size of a write request
  146. *
  147. * @param write_request_p the write request
  148. * @return Returns the size of the write request
  149. */
  150. size_t get_write_request_size(void *write_request_p)
  151. {
  152. WriteRequest *write_request = (WriteRequest *)write_request_p;
  153. #if GOOGLE_PROTOBUF_VERSION < 3001000
  154. size_t size = (size_t)snappy::MaxCompressedLength(write_request->ByteSize());
  155. #else
  156. size_t size = (size_t)snappy::MaxCompressedLength(write_request->ByteSizeLong());
  157. #endif
  158. return (size < INT_MAX) ? size : 0;
  159. }
  160. /**
  161. * Packs a write request into a buffer and clears the request
  162. *
  163. * @param write_request_p the write request
  164. * @param buffer a buffer, where compressed data is written
  165. * @param size gets the size of the write request, returns the size of the compressed data
  166. * @return Returns 0 on success, 1 on failure
  167. */
  168. int pack_and_clear_write_request(void *write_request_p, char *buffer, size_t *size)
  169. {
  170. WriteRequest *write_request = (WriteRequest *)write_request_p;
  171. std::string uncompressed_write_request;
  172. if (write_request->SerializeToString(&uncompressed_write_request) == false)
  173. return 1;
  174. write_request->clear_timeseries();
  175. snappy::RawCompress(uncompressed_write_request.data(), uncompressed_write_request.size(), buffer, size);
  176. return 0;
  177. }
  178. /**
  179. * Writes an unpacked write request into a text buffer
  180. *
  181. * @param write_request_p the write request
  182. * @param buffer a buffer, where text is written
  183. * @param size the size of the buffer
  184. * @return Returns 0 on success, 1 on failure
  185. */
  186. int convert_write_request_to_string(
  187. const char *compressed_write_request,
  188. size_t compressed_size,
  189. char *buffer,
  190. size_t size)
  191. {
  192. size_t uncompressed_size = 0;
  193. snappy::GetUncompressedLength(compressed_write_request, compressed_size, &uncompressed_size);
  194. if (size < uncompressed_size)
  195. return 1;
  196. char *uncompressed_write_request = (char *)malloc(size);
  197. if (snappy::RawUncompress(compressed_write_request, compressed_size, uncompressed_write_request) == false) {
  198. free(uncompressed_write_request);
  199. return 1;
  200. }
  201. WriteRequest *write_request = google::protobuf::Arena::CreateMessage<WriteRequest>(&arena);
  202. if (write_request->ParseFromString(std::string(uncompressed_write_request, uncompressed_size)) == false) {
  203. free(uncompressed_write_request);
  204. return 1;
  205. }
  206. std::string text_write_request(write_request->DebugString());
  207. text_write_request.copy(buffer, size);
  208. free(uncompressed_write_request);
  209. return 0;
  210. }
  211. /**
  212. * Shuts down the Protobuf library
  213. */
  214. void protocol_buffers_shutdown()
  215. {
  216. google::protobuf::ShutdownProtobufLibrary();
  217. }