Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
feat: declare Grafana dashboards with jsonnet (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Basty authored Mar 31, 2023
1 parent 26cb18d commit 3703c5f
Show file tree
Hide file tree
Showing 35 changed files with 2,737 additions and 415 deletions.
118 changes: 118 additions & 0 deletions terraform/monitoring/dashboard.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
local lib = import 'lib.libsonnet';

local grafana = import 'grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;
local annotation = grafana.annotation;
local row = grafana.row;
local statPanel = grafana.statPanel;
local prometheus = grafana.prometheus;
local template = grafana.template;

local ds_prometheus = {
type: 'prometheus',
uid: std.extVar('prometheus_uid'),
};

dashboard.new(
title = std.extVar('dashboard_title'),
uid = std.extVar('dashboard_title'),
schemaVersion = 26,
editable = true,
graphTooltip = 'shared_crosshair',
)
.addAnnotation(
annotation.default +
{
iconColor: 'rgba(0, 211, 255, 1)',
}
)
.addPanel(
statPanel.new(
title = 'Received Items per Hour',
description = 'The number of items received from relay',
datasource = ds_prometheus,
reducerFunction = 'lastNotNull',
)
.addTarget(prometheus.target(
expr = 'sum(rate(received_items{}[1h]))',
legendFormat = 'Received Items',
datasource = ds_prometheus,
)),
gridPos = {
x: 0, y: 0,
w: 10, h: 8
},
)
.addPanel(
statPanel.new(
title = 'Stored Items per Hour',
description = 'The number of items actually stored in the database',
datasource = ds_prometheus,
reducerFunction = 'lastNotNull',
)
.addTarget(prometheus.target(
expr = 'sum(rate(stored_items{}[1h]))',
legendFormat = 'Stored Items',
datasource = ds_prometheus,
)),
gridPos = {
x: 10, y: 0,
w: 10, h: 8
},
)
.addPanel(
statPanel.new(
title = 'Get Queries per Hour',
description = 'The number of items retrieval queries',
datasource = ds_prometheus,
reducerFunction = 'lastNotNull',
)
.addTarget(prometheus.target(
expr = 'sum(rate(get_queries{}[1h]))',
legendFormat = '"Get" Queries',
datasource = ds_prometheus,
)),
gridPos = {
x: 0, y: 8,
w: 10, h: 8
},
)
.addPanel(
statPanel.new(
title = 'Served Items per Hour',
description = 'The number of messages served to clients',
datasource = ds_prometheus,
reducerFunction = 'lastNotNull',
)
.addTarget(prometheus.target(
expr = 'sum(rate(served_items{}[1h]))',
legendFormat = '"Get" Queries',
datasource = ds_prometheus,
)),
gridPos = {
x: 10, y: 8,
w: 10, h: 8
},
)
.addPanel(
statPanel.new(
title = 'Registrations per Hour',
description = 'The number of registrations retrievals',
datasource = ds_prometheus,
reducerFunction = 'lastNotNull',
)
.addTarget(prometheus.target(
expr = 'sum(rate(cached_registrations{}[1h]))',
legendFormat = 'Cache hits',
datasource = ds_prometheus,
))
.addTarget(prometheus.target(
expr = 'sum(rate(fetched_registrations{}[1h]))',
legendFormat = 'Cache misses',
datasource = ds_prometheus,
)),
gridPos = {
x: 0, y: 16,
w: 20, h: 8
},
)
47 changes: 47 additions & 0 deletions terraform/monitoring/grafonnet/alert_condition.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
/**
* Returns a new condition of alert of graph panel.
* Currently the only condition type that exists is a Query condition
* that allows to specify a query letter, time range and an aggregation function.
*
* @name alertCondition.new
*
* @param evaluatorParams Value of threshold
* @param evaluatorType Type of threshold
* @param operatorType Operator between conditions
* @param queryRefId The letter defines what query to execute from the Metrics tab
* @param queryTimeStart Begging of time range
* @param queryTimeEnd End of time range
* @param reducerParams Params of an aggregation function
* @param reducerType Name of an aggregation function
*
* @return A json that represents a condition of alert
*/
new(
evaluatorParams=[],
evaluatorType='gt',
operatorType='and',
queryRefId='A',
queryTimeEnd='now',
queryTimeStart='5m',
reducerParams=[],
reducerType='avg',
)::
{
evaluator: {
params: if std.type(evaluatorParams) == 'array' then evaluatorParams else [evaluatorParams],
type: evaluatorType,
},
operator: {
type: operatorType,
},
query: {
params: [queryRefId, queryTimeStart, queryTimeEnd],
},
reducer: {
params: if std.type(reducerParams) == 'array' then reducerParams else [reducerParams],
type: reducerType,
},
type: 'query',
},
}
43 changes: 43 additions & 0 deletions terraform/monitoring/grafonnet/alertlist.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
/**
* Creates an [Alert list panel](https://grafana.com/docs/grafana/latest/panels/visualizations/alert-list-panel/)
*
* @name alertlist.new
*
* @param title (default `''`)
* @param span (optional)
* @param show (default `'current'`) Whether the panel should display the current alert state or recent alert state changes.
* @param limit (default `10`) Sets the maximum number of alerts to list.
* @param sortOrder (default `'1'`) '1': alerting, '2': no_data, '3': pending, '4': ok, '5': paused
* @param stateFilter (optional)
* @param onlyAlertsOnDashboard (optional) Shows alerts only from the dashboard the alert list is in
* @param transparent (optional) Whether to display the panel without a background
* @param description (optional)
* @param datasource (optional)
*/
new(
title='',
span=null,
show='current',
limit=10,
sortOrder=1,
stateFilter=[],
onlyAlertsOnDashboard=true,
transparent=null,
description=null,
datasource=null,
)::
{
[if transparent != null then 'transparent']: transparent,
title: title,
[if span != null then 'span']: span,
type: 'alertlist',
show: show,
limit: limit,
sortOrder: sortOrder,
[if show != 'changes' then 'stateFilter']: stateFilter,
onlyAlertsOnDashboard: onlyAlertsOnDashboard,
[if description != null then 'description']: description,
datasource: datasource,
},
}
40 changes: 40 additions & 0 deletions terraform/monitoring/grafonnet/annotation.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
default::
{
builtIn: 1,
datasource: '-- Grafana --',
enable: true,
hide: true,
iconColor: 'rgba(0, 211, 255, 1)',
name: 'Annotations & Alerts',
type: 'dashboard',
},

