From f1a28f5137b4c76d9b5405c2a12f7164895f7efe Mon Sep 17 00:00:00 2001
From: "Austin S. Hemmelgarn" <austin@netdata.cloud>
Date: Wed, 2 Aug 2023 08:32:17 -0400
Subject: [PATCH] Fix handling of troubleshooting section in integrations.
 (#15700)

* Fix handling of troubleshooting section in integrations.

* Fix plugin_name key path.
---
 integrations/gen_integrations.py          | 41 ++++++++++++++---------
 integrations/schemas/collector.json       | 38 +--------------------
 integrations/schemas/exporter.json        |  3 ++
 integrations/schemas/notification.json    |  3 ++
 integrations/schemas/shared.json          | 39 +++++++++++++++++++++
 integrations/templates/troubleshooting.md | 37 ++++++++++++++++----
 6 files changed, 102 insertions(+), 59 deletions(-)

diff --git a/integrations/gen_integrations.py b/integrations/gen_integrations.py
index 0246561ceb..19d71d8cca 100755
--- a/integrations/gen_integrations.py
+++ b/integrations/gen_integrations.py
@@ -56,11 +56,13 @@ COLLECTOR_RENDER_KEYS = [
 EXPORTER_RENDER_KEYS = [
     'overview',
     'setup',
+    'troubleshooting',
 ]
 
 NOTIFICATION_RENDER_KEYS = [
     'overview',
     'setup',
+    'troubleshooting',
 ]
 
 GITHUB_ACTIONS = os.environ.get('GITHUB_ACTIONS', False)
@@ -473,12 +475,15 @@ def render_collectors(categories, collectors, ids):
                 }
 
         for key in COLLECTOR_RENDER_KEYS:
-            template = get_jinja_env().get_template(f'{ key }.md')
-            data = template.render(entry=item, related=related)
+            if key in item.keys():
+                template = get_jinja_env().get_template(f'{ key }.md')
+                data = template.render(entry=item, related=related)
 
-            if 'variables' in item['meta']['monitored_instance']:
-                template = get_jinja_env().from_string(data)
-                data = template.render(variables=item['meta']['monitored_instance']['variables'])
+                if 'variables' in item['meta']['monitored_instance']:
+                    template = get_jinja_env().from_string(data)
+                    data = template.render(variables=item['meta']['monitored_instance']['variables'])
+            else:
+                data = ''
 
             item[key] = data
 
@@ -540,12 +545,15 @@ def render_exporters(categories, exporters, ids):
 
     for item in exporters:
         for key in EXPORTER_RENDER_KEYS:
-            template = get_jinja_env().get_template(f'{ key }.md')
-            data = template.render(entry=item)
+            if key in item.keys():
+                template = get_jinja_env().get_template(f'{ key }.md')
+                data = template.render(entry=item)
 
-            if 'variables' in item['meta']:
-                template = get_jinja_env().from_string(data)
-                data = template.render(variables=item['meta']['variables'])
+                if 'variables' in item['meta']:
+                    template = get_jinja_env().from_string(data)
+                    data = template.render(variables=item['meta']['variables'])
+            else:
+                data = ''
 
             item[key] = data
 
@@ -569,12 +577,15 @@ def render_notifications(categories, notifications, ids):
 
     for item in notifications:
         for key in NOTIFICATION_RENDER_KEYS:
-            template = get_jinja_env().get_template(f'{ key }.md')
-            data = template.render(entry=item)
+            if key in item.keys():
+                template = get_jinja_env().get_template(f'{ key }.md')
+                data = template.render(entry=item)
 
-            if 'variables' in item['meta']:
-                template = get_jinja_env().from_string(data)
-                data = template.render(variables=item['meta']['variables'])
+                if 'variables' in item['meta']:
+                    template = get_jinja_env().from_string(data)
+                    data = template.render(variables=item['meta']['variables'])
+            else:
+                data = ''
 
             item[key] = data
 
diff --git a/integrations/schemas/collector.json b/integrations/schemas/collector.json
index dedbf8dd49..ba5d9f9d19 100644
--- a/integrations/schemas/collector.json
+++ b/integrations/schemas/collector.json
@@ -222,43 +222,7 @@
             "$ref": "./shared.json#/$defs/full_setup"
           },
           "troubleshooting": {
-            "type": "object",
-            "description": "Information needed to troubleshoot issues with this collector.",
-            "properties": {
-              "problems": {
-                "type": "object",
-                "description": "Common problems that users face again and again... and their solutions.",
-                "properties": {
-                  "list": {
-                    "type": "array",
-                    "description": "List of common problems.",
-                    "items": {
-                      "type": "object",
-                      "properties": {
-                        "name": {
-                          "type": "string",
-                          "description": "Problem name."
-                        },
-                        "description": {
-                          "type": "string",
-                          "description": "Explanation of the problem and its solution."
-                        }
-                      }
-                    },
-                    "required": [
-                      "name",
-                      "description"
-                    ]
-                  }
-                },
-                "required": [
-                  "list"
-                ]
-              }
-            },
-            "required": [
-              "problems"
-            ]
+            "$ref": "./shared.json#/$defs/troubleshooting"
           },
           "alerts": {
             "type": "array",
diff --git a/integrations/schemas/exporter.json b/integrations/schemas/exporter.json
index 00d8986f29..51c45cabba 100644
--- a/integrations/schemas/exporter.json
+++ b/integrations/schemas/exporter.json
@@ -46,6 +46,9 @@
         },
         "setup": {
           "$ref": "./shared.json#/$defs/full_setup"
+        },
+        "troubleshooting": {
+          "$ref": "./shared.json#/$defs/troubleshooting"
         }
       },
       "required": [
diff --git a/integrations/schemas/notification.json b/integrations/schemas/notification.json
index 1027f9b3d9..2596ca441e 100644
--- a/integrations/schemas/notification.json
+++ b/integrations/schemas/notification.json
@@ -70,6 +70,9 @@
               "$ref": "./shared.json#/$defs/full_setup"
             }
           ]
