Skip to content

Commit

Permalink
Lint and gofmt checks (#24)
Browse files Browse the repository at this point in the history
* Added lint and gofmt build rules

* Lint and gofmt fixes and some very minor refactoring

* Added Circle job for linting and gofmt

* Quickfix: get plz before running tests

* Fix the working dir

* Quickfix: remove creating symlink

* Source ~/.profile before running plz

* Fix the PATH when running plz

* Fix the PATH when running plz - second attempt

* Make a symlink for go

* Removed the unused build_and_test job
  • Loading branch information
idgenchev authored Jul 20, 2021
1 parent 24b0c21 commit 5e21c7f
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 29 deletions.
25 changes: 16 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
# Check https://circleci.com/docs/2.0/ for more details
version: 2
jobs:
build_and_test:
working_directory: ~/aws-service-quotas-exporter
machine: true
test:
docker:
- image: circleci/golang:1.14
working_directory: /go/github.com/thought-machine/aws-service-quotas-exporter
steps:
- checkout
- run: sudo ln -s /usr/local/go/bin/go /usr/local/bin/go
- run: curl https://get.please.build | sh
- run: go get -u golang.org/x/lint/golint
- run:
name: Build Docker Image
command: docker build -f build/Dockerfile-builder . --rm=false -t thoughtmachine/aws-service-quotas-exporter:"$CIRCLE_SHA1"
name: "Lint"
command: source ~/.profile && plz run parallel --include=lint --show_all_output
- run:
name: "gofmt check"
command: source ~/.profile && plz run parallel --include=gofmt --show_all_output
- run:
name: "Test"
command: source ~/.profile && plz test ./... --show_all_output
build_and_release:
working_directory: ~/
machine: true
Expand Down Expand Up @@ -64,7 +74,4 @@ workflows:
ignore: /.*/
test-buildable:
jobs:
- build_and_test:
filters:
branches:
ignore: master
- test
14 changes: 14 additions & 0 deletions cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ go_binary(
"//third_party/go:go-flags",
],
)

sh_cmd(
name = "lint",
cmd = "golint -set_exit_status $SRCS",
srcs = glob(["*.go"]),
labels = ["lint"]
)

sh_cmd(
name = "gofmt",
cmd = "[ -z \"$(gofmt -l $SRCS)\" ] && exit 0 || exit 1",
srcs = glob(["*.go"]),
labels = ["gofmt"],
)
14 changes: 14 additions & 0 deletions pkg/service_exporter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ go_test(
"//third_party/go:testify",
],
)

sh_cmd(
name = "lint",
cmd = "golint -set_exit_status $SRCS",
srcs = glob(["*.go"]),
labels = ["lint"]
)

sh_cmd(
name = "gofmt",
cmd = "[ -z \"$(gofmt -l $SRCS)\" ] && exit 0 || exit 1",
srcs = glob(["*.go"]),
labels = ["gofmt"],
)
16 changes: 6 additions & 10 deletions pkg/service_exporter/service_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewServiceQuotasExporter(region, profile string, refreshPeriod int, include
waitForMetrics: ch,
includedAWSTags: includedAWSTags,
}
go exporter.createQuotasAndDescriptions(false)
go exporter.createOrUpdateQuotasAndDescriptions(false)
go exporter.refreshMetrics()

return exporter, nil
Expand All @@ -63,15 +63,11 @@ func (e *ServiceQuotasExporter) refreshMetrics() {

for {
time.Sleep(time.Duration(e.refreshPeriod) * time.Second)
e.updateMetrics()
e.createOrUpdateQuotasAndDescriptions(true)
}
}

func (e *ServiceQuotasExporter) updateMetrics() {
e.createQuotasAndDescriptions(true)
}

