-
Notifications
You must be signed in to change notification settings - Fork 53
/
types.go
190 lines (168 loc) · 4.11 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package otel
import (
"bytes"
"fmt"
"github.com/rancher/opni/pkg/util"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/yaml.v2"
)
const (
CollectorName = "opni"
MetricsCrdName = "opni-monitoring"
MetricsFeatureFlag = "otel-metrics"
MetricsServiceAccountName = "opni-otel-prometheus-agent"
)
func AgentEndpoint(serviceName string) string {
return fmt.Sprintf("http://%s/api/agent/otel", serviceName)
}
type NodeConfig struct {
Instance string
Logs LoggingConfig
Metrics MetricsConfig
Containerized bool
LogLevel string
}
type AggregatorConfig struct {
AgentEndpoint string
LogsEnabled bool
Metrics MetricsConfig
Containerized bool
LogLevel string
}
type LoggingConfig struct {
Enabled bool
Receivers []string
}
type MetricsConfig struct {
Enabled bool
LogLevel string
ListenPort int
RemoteWriteEndpoint string
WALDir string
// It becomes easier to marshal []promcfg.ScrapeConfig to yaml strings
DiscoveredScrapeCfg string
Spec *OTELSpec
}
type OTELSpec struct {
AdditionalScrapeConfigs []*ScrapeConfig `json:"additionalScrapeConfigs,omitempty"`
Wal *WALConfig `json:"wal,omitempty"`
HostMetrics *bool `json:"hostMetrics,omitempty"`
}
type ScrapeConfig struct {
JobName string `json:"jobName,omitempty"`
Targets []string `json:"targets,omitempty"`
ScrapeInterval string `json:"scrapeInterval,omitempty"`
}
type WALConfig struct {
Enabled bool `json:"enabled,omitempty"`
BufferSize int64 `json:"bufferSize,omitempty"`
TruncateFrequency *durationpb.Duration `json:"truncateFrequency,omitempty"`
}
func (in *MetricsConfig) DeepCopyInto(out *MetricsConfig) {
*out = *in
if in.Spec != nil {
in, out := &in.Spec, &out.Spec
*out = new(OTELSpec)
(*in).DeepCopyInto(*out)
}
}
func (in *MetricsConfig) DeepCopy() *MetricsConfig {
if in == nil {
return nil
}
out := new(MetricsConfig)
in.DeepCopyInto(out)
return out
}
func (in *OTELSpec) DeepCopyInto(out *OTELSpec) {
*out = *in
if in.AdditionalScrapeConfigs != nil {
in, out := &in.AdditionalScrapeConfigs, &out.AdditionalScrapeConfigs
*out = make([]*ScrapeConfig, len(*in))
for i := range *in {
(*out)[i] = (*in)[i].DeepCopy()
}
}
if in.Wal != nil {
in, out := &in.Wal, &out.Wal
*out = new(WALConfig)
(*in).DeepCopyInto(*out)
}
if in.HostMetrics != nil {
in, out := &in.HostMetrics, &out.HostMetrics
*out = new(bool)
**out = **in
}
}
func (o *OTELSpec) DeepCopy() *OTELSpec {
if o == nil {
return nil
}
out := new(OTELSpec)
o.DeepCopyInto(out)
return out
}
func (in *ScrapeConfig) DeepCopyInto(out *ScrapeConfig) {
*out = *in
if in.Targets != nil {
in, out := &in.Targets, &out.Targets
*out = make([]string, len(*in))
copy(*out, *in)
}
}
func (s *ScrapeConfig) DeepCopy() *ScrapeConfig {
if s == nil {
return nil
}
out := new(ScrapeConfig)
s.DeepCopyInto(out)
return out
}
func (in *WALConfig) DeepCopyInto(out *WALConfig) {
*out = *in
if in.TruncateFrequency != nil {
out.TruncateFrequency = util.ProtoClone(in.TruncateFrequency)
}
}
func (w *WALConfig) DeepCopy() *WALConfig {
if w == nil {
return nil
}
out := new(WALConfig)
w.DeepCopyInto(out)
return out
}
func (d NodeConfig) MetricReceivers() []string {
res := []string{}
if d.Metrics.Enabled {
if lo.FromPtrOr(d.Metrics.Spec.HostMetrics, false) {
res = append(res, "hostmetrics")
if d.Containerized {
res = append(res, "kubeletstats")
}
}
}
return res
}
func (o AggregatorConfig) MetricReceivers() []string {
res := []string{}
if o.Metrics.Enabled {
if len(o.Metrics.Spec.AdditionalScrapeConfigs) > 0 {
res = append(res, "prometheus/additional")
}
if len(o.Metrics.DiscoveredScrapeCfg) > 0 {
res = append(res, "prometheus/discovered")
}
}
return res
}
func PromCfgToString(cfgs []yaml.MapSlice) string {
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(&b)
err := yamlEncoder.Encode(&cfgs)
if err != nil {
return ""
}
return b.String()
}