master
go 87 lines 2.02 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package sql
4
5 import (
6 "fmt"
7
8 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
9 )
10
11 const (
12 prioMetricChart = collectorapi.Priority + iota
13 prioQueryTimingChart
14 )
15
16 func (c *Collector) createMetricBlockChart(chartID string, m ConfigMetricBlock, ch ConfigChartConfig, row map[string]string) {
17 if c.seenCharts[chartID] {
18 return
19 }
20 c.seenCharts[chartID] = true
21
22 chart := &collectorapi.Chart{
23 ID: chartID,
24 Title: ch.Title,
25 Units: ch.Units,
26 Type: collectorapi.ChartType(ch.Type),
27 Ctx: fmt.Sprintf("sql.%s_%s", c.Driver, ch.Context),
28 Fam: ch.Family,
29 Priority: prioMetricChart,
30 Labels: []collectorapi.Label{
31 {Key: "driver", Value: c.Driver},
32 },
33 }
34
35 for k, v := range c.StaticLabels {
36 chart.Labels = append(chart.Labels, collectorapi.Label{Key: k, Value: v})
37 }
38
39 for _, lf := range m.LabelsFromRow {
40 if v, ok := row[lf.Source]; ok {
41 chart.Labels = append(chart.Labels, collectorapi.Label{Key: lf.Name, Value: v})
42 }
43 }
44
45 for _, d := range ch.Dims {
46 chart.Dims = append(chart.Dims, &collectorapi.Dim{
47 ID: buildDimID(chartID, d.Name),
48 Name: d.Name,
49 Algo: collectorapi.DimAlgo(ch.Algorithm),
50 })
51 }
52
53 if err := c.Charts().Add(chart); err != nil {
54 c.Warningf("failed to add chart %q: %v", chartID, err)
55 }
56 }
57
58 func (c *Collector) createQueryTimingChart(chartID, label string) {
59 if c.seenCharts[chartID] {
60 return
61 }
62 c.seenCharts[chartID] = true
63
64 chart := &collectorapi.Chart{
65 ID: chartID,
66 Title: "SQL query execution time",
67 Units: "ms",
68 Ctx: fmt.Sprintf("sql.%s_query_time", c.Driver),
69 Fam: "Query/Timings",
70 Priority: prioQueryTimingChart,
71 Labels: []collectorapi.Label{
72 {Key: "driver", Value: c.Driver},
73 {Key: "query_id", Value: label},
74 },
75 Dims: []*collectorapi.Dim{
76 {
77 ID: buildDimID(chartID, "duration"),
78 Name: "duration",
79 },
80 },
81 }
82
83 if err := c.Charts().Add(chart); err != nil {
84 c.Warningf("failed to add query timing chart %q: %v", chartID, err)
85 return
86 }
87 }