Dynatrace/dynatrace-configuration-as-code

Not allowed newline characters in markdown placeholder for dashboards

Closed this issue · 3 comments

163a commented

Describe the bug
I am getting this error with invalid newline characters on dashboard JSON when using the --dry-run feature.
Error: invalid character '\n' in string literal

How to reproduce
Steps to reproduce the behavior:

  • YAML file sample
config:
  - HealthCheck: HealthCheck.json

HealthCheck:
  - name: "\U0001F50E Health Check"
  - sharing_enabled: "true"
  - owner_email: "sample@example.com"

HealthCheck.non-prod:
  - markdown_text: "### Quick links\n\n[Some link](example.com)"
  • JSON file sample
 {
  "dashboardMetadata": {
    "name": "{{ .name}}",
    "shared": {{ .sharing_enabled }},
    "owner": "{{ .owner_email }}",
    "dashboardFilter": {
      "timeframe": "-2h"
    },
    "tags": [
      "Self-monitoring",
      "Health check",
      "Dynatrace"
    ]
  },
  "tiles": [
    {
      "name": "Markdown",
      "nameSize": "",
      "tileType": "MARKDOWN",
      "configured": true,
      "bounds": {
        "top": 0,
        "left": 0,
        "width": 2394,
        "height": 76
      },
      "tileFilter": {},
      "markdown": "{{ .markdown_text }}"
    },
    {
      "name": "ActiveGate Incoming Traffic (by network zone)",
      "nameSize": "",
      "tileType": "DATA_EXPLORER",
      "configured": true,
      "bounds": {
        "top": 76,
        "left": 0,
        "width": 532,
        "height": 380
      },
      "tileFilter": {},
      "customName": "Single value",
      "queries": [
        {
          "id": "A",
          "metric": "dsfm:active_gate.communication.incoming_traffic",
          "spaceAggregation": "SUM",
          "timeAggregation": "DEFAULT",
          "splitBy": [
            "dt.network_zone.id"
          ],
          "filterBy": {
            "nestedFilters": [],
            "criteria": []
          },
          "enabled": true
        }
      ],
      "visualConfig": {
        "type": "GRAPH_CHART",
        "global": {
          "hideLegend": false
        },
        "rules": [
          {
            "matcher": "A:",
            "properties": {
              "color": "DEFAULT",
              "seriesType": "LINE"
            },
            "seriesOverrides": []
          }
        ],
        "axes": {
          "xAxis": {
            "displayName": "",
            "visible": true
          },
          "yAxes": [
            {
              "displayName": "",
              "visible": true,
              "min": "AUTO",
              "max": "AUTO",
              "position": "LEFT",
              "queryIds": [
                "A"
              ],
              "defaultAxis": true
            }
          ]
        },
        "heatmapSettings": {},
        "thresholds": [
          {
            "axisTarget": "LEFT",
            "rules": [
              {
                "color": "#7dc540"
              },
              {
                "color": "#f5d30f"
              },
              {
                "color": "#dc172a"
              }
            ],
            "queryId": "",
            "visible": true
          }
        ],
        "tableSettings": {},
        "graphChartSettings": {
          "connectNulls": false
        },
        "honeycombSettings": {}
      }
    }
  ]
}

Expected behavior
Should be able to provide a dynamic placeholder for markdown text.

Log output

2022-04-12 16:31:58 INFO  Processing environment non-prod...
2022-04-12 16:31:58 INFO  	Processing project projects/self-monitoring...
2022-04-12 16:31:58 ERROR 			Failed file projects/self-monitoring/dashboard/HealthCheck.json is not a valid json: Error: invalid character '\n' in string literal
2022-04-12 16:31:58 ERROR 			Failed file projects/self-monitoring/dashboard/ResourceConsumption.json is not a valid json: Error: invalid character '\n' in string literal

Environment (please complete the following information):

  • OS: Ubuntu Linux
  • Tool version 1.17 NEW_CLI=1

Additional context
Add any other context about the problem here.

This issue is inherent to Go/JSON string literals.

A \n would need to be escaped as \\n - which is inconvenient and (open question) unclear how the Dynatrace API would behave on.

Another option could be to load things as raw strings (see https://stackoverflow.com/a/38979641).

This shouldn't be too hard to fix (thus good first issue label), but needs some open questions answered first:

  • would Dynatrace accept escaped newlines (\n) and handle them normally
  • what does the API return here, if \n is returned this should be possible with monaco
  • can we simply switch to reading this as raw strings, what would the impact be

Hi
As a workaround, I use several variables in the yaml, one for each line in the markdown. e.g. (with links to other dashboards):
- link_name_1: "dashboard/wasJvmMemoryUsageIntNz.name"
- link_id_1: "dashboard/wasJvmMemoryUsageIntNz.id"
- link_name_2: "dashboard/wasJvmMemoryUsageIntEz.name"
- link_id_2: "dashboard/wasJvmMemoryUsageIntEz.id"
And in the dashboard.json:
"markdown": "## Dashboard-Links:\n* ### [{{ .link_name_1 }}](#dashboard;id={{ .link_id_1 }};gtf=-2h;gf=all)\n* ### [{{ .link_name_2 }}](#dashboard;id={{ .link_id_2 }};gtf=-2h;gf=all)\n",

But it would be nice to have the possibility to write the whole markdown string in a yaml variable.

163a commented

I believe this comes down more to YAML syntax versus JSON.
On the API, it happily accepts \n as a newline in markdown.

Just, when moving that to a placeholder (I use overrides for .non-prod and .production), you are now inside YAML file so it requires the \\n as @UnseenWizzard stated.

So, as @mdicss alluded to, my use case was to generate placeholders with the correct dashboard link ID as required for that particular Dynatrace environment.

Therefore, this should work:

HealthCheck.non-prod:
  - markdown_text: "### Quick links\\n[Some link](example.com)"

It has the unfortunate effect that you need to go through all markdowns thoroughly to convert the newline characters, if you wish to turn them into a placeholder.