+        },
+        "troubleshooting": {
+          "$ref": "./shared.json#/$defs/troubleshooting"
         }
       },
       "required": [
diff --git a/integrations/schemas/shared.json b/integrations/schemas/shared.json
index a30d721697..5aa926c3f3 100644
--- a/integrations/schemas/shared.json
+++ b/integrations/schemas/shared.json
@@ -249,6 +249,45 @@
         "configuration"
       ]
     },
+    "troubleshooting": {
+      "type": "object",
+      "description": "Information needed to troubleshoot issues with this collector.",
+      "properties": {
+        "problems": {
+          "type": "object",
+          "description": "Common problems that users face again and again... and their solutions.",
+          "properties": {
+            "list": {
+              "type": "array",
+              "description": "List of common problems.",
+              "items": {
+                "type": "object",
+                "properties": {
+                  "name": {
+                    "type": "string",
+                    "description": "Problem name."
+                  },
+                  "description": {
+                    "type": "string",
+                    "description": "Explanation of the problem and its solution."
+                  }
+                }
+              },
+              "required": [
+                "name",
+                "description"
+              ]
+            }
+          },
+          "required": [
+            "list"
+          ]
+        }
+      },
+      "required": [
+        "problems"
+      ]
+    },
     "_folding": {
       "type": "object",
       "description": "Content folding settings.",
diff --git a/integrations/templates/troubleshooting.md b/integrations/templates/troubleshooting.md
index 56306fa014..2f418dfb9e 100644
--- a/integrations/templates/troubleshooting.md
+++ b/integrations/templates/troubleshooting.md
@@ -1,8 +1,7 @@
-[% if entry.troubleshooting.list or entry.integration_type == 'collector' or entry.integration_type == 'notification' %]
+[% if entry.integration_type == 'collector' %]
+[% if entry.meta.plugin_name == 'go.d.plugin' %]
 ## Troubleshooting
 
-[% if entry.integration_type == 'collector' %]
-[% if entry.plugin_name == 'go.d.plugin' %]
 ### Debug Mode
 
 To troubleshoot issues with the `[[ entry.module_name ]]` collector, run the `go.d.plugin` with the debug option enabled. The output
@@ -26,7 +25,10 @@ should give you clues as to why the collector isn't working.
   ```bash
   ./go.d.plugin -d -m [[ entry.module_name ]]
   ```
-[% elif entry.plugin_name == 'python.d.plugin' %]
+
+[% elif entry.meta.plugin_name == 'python.d.plugin' %]
+## Troubleshooting
+
 ### Debug Mode
 
 To troubleshoot issues with the `[[ entry.module_name ]]` collector, run the `python.d.plugin` with the debug option enabled. The output
@@ -50,7 +52,10 @@ should give you clues as to why the collector isn't working.
   ```bash
   ./python.d.plugin [[ entry.module_name ]] debug trace
   ```
-[% elif entry.plugin_name == 'charts.d.plugin' %]
+
+[% elif entry.meta.plugin_name == 'charts.d.plugin' %]
+## Troubleshooting
+
 ### Debug Mode
 
 To troubleshoot issues with the `[[ entry.module_name ]]` collector, run the `charts.d.plugin` with the debug option enabled. The output
@@ -74,9 +79,22 @@ should give you clues as to why the collector isn't working.
   ```bash
   ./charts.d.plugin debug 1 [[ entry.module_name ]]
   ```
+
+[% else %]
+[% if entry.troubleshooting.problems.list %]
+## Troubleshooting
+
+[% endif %]
 [% endif %]
 [% elif entry.integration_type == 'notification' %]
-[% if not 'cloud-notifications' in entry._src_path %]
+[% if 'cloud-notifications' in entry._src_path %]
+[% if entry.troubleshooting.problems.list %]
+## Troubleshooting
+
+[% endif %]
+[% else %]
+## Troubleshooting
+
 ### Test Notification
 
 You can run the following command by hand, to test alerts configuration:
@@ -96,9 +114,14 @@ export NETDATA_ALARM_NOTIFY_DEBUG=1
 ```
 
 Note that this will test _all_ alert mechanisms for the selected role.
+
+[% elif entry.integration_type == 'exporter' %]
+[% if entry.troubleshooting.problems.list %]
+## Troubleshooting
+
 [% endif %]
 [% endif %]
-[% for item in entry.troubleshooting.list %]
+[% for item in entry.troubleshooting.problems.list %]
 ### [[ item.name ]]
 
 [[ description ]]