Grafana: Difference between revisions

Axka (talk | contribs)
Add intra-wiki links and document code example
Axka (talk | contribs)
Add infobox and Template:File-based codeblocks
 
Line 1: Line 1:
[https://grafana.com/ Grafana] is an open-source, general purpose dashboarding tool, which runs as a web application. It can be used to create a variety of time-series graphs and also for displaying logs. It supports [[Prometheus]], graphite, InfluxDB, opentsdb, [[Grafana Loki]], [[PostgreSQL]] and many other data sources.
'''[https://grafana.com/grafana/ Grafana]''' is an open-source, general purpose visualization and dashboarding tool, which runs as a web application. It can be used to create time-series graphs and display logs. For data sources, it supports [[Prometheus]], graphite, InfluxDB, opentsdb, [[Grafana Loki]], [[PostgreSQL]] and more.


See [https://search.nixos.org/options?query=services.grafana Grafana options].
{{infobox application
  |name=Grafana
  |image=Grafana logo.svg
  |developer=Grafana Labs
  |type=Visualization and dashboarding tool
  |status=Active
  |license=GNU Affero General Public License v3.0 only
  |programmingLanguage=Go and TypeScript
  |github=grafana/grafana
  |documentation=https://grafana.com/docs/grafana/latest/
}}


== Installation ==
== Installation ==


Grafana is available as NixOS module, it can be enabled using the following config:
Grafana is available as NixOS module: {{nixos:option|services.grafana}}. [https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/ Official documentation] for the options inside <code>settings</code>. Here is a basic config:


<syntaxhighlight lang="nix">
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.grafana = {
{
  enable = true;
  services.grafana = {
  settings = {
    enable = true;
    server = {
    settings = {
      http_addr = "127.0.0.1";
      server = {
      http_port = 3000;
        http_addr = "127.0.0.1";
      enforce_domain = true;
        http_port = 3000;
      enable_gzip = true;
        enforce_domain = true;
      domain = "grafana.your.domain";
        enable_gzip = true;
        domain = "grafana.your.domain";
 
        # Alternatively, if you want to serve Grafana from a subpath:
        # domain = "your.domain";
        # root_url = "https://your.domain/grafana/";
        # serve_from_sub_path = true;
      };


       # Alternatively, if you want to serve Grafana from a subpath:
       # Prevents Grafana from phoning home
       # domain = "your.domain";
       #analytics.reporting_enabled = false;
      # root_url = "https://your.domain/grafana/";
      # serve_from_sub_path = true;
     };
     };
    # Prevents Grafana from phoning home   
    #analytics.reporting_enabled = false;
   };
   };
};
}
</syntaxhighlight>
</nowiki>}}
 
[https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/ Read Grafana's documentation for the options in <code>settings</code>.]   


== Usage ==
== Usage ==
Line 39: Line 49:
==== Nginx ====
==== Nginx ====
Here is how to setup [[Nginx]] such that it proxies <code>your.domain/grafana</code> to your Grafana instance:
Here is how to setup [[Nginx]] such that it proxies <code>your.domain/grafana</code> to your Grafana instance:
<syntaxhighlight lang=nix>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.nginx.virtualHosts."your.domain" = {
{
  addSSL = true;
  services.nginx.virtualHosts."your.domain" = {
  enableACME = true;
    addSSL = true;
  locations."/grafana/" = {
    enableACME = true;
    locations."/grafana/" = {
       proxyPass = "http://${toString config.services.grafana.settings.server.http_addr}:${toString config.services.grafana.settings.server.http_port}";
       proxyPass = "http://${toString config.services.grafana.settings.server.http_addr}:${toString config.services.grafana.settings.server.http_port}";
       proxyWebsockets = true;
       proxyWebsockets = true;
       recommendedProxySettings = true;
       recommendedProxySettings = true;
    };
   };
   };
};
}
</syntaxhighlight>
</nowiki>}}


==== Traefik ====
==== Traefik ====
[[Traefik]] is another common reverse proxy, for which the configuration relevant to Grafana would like this:<syntaxhighlight lang="nix" line="1">
[[Traefik]] is another common reverse proxy, for which the configuration relevant to Grafana would like this:
services.traefik = {
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{
  services.traefik = {
     # ...
     # ...
     dynamicConfigOptions = {
     dynamicConfigOptions = {
Line 66: Line 80:
       };
       };
     # ...
     # ...
};
  };
</syntaxhighlight>Alternatively, to use Grafana on <code>grafana.your.domain</code> instead of <code>your.domain/grafana</code>, you could change line 5 above to:<syntaxhighlight lang="diff">
}
</nowiki>}}
 
