go-graphite / carbonapi

Implementation of graphite API (graphite-web) in golang
Other
309 stars 140 forks source link

[BUG] dot escaping error in template #765

Open ihard opened 1 year ago

ihard commented 1 year ago

Describe the bug When templating variables containing a dot (for example, domains), an error occurs when generating a request to the backend.

CarbonAPI Version curent master

Logs {"level":"DEBUG","timestamp":"2023-05-24T14:59:39.269+0300","logger":"zipper","message":"have errors","type":"victoriametrics","protocol":"victoriametrics","name":"http://127.0.0.1:8489","type":"fetch","request":"&MultiFetchRequest{Metrics:[]FetchRequest{FetchRequest{Name:seriesByTag('domain=~(example1\\.com|example2\\.com)', 'name=test_metric'),StartTime:1684926590,StopTime:1684929190,HighPrecisionTimestamps:false,PathExpression:seriesByTag('domain=~(example1\\.com|example2\\.com)', 'name=test_metric'),FilterFunctions:[]*FilteringFunction{},MaxDataPoints:1008,},},}","error":"{\"status\":\"error\",\"errorType\":\"422\",\"error\":\"error when executing query=\\\"test_metric{domain=~\\\\\\\"(example1\\\\\\\\.com|example2\\\\\\\\.com)\\\\\\\"}\\\" on the time range (start=1684926590000, end=1684929190000, step=15000): cannot expand WITH expressions: cannot parse string literal \\\"\\\\\\\"(example1\\\\\\\\.com|example2\\\\\\\\.com)\\\\\\\"\\\": invalid syntax\"}","errorVerbose":"{\"status\":\"error\",\"errorType\":\"422\",\"error\":\"error when executing query=\\\"test_metric{domain=~\\\\\\\"(example1\\\\\\\\.com|example2\\\\\\\\.com)\\\\\\\"}\\\" on the time range (start=1684926590000, end=1684929190000, step=15000): cannot expand WITH expressions: cannot parse string literal \\\"\\\\\\\"(example1\\\\\\\\.com|example2\\\\\\\\.com)\\\\\\\"\\\": invalid syntax\"}\nHTTP Code: 422\n\ngithub.com/go-graphite/carbonapi/zipper/types.init\n\t/go/src/github.com/go-graphite/carbonapi/zipper/types/errors.go:27\nruntime.doInit\n\t/usr/local/go/src/runtime/proc.go:6329\nruntime.doInit\n\t/usr/local/go/src/runtime/proc.go:6306\nruntime.doInit\n\t/usr/local/go/src/runtime/proc.go:6306\nruntime.doInit\n\t/usr/local/go/src/runtime/proc.go:6306\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:233\nruntime.goexit\n\t/usr/local/go/src/runtime/asm_amd64.s:1594"}

CarbonAPI Configuration:

listen: ":8021"

headersToPass:
  - "X-Dashboard-Id"
  - "X-Grafana-Org-Id"
  - "X-Panel-Id"
  - "X-Real-IP"
  - "X-Forwarded-For"
headersToLog:
  - "X-Dashboard-Id"
  - "X-Grafana-Org-Id"
  - "X-Panel-Id"

concurency: 5000

cache:
   type: "mem"
   size_mb: 0
   defaultTimeoutSec: 21600
   shortTimeoutSec: 60

backendCache:
   type: "mem"
   size_mb: 0
   defaultTimeoutSec: 21600
   shortTimeoutSec: 60

truncateTime:       # truncate from/until for identifical results between carbonapi instances. Also reduce load on long-range queries
  "2160h": "1h"     # Timestamp will be truncated to 1 hour round if (until - from) > 90 days
  "720h": "10m"     # Timestamp will be truncated to 10 minute round if (until - from) > 30 days
  "1h": "1m"        # Timestamp will be truncated to 1 minute round if (until - from) > 1 hour
  "0": "10s"

cpus: 4

