Skip to content

Instantly share code, notes, and snippets.

@kylebrandt
Created September 16, 2025 15:21
Show Gist options
  • Save kylebrandt/16b044a84601316eece70f1d6e9ccc95 to your computer and use it in GitHub Desktop.
Save kylebrandt/16b044a84601316eece70f1d6e9ccc95 to your computer and use it in GitHub Desktop.
sqlSchema endpoint example
### Req
POST http://admin:admin@localhost:3000/apis/query.grafana.app/v0alpha1/namespaces/default/sqlschema?ds_type=__expr__&expression=true
content-type: application/json
{
"queries": [
{
"datasource": {
"uid": "9c3MNrV4k",
"type": "grafana-mock-datasource"
},
"frames": [
{
"count": 1,
"disabled": false,
"fields": [
{
"disabled": false,
"labels": "host=a",
"name": "cpu",
"type": "nullable-float64",
"value": "3.14"
},
{
"disabled": false,
"labels": "host=b",
"name": "cpu",
"type": "nullable-float64",
"value": "93.14"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "3",
"labels": "host=x"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "2",
"labels": "host=y"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "4",
"labels": "host=z"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "1",
"labels": "host=u"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "2",
"labels": "host=v"
},
{
"name": "cpu",
"type": "float64",
"disabled": false,
"value": "3",
"labels": "host=w"
}
],
"frameInputType": "numeric-wide",
"meta": {
"type": "numeric-wide",
"typeVersion": [
0,
1
]
},
"name": "New Frame",
"source": "count"
}
],
"generatorSource": "frontend",
"hide": true,
"queryType": "frames_builder",
"rawFrameSource": "staticScenario",
"refId": "cat",
"resultState": "success",
"scenario": "no-data",
"datasourceId": 49,
"intervalMs": 20000,
"maxDataPoints": 1003
},
{
"datasource": {
"uid": "9c3MNrV4k",
"type": "grafana-mock-datasource"
},
"frames": [
{
"count": 1,
"disabled": false,
"fields": [
{
"disabled": false,
"labels": "host=a",
"name": "cpu",
"type": "nullable-float64",
"value": "3.14"
},
{
"disabled": false,
"labels": "host=c",
"name": "cpu",
"type": "float64",
"value": "93.14"
}
],
"frameInputType": "numeric-wide",
"meta": {
"type": "numeric-wide",
"typeVersion": [
0,
1
]
},
"name": "New Frame",
"source": "count"
}
],
"generatorSource": "frontend",
"hide": true,
"queryType": "frames_builder",
"rawFrameSource": "staticScenario",
"refId": "fox",
"resultState": "success",
"scenario": "no-data",
"datasourceId": 49,
"intervalMs": 20000,
"maxDataPoints": 1003
},
{
"datasource": {
"uid": "9c3MNrV4k",
"type": "grafana-mock-datasource"
},
"frames": [
{
"count": 1,
"disabled": false,
"fields": [
{
"disabled": false,
"labels": "host=a",
"name": "cpu",
"type": "nullable-float64",
"value": "3.14"
},
{
"disabled": false,
"labels": "host=e",
"name": "cpu",
"type": "nullable-float64",
"value": "93.14"
}
],
"frameInputType": "numeric-wide",
"meta": {
"type": "numeric-wide",
"typeVersion": [
0,
1
]
},
"name": "New Frame",
"source": "count"
}
],
"generatorSource": "frontend",
"hide": true,
"queryType": "frames_builder",
"rawFrameSource": "staticScenario",
"refId": "dog",
"resultState": "success",
"scenario": "no-data",
"datasourceId": 49,
"intervalMs": 20000,
"maxDataPoints": 1003
},
{
"datasource": {
"type": "__expr__",
"uid": "__expr__",
"name": "Expression"
},
"expression": "$cat + $fox + $dog",
"hide": false,
"refId": "C",
"type": "math",
"window": ""
}
],
"from": "1690967121711",
"to": "1690988721711"
}
HTTP/1.1 200 OK
Audit-Id: 2a1afb05-0d45-4485-9907-67fa854a51e7
Cache-Control: no-store
Content-Type: application/json
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-Xss-Protection: 1; mode=block
Date: Tue, 16 Sep 2025 15:09:44 GMT
Content-Length: 912
Connection: close
{
"kind": "SQLSchemaResponse",
"apiVersion": "query.grafana.app/v0alpha1",
"SQLSchemaResponse": {
"cat": [
{
"Name": "__metric_name__",
"MySQLType": "text",
"Nullable": false,
"DataFrameFieldType": "string"
},
{
"Name": "__value__",
"MySQLType": "double",
"Nullable": true,
"DataFrameFieldType": "*float64"
},
{
"Name": "host",
"MySQLType": "text",
"Nullable": true,
"DataFrameFieldType": "*string"
}
],
"dog": [
{
"Name": "__metric_name__",
"MySQLType": "text",
"Nullable": false,
"DataFrameFieldType": "string"
},
{
"Name": "__value__",
"MySQLType": "double",
"Nullable": true,
"DataFrameFieldType": "*float64"
},
{
"Name": "host",
"MySQLType": "text",
"Nullable": true,
"DataFrameFieldType": "*string"
}
],
"fox": [
{
"Name": "__metric_name__",
"MySQLType": "text",
"Nullable": false,
"DataFrameFieldType": "string"
},
{
"Name": "__value__",
"MySQLType": "double",
"Nullable": true,
"DataFrameFieldType": "*float64"
},
{
"Name": "host",
"MySQLType": "text",
"Nullable": true,
"DataFrameFieldType": "*string"
}
]
}
}
@kylebrandt
Copy link
Author

May change response to something like the following (each var becomes an object instead of array, so we can relay error status to UI.

{
  "kind": "SQLSchemaResponse",
  "apiVersion": "query.grafana.app/v0alpha1",
  "SQLSchemaResponse": {
    "cat": {
      "schema": [
        {
          "Name": "__metric_name__",
          "MySQLType": "text",
          "Nullable": false,
          "DataFrameFieldType": "string"
        },
        {
          "Name": "__value__",
          "MySQLType": "double",
          "Nullable": true,
          "DataFrameFieldType": "*float64"
        },
        {
          "Name": "host",
          "MySQLType": "text",
          "Nullable": true,
          "DataFrameFieldType": "*string"
        }
      ]
    },
    "dog": {
      "schema": [],
      "error": "This query errored, maybe it's response couldn't be converted to sql, I assume this is useful to know"
    }
  }
}
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment