master
go 50 lines 1.62 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package prometheus
4
5 import (
6 "fmt"
7
8 "github.com/netdata/netdata/go/plugins/plugin/framework/charttpl"
9 "gopkg.in/yaml.v2"
10 )
11
12 // chartExpireAfterCycles mirrors V1's stale-chart removal (a chart was dropped after 10 missed
13 // cycles): chartengine autogen removes a chart/dimension after this many successful cycles in
14 // which its series is not seen.
15 const chartExpireAfterCycles = 10
16
17 // buildChartTemplate returns the per-job chart template (charttpl YAML) for the prometheus collector.
18 // It is a pure-autogen template: a stub group satisfies the schema, and chartengine autogen builds
19 // one chart per scraped metric, prefixing the context with context_namespace. The namespace is
20 // "prometheus" or "prometheus.<app>" so contexts match V1 (prometheus.<metric> /
21 // prometheus.<app>.<metric>); autogen joins namespace + "." + metric, so the app's separating dot is
22 // part of the namespace itself.
23 func buildChartTemplate(app string) (string, error) {
24 namespace := "prometheus"
25 if app != "" {
26 namespace = "prometheus." + app
27 }
28
29 spec := charttpl.Spec{
30 Version: charttpl.VersionV1,
31 ContextNamespace: namespace,
32 Engine: &charttpl.Engine{
33 Autogen: &charttpl.EngineAutogen{
34 Enabled: true,
35 ExpireAfterSuccessCycles: chartExpireAfterCycles,
36 },
37 },
38 Groups: []charttpl.Group{{Family: "prometheus"}},
39 }
40
41 if err := spec.Validate(); err != nil {
42 return "", fmt.Errorf("build prometheus chart template: %w", err)
43 }
44
45 raw, err := yaml.Marshal(spec)
46 if err != nil {
47 return "", fmt.Errorf("marshal prometheus chart template: %w", err)
48 }
49 return string(raw), nil
50 }