summaryrefslogtreecommitdiffstats
path: root/exporting/prometheus
diff options
context:
space:
mode:
authorVladimir Kobal <vlad@prokk.net>2020-11-05 19:08:17 +0200
committerGitHub <noreply@github.com>2020-11-05 19:08:17 +0200
commit943ee2482b16a81afd54b426f4fb0952f99c48e7 (patch)
tree5603b2bbd3cba974f665699a2b132256b2dfe912 /exporting/prometheus
parentedd6d02dec1c65593acb9d7b98dbb3f594d58c3a (diff)
Add HTTP and HTTPS support to the simple exporting connector (#9911)
Diffstat (limited to 'exporting/prometheus')
-rw-r--r--exporting/prometheus/remote_write/README.md3
-rw-r--r--exporting/prometheus/remote_write/remote_write.c69
-rw-r--r--exporting/prometheus/remote_write/remote_write.h6
-rw-r--r--exporting/prometheus/remote_write/remote_write_request.h4
4 files changed, 42 insertions, 40 deletions
diff --git a/exporting/prometheus/remote_write/README.md b/exporting/prometheus/remote_write/README.md
index b8dcb875c9..0e67e3459d 100644
--- a/exporting/prometheus/remote_write/README.md
+++ b/exporting/prometheus/remote_write/README.md
@@ -30,6 +30,9 @@ in the Netdata configuration directory and set the following options:
remote write URL path = /receive
```
+You can also add `:https` modifier to the connector type if you need to use the TLS/SSL protocol. For example:
+`remote_write:https:my_instance`.
+
`remote write URL path` is used to set an endpoint path for the remote write protocol. The default value is `/receive`.
For example, if your endpoint is `http://example.domain:example_port/storage/read`:
diff --git a/exporting/prometheus/remote_write/remote_write.c b/exporting/prometheus/remote_write/remote_write.c
index 6106146164..7f905c116f 100644
--- a/exporting/prometheus/remote_write/remote_write.c
+++ b/exporting/prometheus/remote_write/remote_write.c
@@ -10,28 +10,18 @@ char family[PROMETHEUS_ELEMENT_MAX + 1];
char units[PROMETHEUS_ELEMENT_MAX + 1] = "";
/**
- * Send header to a server
+ * Prepare HTTP header
*
- * @param sock a communication socket.
* @param instance an instance data structure.
- * @return Returns 0 on success, 1 on failure.
*/
-int prometheus_remote_write_send_header(int *sock, struct instance *instance)
+void prometheus_remote_write_prepare_header(struct instance *instance)
{
- int flags = 0;
-#ifdef MSG_NOSIGNAL
- flags += MSG_NOSIGNAL;
-#endif
-
struct prometheus_remote_write_specific_config *connector_specific_config =
instance->config.connector_specific_config;
-
- static BUFFER *header;
- if (!header)
- header = buffer_create(0);
+ struct simple_connector_data *simple_connector_data = instance->connector_specific_data;
buffer_sprintf(
- header,
+ simple_connector_data->last_buffer->header,
"POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
@@ -40,17 +30,7 @@ int prometheus_remote_write_send_header(int *sock, struct instance *instance)
"Content-Type: application/x-www-form-urlencoded\r\n\r\n",
connector_specific_config->remote_write_path,
instance->config.destination,
- buffer_strlen((BUFFER *)instance->buffer));
-
- size_t header_len = buffer_strlen(header);
- ssize_t written = send(*sock, buffer_tostring(header), header_len, flags);
-
- buffer_flush(header);
-
- if (written != -1 && (size_t)written == header_len)
- return 0;
- else
- return 1;
+ buffer_strlen(simple_connector_data->last_buffer->buffer));
}
/**
@@ -90,7 +70,8 @@ int process_prometheus_remote_write_response(BUFFER *buffer, struct instance *in
*/
void clean_prometheus_remote_write(struct instance *instance)
{
- freez(instance->connector_specific_data);
+ struct simple_connector_data *simple_connector_data = instance->connector_specific_data;
+ freez(simple_connector_data->connector_specific_data);
struct prometheus_remote_write_specific_config *connector_specific_config =
instance->config.connector_specific_config;
@@ -115,22 +96,32 @@ int init_prometheus_remote_write_instance(struct instance *instance)
instance->end_host_formatting = NULL;
instance->end_batch_formatting = format_batch_prometheus_remote_write;
- instance->send_header = prometheus_remote_write_send_header;
+ instance->prepare_header = prometheus_remote_write_prepare_header;
instance->check_response = process_prometheus_remote_write_response;
instance->buffer = (void *)buffer_create(0);
- if (!instance->buffer) {
- error("EXPORTING: cannot create buffer for AWS Kinesis exporting connector instance %s", instance->config.name);
- return 1;
- }
+
if (uv_mutex_init(&instance->mutex))
return 1;
if (uv_cond_init(&instance->cond_var))
return 1;
+ struct simple_connector_data *simple_connector_data = callocz(1, sizeof(struct simple_connector_data));
+ instance->connector_specific_data = simple_connector_data;
+
+#ifdef ENABLE_HTTPS
+ simple_connector_data->flags = NETDATA_SSL_START;
+ simple_connector_data->conn = NULL;
+ if (instance->config.options & EXPORTING_OPTION_USE_TLS) {
+ security_start_ssl(NETDATA_SSL_CONTEXT_EXPORTING);
+ }
+#endif
+
struct prometheus_remote_write_specific_data *connector_specific_data =
callocz(1, sizeof(struct prometheus_remote_write_specific_data));
- instance->connector_specific_data = (void *)connector_specific_data;
+ simple_connector_data->connector_specific_data = (void *)connector_specific_data;
+
+ simple_connector_init(instance);
connector_specific_data->write_request = init_write_request();
@@ -148,8 +139,10 @@ int init_prometheus_remote_write_instance(struct instance *instance)
*/
int format_host_prometheus_remote_write(struct instance *instance, RRDHOST *host)
{
+ struct simple_connector_data *simple_connector_data =
+ (struct simple_connector_data *)instance->connector_specific_data;
struct prometheus_remote_write_specific_data *connector_specific_data =
- (struct prometheus_remote_write_specific_data *)instance->connector_specific_data;
+ (struct prometheus_remote_write_specific_data *)simple_connector_data->connector_specific_data;
char hostname[PROMETHEUS_ELEMENT_MAX + 1];
prometheus_label_copy(
@@ -225,8 +218,10 @@ int format_chart_prometheus_remote_write(struct instance *instance, RRDSET *st)
*/
int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *rd)
{
+ struct simple_connector_data *simple_connector_data =
+ (struct simple_connector_data *)instance->connector_specific_data;
struct prometheus_remote_write_specific_data *connector_specific_data =
- (struct prometheus_remote_write_specific_data *)instance->connector_specific_data;
+ (struct prometheus_remote_write_specific_data *)simple_connector_data->connector_specific_data;
if (rd->collections_counter && !rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
char name[PROMETHEUS_LABELS_MAX + 1];
@@ -322,8 +317,10 @@ int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *
*/
int format_batch_prometheus_remote_write(struct instance *instance)
{
+ struct simple_connector_data *simple_connector_data =
+ (struct simple_connector_data *)instance->connector_specific_data;
struct prometheus_remote_write_specific_data *connector_specific_data =
- (struct prometheus_remote_write_specific_data *)instance->connector_specific_data;
+ (struct prometheus_remote_write_specific_data *)simple_connector_data->connector_specific_data;
size_t data_size = get_write_request_size(connector_specific_data->write_request);
@@ -342,5 +339,7 @@ int format_batch_prometheus_remote_write(struct instance *instance)
buffer->len = data_size;
instance->stats.buffered_bytes = (collected_number)buffer_strlen(buffer);
+ simple_connector_end_batch(instance);
+
return 0;
}
diff --git a/exporting/prometheus/remote_write/remote_write.h b/exporting/prometheus/remote_write/remote_write.h
index 3bd212414a..d738f5126f 100644
--- a/exporting/prometheus/remote_write/remote_write.h
+++ b/exporting/prometheus/remote_write/remote_write.h
@@ -7,6 +7,10 @@
#include "exporting/prometheus/prometheus.h"
#include "remote_write_request.h"
+struct prometheus_remote_write_specific_data {
+ void *write_request;
+};
+
int init_prometheus_remote_write_instance(struct instance *instance);
extern void clean_prometheus_remote_write(struct instance *instance);
@@ -15,7 +19,7 @@ int format_chart_prometheus_remote_write(struct instance *instance, RRDSET *st);
int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *rd);
int format_batch_prometheus_remote_write(struct instance *instance);
-int prometheus_remote_write_send_header(int *sock, struct instance *instance);
+void prometheus_remote_write_prepare_header(struct instance *instance);
int process_prometheus_remote_write_response(BUFFER *buffer, struct instance *instance);
#endif //NETDATA_EXPORTING_PROMETHEUS_REMOTE_WRITE_H
diff --git a/exporting/prometheus/remote_write/remote_write_request.h b/exporting/prometheus/remote_write/remote_write_request.h
index ea8f0f6795..e1dfacaf86 100644
--- a/exporting/prometheus/remote_write/remote_write_request.h
+++ b/exporting/prometheus/remote_write/remote_write_request.h
@@ -7,10 +7,6 @@
extern "C" {
#endif
-struct prometheus_remote_write_specific_data {
- void *write_request;
-};
-
void *init_write_request();
void add_host_info(