/**
* @name annotation.datasource
*/

datasource(
name,
datasource,
expr=null,
enable=true,
hide=false,
iconColor='rgba(255, 96, 96, 1)',
tags=[],
type='tags',
builtIn=null,
)::
{
datasource: datasource,
enable: enable,
[if expr != null then 'expr']: expr,
hide: hide,
iconColor: iconColor,
name: name,
showIn: 0,
tags: tags,
type: type,
[if builtIn != null then 'builtIn']: builtIn,
},
}
47 changes: 47 additions & 0 deletions terraform/monitoring/grafonnet/bar_gauge_panel.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
/**
* Create a [bar gauge panel](https://grafana.com/docs/grafana/latest/panels/visualizations/bar-gauge-panel/),
*
* @name barGaugePanel.new
*
* @param title Panel title.
* @param description (optional) Panel description.
* @param datasource (optional) Panel datasource.
* @param unit (optional) The unit of the data.
* @param thresholds (optional) An array of threashold values.
*
* @method addTarget(target) Adds a target object.
* @method addTargets(targets) Adds an array of targets.
*/
new(
title,
description=null,
datasource=null,
unit=null,
thresholds=[],
):: {
type: 'bargauge',
title: title,
[if description != null then 'description']: description,
datasource: datasource,
targets: [
],
fieldConfig: {
defaults: {
unit: unit,
thresholds: {
mode: 'absolute',
steps: thresholds,
},
},
},
_nextTarget:: 0,
addTarget(target):: self {
// automatically ref id in added targets.
local nextTarget = super._nextTarget,
_nextTarget: nextTarget + 1,
targets+: [target { refId: std.char(std.codepoint('A') + nextTarget) }],
},
addTargets(targets):: std.foldl(function(p, t) p.addTarget(t), targets, self),
},
}
57 changes: 57 additions & 0 deletions terraform/monitoring/grafonnet/cloudmonitoring.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
/**
* Creates a [Google Cloud Monitoring target](https://grafana.com/docs/grafana/latest/datasources/google-cloud-monitoring/)
*
* @name cloudmonitoring.target
*
* @param metric
* @param project
* @param filters (optional)
* @param groupBys (optional)
* @param period (default: `'cloud-monitoring-auto'`)
* @param crossSeriesReducer (default 'REDUCE_MAX')
* @param valueType (default 'INT64')
* @param perSeriesAligner (default 'ALIGN_DELTA')
* @param metricKind (default 'CUMULATIVE')
* @param unit (optional)
* @param alias (optional)
* @return Panel target
*/

target(
metric,
project,
filters=[],
groupBys=[],
period='cloud-monitoring-auto',
crossSeriesReducer='REDUCE_MAX',
valueType='INT64',
perSeriesAligner='ALIGN_DELTA',
metricKind='CUMULATIVE',
unit=1,
alias=null,
):: {
metricQuery: {
[if alias != null then 'aliasBy']: alias,
alignmentPeriod: period,
crossSeriesReducer: crossSeriesReducer,
[if filters != null then 'filters']: filters,
[if groupBys != null then 'groupBys']: groupBys,
metricKind: metricKind,
metricType: metric,
perSeriesAligner: perSeriesAligner,
projectName: project,
unit: unit,
valueType: valueType,
},
sloQuery: {
[if alias != null then 'aliasBy']: alias,
alignmentPeriod: period,
projectName: project,
selectorName: 'select_slo_health',
serviceId: '',
sloId: '',
},
},
}
Loading

0 comments on commit 3703c5f

Please sign in to comment.