diff --git a/collector/collector.go b/collector/collector.go index 0155fc6..f237894 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -20,7 +20,6 @@ import ( "sync" "time" - "github.com/BurntSushi/toml" _ "github.com/godror/godror" "github.com/prometheus/client_golang/prometheus" ) @@ -59,7 +58,7 @@ func NewExporter(logger *slog.Logger, m *MetricsConfiguration) *Exporter { // set to a blank value. for _, dbconfig := range m.Databases { for label, _ := range dbconfig.Labels { - if (!slices.Contains(allConstLabels, label)) { + if !slices.Contains(allConstLabels, label) { allConstLabels = append(allConstLabels, label) } } @@ -400,12 +399,14 @@ func (e *Exporter) reloadMetrics() { if len(e.CustomMetricsFiles()) > 0 { for _, _customMetrics := range e.CustomMetricsFiles() { metrics := &Metrics{} - if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil { + + if err := loadMetricsConfig(_customMetrics, metrics); err != nil { e.logger.Error("failed to load custom metrics", "error", err) panic(errors.New("Error while loading " + _customMetrics)) } else { e.logger.Info("Successfully loaded custom metrics from " + _customMetrics) } + e.metricsToScrape.Metric = append(e.metricsToScrape.Metric, metrics.Metric...) } } else { diff --git a/collector/data_loader.go b/collector/data_loader.go new file mode 100644 index 0000000..f6a5a38 --- /dev/null +++ b/collector/data_loader.go @@ -0,0 +1,44 @@ +// Copyright (c) 2025, Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. + +package collector + +import ( + "fmt" + "os" + "strings" + + "github.com/BurntSushi/toml" + "gopkg.in/yaml.v2" +) + +func loadYamlMetricsConfig(_metricsFileName string, metrics *Metrics) error { + yamlBytes, err := os.ReadFile(_metricsFileName) + if err != nil { + return fmt.Errorf("cannot read the metrics config %s: %w", _metricsFileName, err) + } + if err := yaml.Unmarshal(yamlBytes, metrics); err != nil { + return fmt.Errorf("cannot unmarshal the metrics config %s: %w", _metricsFileName, err) + } + return nil +} + +func loadTomlMetricsConfig(_customMetrics string, metrics *Metrics) error { + if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil { + return fmt.Errorf("cannot read the metrics config %s: %w", _customMetrics, err) + } + return nil +} + +func loadMetricsConfig(_customMetrics string, metrics *Metrics) error { + if strings.HasSuffix(_customMetrics, "toml") { + if err := loadTomlMetricsConfig(_customMetrics, metrics); err != nil { + return fmt.Errorf("cannot load toml based metrics: %w", err) + } + } else { + if err := loadYamlMetricsConfig(_customMetrics, metrics); err != nil { + return fmt.Errorf("cannot load yaml based metrics: %w", err) + } + } + return nil +} diff --git a/collector/default_metrics.go b/collector/default_metrics.go index 34bd131..5e5111b 100644 --- a/collector/default_metrics.go +++ b/collector/default_metrics.go @@ -20,7 +20,7 @@ var defaultMetricsToml string func (e *Exporter) DefaultMetrics() Metrics { var metricsToScrape Metrics if e.Metrics.Default != "" { - if _, err := toml.DecodeFile(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil { + if err := loadMetricsConfig(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil { e.logger.Error(fmt.Sprintf("there was an issue while loading specified default metrics file at: "+e.Metrics.Default+", proceeding to run with default metrics."), "error", err) } diff --git a/go.mod b/go.mod index 6857935..98a5344 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/golang-jwt/jwt/v5 v5.2.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect - github.com/klauspost/compress v1.18.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/mdlayher/socket v0.4.1 // indirect github.com/mdlayher/vsock v1.2.1 // indirect