@mytechtoday/augment-extensions 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/augment-extensions/coding-standards/c/CHANGELOG.md +55 -0
- package/augment-extensions/coding-standards/c/LICENSE +22 -0
- package/augment-extensions/coding-standards/c/README.md +167 -0
- package/augment-extensions/coding-standards/c/config/defaults.json +26 -0
- package/augment-extensions/coding-standards/c/config/examples/embedded.yaml +25 -0
- package/augment-extensions/coding-standards/c/config/examples/systems.json +31 -0
- package/augment-extensions/coding-standards/c/config/schema.json +244 -0
- package/augment-extensions/coding-standards/c/docs/API.md +613 -0
- package/augment-extensions/coding-standards/c/docs/CONFIGURATION.md +259 -0
- package/augment-extensions/coding-standards/c/docs/USER_GUIDE.md +567 -0
- package/augment-extensions/coding-standards/c/examples/drivers/Makefile +33 -0
- package/augment-extensions/coding-standards/c/examples/drivers/README.md +192 -0
- package/augment-extensions/coding-standards/c/examples/drivers/dma-example.c +224 -0
- package/augment-extensions/coding-standards/c/examples/drivers/example.dts +64 -0
- package/augment-extensions/coding-standards/c/examples/drivers/platform-driver.c +174 -0
- package/augment-extensions/coding-standards/c/examples/embedded/README.md +167 -0
- package/augment-extensions/coding-standards/c/examples/embedded/gpio-control.c +172 -0
- package/augment-extensions/coding-standards/c/examples/embedded/timer-isr.c +198 -0
- package/augment-extensions/coding-standards/c/examples/embedded/uart-communication.c +212 -0
- package/augment-extensions/coding-standards/c/examples/kernel/Makefile +82 -0
- package/augment-extensions/coding-standards/c/examples/kernel/README.md +168 -0
- package/augment-extensions/coding-standards/c/examples/kernel/char-device.c +198 -0
- package/augment-extensions/coding-standards/c/examples/kernel/proc-file.c +131 -0
- package/augment-extensions/coding-standards/c/examples/kernel/simple-module.c +111 -0
- package/augment-extensions/coding-standards/c/examples/legacy/Makefile +62 -0
- package/augment-extensions/coding-standards/c/examples/legacy/README.md +255 -0
- package/augment-extensions/coding-standards/c/examples/legacy/c89-to-c11-migration.c +268 -0
- package/augment-extensions/coding-standards/c/examples/legacy/compatibility-layer.c +239 -0
- package/augment-extensions/coding-standards/c/examples/networking/Makefile +35 -0
- package/augment-extensions/coding-standards/c/examples/networking/README.md +207 -0
- package/augment-extensions/coding-standards/c/examples/networking/protocol-parser.c +270 -0
- package/augment-extensions/coding-standards/c/examples/networking/tcp-server.c +197 -0
- package/augment-extensions/coding-standards/c/examples/networking/udp-multicast.c +220 -0
- package/augment-extensions/coding-standards/c/examples/realtime/Makefile +53 -0
- package/augment-extensions/coding-standards/c/examples/realtime/README.md +199 -0
- package/augment-extensions/coding-standards/c/examples/realtime/deadline-monitoring.c +260 -0
- package/augment-extensions/coding-standards/c/examples/realtime/priority-scheduling.c +258 -0
- package/augment-extensions/coding-standards/c/examples/systems/Makefile +34 -0
- package/augment-extensions/coding-standards/c/examples/systems/README.md +123 -0
- package/augment-extensions/coding-standards/c/examples/systems/ipc-pipes.c +181 -0
- package/augment-extensions/coding-standards/c/examples/systems/process-management.c +153 -0
- package/augment-extensions/coding-standards/c/examples/systems/signal-handling.c +162 -0
- package/augment-extensions/coding-standards/c/module.json +149 -0
- package/augment-extensions/coding-standards/c/rules/categories/drivers.md +635 -0
- package/augment-extensions/coding-standards/c/rules/categories/embedded.md +510 -0
- package/augment-extensions/coding-standards/c/rules/categories/kernel.md +653 -0
- package/augment-extensions/coding-standards/c/rules/categories/legacy.md +526 -0
- package/augment-extensions/coding-standards/c/rules/categories/networking.md +735 -0
- package/augment-extensions/coding-standards/c/rules/categories/realtime.md +631 -0
- package/augment-extensions/coding-standards/c/rules/categories/systems.md +586 -0
- package/augment-extensions/coding-standards/c/rules/universal/const-correctness.md +275 -0
- package/augment-extensions/coding-standards/c/rules/universal/documentation.md +251 -0
- package/augment-extensions/coding-standards/c/rules/universal/error-handling.md +250 -0
- package/augment-extensions/coding-standards/c/rules/universal/header-guards.md +254 -0
- package/augment-extensions/coding-standards/c/rules/universal/memory-safety.md +233 -0
- package/augment-extensions/coding-standards/c/rules/universal/naming.md +146 -0
- package/augment-extensions/coding-standards/c/src/conflict-detector.ts +461 -0
- package/augment-extensions/coding-standards/c/src/prompt-generator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-evaluator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-override.ts +427 -0
- package/augment-extensions/coding-standards/c/src/template-engine.ts +217 -0
- package/augment-extensions/coding-standards/c/templates/prompts/drivers.txt +191 -0
- package/augment-extensions/coding-standards/c/templates/prompts/embedded.txt +164 -0
- package/augment-extensions/coding-standards/c/templates/prompts/kernel.txt +175 -0
- package/augment-extensions/coding-standards/c/templates/prompts/legacy.txt +280 -0
- package/augment-extensions/coding-standards/c/templates/prompts/networking.txt +259 -0
- package/augment-extensions/coding-standards/c/templates/prompts/realtime.txt +219 -0
- package/augment-extensions/coding-standards/c/templates/prompts/systems.txt +147 -0
- package/augment-extensions/coding-standards/c/tests/integration/category-specific.test.ts +356 -0
- package/augment-extensions/coding-standards/c/tests/integration/end-to-end-workflow.test.ts +377 -0
- package/augment-extensions/coding-standards/c/tests/performance/benchmarks.test.ts +407 -0
- package/augment-extensions/coding-standards/c/tests/unit/config-manager.test.ts +345 -0
- package/augment-extensions/coding-standards/c/tests/unit/conflict-detector.test.ts +294 -0
- package/augment-extensions/coding-standards/c/tests/unit/prompt-generator.test.ts +174 -0
- package/augment-extensions/coding-standards/c/tests/unit/registry.test.ts +313 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-evaluator.test.ts +318 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-override.test.ts +326 -0
- package/augment-extensions/coding-standards/c/tests/unit/template-engine.test.ts +314 -0
- package/augment-extensions/coding-standards/go/CHARACTER-COUNT-REPORT.md +135 -0
- package/augment-extensions/coding-standards/go/PHASE1-COMPLETION.md +146 -0
- package/augment-extensions/coding-standards/go/PHASE4-COMPLETION.md +184 -0
- package/augment-extensions/coding-standards/go/README.md +200 -0
- package/augment-extensions/coding-standards/go/VALIDATION-CHECKLIST.md +154 -0
- package/augment-extensions/coding-standards/go/config/examples/example-cli.json +15 -0
- package/augment-extensions/coding-standards/go/config/examples/example-microservices.json +21 -0
- package/augment-extensions/coding-standards/go/config/examples/example-multi-category.yaml +24 -0
- package/augment-extensions/coding-standards/go/config/examples/example-web.json +15 -0
- package/augment-extensions/coding-standards/go/config/schema.json +110 -0
- package/augment-extensions/coding-standards/go/docs/CATEGORIES.md +221 -0
- package/augment-extensions/coding-standards/go/docs/CONFIGURATION.md +198 -0
- package/augment-extensions/coding-standards/go/docs/TROUBLESHOOTING.md +285 -0
- package/augment-extensions/coding-standards/go/examples/cli/cobra-app.go +287 -0
- package/augment-extensions/coding-standards/go/examples/cloud-native-app.go +217 -0
- package/augment-extensions/coding-standards/go/examples/devops-tool.go +250 -0
- package/augment-extensions/coding-standards/go/examples/distributed-system.go +247 -0
- package/augment-extensions/coding-standards/go/examples/microservices/grpc-service.go +253 -0
- package/augment-extensions/coding-standards/go/examples/rest-api.go +270 -0
- package/augment-extensions/coding-standards/go/examples/web/http-server.go +224 -0
- package/augment-extensions/coding-standards/go/module.json +139 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/api-versioning.md +149 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rate-limiting.md +209 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rest-api-design.md +183 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/cloud-config.md +193 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/health-checks.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/kubernetes.md +180 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/automation.md +179 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/ci-cd-integration.md +147 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/infrastructure-as-code.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/caching.md +150 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/consensus.md +187 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/event-sourcing.md +246 -0
- package/augment-extensions/coding-standards/go/rules/cli/command-parsing.md +264 -0
- package/augment-extensions/coding-standards/go/rules/cli/configuration.md +268 -0
- package/augment-extensions/coding-standards/go/rules/cli/cross-platform.md +324 -0
- package/augment-extensions/coding-standards/go/rules/microservices/distributed-tracing.md +253 -0
- package/augment-extensions/coding-standards/go/rules/microservices/grpc.md +257 -0
- package/augment-extensions/coding-standards/go/rules/microservices/metrics.md +278 -0
- package/augment-extensions/coding-standards/go/rules/microservices/service-discovery.md +249 -0
- package/augment-extensions/coding-standards/go/rules/universal/code-organization.md +221 -0
- package/augment-extensions/coding-standards/go/rules/universal/documentation.md +269 -0
- package/augment-extensions/coding-standards/go/rules/universal/performance.md +323 -0
- package/augment-extensions/coding-standards/go/rules/universal/testing.md +162 -0
- package/augment-extensions/coding-standards/go/rules/web/graceful-shutdown.md +249 -0
- package/augment-extensions/coding-standards/go/rules/web/http-handlers.md +164 -0
- package/augment-extensions/coding-standards/go/rules/web/middleware.md +234 -0
- package/augment-extensions/coding-standards/go/rules/web/routing.md +251 -0
- package/augment-extensions/coding-standards/go/templates/prompts/api.md +160 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cli.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cloud-native.md +121 -0
- package/augment-extensions/coding-standards/go/templates/prompts/devops.md +146 -0
- package/augment-extensions/coding-standards/go/templates/prompts/distributed.md +133 -0
- package/augment-extensions/coding-standards/go/templates/prompts/microservices.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/web.md +181 -0
- package/augment-extensions/coding-standards/go/tests/integration/module-integration.test.ts +164 -0
- package/augment-extensions/coding-standards/go/tests/unit/category-selection.test.ts +147 -0
- package/augment-extensions/coding-standards/go/tests/unit/module-structure.test.ts +154 -0
- package/augment-extensions/coding-standards/go/tests/validate-character-count.ps1 +13 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.ps1 +148 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.sh +135 -0
- package/cli/dist/analysis/ast-parser.d.ts +47 -0
- package/cli/dist/analysis/ast-parser.d.ts.map +1 -0
- package/cli/dist/analysis/ast-parser.js +161 -0
- package/cli/dist/analysis/ast-parser.js.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts +27 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.js +189 -0
- package/cli/dist/analysis/complexity-analyzer.js.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts +23 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.js +237 -0
- package/cli/dist/analysis/dependency-analyzer.js.map +1 -0
- package/cli/dist/analysis/index.d.ts +9 -0
- package/cli/dist/analysis/index.d.ts.map +1 -0
- package/cli/dist/analysis/index.js +25 -0
- package/cli/dist/analysis/index.js.map +1 -0
- package/cli/dist/analysis/security-scanner.d.ts +11 -0
- package/cli/dist/analysis/security-scanner.d.ts.map +1 -0
- package/cli/dist/analysis/security-scanner.js +294 -0
- package/cli/dist/analysis/security-scanner.js.map +1 -0
- package/cli/dist/analysis/types.d.ts +151 -0
- package/cli/dist/analysis/types.d.ts.map +1 -0
- package/cli/dist/analysis/types.js +6 -0
- package/cli/dist/analysis/types.js.map +1 -0
- package/cli/dist/cli.js +24 -0
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/code-analysis.d.ts +11 -0
- package/cli/dist/commands/code-analysis.d.ts.map +1 -0
- package/cli/dist/commands/code-analysis.js +412 -0
- package/cli/dist/commands/code-analysis.js.map +1 -0
- package/modules.md +99 -3
- package/package.json +14 -2
- package/cli/dist/commands/agent.d.ts +0 -37
- package/cli/dist/commands/agent.d.ts.map +0 -1
- package/cli/dist/commands/agent.js +0 -222
- package/cli/dist/commands/agent.js.map +0 -1
- package/cli/dist/commands/beads.d.ts +0 -64
- package/cli/dist/commands/beads.d.ts.map +0 -1
- package/cli/dist/commands/beads.js +0 -377
- package/cli/dist/commands/beads.js.map +0 -1
- package/cli/dist/commands/change.d.ts +0 -54
- package/cli/dist/commands/change.d.ts.map +0 -1
- package/cli/dist/commands/change.js +0 -243
- package/cli/dist/commands/change.js.map +0 -1
- package/cli/dist/commands/clean.d.ts +0 -15
- package/cli/dist/commands/clean.d.ts.map +0 -1
- package/cli/dist/commands/clean.js +0 -63
- package/cli/dist/commands/clean.js.map +0 -1
- package/cli/dist/commands/clone.d.ts +0 -15
- package/cli/dist/commands/clone.d.ts.map +0 -1
- package/cli/dist/commands/clone.js +0 -49
- package/cli/dist/commands/clone.js.map +0 -1
- package/cli/dist/commands/config.d.ts +0 -33
- package/cli/dist/commands/config.d.ts.map +0 -1
- package/cli/dist/commands/config.js +0 -166
- package/cli/dist/commands/config.js.map +0 -1
- package/cli/dist/commands/context.d.ts +0 -38
- package/cli/dist/commands/context.d.ts.map +0 -1
- package/cli/dist/commands/context.js +0 -205
- package/cli/dist/commands/context.js.map +0 -1
- package/cli/dist/commands/create.d.ts +0 -18
- package/cli/dist/commands/create.d.ts.map +0 -1
- package/cli/dist/commands/create.js +0 -178
- package/cli/dist/commands/create.js.map +0 -1
- package/cli/dist/commands/diff.d.ts +0 -19
- package/cli/dist/commands/diff.d.ts.map +0 -1
- package/cli/dist/commands/diff.js +0 -104
- package/cli/dist/commands/diff.js.map +0 -1
- package/cli/dist/commands/doctor.d.ts +0 -14
- package/cli/dist/commands/doctor.d.ts.map +0 -1
- package/cli/dist/commands/doctor.js +0 -62
- package/cli/dist/commands/doctor.js.map +0 -1
- package/cli/dist/commands/export.d.ts +0 -28
- package/cli/dist/commands/export.d.ts.map +0 -1
- package/cli/dist/commands/export.js +0 -135
- package/cli/dist/commands/export.js.map +0 -1
- package/cli/dist/commands/import.d.ts +0 -23
- package/cli/dist/commands/import.d.ts.map +0 -1
- package/cli/dist/commands/import.js +0 -118
- package/cli/dist/commands/import.js.map +0 -1
- package/cli/dist/commands/prompt.d.ts +0 -45
- package/cli/dist/commands/prompt.d.ts.map +0 -1
- package/cli/dist/commands/prompt.js +0 -223
- package/cli/dist/commands/prompt.js.map +0 -1
- package/cli/dist/commands/spec.d.ts +0 -57
- package/cli/dist/commands/spec.d.ts.map +0 -1
- package/cli/dist/commands/spec.js +0 -279
- package/cli/dist/commands/spec.js.map +0 -1
- package/cli/dist/commands/stats.d.ts +0 -18
- package/cli/dist/commands/stats.d.ts.map +0 -1
- package/cli/dist/commands/stats.js +0 -85
- package/cli/dist/commands/stats.js.map +0 -1
- package/cli/dist/commands/task.d.ts +0 -65
- package/cli/dist/commands/task.d.ts.map +0 -1
- package/cli/dist/commands/task.js +0 -282
- package/cli/dist/commands/task.js.map +0 -1
- package/cli/dist/commands/template.d.ts +0 -17
- package/cli/dist/commands/template.d.ts.map +0 -1
- package/cli/dist/commands/template.js +0 -55
- package/cli/dist/commands/template.js.map +0 -1
- package/cli/dist/utils/agent-config.d.ts +0 -129
- package/cli/dist/utils/agent-config.d.ts.map +0 -1
- package/cli/dist/utils/agent-config.js +0 -297
- package/cli/dist/utils/agent-config.js.map +0 -1
- package/cli/dist/utils/beads-graph.d.ts +0 -17
- package/cli/dist/utils/beads-graph.d.ts.map +0 -1
- package/cli/dist/utils/beads-graph.js +0 -150
- package/cli/dist/utils/beads-graph.js.map +0 -1
- package/cli/dist/utils/beads-integration.d.ts +0 -112
- package/cli/dist/utils/beads-integration.d.ts.map +0 -1
- package/cli/dist/utils/beads-integration.js +0 -312
- package/cli/dist/utils/beads-integration.js.map +0 -1
- package/cli/dist/utils/beads-reporter.d.ts +0 -17
- package/cli/dist/utils/beads-reporter.d.ts.map +0 -1
- package/cli/dist/utils/beads-reporter.js +0 -160
- package/cli/dist/utils/beads-reporter.js.map +0 -1
- package/cli/dist/utils/cache-manager.d.ts +0 -55
- package/cli/dist/utils/cache-manager.d.ts.map +0 -1
- package/cli/dist/utils/cache-manager.js +0 -150
- package/cli/dist/utils/cache-manager.js.map +0 -1
- package/cli/dist/utils/change-manager.d.ts +0 -70
- package/cli/dist/utils/change-manager.d.ts.map +0 -1
- package/cli/dist/utils/change-manager.js +0 -412
- package/cli/dist/utils/change-manager.js.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.d.ts +0 -66
- package/cli/dist/utils/config-manager-enhanced.d.ts.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.js +0 -77
- package/cli/dist/utils/config-manager-enhanced.js.map +0 -1
- package/cli/dist/utils/context-manager.d.ts +0 -96
- package/cli/dist/utils/context-manager.d.ts.map +0 -1
- package/cli/dist/utils/context-manager.js +0 -258
- package/cli/dist/utils/context-manager.js.map +0 -1
- package/cli/dist/utils/diff-engine.d.ts +0 -78
- package/cli/dist/utils/diff-engine.d.ts.map +0 -1
- package/cli/dist/utils/diff-engine.js +0 -233
- package/cli/dist/utils/diff-engine.js.map +0 -1
- package/cli/dist/utils/export-system.d.ts +0 -101
- package/cli/dist/utils/export-system.d.ts.map +0 -1
- package/cli/dist/utils/export-system.js +0 -289
- package/cli/dist/utils/export-system.js.map +0 -1
- package/cli/dist/utils/health-checker.d.ts +0 -66
- package/cli/dist/utils/health-checker.d.ts.map +0 -1
- package/cli/dist/utils/health-checker.js +0 -285
- package/cli/dist/utils/health-checker.js.map +0 -1
- package/cli/dist/utils/import-system.d.ts +0 -74
- package/cli/dist/utils/import-system.d.ts.map +0 -1
- package/cli/dist/utils/import-system.js +0 -317
- package/cli/dist/utils/import-system.js.map +0 -1
- package/cli/dist/utils/module-cloner.d.ts +0 -40
- package/cli/dist/utils/module-cloner.d.ts.map +0 -1
- package/cli/dist/utils/module-cloner.js +0 -136
- package/cli/dist/utils/module-cloner.js.map +0 -1
- package/cli/dist/utils/prompt-manager.d.ts +0 -90
- package/cli/dist/utils/prompt-manager.d.ts.map +0 -1
- package/cli/dist/utils/prompt-manager.js +0 -302
- package/cli/dist/utils/prompt-manager.js.map +0 -1
- package/cli/dist/utils/spec-manager.d.ts +0 -65
- package/cli/dist/utils/spec-manager.d.ts.map +0 -1
- package/cli/dist/utils/spec-manager.js +0 -329
- package/cli/dist/utils/spec-manager.js.map +0 -1
- package/cli/dist/utils/stats-collector.d.ts +0 -74
- package/cli/dist/utils/stats-collector.d.ts.map +0 -1
- package/cli/dist/utils/stats-collector.js +0 -164
- package/cli/dist/utils/stats-collector.js.map +0 -1
- package/cli/dist/utils/template-engine.d.ts +0 -47
- package/cli/dist/utils/template-engine.d.ts.map +0 -1
- package/cli/dist/utils/template-engine.js +0 -204
- package/cli/dist/utils/template-engine.js.map +0 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# Go Microservices - Metrics and Monitoring
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Metrics enable monitoring and observability of microservices. This document defines best practices for implementing Prometheus metrics in Go microservices.
|
|
6
|
+
|
|
7
|
+
## Core Principles
|
|
8
|
+
|
|
9
|
+
1. **RED Method**: Track Rate, Errors, Duration for all services
|
|
10
|
+
2. **Metric Types**: Use appropriate metric types (Counter, Gauge, Histogram, Summary)
|
|
11
|
+
3. **Labels**: Add relevant labels for filtering and aggregation
|
|
12
|
+
4. **Naming**: Follow Prometheus naming conventions
|
|
13
|
+
5. **Performance**: Minimize overhead in hot paths
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
### GOL.3.2.4.1: Implement RED Metrics
|
|
18
|
+
|
|
19
|
+
**Rule**: Track Rate, Errors, and Duration for all service endpoints.
|
|
20
|
+
|
|
21
|
+
**Severity**: WARNING
|
|
22
|
+
|
|
23
|
+
**Rationale**: RED metrics provide essential service health indicators.
|
|
24
|
+
|
|
25
|
+
**✅ Good**:
|
|
26
|
+
```go
|
|
27
|
+
import (
|
|
28
|
+
"github.com/prometheus/client_golang/prometheus"
|
|
29
|
+
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
var (
|
|
33
|
+
// Rate: Total requests
|
|
34
|
+
requestsTotal = promauto.NewCounterVec(
|
|
35
|
+
prometheus.CounterOpts{
|
|
36
|
+
Name: "service_requests_total",
|
|
37
|
+
Help: "Total number of requests",
|
|
38
|
+
},
|
|
39
|
+
[]string{"method", "endpoint", "status"},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
// Errors: Failed requests
|
|
43
|
+
requestsErrors = promauto.NewCounterVec(
|
|
44
|
+
prometheus.CounterOpts{
|
|
45
|
+
Name: "service_requests_errors_total",
|
|
46
|
+
Help: "Total number of failed requests",
|
|
47
|
+
},
|
|
48
|
+
[]string{"method", "endpoint", "error_type"},
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
// Duration: Request latency
|
|
52
|
+
requestDuration = promauto.NewHistogramVec(
|
|
53
|
+
prometheus.HistogramOpts{
|
|
54
|
+
Name: "service_request_duration_seconds",
|
|
55
|
+
Help: "Request duration in seconds",
|
|
56
|
+
Buckets: prometheus.DefBuckets,
|
|
57
|
+
},
|
|
58
|
+
[]string{"method", "endpoint"},
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
func instrumentHandler(method, endpoint string, handler func() error) error {
|
|
63
|
+
start := time.Now()
|
|
64
|
+
|
|
65
|
+
err := handler()
|
|
66
|
+
|
|
67
|
+
duration := time.Since(start).Seconds()
|
|
68
|
+
requestDuration.WithLabelValues(method, endpoint).Observe(duration)
|
|
69
|
+
|
|
70
|
+
status := "success"
|
|
71
|
+
if err != nil {
|
|
72
|
+
status = "error"
|
|
73
|
+
requestsErrors.WithLabelValues(method, endpoint, err.Error()).Inc()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
requestsTotal.WithLabelValues(method, endpoint, status).Inc()
|
|
77
|
+
|
|
78
|
+
return err
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### GOL.3.2.4.2: Use Appropriate Metric Types
|
|
83
|
+
|
|
84
|
+
**Rule**: Choose the correct Prometheus metric type for each use case.
|
|
85
|
+
|
|
86
|
+
**Severity**: ERROR
|
|
87
|
+
|
|
88
|
+
**Rationale**: Wrong metric types lead to incorrect aggregations and alerts.
|
|
89
|
+
|
|
90
|
+
**Metric Types**:
|
|
91
|
+
- **Counter**: Monotonically increasing values (requests, errors)
|
|
92
|
+
- **Gauge**: Values that can go up or down (active connections, queue size)
|
|
93
|
+
- **Histogram**: Distribution of values (request duration, response size)
|
|
94
|
+
- **Summary**: Similar to histogram but calculates quantiles client-side
|
|
95
|
+
|
|
96
|
+
**✅ Good**:
|
|
97
|
+
```go
|
|
98
|
+
var (
|
|
99
|
+
// Counter: Monotonically increasing
|
|
100
|
+
httpRequestsTotal = promauto.NewCounter(prometheus.CounterOpts{
|
|
101
|
+
Name: "http_requests_total",
|
|
102
|
+
Help: "Total HTTP requests",
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// Gauge: Can increase or decrease
|
|
106
|
+
activeConnections = promauto.NewGauge(prometheus.GaugeOpts{
|
|
107
|
+
Name: "active_connections",
|
|
108
|
+
Help: "Number of active connections",
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
// Histogram: Distribution of values
|
|
112
|
+
requestSize = promauto.NewHistogram(prometheus.HistogramOpts{
|
|
113
|
+
Name: "http_request_size_bytes",
|
|
114
|
+
Help: "HTTP request size in bytes",
|
|
115
|
+
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
|
|
116
|
+
})
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|
120
|
+
httpRequestsTotal.Inc()
|
|
121
|
+
activeConnections.Inc()
|
|
122
|
+
defer activeConnections.Dec()
|
|
123
|
+
|
|
124
|
+
requestSize.Observe(float64(r.ContentLength))
|
|
125
|
+
|
|
126
|
+
// Handle request...
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### GOL.3.2.4.3: Instrument gRPC Services
|
|
131
|
+
|
|
132
|
+
**Rule**: Add Prometheus metrics to all gRPC methods.
|
|
133
|
+
|
|
134
|
+
**Severity**: WARNING
|
|
135
|
+
|
|
136
|
+
**✅ Good**:
|
|
137
|
+
```go
|
|
138
|
+
var (
|
|
139
|
+
grpcRequestsTotal = promauto.NewCounterVec(
|
|
140
|
+
prometheus.CounterOpts{
|
|
141
|
+
Name: "grpc_requests_total",
|
|
142
|
+
Help: "Total gRPC requests",
|
|
143
|
+
},
|
|
144
|
+
[]string{"method", "status"},
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
grpcRequestDuration = promauto.NewHistogramVec(
|
|
148
|
+
prometheus.HistogramOpts{
|
|
149
|
+
Name: "grpc_request_duration_seconds",
|
|
150
|
+
Help: "gRPC request duration",
|
|
151
|
+
Buckets: prometheus.DefBuckets,
|
|
152
|
+
},
|
|
153
|
+
[]string{"method"},
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
func metricsUnaryInterceptor() grpc.UnaryServerInterceptor {
|
|
158
|
+
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
159
|
+
start := time.Now()
|
|
160
|
+
|
|
161
|
+
resp, err := handler(ctx, req)
|
|
162
|
+
|
|
163
|
+
duration := time.Since(start).Seconds()
|
|
164
|
+
grpcRequestDuration.WithLabelValues(info.FullMethod).Observe(duration)
|
|
165
|
+
|
|
166
|
+
status := "success"
|
|
167
|
+
if err != nil {
|
|
168
|
+
status = "error"
|
|
169
|
+
}
|
|
170
|
+
grpcRequestsTotal.WithLabelValues(info.FullMethod, status).Inc()
|
|
171
|
+
|
|
172
|
+
return resp, err
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### GOL.3.2.4.4: Expose Metrics Endpoint
|
|
178
|
+
|
|
179
|
+
**Rule**: Expose Prometheus metrics on `/metrics` endpoint.
|
|
180
|
+
|
|
181
|
+
**Severity**: ERROR
|
|
182
|
+
|
|
183
|
+
**Rationale**: Enables Prometheus to scrape metrics.
|
|
184
|
+
|
|
185
|
+
**✅ Good**:
|
|
186
|
+
```go
|
|
187
|
+
import (
|
|
188
|
+
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
func setupRoutes() http.Handler {
|
|
192
|
+
r := chi.NewRouter()
|
|
193
|
+
|
|
194
|
+
// Metrics endpoint
|
|
195
|
+
r.Handle("/metrics", promhttp.Handler())
|
|
196
|
+
|
|
197
|
+
// Application routes
|
|
198
|
+
r.Get("/api/users", handleUsers)
|
|
199
|
+
|
|
200
|
+
return r
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### GOL.3.2.4.5: Add Business Metrics
|
|
205
|
+
|
|
206
|
+
**Rule**: Track business-specific metrics beyond technical metrics.
|
|
207
|
+
|
|
208
|
+
**Severity**: INFO
|
|
209
|
+
|
|
210
|
+
**✅ Good**:
|
|
211
|
+
```go
|
|
212
|
+
var (
|
|
213
|
+
ordersCreated = promauto.NewCounter(prometheus.CounterOpts{
|
|
214
|
+
Name: "orders_created_total",
|
|
215
|
+
Help: "Total orders created",
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
orderValue = promauto.NewHistogram(prometheus.HistogramOpts{
|
|
219
|
+
Name: "order_value_dollars",
|
|
220
|
+
Help: "Order value in dollars",
|
|
221
|
+
Buckets: prometheus.LinearBuckets(0, 50, 20),
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
userRegistrations = promauto.NewCounterVec(
|
|
225
|
+
prometheus.CounterOpts{
|
|
226
|
+
Name: "user_registrations_total",
|
|
227
|
+
Help: "Total user registrations",
|
|
228
|
+
},
|
|
229
|
+
[]string{"source"},
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
func (s *OrderService) CreateOrder(ctx context.Context, order *Order) error {
|
|
234
|
+
// Create order...
|
|
235
|
+
|
|
236
|
+
// Track business metrics
|
|
237
|
+
ordersCreated.Inc()
|
|
238
|
+
orderValue.Observe(order.Total)
|
|
239
|
+
|
|
240
|
+
return nil
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
func (s *UserService) RegisterUser(ctx context.Context, user *User, source string) error {
|
|
244
|
+
// Register user...
|
|
245
|
+
|
|
246
|
+
userRegistrations.WithLabelValues(source).Inc()
|
|
247
|
+
|
|
248
|
+
return nil
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### GOL.3.2.4.6: Use Metric Labels Wisely
|
|
253
|
+
|
|
254
|
+
**Rule**: Use labels for dimensions, but avoid high cardinality.
|
|
255
|
+
|
|
256
|
+
**Severity**: WARNING
|
|
257
|
+
|
|
258
|
+
**Rationale**: High cardinality labels cause memory issues in Prometheus.
|
|
259
|
+
|
|
260
|
+
**✅ Good**:
|
|
261
|
+
```go
|
|
262
|
+
// Low cardinality labels
|
|
263
|
+
requestsTotal.WithLabelValues("GET", "/api/users", "200").Inc()
|
|
264
|
+
requestsTotal.WithLabelValues("POST", "/api/orders", "201").Inc()
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
**❌ Bad**:
|
|
268
|
+
```go
|
|
269
|
+
// High cardinality - user_id creates millions of time series
|
|
270
|
+
requestsTotal.WithLabelValues("GET", "/api/users", userID).Inc()
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## References
|
|
274
|
+
|
|
275
|
+
- [Prometheus Best Practices](https://prometheus.io/docs/practices/naming/)
|
|
276
|
+
- [RED Method](https://www.weave.works/blog/the-red-method-key-metrics-for-microservices-architecture/)
|
|
277
|
+
- [Prometheus Go Client](https://github.com/prometheus/client_golang)
|
|
278
|
+
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# Go Microservices - Service Discovery
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Service discovery enables microservices to find and communicate with each other dynamically. This document defines best practices for implementing service discovery in Go microservices.
|
|
6
|
+
|
|
7
|
+
## Core Principles
|
|
8
|
+
|
|
9
|
+
1. **Dynamic Registration**: Services register themselves on startup
|
|
10
|
+
2. **Health Checks**: Implement health endpoints for service monitoring
|
|
11
|
+
3. **Load Balancing**: Use client-side or server-side load balancing
|
|
12
|
+
4. **Failure Handling**: Handle service unavailability gracefully
|
|
13
|
+
5. **Configuration**: Use environment variables for service discovery endpoints
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
### GOL.3.2.2.1: Implement Service Registration
|
|
18
|
+
|
|
19
|
+
**Rule**: Register service with discovery system (Consul, etcd, Kubernetes) on startup.
|
|
20
|
+
|
|
21
|
+
**Severity**: ERROR
|
|
22
|
+
|
|
23
|
+
**Rationale**: Enables dynamic service discovery and load balancing.
|
|
24
|
+
|
|
25
|
+
**✅ Good - Consul Registration**:
|
|
26
|
+
```go
|
|
27
|
+
import (
|
|
28
|
+
"github.com/hashicorp/consul/api"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
type ServiceRegistry struct {
|
|
32
|
+
client *api.Client
|
|
33
|
+
serviceID string
|
|
34
|
+
serviceName string
|
|
35
|
+
port int
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func NewServiceRegistry(consulAddr, serviceName string, port int) (*ServiceRegistry, error) {
|
|
39
|
+
config := api.DefaultConfig()
|
|
40
|
+
config.Address = consulAddr
|
|
41
|
+
|
|
42
|
+
client, err := api.NewClient(config)
|
|
43
|
+
if err != nil {
|
|
44
|
+
return nil, err
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return &ServiceRegistry{
|
|
48
|
+
client: client,
|
|
49
|
+
serviceID: fmt.Sprintf("%s-%s", serviceName, uuid.New().String()),
|
|
50
|
+
serviceName: serviceName,
|
|
51
|
+
port: port,
|
|
52
|
+
}, nil
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func (r *ServiceRegistry) Register(ctx context.Context) error {
|
|
56
|
+
registration := &api.AgentServiceRegistration{
|
|
57
|
+
ID: r.serviceID,
|
|
58
|
+
Name: r.serviceName,
|
|
59
|
+
Port: r.port,
|
|
60
|
+
Address: getLocalIP(),
|
|
61
|
+
Check: &api.AgentServiceCheck{
|
|
62
|
+
HTTP: fmt.Sprintf("http://%s:%d/health", getLocalIP(), r.port),
|
|
63
|
+
Interval: "10s",
|
|
64
|
+
Timeout: "3s",
|
|
65
|
+
DeregisterCriticalServiceAfter: "30s",
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return r.client.Agent().ServiceRegister(registration)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
func (r *ServiceRegistry) Deregister() error {
|
|
73
|
+
return r.client.Agent().ServiceDeregister(r.serviceID)
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### GOL.3.2.2.2: Implement Health Check Endpoints
|
|
78
|
+
|
|
79
|
+
**Rule**: Provide `/health` and `/ready` endpoints for service health monitoring.
|
|
80
|
+
|
|
81
|
+
**Severity**: ERROR
|
|
82
|
+
|
|
83
|
+
**Rationale**: Enables load balancers and orchestrators to route traffic appropriately.
|
|
84
|
+
|
|
85
|
+
**✅ Good**:
|
|
86
|
+
```go
|
|
87
|
+
type HealthChecker struct {
|
|
88
|
+
db *sql.DB
|
|
89
|
+
cache *redis.Client
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Health returns basic liveness status
|
|
93
|
+
func (h *HealthChecker) Health(w http.ResponseWriter, r *http.Request) {
|
|
94
|
+
w.WriteHeader(http.StatusOK)
|
|
95
|
+
json.NewEncoder(w).Encode(map[string]string{
|
|
96
|
+
"status": "healthy",
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Ready returns readiness status (checks dependencies)
|
|
101
|
+
func (h *HealthChecker) Ready(w http.ResponseWriter, r *http.Request) {
|
|
102
|
+
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
|
103
|
+
defer cancel()
|
|
104
|
+
|
|
105
|
+
// Check database
|
|
106
|
+
if err := h.db.PingContext(ctx); err != nil {
|
|
107
|
+
w.WriteHeader(http.StatusServiceUnavailable)
|
|
108
|
+
json.NewEncoder(w).Encode(map[string]string{
|
|
109
|
+
"status": "not_ready",
|
|
110
|
+
"reason": "database_unavailable",
|
|
111
|
+
})
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Check cache
|
|
116
|
+
if err := h.cache.Ping(ctx).Err(); err != nil {
|
|
117
|
+
w.WriteHeader(http.StatusServiceUnavailable)
|
|
118
|
+
json.NewEncoder(w).Encode(map[string]string{
|
|
119
|
+
"status": "not_ready",
|
|
120
|
+
"reason": "cache_unavailable",
|
|
121
|
+
})
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
w.WriteHeader(http.StatusOK)
|
|
126
|
+
json.NewEncoder(w).Encode(map[string]string{
|
|
127
|
+
"status": "ready",
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### GOL.3.2.2.3: Implement Service Discovery Client
|
|
133
|
+
|
|
134
|
+
**Rule**: Use service discovery to find and connect to other services.
|
|
135
|
+
|
|
136
|
+
**Severity**: WARNING
|
|
137
|
+
|
|
138
|
+
**✅ Good - Consul Service Discovery**:
|
|
139
|
+
```go
|
|
140
|
+
type ServiceDiscovery struct {
|
|
141
|
+
client *api.Client
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
func NewServiceDiscovery(consulAddr string) (*ServiceDiscovery, error) {
|
|
145
|
+
config := api.DefaultConfig()
|
|
146
|
+
config.Address = consulAddr
|
|
147
|
+
|
|
148
|
+
client, err := api.NewClient(config)
|
|
149
|
+
if err != nil {
|
|
150
|
+
return nil, err
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return &ServiceDiscovery{client: client}, nil
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func (sd *ServiceDiscovery) GetServiceAddress(serviceName string) (string, error) {
|
|
157
|
+
services, _, err := sd.client.Health().Service(serviceName, "", true, nil)
|
|
158
|
+
if err != nil {
|
|
159
|
+
return "", err
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if len(services) == 0 {
|
|
163
|
+
return "", fmt.Errorf("no healthy instances of %s found", serviceName)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Simple round-robin (use random for better distribution)
|
|
167
|
+
service := services[rand.Intn(len(services))]
|
|
168
|
+
|
|
169
|
+
return fmt.Sprintf("%s:%d", service.Service.Address, service.Service.Port), nil
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Usage
|
|
173
|
+
func (c *Client) CallUserService(ctx context.Context, userID int64) (*User, error) {
|
|
174
|
+
addr, err := c.discovery.GetServiceAddress("user-service")
|
|
175
|
+
if err != nil {
|
|
176
|
+
return nil, err
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
conn, err := grpc.Dial(addr, grpc.WithInsecure())
|
|
180
|
+
if err != nil {
|
|
181
|
+
return nil, err
|
|
182
|
+
}
|
|
183
|
+
defer conn.Close()
|
|
184
|
+
|
|
185
|
+
client := userv1.NewUserServiceClient(conn)
|
|
186
|
+
resp, err := client.GetUser(ctx, &userv1.GetUserRequest{Id: userID})
|
|
187
|
+
if err != nil {
|
|
188
|
+
return nil, err
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return &User{
|
|
192
|
+
ID: resp.User.Id,
|
|
193
|
+
Name: resp.User.Name,
|
|
194
|
+
}, nil
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### GOL.3.2.2.4: Implement Graceful Deregistration
|
|
199
|
+
|
|
200
|
+
**Rule**: Deregister service from discovery system during shutdown.
|
|
201
|
+
|
|
202
|
+
**Severity**: ERROR
|
|
203
|
+
|
|
204
|
+
**Rationale**: Prevents routing traffic to terminated services.
|
|
205
|
+
|
|
206
|
+
**✅ Good**:
|
|
207
|
+
```go
|
|
208
|
+
func main() {
|
|
209
|
+
registry, err := NewServiceRegistry("localhost:8500", "user-service", 8080)
|
|
210
|
+
if err != nil {
|
|
211
|
+
log.Fatal(err)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Register service
|
|
215
|
+
if err := registry.Register(context.Background()); err != nil {
|
|
216
|
+
log.Fatal(err)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Start server
|
|
220
|
+
srv := startServer()
|
|
221
|
+
|
|
222
|
+
// Wait for shutdown signal
|
|
223
|
+
quit := make(chan os.Signal, 1)
|
|
224
|
+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
225
|
+
<-quit
|
|
226
|
+
|
|
227
|
+
log.Println("Shutting down...")
|
|
228
|
+
|
|
229
|
+
// Deregister from service discovery
|
|
230
|
+
if err := registry.Deregister(); err != nil {
|
|
231
|
+
log.Printf("Failed to deregister: %v", err)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Shutdown server
|
|
235
|
+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
236
|
+
defer cancel()
|
|
237
|
+
|
|
238
|
+
if err := srv.Shutdown(ctx); err != nil {
|
|
239
|
+
log.Fatal(err)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## References
|
|
245
|
+
|
|
246
|
+
- [Consul Service Discovery](https://www.consul.io/docs/discovery/services)
|
|
247
|
+
- [etcd Service Discovery](https://etcd.io/docs/latest/dev-guide/grpc_naming/)
|
|
248
|
+
- [Kubernetes Service Discovery](https://kubernetes.io/docs/concepts/services-networking/service/)
|
|
249
|
+
|