Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Loki target #657

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ x.x.x ?
* 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
================
Expand Down
23 changes: 23 additions & 0 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
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
Loading