Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
Move all code except for main code into pkg directory.
  • Loading branch information
janboll committed Sep 20, 2022
1 parent f3336e9 commit be46e3d
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 142 deletions.
108 changes: 9 additions & 99 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"errors"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/app-sre/aws-resource-exporter/pkg"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/go-kit/kit/log/level"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -33,104 +31,15 @@ const (
var (
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()

exporterMetrics *ExporterMetrics
)

func main() {
os.Exit(run())
}

type BaseConfig struct {
Enabled bool `yaml:"enabled"`
Interval *time.Duration `yaml:"interval"`
CacheTTL *time.Duration `yaml:"cache_ttl"`
}

type RDSConfig struct {
BaseConfig `yaml:"base,inline"`
Regions []string `yaml:"regions"`
}

type VPCConfig struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Regions []string `yaml:"regions"`
}

type Route53Config struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Region string `yaml:"region"` // Use only a single Region for now, as the current metric is global
}

type EC2Config struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Regions []string `yaml:"regions"`
}

type Config struct {
RdsConfig RDSConfig `yaml:"rds"`
VpcConfig VPCConfig `yaml:"vpc"`
Route53Config Route53Config `yaml:"route53"`
EC2Config EC2Config `yaml:"ec2"`
}

func durationPtr(duration time.Duration) *time.Duration {
return &duration
}

func loadExporterConfiguration(logger log.Logger, configFile string) (*Config, error) {
var config Config
file, err := ioutil.ReadFile(configFile)
if err != nil {
level.Error(logger).Log("Could not load configuration file")
return nil, errors.New("Could not load configuration file: " + configFile)
}
yaml.Unmarshal(file, &config)

if config.RdsConfig.CacheTTL == nil {
config.RdsConfig.CacheTTL = durationPtr(35 * time.Second)
}
if config.VpcConfig.CacheTTL == nil {
config.VpcConfig.CacheTTL = durationPtr(35 * time.Second)
}
if config.Route53Config.CacheTTL == nil {
config.Route53Config.CacheTTL = durationPtr(35 * time.Second)
}
if config.EC2Config.CacheTTL == nil {
config.EC2Config.CacheTTL = durationPtr(35 * time.Second)
}

if config.RdsConfig.Interval == nil {
config.RdsConfig.Interval = durationPtr(15 * time.Second)
}
if config.VpcConfig.Interval == nil {
config.VpcConfig.Interval = durationPtr(15 * time.Second)
}
if config.Route53Config.Interval == nil {
config.Route53Config.Interval = durationPtr(15 * time.Second)
}
if config.EC2Config.Interval == nil {
config.EC2Config.Interval = durationPtr(15 * time.Second)
}

if config.VpcConfig.Timeout == nil {
config.VpcConfig.Timeout = durationPtr(10 * time.Second)
}
if config.Route53Config.Timeout == nil {
config.Route53Config.Timeout = durationPtr(10 * time.Second)
}
if config.EC2Config.Timeout == nil {
config.EC2Config.Timeout = durationPtr(10 * time.Second)
}
return &config, nil
}

func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collector, error) {
var collectors []prometheus.Collector
config, err := loadExporterConfiguration(logger, configFile)
config, err := pkg.LoadExporterConfiguration(logger, configFile)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +55,7 @@ func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collect
sess := session.Must(session.NewSession(config))
vpcSessions = append(vpcSessions, sess)
}
vpcExporter := NewVPCExporter(vpcSessions, logger, config.VpcConfig)
vpcExporter := pkg.NewVPCExporter(vpcSessions, logger, config.VpcConfig)
collectors = append(collectors, vpcExporter)
go vpcExporter.CollectLoop()
}
Expand All @@ -158,7 +67,7 @@ func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collect
sess := session.Must(session.NewSession(config))
rdsSessions = append(rdsSessions, sess)
}
rdsExporter := NewRDSExporter(rdsSessions, logger, config.RdsConfig)
rdsExporter := pkg.NewRDSExporter(rdsSessions, logger, config.RdsConfig)
collectors = append(collectors, rdsExporter)
go rdsExporter.CollectLoop()
}
Expand All @@ -170,15 +79,15 @@ func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collect
sess := session.Must(session.NewSession(config))
ec2Sessions = append(ec2Sessions, sess)
}
ec2Exporter := NewEC2Exporter(ec2Sessions, logger, config.EC2Config)
ec2Exporter := pkg.NewEC2Exporter(ec2Sessions, logger, config.EC2Config)
collectors = append(collectors, ec2Exporter)
go ec2Exporter.CollectLoop()
}
level.Info(logger).Log("msg", "Will Route53 metrics be gathered?", "route53-enabled", config.Route53Config.Enabled)
if config.Route53Config.Enabled {
awsConfig := aws.NewConfig().WithRegion(config.Route53Config.Region)
sess := session.Must(session.NewSession(awsConfig))
r53Exporter := NewRoute53Exporter(sess, logger, config.Route53Config)
r53Exporter := pkg.NewRoute53Exporter(sess, logger, config.Route53Config)
collectors = append(collectors, r53Exporter)
go r53Exporter.CollectLoop()
}
Expand All @@ -197,7 +106,8 @@ func run() int {
level.Info(logger).Log("msg", "Starting"+namespace, "version", version.Info())
level.Info(logger).Log("msg", "Build context", version.BuildContext())

exporterMetrics = NewExporterMetrics()
pkg.AwsExporterMetrics = pkg.NewExporterMetrics()

var configFile string
if path := os.Getenv("AWS_RESOURCE_EXPORTER_CONFIG_FILE"); path != "" {
configFile = path
Expand All @@ -209,7 +119,7 @@ func run() int {
level.Error(logger).Log("msg", "Could not load configuration file", "err", err)
return 1
}
collectors := append(cs, exporterMetrics)
collectors := append(cs, pkg.AwsExporterMetrics)
prometheus.MustRegister(
collectors...,
)
Expand Down
94 changes: 94 additions & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package pkg

import (
"errors"
"io/ioutil"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"gopkg.in/yaml.v2"
)

type BaseConfig struct {
Enabled bool `yaml:"enabled"`
Interval *time.Duration `yaml:"interval"`
CacheTTL *time.Duration `yaml:"cache_ttl"`
}

type RDSConfig struct {
BaseConfig `yaml:"base,inline"`
Regions []string `yaml:"regions"`
}

type VPCConfig struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Regions []string `yaml:"regions"`
}

type Route53Config struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Region string `yaml:"region"` // Use only a single Region for now, as the current metric is global
}