upstreams:
    graphite09compat: false
    buckets: 10
    concurrencyLimitPerServer: 0
    keepAliveInterval: "10s"
    maxIdleConnsPerHost: 1000
    backendsv2:
        backends:
          -
            groupName: "graphite-clickhouse"
            protocol: "carbonapi_v3_pb"
            lbMethod: "all"
            maxTries: 3
            maxBatchSize: 0
            keepAliveInterval: "10s"
            concurrencyLimit: 0
            maxIdleConnsPerHost: 1000
            timeouts:
                find: "10s"
                render: "50s"
                connect: "500ms"
            servers:
                - "http://127.0.0.1:9093"
          -
            groupName: "vmselect"
            protocol: "victoriametrics"
            lbMethod: "all"
            maxTries: 3
            maxBatchSize: 0
            timeouts:
                find: "10s"
                render: "50s"
                connect: "500ms"
            backendOptions:
                fallback_version: "v1.90.0"
                max_points_per_query: 100000
                probe_version_interval: "600s"
                vmClusterTenantID: "0"
                tagMetricOnly: true
            servers:
                - "http://127.0.0.1:8489"

expireDelaySec: 600

logger:
    - logger: ""
      file: "stdout"
      level: "debug"
      encoding: "json"

Simplified query (if applicable) metrics

cat test_metric.json
{"metric":{"__name__":"test_metric", "domain":"example1.com"},"values":[1,1,1],"timestamps":[1684927550000,1684928550000,1684929550000]}
{"metric":{"__name__":"test_metric", "domain":"example2.com"},"values":[1,1,1],"timestamps":[1684927550000,1684928550000,1684929550000]}

curl --data-binary "@test_metric.json" -X POST http://localhost:8571/insert/0/prometheus/api/v1/import

**Grafana dashboard

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "target": {
          "limit": 100,
          "matchAny": false,
          "tags": [],
          "type": "dashboard"
        },
        "type": "dashboard"
      }
    ]
  },
  "description": "",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 6251,
  "links": [],
  "liveNow": false,
  "panels": [
    {
      "datasource": {
        "type": "graphite",
        "uid": "${ds}"
      },
      "description": "",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "graphite",
            "uid": "${ds}"
          },
          "refId": "A",
          "target": "seriesByTag('domain=~${domain:regex}', 'name=test_metric')"
        }
      ],
      "title": "Fail request with template",
      "type": "timeseries"
    },
    {
      "datasource": {
        "type": "graphite",
        "uid": "${ds}"
      },
      "description": "",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 0
      },
      "id": 3,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "graphite",
            "uid": "${ds}"
          },
          "refCount": 0,
          "refId": "A",
          "target": "seriesByTag('domain=example1.com', 'name=test_metric')"
        }
      ],
      "title": "Ok request",
      "type": "timeseries"
    }
  ],
  "refresh": false,
  "schemaVersion": 37,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "current": {
          "selected": false,
          "text": "Graphite-VM-Test",
          "value": "Graphite-VM-Test"
        },
        "hide": 0,
        "includeAll": false,
        "multi": false,
        "name": "ds",
        "options": [],
        "query": "graphite",
        "queryValue": "",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "type": "datasource"
      },
      {
        "current": {
          "selected": false,
          "text": [
            "All"
          ],
          "value": [
            "$__all"
          ]
        },
        "datasource": {
          "type": "graphite",
          "uid": "${ds}"
        },
        "definition": "",
        "hide": 0,
        "includeAll": true,
        "multi": true,
        "name": "domain",
        "options": [],
        "query": {
          "queryType": "Default",
          "refId": "A",
          "target": "tag_values(domain, name=test_metric)"
        },
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      }
    ]
  },
  "time": {
    "from": "2023-05-24T11:09:53.701Z",
    "to": "2023-05-24T11:53:09.665Z"
  },
  "timepicker": {},
  "timezone": "",
  "title": "Test metric template",
  "uid": "sEbh1vQ4k",
  "version": 3,
  "weekStart": ""
}