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

Publish to Grafana instance using REST API #307

Open
peay opened this issue Jan 21, 2021 · 5 comments
Open

Publish to Grafana instance using REST API #307

peay opened this issue Jan 21, 2021 · 5 comments

Comments

@peay
Copy link

peay commented Jan 21, 2021

Really great library. I would like to get some thoughts on a potential feature. For development, it would be useful to be able to directly publish to a Grafana instance to preview/test using the REST API. This is especially useful if you don't have access to the data sources locally.

API could take the form of a method

class Dashboard:
    ...
    def preview(self, grafana_url: str, grafana_auth: str, folder: Optional[str] = None) -> str:
       ....

preview would create (or update, if it exists based on title) a dashboard named {self.title} (Preview), and would return the URL to the preview dashboard. This could also return a context manager that would delete the preview dashboard on __exit__, so that you could do:

with dashboard.preview(...) as preview:
   webbrowser.open(preview.url)
   _ = input() # Wait for a keystroke, then cleanup
@bboreham
Copy link
Contributor

Interesting idea.

Where would this code run? Would there be a daemon beside the Grafana instance running Python code?

@peay
Copy link
Author

peay commented Jan 22, 2021

I have in mind to run this from Jupyter notebooks, to achieve a kind of 'REPL' workflow: edit the dashboard definition in a notebook, periodically preview the result in a remote Grafana.

Alternatively, you could edit a Python file with a dashboard locally, with a

dashboard = ...

if __name__ == "__main__":
    with dashboard.preview(...) as preview:
        webbrowser.open(preview.url)
        _ = input() # Wait for a keystroke, then cleanup

Then, you can periodically run the script to preview.

@JamesGibo
Copy link
Collaborator

Yeah, I like the idea.

For me the the ability to preview a dashboard from the a Python file would be more useful than from a Jupyter notebook.

If we go to the effort of adding support for grafanalib to preview a grafana dashboard we might as well add support for it to publish/provision dashboards as the process will be almost the same.

https://grafana.com/docs/grafana/latest/http_api/dashboard/

Here is some Python code from Ansible for creating/updating dashboards:
https://github.com/ansible-collections/community.grafana/blob/770421f3f227f19c0cb227fdbf681be80d69225a/plugins/modules/grafana_dashboard.py#L295-L315

@JamesGibo
Copy link
Collaborator

There is also this project that pushes grafanalib dashboards to grafana
https://github.com/uritau/grafana_as_code

@JamesGibo
Copy link
Collaborator

#370 example code to publish a dashboard in python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants