Skip to content

Commit

Permalink
Merge pull request #132 from digitalocean/hlee/add-pprof
Browse files Browse the repository at this point in the history
Enable pprof in debug mode
  • Loading branch information
house-lee authored Jun 14, 2024
2 parents 705da85 + dcb5a3a commit db0a15d
Show file tree
Hide file tree
Showing 33 changed files with 589 additions and 270 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.21.1]
go-version: [1.22.3]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [Unreleased](https://github.com/digitalocean/droplet-agent/tree/HEAD)

## [1.2.8](https://github.com/digitalocean/droplet-agent/tree/1.2.8) (2024-06-14)
### Updated
- Properly close the response body when requesting metadata service to avoid memory leak
- Enable `pprof` in debug mode
- Upgraded to go 1.22
- Switched the usage of gomock from `golang/mock` to `uber-go/mock`

### Related PRs
- Explicitly disable CGO when building binary [\#104](https://github.com/digitalocean/droplet-agent/pull/104)

## [1.2.7](https://github.com/digitalocean/droplet-agent/tree/1.2.7) (2023-09-20)
### Updated
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ now = $(shell date -u)
fpm = @docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -u $(shell id -u) digitalocean/fpm:latest
shellcheck = @docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -u $(shell id -u) koalaman/shellcheck:v0.6.0
version_check = @./scripts/check_version.sh
linter = docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -e "GOOS=$(GOOS)" -e "GOARCH=$(GOARCH)" -e "GO111MODULE=on" -e "GOFLAGS=-mod=vendor" -e "XDG_CACHE_HOME=$(CURDIR)/target/.cache/go" \
-u $(shell id -u) golangci/golangci-lint:v1.54 \
linter = docker run --platform linux/amd64 --rm -i -v "$(CURDIR):$(CURDIR)" -w "$(CURDIR)" -e "GOOS=$(GOOS)" -e "GOARCH=$(GOARCH)" -e "GO111MODULE=on" -e "GOFLAGS=-mod=vendor -buildvcs=false" -e "XDG_CACHE_HOME=$(CURDIR)/target/.cache/go" \
-u $(shell id -u) golangci/golangci-lint:v1.58 \
golangci-lint run --skip-files=.*_test.go -D errcheck -E revive -E gosec -E gofmt

go_docker_linux = golang:1.21.1
go_docker_linux = golang:1.22.3
ifeq ($(GOOS), linux)
go = docker run --platform linux/amd64 --rm -i \
-e "GOOS=$(GOOS)" \
Expand Down Expand Up @@ -237,3 +237,4 @@ mockgen:
mockgen -source=internal/metadata/actioner/do_managed_keys_actioner.go -package=mocks -destination=internal/metadata/actioner/internal/mocks/mocks.go
GOOS=linux mockgen -source=internal/netutil/tcp_sniffer_helper_linux.go -package=mocks -destination=internal/netutil/internal/mocks/dependent_functions_mock.go
mockgen -source=internal/metadata/updater/updater.go -package=updater -destination=internal/metadata/updater/updater_mocks.go
mockgen -destination=internal/metadata/updater/readcloser_mocks.go -package=updater -build_flags=--mod=mod io ReadCloser
5 changes: 5 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main
import (
"context"
"encoding/json"
"net/http"
_ "net/http/pprof" // #nosec G108
"os"
"os/signal"
"syscall"
Expand All @@ -27,6 +29,9 @@ func main() {

if cfg.DebugMode {
log.EnableDebug()
go func() {
http.ListenAndServe(config.AppDebugAddr, nil) // #nosec G114
}()
log.Info("Debug mode enabled")
}
if cfg.UseSyslog {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/digitalocean/droplet-agent

go 1.21
go 1.22

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/golang/mock v1.6.0
github.com/opencontainers/selinux v1.11.0
golang.org/x/crypto v0.16.0
golang.org/x/net v0.19.0
golang.org/x/sync v0.5.0
golang.org/x/sys v0.15.0
golang.org/x/time v0.3.0
)

require go.uber.org/mock v0.4.0
27 changes: 2 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "time"
const (
AppFullName = "DigitalOcean Droplet Agent (code name: DOTTY)"
AppShortName = "Droplet Agent"
AppDebugAddr = "127.0.0.1:304"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package config

const version = "v1.2.7"
const version = "v1.2.8"
5 changes: 2 additions & 3 deletions internal/metadata/actioner/do_managed_keys_actioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package actioner
import (
"encoding/json"
"errors"
"github.com/golang/mock/gomock"
"testing"

"github.com/digitalocean/droplet-agent/internal/sysaccess"

"github.com/digitalocean/droplet-agent/internal/log"
"github.com/digitalocean/droplet-agent/internal/metadata"
"github.com/digitalocean/droplet-agent/internal/metadata/actioner/internal/mocks"
"github.com/digitalocean/droplet-agent/internal/sysaccess"
"go.uber.org/mock/gomock"
)

func Test_dottyKeysActioner_do(t *testing.T) {
Expand Down
13 changes: 9 additions & 4 deletions internal/metadata/actioner/internal/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions internal/metadata/updater/readcloser_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (u *agentInfoUpdaterImpl) Update(md *metadata.Metadata) error {
if err != nil {
return fmt.Errorf("%w:%v", ErrUpdateMetadataFailed, err)
}
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()

success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
Expand Down
9 changes: 7 additions & 2 deletions internal/metadata/updater/updater_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions internal/metadata/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/digitalocean/droplet-agent/internal/metadata"
"github.com/digitalocean/droplet-agent/internal/mockutils"
"github.com/golang/mock/gomock"
"go.uber.org/mock/gomock"
)

func Test_agentInfoUpdaterImpl_Update(t *testing.T) {
Expand All @@ -24,12 +24,12 @@ func Test_agentInfoUpdaterImpl_Update(t *testing.T) {
}
tests := []struct {
name string
expectations func(client *MockhttpClient)
expectations func(client *MockhttpClient, respBody *MockReadCloser)
wantErr bool
}{
{
"successful response",
func(client *MockhttpClient) {
func(client *MockhttpClient, respBody *MockReadCloser) {
reqMatcher := &mockutils.HTTPRequestMatcher{
ExpectedRequest: newRequest(t, []byte("{\"dotty_status\":\"running\",\"ssh_info\":{\"port\":256}}")),
}
Expand All @@ -40,18 +40,19 @@ func Test_agentInfoUpdaterImpl_Update(t *testing.T) {
},
{
"unsuccessful response code",
func(client *MockhttpClient) {
func(client *MockhttpClient, respBody *MockReadCloser) {
reqMatcher := &mockutils.HTTPRequestMatcher{
ExpectedRequest: newRequest(t, []byte("{\"dotty_status\":\"running\",\"ssh_info\":{\"port\":256}}")),
}

client.EXPECT().Do(reqMatcher).Return(&http.Response{StatusCode: 404}, nil)
client.EXPECT().Do(reqMatcher).Return(&http.Response{StatusCode: 404, Body: respBody}, nil)
respBody.EXPECT().Close()
},
true,
},
{
"error from http client",
func(client *MockhttpClient) {
func(client *MockhttpClient, respBody *MockReadCloser) {
reqMatcher := &mockutils.HTTPRequestMatcher{
ExpectedRequest: newRequest(t, []byte("{\"dotty_status\":\"running\",\"ssh_info\":{\"port\":256}}")),
}
Expand All @@ -65,7 +66,8 @@ func Test_agentInfoUpdaterImpl_Update(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := NewMockhttpClient(ctrl)
tt.expectations(client)
readCloser := NewMockReadCloser(ctrl)
tt.expectations(client, readCloser)
m := &agentInfoUpdaterImpl{
client: client,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/metadata/watcher/web_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (w *webBasedWatcher) Run() error {
}

r := http.NewServeMux()
r.HandleFunc("/new_metadata", func(rw http.ResponseWriter, r *http.Request) {
r.HandleFunc("/new_metadata", func(rw http.ResponseWriter, _ *http.Request) {
if !w.limiter.Allow() {
rw.WriteHeader(http.StatusTooManyRequests)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/mockutils/http_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"net/http"

"github.com/golang/mock/gomock"
"go.uber.org/mock/gomock"
)

var _ gomock.Matcher = &HTTPRequestMatcher{}
Expand Down
Loading

0 comments on commit db0a15d

Please sign in to comment.