grafana / grafonnet

Jsonnet library for generating Grafana dashboards.
https://grafana.github.io/grafonnet/
Apache License 2.0
320 stars 18 forks source link

Support for AWS CloudWatch Datasource in Grafana Variables #168

Closed isarns closed 4 months ago

isarns commented 4 months ago

Expected Behavior

I would like to be able to define a Grafana variable with a CloudWatch datasource in my Jsonnet code. The expected JSON output for the variable should look like this:

{
  "current": {
    "selected": false,
    "text": "app/alb-name123123",
    "value": "app/alb-name123123"
  },
  "datasource": {
    "type": "cloudwatch",
    "uid": "cloudwatch-ds"
  },
  "definition": "",
  "hide": 0,
  "includeAll": false,
  "label": "alb_name",
  "multi": false,
  "name": "ALB",
  "options": [],
  "query": {
    "dimensionFilters": {},
    "dimensionKey": "LoadBalancer",
    "logGroupPrefix": "",
    "metricName": "RequestCount",
    "namespace": "AWS/ApplicationELB",
    "queryType": "dimensionValues",
    "refId": "CloudWatchVariableQueryEditor-VariableQuery",
    "region": "default",
    "resourceType": "",
    "tags": {}
  },
  "refresh": 1,
  "regex": "",
  "skipUrlSync": false,
  "sort": 1,
  "type": "query"
}

Current Behavior

Currently, I am unable to find a way to specify a CloudWatch datasource for a Grafana variable using Graffonet.

Environment

Possible Solution

Any suggestions for a possible solution or workaround.

Duologic commented 4 months ago

It looks like this is possible with the current grafonnet, example:

local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
local query = g.dashboard.variable.query;
local cloudwatch = g.query.cloudWatch.CloudWatchMetricsQuery;

query.new(
  'ALB',
  cloudwatch.withQueryType('dimensionValues')
  + cloudwatch.withNamespace('AWS/ApplicationELB')
  + cloudwatch.withRefId('CloudWatchVariableQueryEditor-VariableQuery')
  + cloudwatch.withMetricName('RequestCount')
  + { dimensionKey: 'LoadBalancer' }  // didn't find in `cloudwatch`
)
+ query.withDatasource('cloudwatch', 'cloudwatch-ds')
+ query.generalOptions.withLabel('alb_name')
+ query.generalOptions.withCurrent('app/alb-name123123')
+ { multi:: false }  // workaround for bug on `withCurrent`