Na podstawie this answer i this answer udało mi się umieścić na mojej stronie deskę rozdzielczą Grafana.
Umieść swoje iframe
:
<iframe id="dashboard"></iframe>
A potem karmić go treścią Grafana za pomocą żądań AJAX tak:
<script type="text/javascript">
$.ajax(
{
type: 'GET',
url: 'http://localhost:3000/dashboard/db/your-dashboard-here',
contentType: 'application/json',
beforeSend: function(xhr, settings) {
xhr.setRequestHeader(
'Authorization', 'Basic ' + window.btoa('admin:admin')
);
},
success: function(data) {
$('#dashboard').attr('src', 'http://localhost:3000/dashboard/db/your-dashboard-here');
$('#dashboard').contents().find('html').html(data);
}
}
);
</script>
żądania AJAX jest obowiązkowa, ponieważ pozwala ustawić nagłówek z poświadczeniami.
W tym momencie otrzymujesz pustą odpowiedź od serwera Grafana z powodu CORS. Musisz włączyć proxy dla Grafana. Jest przykładem konfiguracja Grafana i nginx kontenerów Docker korzystających docker-komponować poniżej:
version: '2.1'
services:
grafana:
image: grafana/grafana
nginx:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 3000:80
Ostatnią rzeczą, którą trzeba zrobić jest zapewnienie plik nginx.conf:
events {
worker_connections 1024;
}
http {
#
# Acts as a nginx HTTPS proxy server
# enabling CORS only to domains matched by regex
# /https?://.*\.mckinsey\.com(:[0-9]+)?)/
#
# Based on:
# * http://blog.themillhousegroup.com/2013/05/nginx-as-cors-enabled-https-proxy.html
# * http://enable-cors.org/server_nginx.html
#
server {
listen 80;
location/{
#if ($http_origin ~* (https?://.*\.tarunlalwani\.com(:[0-9]+)?$)) {
# set $cors "1";
#}
set $cors "1";
# OPTIONS indicates a CORS pre-flight request
if ($request_method = 'OPTIONS') {
set $cors "${cors}o";
}
# Append CORS headers to any request from
# allowed CORS domain, except OPTIONS
if ($cors = "1") {
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials true always;
proxy_pass http://grafana:3000;
}
# OPTIONS (pre-flight) request from allowed
# CORS domain. return response directly
if ($cors = "1o") {
add_header 'Access-Control-Allow-Origin' '$http_origin' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Origin,Content-Type,Accept,Authorization' always;
add_header Content-Length 0;
add_header Content-Type text/plain;
return 204;
}
# Requests from non-allowed CORS domains
proxy_pass http://grafana:3000;
}
}
}
baz ten plik na przewidziane here, ale ważna różnica jest
add_header 'Access-Control-Allow-Headers' 'Origin,Content-Type,Accept,Authorization' always;
oznacza to, że pozwala na ustalenie Authorization
nagłówek.
Czy byłeś w stanie poczynić postępy w tej sprawie? – creimers
Poddałem się z bezpośrednim wstawianiem diagramów grafów. Zamiast tego w mojej aplikacji wyeksponowałem interesujące części [Graphite API] (https://graphite-api.readthedocs.io/en/latest/). Zwracają dane metryk w json. W panelu administratora aplikacji renderuję te dane jako wykresy za pomocą [chart.js] (http://www.chartjs.org/). Trochę nudne, ponieważ grafana robi już to samo, używając tego samego interfejsu grafitowego, ale nie znalazłem sposobu na ponowne użycie go z odpowiednimi ograniczeniami. – fracz
Dzięki. Miałem nadzieję ominąć to ... – creimers