func (e *ServiceQuotasExporter) createQuotasAndDescriptions(refresh bool) {
func (e *ServiceQuotasExporter) createOrUpdateQuotasAndDescriptions(update bool) {
quotas, err := e.quotasClient.QuotasAndUsage()
if err != nil {
log.Fatalf("Could not retrieve quotas and limits: %s", err)
Expand All @@ -91,9 +87,9 @@ func (e *ServiceQuotasExporter) createQuotasAndDescriptions(refresh bool) {
labelValues = append(labelValues, quota.Tags[prometheusFormatTag])
}

if refresh {
if update {
if resourceMetric, ok := e.metrics[key]; ok {
log.Infof("Refreshing metrics for resource (%s)", resourceID)
log.Infof("Updating metrics for resource (%s)", resourceID)
resourceMetric.usage = quota.Usage
resourceMetric.limit = quota.Quota
resourceMetric.labelValues = labelValues
Expand All @@ -116,7 +112,7 @@ func (e *ServiceQuotasExporter) createQuotasAndDescriptions(refresh bool) {
}
}

if !refresh {
if !update {
close(e.waitForMetrics)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/service_exporter/service_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestUpdateMetrics(t *testing.T) {
refreshPeriod: 360,
}

exporter.updateMetrics()
exporter.createOrUpdateQuotasAndDescriptions(true)

expectedMetrics := map[string]Metric{
"i-asdasd1": Metric{usage: 5, limit: 10, labelValues: []string{"i-asdasd1", "dummy-value"}},
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestCreateQuotasAndDescriptions(t *testing.T) {
includedAWSTags: []string{"dummy-tag", "dummy-tag2"},
}

exporter.createQuotasAndDescriptions(false)
exporter.createOrUpdateQuotasAndDescriptions(false)

firstUsageDesc := newDesc(region, firstQ.Name, "used_total", "Used amount of desc1", []string{"resource", "dummy_tag", "dummy_tag2"})
firstLimitDesc := newDesc(region, firstQ.Name, "limit_total", "Limit of desc1", []string{"resource", "dummy_tag", "dummy_tag2"})
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestCreateQuotasAndDescriptionsRefresh(t *testing.T) {
refreshPeriod: 360,
}

exporter.updateMetrics()
exporter.createOrUpdateQuotasAndDescriptions(true)

expectedMetrics := map[string]Metric{
"i-asdasd1": Metric{usage: 5, limit: 10, labelValues: []string{"i-asdasd1", "dummy-value"}, usageDesc: desc},
Expand Down
14 changes: 14 additions & 0 deletions pkg/service_quotas/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ go_test(
"//third_party/go:testify",
],
)

sh_cmd(
name = "lint",
cmd = "golint -set_exit_status $SRCS",
srcs = glob(["*.go"]),
labels = ["lint"]
)

sh_cmd(
name = "gofmt",
cmd = "[ -z \"$(gofmt -l $SRCS)\" ] && exit 0 || exit 1",
srcs = glob(["*.go"]),
labels = ["gofmt"],
)
2 changes: 1 addition & 1 deletion pkg/service_quotas/asg_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *ASGUsageCheck) Usage() ([]QuotaUsage, error) {
numRunningInstances := 0
for _, instance := range asg.Instances {
if isRunning(instance) {
numRunningInstances += 1
numRunningInstances++
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/service_quotas/ec2_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (c *RulesPerSecurityGroupUsageCheck) Usage() ([]QuotaUsage, error) {
func(page *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool {
if page != nil {
for _, group := range page.SecurityGroups {
var inboundRules int = 0
var outboundRules int = 0
var inboundRules int
var outboundRules int

tags := ec2TagsToQuotaUsageTags(group.Tags)

Expand All @@ -66,7 +66,7 @@ func (c *RulesPerSecurityGroupUsageCheck) Usage() ([]QuotaUsage, error) {
ResourceName: group.GroupId,
Description: inboundRulesPerSecGrpDesc,
Usage: float64(inboundRules),
Tags: tags,
Tags: tags,
}

for _, rule := range group.IpPermissionsEgress {
Expand All @@ -79,7 +79,7 @@ func (c *RulesPerSecurityGroupUsageCheck) Usage() ([]QuotaUsage, error) {
ResourceName: group.GroupId,
Description: outboundRulesPerSecGrpDesc,
Usage: float64(outboundRules),
Tags: tags,
Tags: tags,
}

quotaUsages = append(quotaUsages, []QuotaUsage{inboundUsage, outboundUsage}...)
Expand Down
5 changes: 3 additions & 2 deletions pkg/service_quotas/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"strings"
)


var invalidLabelCharactersRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

// ToPrometheusNamingFormat modifies string `s` to conform with the Prom naming
// conventions
func ToPrometheusNamingFormat(s string) string {
return toSnakeCase(invalidLabelCharactersRE.ReplaceAllString(s, "_"))
}
Expand Down

0 comments on commit 5e21c7f

Please sign in to comment.