]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
open_telemetry: Unify the configuration handling between exporter and receiver.
authorFlorian Forster <octo@collectd.org>
Sun, 4 Feb 2024 07:46:23 +0000 (08:46 +0100)
committerFlorian Forster <octo@collectd.org>
Tue, 20 Feb 2024 14:28:50 +0000 (15:28 +0100)
src/open_telemetry_exporter.cc
src/open_telemetry_receiver.cc

index 564f9949c387a379f5c476ded607b1528301eeee..795bbd7ca5ee8e15d60fa7969c5c65d6ca6abe8c 100644 (file)
@@ -187,7 +187,7 @@ static int ot_write(metric_family_t const *fam, user_data_t *user_data) {
   return 0;
 }
 
-static int config_get_file(oconfig_item_t const *ci, grpc::string *out) {
+int config_get_file(oconfig_item_t const *ci, grpc::string *out) {
   char *path = NULL;
   int err = cf_util_get_string(ci, &path);
   if (err) {
@@ -223,7 +223,7 @@ int exporter_config(oconfig_item_t *ci) {
   ot_callback_t *cb = (ot_callback_t *)calloc(1, sizeof(*cb));
   if (cb == NULL) {
     ERROR("open_telemetry plugin: calloc failed.");
-    return -1;
+    return ENOMEM;
   }
 
   cb->reference_count = 1;
index 56c644d56ebd63677a4c8b0d0c7717aa9782b8a0..921f26e0ac679b1e5c7c9913153aee355ab62c09 100644 (file)
@@ -81,27 +81,6 @@ struct Listener {
 };
 static std::vector<Listener> listeners;
 
-/*
- * helper functions
- */
-static grpc::string read_file(const char *filename) {
-  std::ifstream f;
-  grpc::string s, content;
-
-  f.open(filename);
-  if (!f.is_open()) {
-    ERROR("open_telemetry plugin: Failed to open '%s'", filename);
-    return "";
-  }
-
-  while (std::getline(f, s)) {
-    content += s;
-    content.push_back('\n');
-  }
-  f.close();
-  return content;
-} /* read_file */
-
 /*
  * proto conversion
  */
@@ -448,6 +427,9 @@ static void receiver_install_callbacks(void) {
   done = true;
 }
 
+// config_get_file is implemented in src/open_telemetry_exporter.cc
+int config_get_file(oconfig_item_t const *ci, grpc::string *out);
+
 /*
  * collectd plugin interface
  */
@@ -478,47 +460,33 @@ int receiver_config(oconfig_item_t *ci) {
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
 
+    int err = 0;
     if (!strcasecmp("EnableSSL", child->key)) {
-      if (cf_util_get_boolean(child, &use_ssl)) {
-        ERROR("open_telemetry plugin: Option `%s` expects a boolean value",
-              child->key);
-        return -1;
-      }
+      err = cf_util_get_boolean(child, &use_ssl);
     } else if (!strcasecmp("SSLCACertificateFile", child->key)) {
-      char *certs = NULL;
-      if (cf_util_get_string(child, &certs)) {
-        ERROR("open_telemetry plugin: Option `%s` expects a string value",
-              child->key);
-        return -1;
-      }
-      ssl_opts->pem_root_certs = read_file(certs);
+      err = config_get_file(child, &ssl_opts->pem_root_certs);
     } else if (!strcasecmp("SSLCertificateKeyFile", child->key)) {
-      char *key = NULL;
-      if (cf_util_get_string(child, &key)) {
-        ERROR("open_telemetry plugin: Option `%s` expects a string value",
-              child->key);
-        return -1;
-      }
-      pkcp.private_key = read_file(key);
+      err = config_get_file(child, &pkcp.private_key);
     } else if (!strcasecmp("SSLCertificateFile", child->key)) {
-      char *cert = NULL;
-      if (cf_util_get_string(child, &cert)) {
-        ERROR("open_telemetry plugin: Option `%s` expects a string value",
-              child->key);
-        return -1;
-      }
-      pkcp.cert_chain = read_file(cert);
+      err = config_get_file(child, &pkcp.cert_chain);
     } else if (!strcasecmp("VerifyPeer", child->key)) {
       bool verify = false;
-      if (cf_util_get_boolean(child, &verify)) {
-        return -1;
+      err = cf_util_get_boolean(child, &verify);
+      if (!err) {
+        ssl_opts->client_certificate_request =
+            verify ? GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
+                   : GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE;
       }
-      ssl_opts->client_certificate_request =
-          verify ? GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
-                 : GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE;
     } else {
-      WARNING("open_telemetry plugin: Option `%s` not allowed in <%s> block.",
-              child->key, ci->key);
+      ERROR("open_telemetry plugin: Option \"%s\" is not allowed inside a "
+            "\"%s\" block.",
+            child->key, ci->key);
+      err = EINVAL;
+    }
+
+    if (err) {
+      delete ssl_opts;
+      return err;
     }
   }