Skip to content

Commit 1aed921

Browse files
committed
Add profiling mode (#15)
Add pprof serves Add meta data to api.swagger.json Improve the usage of tracing Improve CI
1 parent 9eb534d commit 1aed921

File tree

23 files changed

+148
-351
lines changed

23 files changed

+148
-351
lines changed

.github/workflows/coverage.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ jobs:
2323
uses: actions/checkout@v2
2424
- name: Run coverage
2525
run: |
26-
go env -w CGO_ENABLED=1
27-
go test -v -coverprofile="coverage.out" -covermode=atomic ./...
28-
go tool cover -html="coverage.out" -o coverage.html
26+
go env -w CGO_ENABLED=0
27+
go test -v -coverprofile coverage.out ./...
28+
go tool cover -html coverage.out -o coverage.html
2929
- name: Upload coverage to Codecov
3030
uses: codecov/codecov-action@v1
3131
with:

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
go-version: [1.17.x]
16-
os: [ubuntu-latest, macos-latest]
16+
os: [ubuntu-latest, macos-latest, windows-latest]
1717
runs-on: ${{ matrix.os }}
1818
steps:
1919
- name: Install Go

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
# vendor/
1616

1717
.vscode
18+
logs.json

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ COPY --from=builder /home/${GITHUB_PATH}/config.yml .
2020
COPY --from=builder /home/${GITHUB_PATH}/migrations/ ./migrations/
2121

2222
RUN chown root:root reddit-feed-api
23-
CMD ["./reddit-feed-api", "--migration", "up"]
23+
CMD ["./reddit-feed-api"]

Makefile

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ ifneq ("1.17","$(shell printf "$(GO_VERSION_SHORT)\n1.17" | sort -V | head -1)")
33
$(error NEED GO VERSION >= 1.17. Found: $(GO_VERSION_SHORT))
44
endif
55

6-
export GO111MODULE=on
7-
86
###############################################################################
97

108
SERVICE_NAME=reddit-feed-api
119
SERVICE_PATH=github.com/arttet/reddit-feed-api
10+
SERVICE_MAIN=cmd/$(SERVICE_NAME)/main.go
11+
SERVICE_EXE=./bin/$(SERVICE_NAME)$(shell go env GOEXE)
1212

1313
###############################################################################
1414

@@ -23,29 +23,32 @@ ifeq ("NT", "$(findstring NT,$(OS_NAME))")
2323
OS_NAME=Windows
2424
endif
2525

26-
ifeq ("Windows", "$(OS_NAME)")
27-
OS_ARCH:=$(addsuffix .exe,$(OS_ARCH))
28-
endif
29-
3026
###############################################################################
3127

3228
.PHONY: build
3329
build: generate .build
34-
ifeq ("NT", "$(findstring NT,$(shell uname -s))")
35-
mv ./bin/$(SERVICE_NAME) ./bin/$(SERVICE_NAME).exe
36-
endif
3730

3831
.PHONY: run
3932
run:
40-
go run cmd/reddit-feed-api/main.go
33+
go run \
34+
-gcflags='-m' \
35+
-gcflags='$(SERVICE_PATH)/internal/api=-m' \
36+
-gcflags='$(SERVICE_PATH)/internal/server=-m' \
37+
$(SERVICE_MAIN) --cfg config-dev.yml
4138

4239
.PHONY: test
4340
test:
4441
go test -v -timeout 30s -coverprofile cover.out ./...
4542
go tool cover -func cover.out | grep total | awk '{print ($$3)}'
4643

44+
.PHONY: bench
45+
bench:
46+
go test -cpuprofile cpu.prof -memprofile mem.prof -bench ./...
47+
4748
.PHONY: lint
4849
lint:
50+
@command -v golangci-lint 2>&1 > /dev/null || (echo "Install golangci-lint" && \
51+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(shell go env GOPATH)/bin" v1.42.1)
4952
golangci-lint run ./...
5053

5154
.PHONY: tidy
@@ -61,6 +64,10 @@ style:
6164
cover:
6265
go tool cover -html cover.out
6366

67+
# Enable pprof: internal/server/status.go:13
68+
.PHONY: pprof
69+
pprof: .pprof-cpu
70+
6471
.PHONY: grpcui
6572
grpcui:
6673
grpcui -plaintext 0.0.0.0:8082
@@ -71,6 +78,8 @@ grpcui:
7178
deps: .deps
7279

7380
.deps:
81+
go env -w GO111MODULE=on
82+
7483
@ # https://pkg.go.dev/google.golang.org/protobuf/cmd/protoc-gen-go
7584
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
7685

@@ -93,7 +102,7 @@ generate: .generate
93102
.generate:
94103
@command -v buf 2>&1 > /dev/null || (echo "Install buf" && \
95104
mkdir -p "$(GOBIN)" && \
96-
curl -k -sSL0 https://github.com/bufbuild/buf/releases/download/$(BUF_VERSION)/buf-$(OS_NAME)-$(OS_ARCH) -o "$(GOBIN)/buf" && \
105+
curl -k -sSL0 https://github.com/bufbuild/buf/releases/download/$(BUF_VERSION)/buf-$(OS_NAME)-$(OS_ARCH)$(shell go env GOEXE) -o "$(GOBIN)/buf" && \
97106
chmod +x "$(GOBIN)/buf")
98107
@PATH="$(GOBIN):$(PATH)" buf generate
99108

@@ -111,4 +120,14 @@ generate: .generate
111120
-X '$(SERVICE_PATH)/internal/config.version=$(VERSION)' \
112121
-X '$(SERVICE_PATH)/internal/config.commitHash=$(COMMIT_HASH)' \
113122
" \
114-
-o ./bin/reddit-feed-api ./cmd/reddit-feed-api/main.go
123+
-o $(SERVICE_EXE) $(SERVICE_MAIN)
124+
125+
###############################################################################
126+
127+
.pprof-cpu:
128+
go tool pprof http://localhost:8000/debug/pprof/profile?seconds=30
129+
130+
.pprof-mem:
131+
go tool pprof -alloc_space http://localhost:8000/debug/pprof/heap
132+
133+
###############################################################################

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Reddit Feed API
22

33
[![Go](https://img.shields.io/badge/Go-1.17-blue.svg)](https://golang.org)
4-
[![Tests](https://github.com/arttet/reddit-feed-api/actions/workflows/tests.yml/badge.svg?branch=develop)](https://github.com/arttet/reddit-feed-api/actions/workflows/tests.yml)
4+
[![tests](https://github.com/arttet/reddit-feed-api/actions/workflows/tests.yml/badge.svg?branch=develop)](https://github.com/arttet/reddit-feed-api/actions/workflows/tests.yml)
55
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/arttet/reddit-feed-api/blob/main/LICENSE)
66

77
## Build project
@@ -69,6 +69,8 @@ Service condition and its information
6969
- [/live](http://localhost:8000/live)- Layed whether the server is running
7070
- [/ready](http://localhost:8000/ready) - Is it ready to accept requests?
7171
- [/version](http://localhost:8000/version) - Version and assembly information
72+
- [/debug/swagger/{swagger_file}](http://localhost:8000/debug/swagger/api.swagger.json) - Show a Swagger file in debug mode
73+
- [/debug/pprof](http://localhost:8000/debug/pprof) - Profiles when profiling enables
7274

7375
### Kafka
7476

cmd/reddit-feed-api/main.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/arttet/reddit-feed-api/internal/server"
99
"github.com/arttet/reddit-feed-api/internal/tracer"
1010

11+
"github.com/pressly/goose/v3"
12+
1113
"go.uber.org/zap"
1214

1315
_ "github.com/jackc/pgx/v4"
@@ -16,22 +18,17 @@ import (
1618
)
1719

1820
func main() {
19-
migration := flag.String("migration", "", "Defines the migration start option")
21+
migration := flag.Bool("migration", true, "Defines the migration start option")
2022
configYML := flag.String("cfg", "config.yml", "Defines the configuration file option")
2123
flag.Parse()
2224

23-
logger, err := zap.NewProduction()
24-
if err != nil {
25-
panic(err)
26-
}
27-
2825
if err := config.ReadConfigYML(*configYML); err != nil {
29-
logger.Fatal("reading configuration", zap.Error(err))
26+
panic(err)
3027
}
3128

3229
cfg := config.GetConfigInstance()
3330

34-
logger, err = cfg.Logger.Build()
31+
logger, err := cfg.Logger.Build()
3532
if err != nil {
3633
panic(err)
3734
}
@@ -40,23 +37,22 @@ func main() {
4037

4138
logger.Info("starting service",
4239
zap.String("name", cfg.Project.Name),
40+
zap.String("yml", *configYML),
4341
zap.Bool("debug", cfg.Project.Debug),
4442
zap.String("environment", cfg.Project.Environment),
4543
zap.String("commit_hash", cfg.Project.CommitHash),
4644
zap.String("version", cfg.Project.Version),
4745
)
4846

49-
dsn := cfg.Database.String()
50-
db, err := database.NewConnection(dsn, cfg.Database.Driver)
47+
db, err := database.NewConnection(cfg.Database.String(), cfg.Database.Driver)
5148
if err != nil {
5249
logger.Fatal("database initialization", zap.Error(err))
5350
}
5451
defer db.Close()
5552

56-
if *migration != "" {
57-
if err := database.Migrate(db.DB, *migration); err != nil {
53+
if *migration {
54+
if err = goose.Up(db.DB, cfg.Database.MigrationsDir); err != nil {
5855
logger.Error("migrations initialization", zap.Error(err))
59-
return
6056
}
6157
}
6258

config-dev.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ database:
4040
password: docker
4141
name: reddit_feed_api
4242
sslmode: disable
43+
migrationsDir: migrations
4344
driver: pgx
4445

4546
jaeger:
@@ -57,7 +58,7 @@ status:
5758
port: 8000
5859
livenessPath: /live
5960
readinessPath: /ready
60-
swaggerPath: /swagger/
61+
swaggerPath: /debug/swagger/
6162
versionPath: /version
6263

6364
kafka:

config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ database:
3636
password: docker
3737
name: reddit_feed_api
3838
sslmode: disable
39+
migrationsDir: migrations
3940
driver: pgx
4041

4142
jaeger:
@@ -53,7 +54,7 @@ status:
5354
port: 8000
5455
livenessPath: /live
5556
readinessPath: /ready
56-
swaggerPath: /swagger/
57+
swaggerPath: /debug/swagger/
5758
versionPath: /version
5859

5960
kafka:

external/swagger/api.swagger.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"swagger": "2.0",
33
"info": {
4-
"title": "reddit/reddit_feed_api/v1/reddit_feed_api.proto",
5-
"version": "version not set"
4+
"title": "Reddit Feed API",
5+
"version": "1.0",
6+
"license": {
7+
"name": "MIT",
8+
"url": "https://github.com/arttet/reddit-feed-api/blob/main/LICENSE"
9+
}
610
},
711
"consumes": [
812
"application/json"

0 commit comments

Comments
 (0)