Skip to content

Commit

Permalink
Merge pull request #179 from heroku/update-deps
Browse files Browse the repository at this point in the history
Updating dependencies & some misc cleanup work

- [ ] Update to a more recent go version
- [ ] Remove librato provider support
- [ ] Migrate to github actions
  - [ ] unit tests
  - [ ] code coverage step
  - [ ] linting
  - [ ] validating gomod
- [ ] Makefile cleanup
  • Loading branch information
slizco committed Apr 4, 2023
2 parents 280e8db + 4e8743f commit 38eb879
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 2,513 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: ci

on: [ push ]

jobs:
ci:
runs-on: [ ubuntu-latest ]
strategy:
matrix:
target: [ test, lint ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
- name: run ${{ matrix.target }}
run: make ${{ matrix.target }}
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ issues:
- path: cmd/s3env/main.go
linters:
- gochecknoinits # TODO: FixMe
- path: go-kit/metrics/provider/librato/librato_test.go
linters:
- gocyclo # TODO: Refactor TestLibratoHistogramJSONMarshalers
- path: grpc/
linters:
- lll # GRPC code has notoriously long function signatures.
- path: go-kit/metrics/provider/librato/librato.go
linters:
- maligned # TODO: evaluate if this makes sense
- path: testing/mustcert/example_test.go
linters:
- gosec # Don't run gosec, it's insecure because it's a test
Expand Down
5 changes: 0 additions & 5 deletions Dockerfile

This file was deleted.

53 changes: 3 additions & 50 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ GOARCH = $(shell go env GOARCH)
GOPATH = $(shell go env GOPATH)
TOOLS_DIR = $(TOP_LEVEL)/.tools
TOOLS_BIN = $(TOOLS_DIR)/bin
CIRCLECI_DIR = $(TOP_LEVEL)/.circleci
# Make sure this is in-sync with the version in the circle ci config
GOLANGCI_LINT_VERSION := 1.18.0
CIRCLECI_CONFIG := $(CIRCLECI_DIR)/config.yml
PROCESSED_CIRCLECI_CONFIG := $(CIRCLECI_DIR)/.processed.yml
GOLANGCI_LINT_URL := https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_LINT_VERSION)/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(GOOS)-$(GOARCH).tar.gz
GOLANGCI_LINT := $(TOOLS_DIR)/golangci-lint-v$(GOLANGCI_LINT_VERSION)
GOLANGCI_LINT_VERSION := v1.38.0
PKG_SPEC := ./...
MOD := -mod=readonly
GOTEST := go test $(MOD)
Expand Down Expand Up @@ -52,35 +47,9 @@ $(PROCESSED_CIRCLECI_CONFIG): $(CIRCLECI_CONFIG)
.PHONY: precommit
precommit: lint test coverage

# Ensures the correct version of golangci-lint is present
$(GOLANGCI_LINT):
rm -f $(TOOLS_DIR)/golangci-lint*
mkdir -p $(TOOLS_DIR)
curl -L $(GOLANGCI_LINT_URL) | tar -zxf - -C $(TOOLS_DIR) --strip=1 golangci-lint-$(GOLANGCI_LINT_VERSION)-$(GOOS)-$(GOARCH)/golangci-lint
mv $(TOOLS_DIR)/golangci-lint $(GOLANGCI_LINT)

.PHONY: help
help: # Prints out help
@IFS=$$'\n' ; \
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
printf "%-30s %s\n" "target" "help" ; \
printf "%-30s %s\n" "------" "----" ; \
for help_line in $${help_lines[@]}; do \
IFS=$$':' ; \
help_split=($$help_line) ; \
help_command=`echo $${help_split[0]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
help_info=`echo $${help_split[2]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
printf '\033[36m'; \
printf "%-30s %s" $$help_command ; \
printf '\033[0m'; \
printf "%s\n" $$help_info; \
done
@echo
@echo "'ci-' targets require the CircleCI cli tool: https://circleci.com/docs/2.0/local-cli/"

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Runs golangci-lint. Override defaults with LINT_RUN_OPTS
$(GOLANGCI_LINT) run $(LINT_RUN_OPTS) $(PKG_SPEC)
lint: ## Runs golangci-lint.
docker run --rm -v $$(pwd):/app -w /app golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) golangci-lint run -v

