Sobes.tech
Junior

How do you set up graphs on the Grafana panel?

sobes.tech AI

Answer from AI

There are several methods:

  1. Visual Editor: In the panel editing mode, select the visualization type "Graph". On the Query tab, add data sources (e.g., Prometheus, InfluxDB). On the "Graph" tab, configure the appearance: axes, legend, time range.

    // Example of Y-axis configuration
    // Setting minimum and maximum values
    // Unit: bytes, s, requests, etc.
    // Label: Axis name
    
  2. PromQL (for Prometheus): Use the Prometheus query language to fetch data and create graphs.

    // Example query: average CPU load over the last 5 minutes
    avg(rate(node_cpu_seconds_total{mode="idle"}[5m]))
    
  3. InfluxQL (for InfluxDB): Query language for InfluxDB.

    -- Example query: average memory_usage metric over the last hour, grouped by host and device tags
    SELECT mean("memory_usage") FROM "telegraf"."autogen"."mem" WHERE time >= now() - 1h GROUP BY time(1m), "host", "device" fill(none)
    
  4. Jsonnet/Grafana Dashboard as Code: Description of the panel and graphs in a configuration file. Allows version-controlled management of panels and automation of their creation.

    // Example of defining a graph in Jsonnet
    {
      title: 'CPU Usage',
      type: 'graph',
      lines: true,
      targets: [
        {
          expr: '100 - avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100',
          legendFormat: 'CPU Busy',
          refId: 'A',
        },
      ],
      fieldConfig: {
        defaults: {
          unit: 'percent',
        },
      },
    }
    

Editing visual settings is done through the "Graph" or "Visualization" tab, depending on the graph type and Grafana version. Here, colors, line styles, point display, graph area, tooltips, and other elements are configured.

Important aspects include choosing the correct query time range, data refresh interval, and using aggregation functions (avg, sum, rate, etc.) to process data from sources.