type EC2Config struct {
BaseConfig `yaml:"base,inline"`
Timeout *time.Duration `yaml:"timeout"`
Regions []string `yaml:"regions"`
}

type Config struct {
RdsConfig RDSConfig `yaml:"rds"`
VpcConfig VPCConfig `yaml:"vpc"`
Route53Config Route53Config `yaml:"route53"`
EC2Config EC2Config `yaml:"ec2"`
}

func LoadExporterConfiguration(logger log.Logger, configFile string) (*Config, error) {
var config Config
file, err := ioutil.ReadFile(configFile)
if err != nil {
level.Error(logger).Log("Could not load configuration file")
return nil, errors.New("Could not load configuration file: " + configFile)
}
yaml.Unmarshal(file, &config)

if config.RdsConfig.CacheTTL == nil {
config.RdsConfig.CacheTTL = durationPtr(35 * time.Second)
}
if config.VpcConfig.CacheTTL == nil {
config.VpcConfig.CacheTTL = durationPtr(35 * time.Second)
}
if config.Route53Config.CacheTTL == nil {
config.Route53Config.CacheTTL = durationPtr(35 * time.Second)
}
if config.EC2Config.CacheTTL == nil {
config.EC2Config.CacheTTL = durationPtr(35 * time.Second)
}

if config.RdsConfig.Interval == nil {
config.RdsConfig.Interval = durationPtr(15 * time.Second)
}
if config.VpcConfig.Interval == nil {
config.VpcConfig.Interval = durationPtr(15 * time.Second)
}
if config.Route53Config.Interval == nil {
config.Route53Config.Interval = durationPtr(15 * time.Second)
}
if config.EC2Config.Interval == nil {
config.EC2Config.Interval = durationPtr(15 * time.Second)
}

if config.VpcConfig.Timeout == nil {
config.VpcConfig.Timeout = durationPtr(10 * time.Second)
}
if config.Route53Config.Timeout == nil {
config.Route53Config.Timeout = durationPtr(10 * time.Second)
}
if config.EC2Config.Timeout == nil {
config.EC2Config.Timeout = durationPtr(10 * time.Second)
}
return &config, nil
}
5 changes: 5 additions & 0 deletions pkg/constats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pkg

const (
namespace = "aws_resources_exporter"
)
11 changes: 5 additions & 6 deletions ec2.go → pkg/ec2.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main
package pkg

import (
"context"
"sync"
"time"

"github.com/app-sre/aws-resource-exporter/pkg"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -25,7 +24,7 @@ var TransitGatewaysUsage *prometheus.Desc = prometheus.NewDesc(prometheus.BuildF

type EC2Exporter struct {
sessions []*session.Session
cache pkg.MetricsCache
cache MetricsCache

logger log.Logger
timeout time.Duration
Expand All @@ -37,7 +36,7 @@ func NewEC2Exporter(sessions []*session.Session, logger log.Logger, config EC2Co
level.Info(logger).Log("msg", "Initializing EC2 exporter")
return &EC2Exporter{
sessions: sessions,
cache: *pkg.NewMetricsCache(*config.CacheTTL),
cache: *NewMetricsCache(*config.CacheTTL),

logger: logger,
timeout: *config.Timeout,
Expand Down Expand Up @@ -77,14 +76,14 @@ func (e *EC2Exporter) collectInRegion(sess *session.Session, logger log.Logger,
quota, err := getQuotaValueWithContext(serviceQuotaSvc, ec2ServiceCode, transitGatewayPerAccountQuotaCode, ctx)
if err != nil {
level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error())
exporterMetrics.IncrementErrors()
AwsExporterMetrics.IncrementErrors()
return
}

gateways, err := getAllTransitGatewaysWithContext(ec2Svc, ctx)
if err != nil {
level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error())
exporterMetrics.IncrementErrors()
AwsExporterMetrics.IncrementErrors()
return
}

Expand Down
4 changes: 3 additions & 1 deletion exporter.go → pkg/exporter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pkg

import (
"sync"
Expand All @@ -7,6 +7,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

var AwsExporterMetrics *ExporterMetrics

// ExporterMetrics defines an instance of the exporter metrics
type ExporterMetrics struct {
sess *session.Session
Expand Down
2 changes: 1 addition & 1 deletion proxy.go → pkg/proxy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pkg

import (
"errors"
Expand Down
Loading

0 comments on commit be46e3d

Please sign in to comment.