json_exporter/config/config.go
ngrebels ca1304003c
Support Value Conversions (#172)
* Bump k8s.io/client-go from 0.24.2 to 0.24.3 (#171)

Bumps [k8s.io/client-go](https://github.com/kubernetes/client-go) from 0.24.2 to 0.24.3.
- [Release notes](https://github.com/kubernetes/client-go/releases)
- [Changelog](https://github.com/kubernetes/client-go/blob/master/CHANGELOG.md)
- [Commits](https://github.com/kubernetes/client-go/compare/v0.24.2...v0.24.3)

---
updated-dependencies:
- dependency-name: k8s.io/client-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* Bump github.com/prometheus/common from 0.35.0 to 0.37.0 (#170)

Bumps [github.com/prometheus/common](https://github.com/prometheus/common) from 0.35.0 to 0.37.0.
- [Release notes](https://github.com/prometheus/common/releases)
- [Commits](https://github.com/prometheus/common/compare/v0.35.0...v0.37.0)

---
updated-dependencies:
- dependency-name: github.com/prometheus/common
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* Added a value converter for dynamic values and associated tests

Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* Refactored into functions and created a type

Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* value converter: added example

Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* Remove underscore from variable name

Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

* Fix formatting error from merging

Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>

Signed-off-by: ngrebels <ngrebels@mathworks.com>
Signed-off-by: Yao Hong Kok <yaokok@mathworks.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yao Hong Kok <yaokok@mathworks.com>
2022-10-05 18:32:05 +02:00

100 lines
2.6 KiB
Go

// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"io/ioutil"
pconfig "github.com/prometheus/common/config"
"gopkg.in/yaml.v2"
)
// Metric contains values that define a metric
type Metric struct {
Name string
Path string
Labels map[string]string
Type ScrapeType
ValueType ValueType
EpochTimestamp string
Help string
Values map[string]string
ValueConverter ValueConverterType
}
type ValueConverterType map[string]map[string]string
type ScrapeType string
const (
ValueScrape ScrapeType = "value" // default
ObjectScrape ScrapeType = "object"
)
type ValueType string
const (
ValueTypeGauge ValueType = "gauge"
ValueTypeCounter ValueType = "counter"
ValueTypeUntyped ValueType = "untyped"
)
// Config contains multiple modules.
type Config struct {
Modules map[string]Module `yaml:"modules"`
}
// Module contains metrics and headers defining a configuration
type Module struct {
Headers map[string]string `yaml:"headers,omitempty"`
Metrics []Metric `yaml:"metrics"`
HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,omitempty"`
Body Body `yaml:"body,omitempty"`
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
}
type Body struct {
Content string `yaml:"content"`
Templatize bool `yaml:"templatize,omitempty"`
}
func LoadConfig(configPath string) (Config, error) {
var config Config
data, err := ioutil.ReadFile(configPath)
if err != nil {
return config, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return config, err
}
// Complete Defaults
for _, module := range config.Modules {
for i := 0; i < len(module.Metrics); i++ {
if module.Metrics[i].Type == "" {
module.Metrics[i].Type = ValueScrape
}
if module.Metrics[i].Help == "" {
module.Metrics[i].Help = module.Metrics[i].Name
}
if module.Metrics[i].ValueType == "" {
module.Metrics[i].ValueType = ValueTypeUntyped
}
}
}
return config, nil
}