| 1 | // Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | // under the Apache License Version 2.0. |
| 3 | // This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | // Copyright 2016-present Datadog, Inc. |
| 5 | |
| 6 | package ddprofiledefinition |
| 7 | |
| 8 | import ( |
| 9 | "maps" |
| 10 | "regexp" |
| 11 | "slices" |
| 12 | "text/template" |
| 13 | ) |
| 14 | |
| 15 | // ProfileMetricType metric type used to override default type of the metric |
| 16 | // By default metric type is derived from the type of the SNMP value, for example Counter32/64 -> rate. |
| 17 | type ProfileMetricType string |
| 18 | |
| 19 | const ( |
| 20 | // ProfileMetricTypeGauge is used to create a gauge metric |
| 21 | ProfileMetricTypeGauge ProfileMetricType = "gauge" |
| 22 | |
| 23 | // ProfileMetricTypeMonotonicCount is used to create a monotonic_count metric |
| 24 | ProfileMetricTypeMonotonicCount ProfileMetricType = "monotonic_count" |
| 25 | |
| 26 | // ProfileMetricTypeMonotonicCountAndRate is used to create a monotonic_count and rate metric |
| 27 | ProfileMetricTypeMonotonicCountAndRate ProfileMetricType = "monotonic_count_and_rate" |
| 28 | |
| 29 | // ProfileMetricTypeRate is used to create a rate metric |
| 30 | ProfileMetricTypeRate ProfileMetricType = "rate" |
| 31 | |
| 32 | // ProfileMetricTypeFlagStream is used to create metric based on a value that represent flags |
| 33 | // See details in https://github.com/DataDog/integrations-core/pull/7072 |
| 34 | ProfileMetricTypeFlagStream ProfileMetricType = "flag_stream" |
| 35 | |
| 36 | // ProfileMetricTypeCounter is DEPRECATED |
| 37 | // `counter` is deprecated in favour of `rate` |
| 38 | ProfileMetricTypeCounter ProfileMetricType = "counter" |
| 39 | |
| 40 | // ProfileMetricTypePercent is DEPRECATED |
| 41 | // `percent` is deprecated in favour of `scale_factor` |
| 42 | ProfileMetricTypePercent ProfileMetricType = "percent" |
| 43 | ) |
| 44 | |
| 45 | // MetricsConfig holds configs for a metric |
| 46 | type MetricsConfig struct { |
| 47 | // MIB the MIB used for this metric |
| 48 | MIB string `yaml:"MIB,omitempty" json:"MIB,omitempty"` |
| 49 | |
| 50 | // Symbol configs |
| 51 | Symbol SymbolConfig `yaml:"symbol,omitempty" json:"symbol"` |
| 52 | |
| 53 | // Table the table OID |
| 54 | Table SymbolConfig `yaml:"table,omitempty" json:"table"` |
| 55 | // Table configs |
| 56 | Symbols []SymbolConfig `yaml:"symbols,omitempty" json:"symbols,omitempty"` |
| 57 | |
| 58 | // `static_tags` is not exposed as json at the moment since we need to evaluate if we want to expose it via UI |
| 59 | StaticTags []StaticMetricTagConfig `yaml:"static_tags,omitempty" json:"-"` |
| 60 | MetricTags MetricTagConfigList `yaml:"metric_tags,omitempty" json:"metric_tags,omitempty"` |
| 61 | |
| 62 | Options MetricsConfigOption `yaml:"options,omitempty" json:"options"` |
| 63 | |
| 64 | // DEPRECATED: Use .Symbol instead |
| 65 | OID string `yaml:"OID,omitempty" json:"OID,omitempty" jsonschema:"-"` |
| 66 | // DEPRECATED: Use .Symbol instead |
| 67 | Name string `yaml:"name,omitempty" json:"name,omitempty" jsonschema:"-"` |
| 68 | // DEPRECATED: use Symbol.MetricType instead. |
| 69 | MetricType ProfileMetricType `yaml:"metric_type,omitempty" json:"metric_type,omitempty" jsonschema:"-"` |
| 70 | } |
| 71 | |
| 72 | // Clone duplicates this MetricsConfig |
| 73 | func (m MetricsConfig) Clone() MetricsConfig { |
| 74 | return MetricsConfig{ |
| 75 | MIB: m.MIB, |
| 76 | Table: m.Table.Clone(), |
| 77 | Symbol: m.Symbol.Clone(), |
| 78 | Symbols: cloneSlice(m.Symbols), |
| 79 | StaticTags: slices.Clone(m.StaticTags), |
| 80 | MetricTags: cloneSlice(m.MetricTags), |
| 81 | Options: m.Options, |
| 82 | |
| 83 | OID: m.OID, |
| 84 | Name: m.Name, |
| 85 | MetricType: m.MetricType, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // IsColumn returns true if the metrics config define columns metrics |
| 90 | func (m *MetricsConfig) IsColumn() bool { |
| 91 | return len(m.Symbols) > 0 |
| 92 | } |
| 93 | |
| 94 | // IsScalar returns true if the metrics config define scalar metrics |
| 95 | func (m *MetricsConfig) IsScalar() bool { |
| 96 | return m.Symbol.OID != "" && m.Symbol.Name != "" |
| 97 | } |
| 98 | |
| 99 | // SymbolConfigCompat is used to deserialize string field or SymbolConfig. |
| 100 | // For OID/Name to Symbol harmonization: |
| 101 | // When users declare metric tag like: |
| 102 | // |
| 103 | // metric_tags: |
| 104 | // - OID: 1.2.3 |
| 105 | // symbol: aSymbol |
| 106 | // |
| 107 | // this will lead to OID stored as MetricTagConfig.OID and name stored as MetricTagConfig.Symbol.Name |
| 108 | // When this happens, in validateEnrichMetricTags we harmonize by moving MetricTagConfig.OID to MetricTagConfig.Symbol.OID. |
| 109 | type SymbolConfigCompat SymbolConfig |
| 110 | |
| 111 | // Clone creates a duplicate of this SymbolConfigCompat |
| 112 | func (s SymbolConfigCompat) Clone() SymbolConfigCompat { |
| 113 | return SymbolConfigCompat(SymbolConfig(s).Clone()) |
| 114 | } |
| 115 | |
| 116 | // SymbolConfig holds info for a single symbol/oid |
| 117 | type SymbolConfig struct { |
| 118 | OID string `yaml:"OID,omitempty" json:"OID,omitempty"` |
| 119 | Name string `yaml:"name,omitempty" json:"name,omitempty"` |
| 120 | |
| 121 | ExtractValue string `yaml:"extract_value,omitempty" json:"extract_value,omitempty"` |
| 122 | ExtractValueCompiled *regexp.Regexp `yaml:"-" json:"-"` |
| 123 | |
| 124 | MatchPattern string `yaml:"match_pattern,omitempty" json:"match_pattern,omitempty"` |
| 125 | MatchValue string `yaml:"match_value,omitempty" json:"match_value,omitempty"` |
| 126 | MatchPatternCompiled *regexp.Regexp `yaml:"-" json:"-"` |
| 127 | |
| 128 | ScaleFactor float64 `yaml:"scale_factor,omitempty" json:"scale_factor,omitempty"` |
| 129 | Format string `yaml:"format,omitempty" json:"format,omitempty"` |
| 130 | ConstantValueOne bool `yaml:"constant_value_one,omitempty" json:"constant_value_one,omitempty"` |
| 131 | |
| 132 | // `metric_type` is used for force the metric type |
| 133 | // When empty, by default, the metric type is derived from SNMP OID value type. |
| 134 | // Valid `metric_type` types: `gauge`, `rate`, `monotonic_count`, `monotonic_count_and_rate` |
| 135 | // Deprecated types: `counter` (use `rate` instead), percent (use `scale_factor` instead) |
| 136 | MetricType ProfileMetricType `yaml:"metric_type,omitempty" json:"metric_type,omitempty"` |
| 137 | |
| 138 | ChartMeta ChartMeta `yaml:"chart_meta,omitempty" json:"chart_meta"` |
| 139 | |
| 140 | Mapping MappingConfig `yaml:"mapping,omitempty" json:"mapping"` |
| 141 | Transform string `yaml:"transform,omitempty" json:"transform,omitempty"` |
| 142 | TransformCompiled *template.Template `yaml:"-" json:"-"` |
| 143 | } |
| 144 | |
| 145 | // Clone creates a duplicate of this SymbolConfig |
| 146 | func (s SymbolConfig) Clone() SymbolConfig { |
| 147 | ss := s |
| 148 | ss.Mapping = ss.Mapping.Clone() |
| 149 | return ss |
| 150 | } |
| 151 | |
| 152 | type ChartMeta struct { |
| 153 | Description string `yaml:"description,omitempty" json:"description,omitempty"` |
| 154 | Family string `yaml:"family,omitempty" json:"family,omitempty"` |
| 155 | Unit string `yaml:"unit,omitempty" json:"unit,omitempty"` |
| 156 | Type string `yaml:"type,omitempty" json:"type,omitempty"` |
| 157 | } |
| 158 | |
| 159 | // MetricTagConfig holds metric tag info |
| 160 | type MetricTagConfig struct { |
| 161 | Tag string `yaml:"tag" json:"tag"` |
| 162 | |
| 163 | // Table config |
| 164 | Index uint `yaml:"index,omitempty" json:"index,omitempty"` |
| 165 | |
| 166 | Table string `yaml:"table,omitempty" json:"table,omitempty"` |
| 167 | |
| 168 | // DEPRECATED: Use .Symbol instead |
| 169 | Column SymbolConfig `yaml:"column,omitempty" json:"-"` |
| 170 | |
| 171 | // DEPRECATED: use .Symbol instead |
| 172 | OID string `yaml:"OID,omitempty" json:"-" jsonschema:"-"` |
| 173 | // Symbol records the OID to be parsed. Note that .Symbol.Name is ignored: |
| 174 | // set .Tag to specify the tag name. If a serialized Symbol is a string |
| 175 | // instead of an object, it will be treated like {name: <value>}; this use |
| 176 | // pattern is deprecated |
| 177 | Symbol SymbolConfigCompat `yaml:"symbol,omitempty" json:"symbol"` |
| 178 | |
| 179 | // LookupSymbol optionally resolves cross-table tags by matching a value from the |
| 180 | // current row index against a column in the referenced table, then reading Symbol |
| 181 | // from the matched row in that table. |
| 182 | LookupSymbol SymbolConfigCompat `yaml:"lookup_symbol,omitempty" json:"lookup_symbol"` |
| 183 | |
| 184 | IndexTransform []MetricIndexTransform `yaml:"index_transform,omitempty" json:"index_transform,omitempty"` |
| 185 | |
| 186 | MappingRef string `yaml:"mapping_ref,omitempty" json:"mapping_ref,omitempty"` |
| 187 | Mapping MappingConfig `yaml:"mapping,omitempty" json:"mapping"` |
| 188 | |
| 189 | // Regex |
| 190 | // Match/Tags are not exposed as json (UI) since ExtractValue can be used instead |
| 191 | Match string `yaml:"match,omitempty" json:"-"` |
| 192 | Tags map[string]string `yaml:"tags,omitempty" json:"-"` |
| 193 | Pattern *regexp.Regexp `yaml:"-" json:"-"` |
| 194 | |
| 195 | SymbolTag string `yaml:"-" json:"-"` |
| 196 | } |
| 197 | |
| 198 | // Clone duplicates this MetricTagConfig |
| 199 | func (m MetricTagConfig) Clone() MetricTagConfig { |
| 200 | m2 := m // non-pointer assignment shallow-copies members |
| 201 | // deep copy symbols and structures |
| 202 | m2.Column = m.Column.Clone() |
| 203 | m2.Symbol = m.Symbol.Clone() |
| 204 | m2.LookupSymbol = m.LookupSymbol.Clone() |
| 205 | m2.IndexTransform = slices.Clone(m.IndexTransform) |
| 206 | m2.Mapping = m.Mapping.Clone() |
| 207 | m2.Tags = maps.Clone(m.Tags) |
| 208 | return m2 |
| 209 | } |
| 210 | |
| 211 | type GlobalMetricTagConfig struct { |
| 212 | MetricTagConfig `yaml:",inline" json:",inline"` |
| 213 | Consumers ConsumerSet `yaml:"consumers,omitempty" json:"consumers,omitempty"` |
| 214 | } |
| 215 | |
| 216 | func (m GlobalMetricTagConfig) Clone() GlobalMetricTagConfig { |
| 217 | return GlobalMetricTagConfig{ |
| 218 | MetricTagConfig: m.MetricTagConfig.Clone(), |
| 219 | Consumers: m.Consumers.Clone(), |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | type StaticMetricTagConfig struct { |
| 224 | Tag string `yaml:"tag" json:"tag"` |
| 225 | Value string `yaml:"value" json:"value"` |
| 226 | } |
| 227 | |
| 228 | func (s StaticMetricTagConfig) Clone() StaticMetricTagConfig { |
| 229 | return StaticMetricTagConfig{ |
| 230 | Tag: s.Tag, |
| 231 | Value: s.Value, |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // MetricTagConfigList holds configs for a list of metric tags |
| 236 | type MetricTagConfigList []MetricTagConfig |
| 237 | |
| 238 | // MetricIndexTransform holds configs for metric index transform |
| 239 | type MetricIndexTransform struct { |
| 240 | Start uint `yaml:"start" json:"start"` |
| 241 | End uint `yaml:"end" json:"end"` |
| 242 | DropRight uint `yaml:"drop_right,omitempty" json:"drop_right,omitempty"` |
| 243 | } |
| 244 | |
| 245 | // MetricsConfigOption holds config for metrics options |
| 246 | type MetricsConfigOption struct { |
| 247 | Placement uint `yaml:"placement,omitempty" json:"placement,omitempty"` |
| 248 | MetricSuffix string `yaml:"metric_suffix,omitempty" json:"metric_suffix,omitempty"` |
| 249 | } |