github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/prometheus/config/config.go (about)

     1  // Copyright 2023 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  
    21  	"gopkg.in/yaml.v3"
    22  )
    23  
    24  type Metric struct {
    25  	Name     string   `yaml:"name"`
    26  	Category string   `yaml:"category"`
    27  	Gadget   string   `yaml:"gadget"`
    28  	Type     string   `yaml:"type"`
    29  	Field    string   `yaml:"field,omitempty"`
    30  	Labels   []string `yaml:"labels,omitempty"`
    31  	Selector []string `yaml:"selector,omitempty"`
    32  	Bucket   Bucket   `yaml:"bucket,omitempty"`
    33  }
    34  
    35  type Config struct {
    36  	MetricsName string   `yaml:"metrics_name"`
    37  	Metrics     []Metric `yaml:"metrics"`
    38  }
    39  
    40  type Bucket struct {
    41  	Unit       string  `yaml:"unit"`
    42  	Type       string  `yaml:"type"`
    43  	Min        int     `yaml:"min"`
    44  	Max        int     `yaml:"max"`
    45  	Multiplier float64 `yaml:"multiplier"`
    46  }
    47  
    48  func ParseConfig(configBytes []byte) (*Config, error) {
    49  	config := &Config{}
    50  	if err := yaml.Unmarshal(configBytes, config); err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	if config.MetricsName == "" {
    55  		return nil, errors.New("metrics_name is missing")
    56  	}
    57  
    58  	if config.Metrics == nil {
    59  		return nil, errors.New("metrics section is missing")
    60  	}
    61  
    62  	for _, metric := range config.Metrics {
    63  		if metric.Name == "" {
    64  			return nil, errors.New("metric name is missing")
    65  		}
    66  
    67  		if metric.Category == "" {
    68  			return nil, fmt.Errorf("metric category is missing in %q", metric.Name)
    69  		}
    70  
    71  		if metric.Gadget == "" {
    72  			return nil, fmt.Errorf("metric gadget is missing in %q", metric.Name)
    73  		}
    74  
    75  		if metric.Type == "" {
    76  			return nil, fmt.Errorf("metric type is missing in %q", metric.Name)
    77  		}
    78  	}
    79  
    80  	return config, nil
    81  }