Skip to content

Commit

Permalink
Merge branch 'main' into alert_rule_dashboard_and_panel
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesGibo authored Jul 16, 2024
2 parents a52f7cb + eedff24 commit 86f0a5f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-sphinx-and-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- name: Link Checker
id: lc
uses: lycheeverse/lychee-action@v1.9.3
uses: lycheeverse/lychee-action@v1.10.0
with:
args: --verbose **/*.html
- name: Fail if there were link errors
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5.0.0
uses: actions/setup-python@v5.1.1
with:
python-version: '3.10'
- name: Build a binary wheel and a source tarball
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5.0.0
uses: actions/setup-python@v5.1.1
with:
python-version: ${{ matrix.python }}
- name: Run tests
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
Changelog
=========


x.x.x ?
==================
=======

* AlertRulev9 propagates dashbord_uid and panel_uid to the alert rule
* Added ``tooltipSort`` parameter to PieChartv2 panel
* Fix mappings for Table
* Added support for AWS Cross-Account in CloudwatchMetricsTarget
* Added `LokiTarget`

0.7.1 2024-01-12
==================
================

* Extended DashboardLink to support links to dashboards and urls, as per the docs_

Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sphinx == 7.2.6
sphinx == 7.4.3
sphinx_rtd_theme == 2.0.0
12 changes: 9 additions & 3 deletions grafanalib/cloudwatch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Helpers to create Cloudwatch-specific Grafana queries."""

import attr

from attr.validators import instance_of

from grafanalib.core import Target


Expand All @@ -22,6 +22,8 @@ class CloudwatchMetricsTarget(Target):
:param expression: Cloudwatch Metric math expressions
:param id: unique id
:param matchExact: Only show metrics that exactly match all defined dimension names.
:param account: AWS Account where Cloudwatch is used
:param accountId: AWS Account ID where Cloudwatch is used
:param metricName: Cloudwatch metric name
:param namespace: Cloudwatch namespace
:param period: Cloudwatch data period
Expand All @@ -32,11 +34,14 @@ class CloudwatchMetricsTarget(Target):
:param hide: controls if given metric is displayed on visualization
:param datasource: Grafana datasource name
"""

alias = attr.ib(default="")
dimensions = attr.ib(factory=dict, validator=instance_of(dict))
expression = attr.ib(default="")
id = attr.ib(default="")
matchExact = attr.ib(default=True, validator=instance_of(bool))
account = attr.ib(default="")
accountId = attr.ib(default="")
metricName = attr.ib(default="")
namespace = attr.ib(default="")
period = attr.ib(default="")
Expand All @@ -48,13 +53,14 @@ class CloudwatchMetricsTarget(Target):
datasource = attr.ib(default=None)

def to_json_data(self):

return {
"alias": self.alias,
"dimensions": self.dimensions,
"expression": self.expression,
"id": self.id,
"matchExact": self.matchExact,
"account": self.account,
"accountId": self.accountId,
"metricName": self.metricName,
"namespace": self.namespace,
"period": self.period,
Expand Down Expand Up @@ -88,6 +94,7 @@ class CloudwatchLogsInsightsTarget(Target):
:param hide: controls if given metric is displayed on visualization
:param datasource: Grafana datasource name
"""

expression = attr.ib(default="")
id = attr.ib(default="")
logGroupNames = attr.ib(factory=list, validator=instance_of(list))
Expand All @@ -99,7 +106,6 @@ class CloudwatchLogsInsightsTarget(Target):
datasource = attr.ib(default=None)

def to_json_data(self):

return {
"expression": self.expression,
"id": self.id,
Expand Down
32 changes: 30 additions & 2 deletions grafanalib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,29 @@ def to_json_data(self):
}


# Currently not deriving from `Target` because Grafana errors if fields like `query` are added to Loki targets
@attr.s
class LokiTarget(object):
"""
Target for Loki LogQL queries
"""

datasource = attr.ib(default='', validator=instance_of(str))
expr = attr.ib(default='', validator=instance_of(str))
hide = attr.ib(default=False, validator=instance_of(bool))

def to_json_data(self):
return {
'datasource': {
'type': 'loki',
'uid': self.datasource,
},
'expr': self.expr,
'hide': self.hide,
'queryType': 'range',
}


@attr.s
class SqlTarget(Target):
"""
Expand Down Expand Up @@ -3320,7 +3343,8 @@ def to_json_data(self):
'displayMode': self.displayMode,
'filterable': self.filterable,
},
'unit': self.unit
'unit': self.unit,
'mappings': self.mappings
},
'overrides': self.overrides
},
Expand Down Expand Up @@ -3841,6 +3865,8 @@ class PieChartv2(Panel):
:param reduceOptionsValues: Calculate a single value per column or series or show each row
:param tooltipMode: Tooltip mode
single (Default), multi, none
:param tooltipSort: To sort the tooltips
none (Default), asc, desc
:param unit: units
"""

Expand All @@ -3856,6 +3882,7 @@ class PieChartv2(Panel):
reduceOptionsFields = attr.ib(default='', validator=instance_of(str))
reduceOptionsValues = attr.ib(default=False, validator=instance_of(bool))
tooltipMode = attr.ib(default='single', validator=instance_of(str))
tooltipSort = attr.ib(default='none', validator=instance_of(str))
unit = attr.ib(default='', validator=instance_of(str))

def to_json_data(self):
Expand All @@ -3880,7 +3907,8 @@ def to_json_data(self):
},
'pieType': self.pieType,
'tooltip': {
'mode': self.tooltipMode
'mode': self.tooltipMode,
'sort': self.tooltipSort
},
'legend': {
'displayMode': self.legendDisplayMode,
Expand Down
26 changes: 26 additions & 0 deletions grafanalib/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,32 @@ def test_target_invalid():
)


def test_loki_target():
t = G.Dashboard(
title='unittest',
uid='unit-test-uid',
timezone='browser',
panels=[
G.TimeSeries(
title='Some logs',
targets=[
G.LokiTarget(
datasource='my-logs',
expr='{pod="unittest"} |= "hello"',
),
],
gridPos=G.GridPos(h=10, w=24, x=0, y=0),
),
],
).auto_panel_ids()

dashboard_json = t.to_json_data()
target_json = dashboard_json['panels'][0].targets[0].to_json_data()
# Grafana wants type/uid fields for Loki targets (as of 2024-04)
assert target_json['datasource']['type'] == 'loki'
assert target_json['datasource']['uid'] == 'my-logs'


def test_sql_target():
t = G.Table(
dataSource="some data source",
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def local_file(name):
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
Expand Down

0 comments on commit 86f0a5f

Please sign in to comment.