master
go 88 lines 2.21 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package chartemit
4
5 import (
6 "sort"
7 "strings"
8
9 "github.com/netdata/netdata/go/plugins/pkg/netdataapi"
10 "github.com/netdata/netdata/go/plugins/plugin/framework/chartengine"
11 )
12
13 var wireValueReplacer = strings.NewReplacer(
14 "'", "",
15 "\n", " ",
16 "\r", " ",
17 "\x00", "",
18 )
19
20 func sanitizeWireValue(value string) string {
21 return wireValueReplacer.Replace(value)
22 }
23
24 func sanitizeWireID(value string) string {
25 return sanitizeWireValue(strings.TrimSpace(value))
26 }
27
28 func emitChart(api *netdataapi.API, env EmitEnv, chartID string, meta chartengine.ChartMeta, obsolete bool) {
29 opts := ""
30 if obsolete {
31 opts = "obsolete"
32 }
33 api.CHART(netdataapi.ChartOpts{
34 TypeID: sanitizeWireID(env.TypeID),
35 ID: sanitizeWireID(chartID),
36 Name: "",
37 Title: sanitizeWireValue(meta.Title),
38 Units: sanitizeWireValue(meta.Units),
39 Family: sanitizeWireValue(meta.Family),
40 Context: sanitizeWireValue(meta.Context),
41 ChartType: string(meta.Type),
42 Priority: meta.Priority,
43 UpdateEvery: env.UpdateEvery,
44 Options: opts,
45 Plugin: sanitizeWireValue(env.Plugin),
46 Module: sanitizeWireValue(env.Module),
47 })
48 }
49
50 func emitChartLabels(api *netdataapi.API, env EmitEnv, chartLabels map[string]string) {
51 chartKeys := make([]string, 0, len(chartLabels))
52 for key := range chartLabels {
53 if strings.TrimSpace(key) == "" || key == collectJobReservedLabel {
54 continue
55 }
56 chartKeys = append(chartKeys, key)
57 }
58 sort.Strings(chartKeys)
59
60 keys := make([]string, 0, len(env.JobLabels))
61 for key := range env.JobLabels {
62 if _, overridden := chartLabels[key]; overridden {
63 continue
64 }
65 if key == collectJobReservedLabel {
66 continue
67 }
68 keys = append(keys, key)
69 }
70 sort.Strings(keys)
71 for _, key := range keys {
72 sKey := sanitizeWireID(key)
73 if sKey == "" {
74 continue
75 }
76 api.CLABEL(sKey, sanitizeWireValue(env.JobLabels[key]), labelSourceConf)
77 }
78 for _, key := range chartKeys {
79 sKey := sanitizeWireID(key)
80 if sKey == "" {
81 continue
82 }
83 api.CLABEL(sKey, sanitizeWireValue(chartLabels[key]), labelSourceAuto)
84 }
85 if strings.TrimSpace(env.JobName) != "" {
86 api.CLABEL(collectJobReservedLabel, sanitizeWireValue(env.JobName), labelSourceAuto)
87 }
88 }