.PHONY: test
test: ## Runs go test. Override defaults with GOTEST_OPT
Expand Down Expand Up @@ -109,19 +78,3 @@ proto: $(TOOLS_BIN)/protoc $(TOOLS_BIN)/protoc-gen-go | $(TOOLS_BIN) ## Regenera
--go_out=paths=source_relative:. \
--loggingtags_out=. \
./cmd/protoc-gen-loggingtags/internal/test/*.proto


.PHONY: ci-lint
ci-lint: ## Runs the ci based lint job locally.
ci-lint: $(PROCESSED_CIRCLECI_CONFIG)
circleci local execute --job golang/golangci-lint -c $(PROCESSED_CIRCLECI_CONFIG) -v "$(GOPATH)/pkg":/go/pkg

.PHONY: ci-test
ci-test: ## Runs the ci based test job locally
ci-test: $(PROCESSED_CIRCLECI_CONFIG)
circleci local execute --job golang/test -c $(PROCESSED_CIRCLECI_CONFIG) -v "$(GOPATH)/pkg":/go/pkg

.PHONY: ci-coverage
ci-coverage: ## Runs the ci based coverage job locally
ci-coverage: $(PROCESSED_CIRCLECI_CONFIG)
circleci local execute --job golang/cover -c $(PROCESSED_CIRCLECI_CONFIG) -v "$(GOPATH)/pkg":/go/pkg
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# x

[![CircleCI](https://circleci.com/gh/heroku/x.svg?style=svg)](https://circleci.com/gh/heroku/x) [![GoDoc](https://godoc.org/github.com/heroku/x?status.svg)](http://godoc.org/github.com/heroku/x) ![Security Code Scanning - action](https://github.com/heroku/x/workflows/Code%20scanning%20-%20action/badge.svg)
![CI Testing Status](https://github.com/heroku/x/workflows/ci/badge.svg) [![GoDoc](https://godoc.org/github.com/heroku/x?status.svg)](http://godoc.org/github.com/heroku/x) ![Security Code Scanning - action](https://github.com/heroku/x/workflows/Code%20scanning%20-%20action/badge.svg)

A set of packages for reuse within Heroku Go applications.

Expand All @@ -14,16 +14,8 @@ A set of packages for reuse within Heroku Go applications.
The Makefile provides a few targets to help ensure the code is linted and tested.

```console
$ make help
target help
------ ----
lint Runs golangci-lint. Override defaults with LINT_RUN_OPTS
test Runs go test. Override defaults with GOTEST_OPT
coverage Generates a coverage profile and opens a web browser with the results
proto Regenerate protobuf files
ci-lint Runs the ci based lint job locally.
ci-test Runs the ci based test job locally
ci-coverage Runs the ci based coverage job locally

'ci-' targets require the CircleCI cli tool: https://circleci.com/docs/2.0/local-cli/
```
79 changes: 0 additions & 79 deletions cmdutil/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,16 @@
package metrics

import (
"net/url"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/heroku/x/cmdutil/metrics/otel"
"github.com/heroku/x/go-kit/metrics"
"github.com/heroku/x/go-kit/metrics/provider/librato"
"github.com/heroku/x/go-kit/runtimemetrics"
)

// StartLibrato initializes a new librato provider given the Config, and sets
// up a runtimemetrics.Collector on it. The runtime collector will emit on
// the librato.Provider every Config.ReportInterval ticks. You need to call
// Stop on the librato.Provider before you tear down the process.
func StartLibrato(logger log.FieldLogger, cfg Config) metrics.Provider {
logger = logger.WithField("at", "librato")

l := librato.New(
cfg.Librato.URL(),
cfg.ReportInterval,
librato.WithSource(cfg.Source),
librato.WithPrefix(cfg.Prefix),
librato.WithPercentilePrefix(".perc"),
librato.WithResetCounters(),
librato.WithSSA(),
librato.WithErrorHandler(func(err error) {
logLibratoError(logger, err)
}),
librato.WithRequestDebugging(),
)

if cfg.Librato.TagsEnabled {
librato.WithTags(cfg.DefaultTags...)(l.(*librato.Provider))
}

c := runtimemetrics.NewCollector(l)
go func() {
for range time.Tick(cfg.ReportInterval) {
c.Collect()
}
}()

return l
}

// Config stores all the env related config to bootstrap metrics.
type Config struct {
ReportInterval time.Duration `env:"METRICS_REPORT_INTERVAL,default=60s"`
Source string `env:"METRICS_SOURCE"`
Prefix string `env:"METRICS_PREFIX"`
DefaultTags []string `env:"METRICS_DEFAULT_TAGS"`
Librato Librato
OTEL otel.Config
}

// Librato stores all related librato config to be able to connect to its API.
type Librato struct {
APIURL *url.URL `env:"LIBRATO_API_URL"`
User string `env:"LIBRATO_USER"`
Password string `env:"LIBRATO_PASSWORD"`
TagsEnabled bool `env:"LIBRATO_TAGS_ENABLED"`
}

// URL returns the specified LIBRATO_API_URL, if any. Otherwise it defaults to
// the default Librato URL. The credentials are applied from LIBRATO_USER and
// LIBRATO_PASSWORD.
func (l Librato) URL() *url.URL {
if l.APIURL != nil {
l.APIURL.User = url.UserPassword(l.User, l.Password)
return l.APIURL
}

u, err := url.Parse(librato.DefaultURL)
if err != nil {
panic(errors.Wrap(err, "librato URL invalid"))
}
u.User = url.UserPassword(l.User, l.Password)
return u
}

type requester interface {
Request() string
}

func logLibratoError(l log.FieldLogger, err error) {
if r, ok := err.(requester); ok {
l = l.WithField("request_body", r.Request())
}
l.WithError(err).Warn()
}
11 changes: 3 additions & 8 deletions cmdutil/service/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/heroku/x/cmdutil"
"github.com/heroku/x/cmdutil/debug"
"github.com/heroku/x/cmdutil/metrics"
"github.com/heroku/x/cmdutil/oc"
"github.com/heroku/x/cmdutil/rollbar"
"github.com/heroku/x/cmdutil/signals"
Expand Down Expand Up @@ -73,13 +72,9 @@ func New(appConfig interface{}, ofs ...OptionFunc) *Standard {
Logger: logger,
}

if sc.Metrics.Librato.User != "" {
s.MetricsProvider = metrics.StartLibrato(logger, sc.Metrics)
} else {
l2met := l2met.New(logger)
s.MetricsProvider = l2met
s.Add(cmdutil.NewContextServer(l2met.Run))
}
l2met := l2met.New(logger)
s.MetricsProvider = l2met
s.Add(cmdutil.NewContextServer(l2met.Run))

s.Add(debug.New(logger, sc.Debug.Port))
s.Add(signals.NewServer(logger, syscall.SIGINT, syscall.SIGTERM))
Expand Down
Loading

0 comments on commit 38eb879

Please sign in to comment.