|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | +) |
| 22 | + |
| 23 | +// Metrics is the top level configuration object. |
| 24 | +type Metrics struct { |
| 25 | + Spec MetricsSpec `yaml:"spec" json:"spec"` |
| 26 | +} |
| 27 | + |
| 28 | +// MetricsSpec is the configuration describing the custom resource state metrics to generate. |
| 29 | +type MetricsSpec struct { |
| 30 | + // Resources is the list of custom resources to be monitored. A resource with the same GroupVersionKind may appear |
| 31 | + // multiple times (e.g., to customize the namespace or subsystem,) but will incur additional overhead. |
| 32 | + Resources []Resource `yaml:"resources" json:"resources"` |
| 33 | +} |
| 34 | + |
| 35 | +// Resource configures a custom resource for metric generation. |
| 36 | +type Resource struct { |
| 37 | + // MetricNamePrefix defines a prefix for all metrics of the resource. |
| 38 | + // If set to "", no prefix will be added. |
| 39 | + // Example: If set to "foo", MetricNamePrefix will be "foo_<metric>". |
| 40 | + MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"` |
| 41 | + |
| 42 | + // GroupVersionKind of the custom resource to be monitored. |
| 43 | + GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"` |
| 44 | + |
| 45 | + // Labels are added to all metrics. If the same key is used in a metric, the value from the metric will overwrite the value here. |
| 46 | + Labels `yaml:",inline" json:",inline"` |
| 47 | + |
| 48 | + // Metrics are the custom resource fields to be collected. |
| 49 | + Metrics []Generator `yaml:"metrics" json:"metrics"` |
| 50 | + // ErrorLogV defines the verbosity threshold for errors logged for this resource. |
| 51 | + ErrorLogV int32 `yaml:"errorLogV" json:"errorLogV"` |
| 52 | + |
| 53 | + // ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize. |
| 54 | + ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"` |
| 55 | +} |
| 56 | + |
| 57 | +// GroupVersionKind is the Kubernetes group, version, and kind of a resource. |
| 58 | +type GroupVersionKind struct { |
| 59 | + Group string `yaml:"group" json:"group"` |
| 60 | + Version string `yaml:"version" json:"version"` |
| 61 | + Kind string `yaml:"kind" json:"kind"` |
| 62 | +} |
| 63 | + |
| 64 | +func (gvk GroupVersionKind) String() string { |
| 65 | + return fmt.Sprintf("%s_%s_%s", gvk.Group, gvk.Version, gvk.Kind) |
| 66 | +} |
| 67 | + |
| 68 | +// Labels is common configuration of labels to add to metrics. |
| 69 | +type Labels struct { |
| 70 | + // CommonLabels are added to all metrics. |
| 71 | + CommonLabels map[string]string `yaml:"commonLabels,omitempty" json:"commonLabels,omitempty"` |
| 72 | + // LabelsFromPath adds additional labels where the value is taken from a field in the resource. |
| 73 | + LabelsFromPath map[string][]string `yaml:"labelsFromPath,omitempty" json:"labelsFromPath,omitempty"` |
| 74 | +} |
| 75 | + |
| 76 | +// Generator describes a unique metric name. |
| 77 | +type Generator struct { |
| 78 | + // Name of the metric. Subject to prefixing based on the configuration of the Resource. |
| 79 | + Name string `yaml:"name" json:"name"` |
| 80 | + // Help text for the metric. |
| 81 | + Help string `yaml:"help" json:"help"` |
| 82 | + // Each targets a value or values from the resource. |
| 83 | + Each Metric `yaml:"each" json:"each"` |
| 84 | + |
| 85 | + // Labels are added to all metrics. Labels from Each will overwrite these if using the same key. |
| 86 | + Labels `yaml:",inline" json:",inline"` // json will inline because it is already tagged |
| 87 | + // ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting. |
| 88 | + ErrorLogV int32 `yaml:"errorLogV,omitempty" json:"errorLogV,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// Metric defines a metric to expose. |
| 92 | +// +union |
| 93 | +type Metric struct { |
| 94 | + // Type defines the type of the metric. |
| 95 | + // +unionDiscriminator |
| 96 | + Type MetricType `yaml:"type" json:"type"` |
| 97 | + |
| 98 | + // Gauge defines a gauge metric. |
| 99 | + // +optional |
| 100 | + Gauge *MetricGauge `yaml:"gauge,omitempty" json:"gauge,omitempty"` |
| 101 | + // StateSet defines a state set metric. |
| 102 | + // +optional |
| 103 | + StateSet *MetricStateSet `yaml:"stateSet,omitempty" json:"stateSet,omitempty"` |
| 104 | + // Info defines an info metric. |
| 105 | + // +optional |
| 106 | + Info *MetricInfo `yaml:"info,omitempty" json:"info,omitempty"` |
| 107 | +} |
| 108 | + |
| 109 | +// ConfigDecoder is for use with FromConfig. |
| 110 | +type ConfigDecoder interface { |
| 111 | + Decode(v interface{}) (err error) |
| 112 | +} |
0 commit comments