A comprehensive Go utility package providing common application framework components for building production-ready HTTP services.
- Version Management: Automatically extracts version information from VCS (Git) build info
- HTTP Headers: Pre-configured internal service headers with app name and version
- Pre-configured Echo Server: Ready-to-use Echo instance with security and monitoring middleware
- IP Extraction: Proper IP handling with real IP header support and CIDR trust ranges
- Sentry Integration: Built-in error tracking with Sentry middleware
- Route Rendering: Automatic HTML route listing for service discovery
- Prometheus Integration: Comprehensive HTTP metrics (request count, response duration)
- Metadata Management: Service configuration tracking and metrics registration
- pprof Support: Built-in profiling endpoint handler
- Service Discovery: Track internal and external service dependencies
- Database Monitoring: Connection tracking and replica status
- API Exposure: Public/private API configuration tracking
- Pre-configured Client: HTTP client with automatic metrics and internal headers
- Transport Wrappers: Composable transport layers for metrics and header injection
- Caller Tracking: Context-based caller identification for metrics
- X-Request-ID: Automatic generation, validation, and propagation
- Context Integration: Request ID stored in context for easy access
- Header Management: CORS and custom header support
- Metadata Context: Store and retrieve request metadata (IP, User-Agent, Platform, Version, Country)
- Debug Support: Debug ID and SQL group tracking for logging
- Integration Support: Sentry Hub and method tracking
Please see example/main.go
e := appkit.NewEcho()
// Add Prometheus metrics
e.Use(appkit.HTTPMetrics("api-service"))
// Route rendering for service discovery
e.GET("/", appkit.RenderRoutes("API Service", e))
// pprof endpoints
e.GET("/debug/pprof/*", appkit.PprofHandler)metadata := appkit.NewMetadataManager(appkit.MetadataOpts{
DBs: []appkit.DBMetadata{
appkit.NewDBMetadata("postgres", 10, false),
appkit.NewDBMetadata("redis", 5, true),
},
HasPublicAPI: true,
HasPrivateAPI: false,
HasBrokersrvQueue: true,
HasCronJobs: true,
Services: []appkit.ServiceMetadata{
appkit.NewServiceMetadata("auth-service", appkit.MetadataServiceTypeSync),
appkit.NewServiceMetadata("payment-service", appkit.MetadataServiceTypeExternal),
},
})
// Register metrics
metadata.RegisterMetrics()
// Add metadata endpoint
e.GET("/metadata", metadata.Handler)func callInternalService() {
headers := appkit.NewInternalHeaders("worker-service", appkit.Version())
req, _ := http.NewRequest("GET", "http://api-service/internal/data", nil)
req.Header = headers
// Make request with proper internal headers
client.Do(req)
}// Create pre-configured HTTP client for internal service calls
client := appkit.NewHTTPClient("externalsrv", appkit.Version(), 30*time.Second)
// Set caller name in context for metrics tracking
ctx := appkit.NewCallerNameContext(context.Background(), "externalsrv")
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://api-service/endpoint", nil)
resp, _ := client.Do(req)
// Usage with clients generated by vmkteam/rpcgen
arithsrvClient := arithsrv.NewClient(arithsrvUrl, appkit.NewHTTPClient("arithsrv", appkit.Version(), 5*time.Second))Version() string- Returns VCS revision or "devel"NewInternalHeaders(appName, version string) http.Header- Creates standardized internal headers
NewEcho() *echo.Echo- Creates pre-configured Echo instanceRenderRoutes(appName string, e *echo.Echo) echo.HandlerFunc- HTML route rendererPprofHandler(c echo.Context) error- pprof endpoint handlerEchoHandlerFunc(next http.HandlerFunc) echo.HandlerFunc- HTTP handler wrapper
HTTPMetrics(serverName string) echo.MiddlewareFunc- Prometheus HTTP metrics middlewareMetadataManager- Service metadata configuration and metrics
NewHTTPClient(appName, version string, timeout time.Duration) *http.Client- Creates HTTP client with metrics and internal headersWithMetricsTransport(base http.RoundTripper) http.RoundTripper- Wraps transport with Prometheus metricsWithHeadersTransport(base http.RoundTripper, headers http.Header) http.RoundTripper- Wraps transport to inject headers for each requestNewCallerNameContext(ctx context.Context, callerName string) context.Context- Creates context with caller name for metricsCallerNameFromContext(ctx context.Context) string- Retrieves caller name from context
XRequestIDFromContext(ctx context.Context) string- Retrieves X-Request-ID from contextNewXRequestIDContext(ctx context.Context, requestID string) context.Context- Creates context with X-Request-IDSetXRequestIDFromCtx(ctx context.Context, req *http.Request)- Adds X-Request-ID from context to request headersCORS(next http.Handler, headers ...string) http.Handler- CORS middleware for HTTP handlersXRequestID(next http.Handler) http.Handler- X-Request-ID middleware for HTTP handlersEchoHandler(next http.Handler) echo.HandlerFunc- Wraps HTTP handler as Echo handlerEchoSentryHubContext() echo.MiddlewareFunc- Echo middleware to apply Sentry hub to contextEchoIPContext() echo.MiddlewareFunc- Echo middleware to apply client IP to context
NewDebugIDContext(ctx context.Context, debugID uint64) context.Context- Creates context with debug IDDebugIDFromContext(ctx context.Context) uint64- Retrieves debug ID from contextNewSQLGroupContext(ctx context.Context, group string) context.Context- Creates context with SQL group for debug loggingSQLGroupFromContext(ctx context.Context) string- Retrieves SQL group from contextNewIPContext(ctx context.Context, ip string) context.Context- Creates context with IP addressIPFromContext(ctx context.Context) string- Retrieves IP address from contextNewUserAgentContext(ctx context.Context, ua string) context.Context- Creates context with User-AgentUserAgentFromContext(ctx context.Context) string- Retrieves User-Agent from contextNewNotificationContext(ctx context.Context) context.Context- Creates context with JSONRPC2 notification flagNotificationFromContext(ctx context.Context) bool- Retrieves JSONRPC2 notification flag from contextNewIsDevelContext(ctx context.Context, isDevel bool) context.Context- Creates context with isDevel flagIsDevelFromContext(ctx context.Context) bool- Retrieves isDevel flag from contextNewPlatformContext(ctx context.Context, platform string) context.Context- Creates context with platformPlatformFromContext(ctx context.Context) string- Retrieves platform from contextNewVersionContext(ctx context.Context, version string) context.Context- Creates context with versionVersionFromContext(ctx context.Context) string- Retrieves version from contextNewCountryContext(ctx context.Context, country string) context.Context- Creates context with countryCountryFromContext(ctx context.Context) string- Retrieves country from contextNewMethodContext(ctx context.Context, method string) context.Context- Creates context with methodMethodFromContext(ctx context.Context) string- Retrieves method from context
- Echo - High performance HTTP framework
- Prometheus - Metrics collection and monitoring
- Sentry - Error tracking and monitoring
app_http_requests_total- Total HTTP requests by method/path/statusapp_http_responses_duration_seconds- Response time distribution
app_http_client_requests_total- Total client requests by code/method/caller/originapp_http_client_responses_duration_seconds- Client response time distributionapp_http_client_requests_inflight- Current inflight client requests by caller/origin
app_metadata_service- Service configuration informationapp_metadata_db_connections_total- Database connection countsapp_metadata_services- Service dependencies
MIT License - see LICENSE file for details.