Skip to content

Commit 2515498

Browse files
rameshsunkaraRamesh Sunkaragithub-advanced-security[bot]Copilot
authored
Add Go 1.25 Flight Recorder Integration for Performance Debugging (#72)
* Upgrade to golang 1.25 * use golang ci latest * fix lint runner * Add flight recording * update readme * update readme * Potential fix for code scanning alert no. 48: Uncontrolled data used in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix lint issue * fix tests * improve code coverage * Update pkg/flightrecorder/flightrecorder.go Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Ramesh Sunkara <[email protected]> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent f8a6868 commit 2515498

File tree

18 files changed

+847
-153
lines changed

18 files changed

+847
-153
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ dbName=ecommerce
33
dbHosts=localhost:27022
44
DBCredentialsSideCar=./localDevelopment/db-credentials-sidecar.json
55
printDBQueries=true
6-
logLevel=debug
6+
logLevel=debug
7+
enableTracing=true

.env.example

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Environment Configuration Example
2+
# Copy this file to .env and update with your actual values
3+
4+
# Application Environment (local, dev, staging, prod)
5+
environment=local
6+
7+
# Database Configuration
8+
dbName=ecommerce
9+
dbHosts=localhost:27022
10+
DBCredentialsSideCar=./localDevelopment/db-credentials-sidecar.json
11+
printDBQueries=true
12+
13+
# Logging Configuration
14+
logLevel=debug
15+
16+
# Tracing Configuration
17+
# Enable flight recorder for slow request tracing (>500ms)
18+
# Disabled by default in production to avoid overhead
19+
enableTracing=true

.github/copilot-instructions.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ When generating code for this project, please ensure compliance with the followi
1414

1515
### Linting Compliance
1616
- **golangci-lint**: All generated code must pass our golangci-lint configuration
17+
- **Verify before committing**: Run `make lint` to check for linting issues locally
1718
- **Common lint rules to follow**:
1819
- No unused variables or imports
1920
- Proper error handling (never ignore errors)
2021
- Use `require` for error assertions in tests, `assert` for other validations
2122
- Avoid useless assertions (comparing variables to themselves)
2223
- Add proper context to error messages
24+
- **Fix issues promptly**: Address all linting issues before submitting code
2325

2426
### Testing Standards
2527
- **Test naming**: Use `Test<FunctionName>` pattern
@@ -141,4 +143,20 @@ lgr.Info().
141143
- Validate all required configuration at startup
142144
- Provide sensible defaults where appropriate
143145

146+
## Development Workflow
147+
148+
### Before Committing Code
149+
1. **Format**: Run `gofmt -s -w .` to format code
150+
2. **Lint**: Run `make lint` to verify code passes all linting rules
151+
3. **Test**: Run `make test` to ensure all tests pass
152+
4. **Build**: Run `make build` to verify the application builds successfully
153+
154+
### Available Make Targets
155+
- `make lint` - Run golangci-lint to check for code quality issues
156+
- `make test` - Run all unit tests
157+
- `make ci-coverage` - Run tests with coverage reporting
158+
- `make build` - Build the application binary
159+
- `make docker-build` - Build the Docker image
160+
- `make docker-start` - Build and run the application in Docker
161+
144162
When generating code, please ensure it follows these patterns and will pass both our linting rules and maintain consistency with the existing codebase architecture.

.github/workflows/cibuild.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: 🧱 Setup Go
2121
uses: actions/setup-go@v5
2222
with:
23-
go-version: '1.24'
23+
go-version: '1.25.4'
2424
cache: true
2525

2626
- name: 🎗️ Check go mod
@@ -30,9 +30,7 @@ jobs:
3030
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
3131

3232
- name: 🛡️ Lint
33-
uses: golangci/golangci-lint-action@v8
34-
with:
35-
version: v2.1.0
33+
uses: golangci/golangci-lint-action@v9
3634

3735
- name: 🏗️ Build
3836
run: make build
@@ -49,7 +47,7 @@ jobs:
4947
- name: 🧱 Setup Go
5048
uses: actions/setup-go@v5
5149
with:
52-
go-version: '1.24'
50+
go-version: '1.25.4'
5351
cache: true
5452

5553
- name: 👮‍ Run Tests and Check Code Coverage

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ coverage.out
66
coverage.html
77
coverage.txt
88

9+
# Environment variables (sensitive data)
10+
.env
11+
12+
# Flight recorder trace files
13+
traces/
14+
*.trace
15+
916

1017
# Created by https://www.toptal.com/developers/gitignore/api/go,intellij+iml,vs
1118
# Edit at https://www.toptal.com/developers/gitignore?templates=go,intellij+iml,vs

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# syntax=docker/dockerfile:1.4 # Enable BuildKit features
22

33
# Stage 1: Build the Go binary
4-
FROM golang:1.24.3 AS builder
4+
FROM golang:1.25.4 AS builder
55
LABEL stage=builder
66

77
# Set working directory

Makefile

Lines changed: 96 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
SHELL = /bin/bash
22

3+
# ========================================
4+
# Configuration & Variables
5+
# ========================================
6+
37
# Load and export environment variables from .env file if it exists
48
ifneq (,$(wildcard ./.env))
59
include .env
@@ -9,8 +13,6 @@ else
913
endif
1014

1115
# Get the number of CPU cores for parallelism
12-
#get_cpu_cores := $(shell getconf _NPROCESSORS_ONLN)
13-
# Shell function to determine the number of CPU cores based on the OS
1416
get_cpu_cores = \
1517
if [ "$$(uname)" = "Linux" ]; then \
1618
nproc; \
@@ -21,59 +23,43 @@ get_cpu_cores = \
2123
echo 1; \
2224
fi
2325

24-
# Assign the result of the get_cpu_cores shell command to a variable
2526
cpu_cores := $(shell $(get_cpu_cores))
2627

2728
# Project-specific variables
2829
PROJECT_NAME := $(shell basename "$(PWD)" | tr '[:upper:]' '[:lower:]')
2930
VERSION ?= $(shell git rev-parse --short HEAD)
3031
LDFLAGS := -ldflags "-X main.version=$(VERSION)"
31-
32-
DOCKER_IMAGE_NAME := $(PROJECT_NAME):$(VERSION)
33-
DOCKER_CONTAINER_NAME := $(PROJECT_NAME)-$(VERSION)
3432
MODULE := $(shell go list -m)
3533
TEST_COVERAGE_THRESHOLD := 70
3634

37-
# Command to calculate test coverage will be computed when needed
35+
# Docker variables
36+
DOCKER_IMAGE_NAME := $(PROJECT_NAME):$(VERSION)
37+
DOCKER_CONTAINER_NAME := $(PROJECT_NAME)-$(VERSION)
3838

3939
# Helper variables
4040
GO_BUILD_CMD := CGO_ENABLED=0 go build $(LDFLAGS) -o $(PROJECT_NAME)
4141
GO_TEST_CMD := go test -race ./... -v -coverprofile=coverage.out -covermode=atomic -parallel=$(cpu_cores)
4242

43+
# ========================================
44+
# Development & Running
45+
# ========================================
46+
4347
## Start all necessary services and API server
4448
.PHONY: start
4549
start: setup run ## Start all necessary services and API server
4650

47-
## Start only dependencies (Docker containers)
51+
## Start only dependencies (Docker Compose services)
4852
.PHONY: setup
4953
setup: docker-compose-up ## Start only dependencies
5054

51-
## Run the API server
55+
## Run the API server locally
5256
.PHONY: run
5357
run: ## Run the API server
5458
go run $(LDFLAGS) main.go
5559

56-
## Start docker-compose services
57-
.PHONY: docker-compose-up
58-
docker-compose-up:
59-
docker-compose up -d
60-
61-
## Stop docker-compose services
62-
.PHONY: docker-compose-down
63-
docker-compose-down:
64-
docker-compose down
65-
66-
## Stop docker-compose services and remove volumes
67-
.PHONY: docker-compose-down-volumes
68-
docker-compose-down-volumes:
69-
docker-compose down -v
70-
71-
## Remove only the docker-compose volumes (database data)
72-
.PHONY: clean-volumes
73-
clean-volumes:
74-
@echo "Removing docker-compose volumes..."
75-
@docker volume ls -q --filter name=orders | xargs -r docker volume rm
76-
@echo "Volumes removed."
60+
# ========================================
61+
# Build & Version
62+
# ========================================
7763

7864
## Build the API server binary
7965
.PHONY: build
@@ -85,6 +71,10 @@ build: ## Build the API server binary
8571
version: ## Display the current version of the API server
8672
@echo $(VERSION)
8773

74+
# ========================================
75+
# Testing & Coverage
76+
# ========================================
77+
8878
## Run tests with coverage
8979
.PHONY: test
9080
test: ## Run tests with coverage
@@ -97,7 +87,7 @@ coverage: test ## Generate and display the code coverage report
9787
@go tool cover -func=coverage.out | grep total
9888
@go tool cover -html=coverage.out
9989

100-
## Check if test coverage meets the threshold
90+
## Check if test coverage meets the threshold (for CI)
10191
.PHONY: ci-coverage
10292
ci-coverage: test ## Check if test coverage meets the threshold
10393
@coverage=$(shell go tool cover -func=coverage.out | grep total | awk '{print $$3}' | sed 's/%//g'); \
@@ -113,6 +103,10 @@ ci-coverage: test ## Check if test coverage meets the threshold
113103
echo "Test coverage meets the threshold."; \
114104
fi
115105

106+
# ========================================
107+
# Code Quality & Formatting
108+
# ========================================
109+
116110
## Tidy Go modules
117111
.PHONY: tidy
118112
tidy: ## Tidy Go modules
@@ -133,6 +127,37 @@ lint: ## Run the linter
133127
lint-fix: ## Run the linter and fix issues
134128
golangci-lint run --fix
135129

130+
# ========================================
131+
# Debugging & Diagnostics
132+
# ========================================
133+
134+
## Analyze a trace file with go tool trace
135+
.PHONY: trace
136+
trace: ## Analyze a trace file (usage: make trace TRACE_FILE=./traces/slow-request-GET-orders-1234567890.trace)
137+
@if [ -z "$(TRACE_FILE)" ]; then \
138+
echo "Error: TRACE_FILE is required"; \
139+
echo "Usage: make trace TRACE_FILE=./traces/slow-request-GET-orders-1234567890.trace"; \
140+
echo ""; \
141+
echo "Available trace files:"; \
142+
if [ -d "./traces" ] && [ -n "$$(ls -A ./traces 2>/dev/null)" ]; then \
143+
ls -lhtr ./traces/*.trace 2>/dev/null | tail -10 || echo " No .trace files found in ./traces"; \
144+
else \
145+
echo " No traces directory or no trace files found"; \
146+
fi; \
147+
exit 1; \
148+
fi; \
149+
if [ ! -f "$(TRACE_FILE)" ]; then \
150+
echo "Error: Trace file not found: $(TRACE_FILE)"; \
151+
exit 1; \
152+
fi; \
153+
echo "Analyzing trace file: $(TRACE_FILE)"; \
154+
echo "This will start a web server and open the trace viewer in your browser..."; \
155+
go tool trace $(TRACE_FILE)
156+
157+
# ========================================
158+
# Utilities & Miscellaneous
159+
# ========================================
160+
136161
## Generate OWASP report
137162
.PHONY: owasp-report
138163
owasp-report: ## Generate OWASP report
@@ -143,13 +168,35 @@ owasp-report: ## Generate OWASP report
143168
go-work: ## Generate Go work file
144169
go work init .
145170

146-
## Clean all Docker resources (keeps database data)
147-
.PHONY: clean
148-
clean: docker-compose-down docker-clean ## Clean all Docker resources (keeps database data)
171+
# ========================================
172+
# Docker Compose Services
173+
# ========================================
149174

150-
## Clean all Docker resources including volumes (removes database data)
151-
.PHONY: clean-all
152-
clean-all: docker-compose-down-volumes docker-clean ## Clean all Docker resources including volumes (removes database data)
175+
## Start docker-compose services
176+
.PHONY: docker-compose-up
177+
docker-compose-up:
178+
docker-compose up -d
179+
180+
## Stop docker-compose services
181+
.PHONY: docker-compose-down
182+
docker-compose-down:
183+
docker-compose down
184+
185+
## Stop docker-compose services and remove volumes
186+
.PHONY: docker-compose-down-volumes
187+
docker-compose-down-volumes:
188+
docker-compose down -v
189+
190+
## Remove only the docker-compose volumes (database data)
191+
.PHONY: clean-volumes
192+
clean-volumes:
193+
@echo "Removing docker-compose volumes..."
194+
@docker volume ls -q --filter name=orders | xargs -r docker volume rm
195+
@echo "Volumes removed."
196+
197+
# ========================================
198+
# Docker Image & Container Management
199+
# ========================================
153200

154201
## Build the Docker image
155202
.PHONY: docker-build
@@ -223,6 +270,18 @@ docker-clean-build-images:
223270
echo "No build images to remove."; \
224271
fi
225272

273+
## Clean all Docker resources (keeps database data)
274+
.PHONY: clean
275+
clean: docker-compose-down docker-clean ## Clean all Docker resources (keeps database data)
276+
277+
## Clean all Docker resources including volumes (removes database data)
278+
.PHONY: clean-all
279+
clean-all: docker-compose-down-volumes docker-clean ## Clean all Docker resources including volumes (removes database data)
280+
281+
# ========================================
282+
# Help
283+
# ========================================
284+
226285
## Display help
227286
.PHONY: help
228287
help:

0 commit comments

Comments
 (0)