grafana / grafonnet

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

Adding Cloudwatch target in Grafonnet v9.4.0 #38

Closed MartinRoman7 closed 1 year ago

MartinRoman7 commented 1 year ago

Hello team!

I'm using the new Grafonnet version (9.4.0), and I identified that adding a new target in the panel is different compared with the old version. In the oldest Grafonnet version, the way that I followed to add a new target in the panel was by using the following function:

.addTargets(
    [
      cloudwatch.target(
        region='$region',
        namespace='AWS/RDS',
        metric=metric_one,
        datasource='$datasource',
        statistic=statistic_one,
        dimensions={ DBInstanceIdentifier: '$dbName' }
      )
    ]

But in the new Grafonnet version (9.4.0), I found that it is necessary to use the grafonnet.query.cloudwatch subpackage to write a query. In the examples folder, I see a grafonnet.query.prometheus example:

prometheusQuery.new(
      '$' + variables.datasource.name,
      |||
        sum by (cluster, namespace, job) (
            rate(
                process_cpu_seconds_total{
                    cluster=~"$cluster",
                    namespace=~"$namespace",
                    job=~"$job"
                }
            [$__rate_interval])
        )
      |||
    ) 

Do you know if there is a way to "translate" the Builder configuration to a Code option to use the query subpackage?

image

Also, if you have an example using grafonnet.query.cloudwatch would be a great help.

Thanks in advance!

Duologic commented 1 year ago

I'm not 100% sure what you are asking, but it did bring to my attention that the docs for the CloudWatch datasource are empty, which indicates the code isn't there either. I'll have a look at that.

MartinRoman7 commented 1 year ago

Hi, Duologic!

Basically, I would like to know if you have an example using grafonnet.query.cloudwatch. And also, I'm using an old Grafonnet version, the way to add a new target is by using the following code:

.addTargets(
    [
      cloudwatch.target(
        region='$region',
        namespace='AWS/RDS',
        metric=metric_one,
        datasource='$datasource',
        statistic=statistic_one,
        dimensions={ DBInstanceIdentifier: '$dbName' }
      )
    ]

I would like to know if there is a way to translate the old cloudwatch.target schema to the new grafonnet.query.cloudwatch for all the variables.

Thanks!

Duologic commented 1 year ago

The docs now include the Cloudwatch query: https://grafana.github.io/grafonnet/grafonnet/query/cloudWatch.html

I believe your example should look something like this (I have not tested this and I also don't know anything about cloudwatch):

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

cloudwatch.withRegion('$region')
+ cloudwatch.withNamespace('AWS/RDS')
+ cloudwatch.withMetricName(metric_one)
+ cloudwatch.withDatasource('$datasource')
+ cloudwatch.withStatistics([statistic_one])
+ cloudwatch.withDimensions({ DBInstanceIdentifier: '$dbName' })
MartinRoman7 commented 1 year ago

I found another way by using the fn .withTargets(targets) in the panel. I created a new file, targets.libsonnet:

// target example

cpuUtilization(metric,statistic):
    {
      'datasource': '$' + variables.name.name,
      'dimensions': {
        'DBInstanceIdentifier': '$' + variables.name.name
      },
      'highResolution': false,
      'metricName': metric,
      'namespace': 'AWS/RDS',
      'period': 'auto',
      'refId': 'A',
      'region': '$' + variables.region.name,
      'statistics': [
        statistic
      ]
    },

And then importing the targets.libsonnet in the panels.libsonnet file

Thank you for your help. I really appreciate it!