Alternatively, to use Grafana on <code>grafana.your.domain</code> instead of <code>your.domain/grafana</code>, you could change line 5 above to:
 
<syntaxhighlight lang="diff">
-rule = "Host(`your.domain`) && PathPrefix(`/grafana`)";
-rule = "Host(`your.domain`) && PathPrefix(`/grafana`)";
+rule = "Host(`grafana.your.domain`)";
+rule = "Host(`grafana.your.domain`)";
Line 88: Line 107:
Grafana supports [https://grafana.com/docs/grafana/latest/administration/provisioning/ provisioning] data sources, dashboards and alerting using {{nixos:option|services.grafana.provision}}. Note that removing a provision and switching to the new NixOS configuration does not currently remove the provisioned items; you have to define them, for example, in <code>deleteDatasources</code>.
Grafana supports [https://grafana.com/docs/grafana/latest/administration/provisioning/ provisioning] data sources, dashboards and alerting using {{nixos:option|services.grafana.provision}}. Note that removing a provision and switching to the new NixOS configuration does not currently remove the provisioned items; you have to define them, for example, in <code>deleteDatasources</code>.


<syntaxhighlight lang="nix">
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.grafana = {
{
  declarativePlugins = with pkgs.grafanaPlugins; [ ... ];
  services.grafana = {
    declarativePlugins = with pkgs.grafanaPlugins; [ ... ];


  provision = {
    provision = {
    enable = true;
      enable = true;


    # Creates a *mutable* dashboard provider, pulling from /etc/grafana-dashboards.
      # Creates a *mutable* dashboard provider, pulling from /etc/grafana-dashboards.
    # With this, you can manually provision dashboards from JSON with `environment.etc` like below.
      # With this, you can manually provision dashboards from JSON with `environment.etc` like below.
    dashboards.settings.providers = [{
      dashboards.settings.providers = [{
      name = "my dashboards";
        name = "my dashboards";
      disableDeletion = true;
        disableDeletion = true;
      options = {
        options = {
        path = "/etc/grafana-dashboards";
          path = "/etc/grafana-dashboards";
        foldersFromFilesStructure = true;
          foldersFromFilesStructure = true;
      };
        };
    }];
      }];


    datasources.settings.datasources = [
      datasources.settings.datasources = [
      # Provisioning a built-in data source
        # Provisioning a built-in data source
      {
        {
        name = "Prometheus";
          name = "Prometheus";
        type = "prometheus";
          type = "prometheus";
        url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
          url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
        isDefault = true;
          isDefault = true;
        editable = false;
          editable = false;
      }
        }
      # All plugins can be provisioned but it's not always documented: https://github.com/fr-ser/grafana-sqlite-datasource/blob/main/docs/faq.md#can-i-use-provisioning-with-this-plugin
        # All plugins can be provisioned but it's not always documented: https://github.com/fr-ser/grafana-sqlite-datasource/blob/main/docs/faq.md#can-i-use-provisioning-with-this-plugin
      # Compare below with https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/setup/provisioning/
        # Compare below with https://grafana.com/docs/plugins/yesoreyeram-infinity-datasource/latest/setup/provisioning/
      {
        {
        name = "Infinity";
          name = "Infinity";
        type = "yesoreyeram-infinity-datasource";
          type = "yesoreyeram-infinity-datasource";
        editable = false;
          editable = false;
      }
        }
    ];
      ];


    # Note: removing attributes from the above `datasources.settings.datasources` is not currently enough for them to be deleted;
      # Note: removing attributes from the above `datasources.settings.datasources` is not currently enough for them to be deleted;
    # One needs to use the following option:
      # One needs to use the following option:
    # datasources.settings.deleteDatasources = [ { name = "foo"; orgId = 1; } { name = "bar"; orgId = 1; } ];
      # datasources.settings.deleteDatasources = [ { name = "foo"; orgId = 1; } { name = "bar"; orgId = 1; } ];
    };
   };
   };
};


# see `dashboards.settings.providers` above
 
environment.etc."grafana-dashboards/some-dashboard.json".source = ./some-dashboard.json;
  # see `dashboards.settings.providers` above
</syntaxhighlight>
  environment.etc."grafana-dashboards/some-dashboard.json".source = ./some-dashboard.json;
}
</nowiki>}}


== External Links ==
== External Links ==