grpc

package
v2.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 34 Imported by: 0

README

gRPC Server Package

A production-ready, reusable gRPC server with Echo HTTP framework integration for Go applications. This package provides a clean, configuration-driven API for setting up gRPC servers with HTTP/REST gateway support, built-in observability, health checks, and graceful shutdown capabilities.

Features

  • Echo Framework Integration: Full-featured HTTP server using Echo v4
  • Dual Protocol Support: Run gRPC and HTTP services on the same port (H2C) or separate ports
  • gRPC Gateway: Automatic HTTP/REST endpoints for gRPC services
  • Zero Configuration: Works out-of-the-box with sensible defaults
  • Production Ready: Built-in Prometheus metrics, health checks, and graceful shutdown
  • Highly Configurable: Extensive configuration options including CORS, rate limiting, and custom middleware
  • Observability: Prometheus metrics with detailed request tracking for both gRPC and HTTP
  • Easy Integration: Clean API that works with any gRPC service implementation

Installation

go get github.com/jasoet/pkg/v2/grpc

Quick Start

Basic Usage (H2C Mode)
package main

import (
    "log"
    "google.golang.org/grpc"

    grpcserver "github.com/jasoet/pkg/grpc"
    calculatorv1 "your-module/gen/calculator/v1"
    "your-module/internal/service"
)

func main() {
    // Define service registrar
    serviceRegistrar := func(s *grpc.Server) {
        calculatorService := service.NewCalculatorService()
        calculatorv1.RegisterCalculatorServiceServer(s, calculatorService)
    }

    // Start server with minimal configuration
    log.Println("Starting gRPC server on :8080...")
    if err := grpcserver.Start("8080", serviceRegistrar); err != nil {
        log.Fatalf("Failed to start server: %v", err)
    }
}

This starts a server in H2C mode where both gRPC and HTTP endpoints are available on port 8080:

  • gRPC endpoints: localhost:8080
  • HTTP gateway: http://localhost:8080/api/v1/
  • Health checks: http://localhost:8080/health
  • Metrics: http://localhost:8080/metrics

Server Modes

H2C Mode (Default)

Single port serving both gRPC and HTTP traffic using HTTP/2 cleartext protocol:

grpcserver.Start("8080", serviceRegistrar)
// or explicitly
grpcserver.StartH2C("8080", serviceRegistrar)
Separate Mode

Different ports for gRPC and HTTP services:

grpcserver.StartSeparate("9090", "9091", serviceRegistrar)

Advanced Configuration

Echo Integration with Custom Routes
package main

import (
    "log"
    "time"

    "github.com/labstack/echo/v4"
    "google.golang.org/grpc"

    grpcserver "github.com/jasoet/pkg/grpc"
)

func main() {
    // Create advanced configuration
    config := grpcserver.DefaultConfig()

    // Server Configuration
    config.GRPCPort = "50051"
    config.Mode = grpcserver.H2CMode

    // Timeouts
    config.ShutdownTimeout = 45 * time.Second
    config.ReadTimeout = 10 * time.Second
    config.WriteTimeout = 15 * time.Second
    config.IdleTimeout = 120 * time.Second
    config.MaxConnectionIdle = 30 * time.Minute
    config.MaxConnectionAge = 60 * time.Minute
    config.MaxConnectionAgeGrace = 10 * time.Second

    // Production Features
    config.EnableMetrics = true
    config.EnableHealthCheck = true
    config.EnableLogging = true
    config.EnableReflection = true

    // Echo-specific Features
    config.EnableCORS = true
    config.EnableRateLimit = true
    config.RateLimit = 100.0 // requests per second

    // Gateway Configuration
    config.GatewayBasePath = "/api/v1"

    // Register gRPC services
    config.ServiceRegistrar = func(srv *grpc.Server) {
        // Register your gRPC services here
        log.Println("Registering gRPC services...")
    }

    // Configure Echo with custom routes
    config.EchoConfigurer = func(e *echo.Echo) {
        // Add custom REST endpoints
        e.GET("/status", func(c echo.Context) error {
            return c.JSON(200, map[string]interface{}{
                "service": "my-service",
                "status":  "running",
            })
        })

        // Add custom middleware
        e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
            return func(c echo.Context) error {
                log.Printf("Custom middleware: %s %s", c.Request().Method, c.Path())
                return next(c)
            }
        })

        log.Println("Custom Echo routes configured")
    }

    // Custom gRPC configuration
    config.GRPCConfigurer = func(s *grpc.Server) {
        log.Println("Applying custom gRPC configuration...")
        // Add interceptors, custom options, etc.
    }

    // Custom shutdown handler
    config.Shutdown = func() error {
        log.Println("Running custom cleanup...")
        // Close connections, cleanup resources
        return nil
    }

    // Start server
    if err := grpcserver.StartWithConfig(config); err != nil {
        log.Fatalf("Failed to start server: %v", err)
    }
}
Using Echo Middleware
import (
    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
)

config := grpcserver.DefaultConfig()

// Add Echo middleware via configuration
config.Middleware = []echo.MiddlewareFunc{
    middleware.RequestID(),
    middleware.Secure(),
    middleware.Gzip(),
}

// Or configure via EchoConfigurer
config.EchoConfigurer = func(e *echo.Echo) {
    e.Use(middleware.RequestID())
    e.Use(middleware.Secure())
    e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
        Level: 5,
    }))
}

OpenTelemetry Integration

The gRPC server package supports OpenTelemetry for comprehensive observability with distributed tracing, metrics, and structured logging. When OTelConfig is provided, it replaces traditional Prometheus metrics with OpenTelemetry instrumentation.

Basic OpenTelemetry Setup
package main

import (
    "context"
    "log"

    "github.com/jasoet/pkg/logging"
    "github.com/jasoet/pkg/otel"
    grpcserver "github.com/jasoet/pkg/grpc"
    "google.golang.org/grpc"
)

func main() {
    // Create OTel config with logging (traces and metrics optional)
    otelCfg := otel.NewConfig("my-grpc-service").
        WithServiceVersion("1.0.0")

    // Or use logging package for better log-span correlation
    loggerProvider := logging.NewLoggerProvider("my-grpc-service", false)
    otelCfg.WithLoggerProvider(loggerProvider)

    // Start server with OTel
    server, err := grpcserver.New(
        grpcserver.WithGRPCPort("50051"),
        grpcserver.WithOTelConfig(otelCfg),
        grpcserver.WithServiceRegistrar(func(s *grpc.Server) {
            // Register your services
        }),
    )
    if err != nil {
        log.Fatal(err)
    }

    if err := server.Start(); err != nil {
        log.Fatal(err)
    }
}
Full OpenTelemetry Stack (Traces + Metrics + Logs)
package main

import (
    "context"
    "log"
    "time"

    "github.com/jasoet/pkg/logging"
    "github.com/jasoet/pkg/otel"
    grpcserver "github.com/jasoet/pkg/grpc"
    "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
    "go.opentelemetry.io/otel/sdk/metric"
    "go.opentelemetry.io/otel/sdk/resource"
    "go.opentelemetry.io/otel/sdk/trace"
    semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
    "google.golang.org/grpc"
)

func main() {
    ctx := context.Background()

    // Setup resource attributes
    res, err := resource.New(ctx,
        resource.WithAttributes(
            semconv.ServiceNameKey.String("my-grpc-service"),
            semconv.ServiceVersionKey.String("1.0.0"),
        ),
    )
    if err != nil {
        log.Fatal(err)
    }

    // Setup TracerProvider with OTLP exporter
    traceExporter, err := otlptracehttp.New(ctx,
        otlptracehttp.WithEndpoint("localhost:4318"),
        otlptracehttp.WithInsecure(),
    )
    if err != nil {
        log.Fatal(err)
    }

    tracerProvider := trace.NewTracerProvider(
        trace.WithBatcher(traceExporter),
        trace.WithResource(res),
    )

    // Setup MeterProvider
    meterProvider := metric.NewMeterProvider(
        metric.WithResource(res),
    )

    // Setup LoggerProvider with trace correlation
    loggerProvider := logging.NewLoggerProvider("my-grpc-service", false)

    // Create OTel config
    otelCfg := &otel.Config{
        ServiceName:    "my-grpc-service",
        ServiceVersion: "1.0.0",
        TracerProvider: tracerProvider,
        MeterProvider:  meterProvider,
        LoggerProvider: loggerProvider,
    }

    // Start gRPC server with full OTel instrumentation
    server, err := grpcserver.New(
        grpcserver.WithGRPCPort("50051"),
        grpcserver.WithOTelConfig(otelCfg),
        grpcserver.WithServiceRegistrar(func(s *grpc.Server) {
            // Register your services
        }),
        grpcserver.WithShutdownHandler(func() error {
            // Shutdown OTel providers
            ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
            defer cancel()

            if err := tracerProvider.Shutdown(ctx); err != nil {
                log.Printf("Error shutting down tracer provider: %v", err)
            }
            if err := meterProvider.Shutdown(ctx); err != nil {
                log.Printf("Error shutting down meter provider: %v", err)
            }
            return nil
        }),
    )
    if err != nil {
        log.Fatal(err)
    }

    if err := server.Start(); err != nil {
        log.Fatal(err)
    }
}
What Gets Instrumented

When OTelConfig is provided, the server automatically instruments:

gRPC Server
  • Traces: Distributed tracing for all gRPC methods with semantic conventions
  • Metrics:
    • rpc.server.request.count - Total gRPC requests by method and status
    • rpc.server.duration - Request duration histogram
    • rpc.server.active_requests - Active concurrent requests
  • Logs: Structured logs with automatic trace_id/span_id correlation
HTTP Gateway
  • Traces: HTTP request spans linked to gRPC spans
  • Metrics:
    • http.server.request.count - Total HTTP requests
    • http.server.request.duration - Request duration histogram
    • http.server.active_requests - Active concurrent requests
  • Logs: HTTP access logs with trace correlation
Log-Span Correlation

When using the logging package LoggerProvider, all logs automatically include trace_id and span_id fields, enabling you to:

  • Click a span in Grafana → See all related logs
  • Click a log → Jump to the trace
  • Filter logs by trace ID
{
  "level": "info",
  "scope": "grpc.server",
  "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
  "span_id": "00f067aa0ba902b7",
  "rpc.system": "grpc",
  "rpc.method": "/calculator.v1.CalculatorService/Add",
  "rpc.grpc.status_code": 0,
  "message": "gRPC /calculator.v1.CalculatorService/Add"
}
Backwards Compatibility

The gRPC server maintains backwards compatibility:

  • Without OTelConfig: Uses traditional Prometheus metrics and standard logging
  • With OTelConfig: Uses OpenTelemetry instrumentation
  • Health checks work in both modes
  • No breaking changes to existing code
Migration from Prometheus to OTel

Before (Prometheus):

server, err := grpcserver.New(
    grpcserver.WithGRPCPort("50051"),
    grpcserver.WithMetrics(),  // Prometheus metrics
    grpcserver.WithLogging(),  // Standard logging
    grpcserver.WithServiceRegistrar(serviceRegistrar),
)

After (OpenTelemetry):

otelCfg := otel.NewConfig("my-service").
    WithTracerProvider(tracerProvider).
    WithMeterProvider(meterProvider)

server, err := grpcserver.New(
    grpcserver.WithGRPCPort("50051"),
    grpcserver.WithOTelConfig(otelCfg),  // Replaces metrics and logging
    grpcserver.WithServiceRegistrar(serviceRegistrar),
)

Configuration Options

Core Settings
  • GRPCPort: Port for gRPC server (required)
  • HTTPPort: Port for HTTP gateway (required for separate mode)
  • Mode: Server mode (H2CMode or SeparateMode)
Timeouts
  • ShutdownTimeout: Graceful shutdown timeout (default: 30s)
  • ReadTimeout: HTTP read timeout (default: 5s)
  • WriteTimeout: HTTP write timeout (default: 10s)
  • IdleTimeout: HTTP idle timeout (default: 60s)
  • MaxConnectionIdle: Max connection idle time (default: 15m)
  • MaxConnectionAge: Max connection age (default: 30m)
  • MaxConnectionAgeGrace: Connection age grace period (default: 5s)
Features
  • EnableMetrics: Enable Prometheus metrics (default: true)
  • MetricsPath: Metrics endpoint path (default: "/metrics")
  • EnableHealthCheck: Enable health check endpoints (default: true)
  • HealthPath: Health check path (default: "/health")
  • EnableLogging: Enable request logging (default: true)
  • EnableReflection: Enable gRPC reflection (default: true)
Echo-Specific Features
  • EnableCORS: Enable CORS middleware (default: false)
  • EnableRateLimit: Enable rate limiting middleware (default: false)
  • RateLimit: Requests per second for rate limiting (default: 100.0)
  • Middleware: Custom Echo middleware functions
Gateway Configuration
  • GatewayBasePath: Base path for gRPC gateway routes (default: "/api/v1")
Customization Hooks
  • ServiceRegistrar: Function to register gRPC services
  • GRPCConfigurer: Function to customize gRPC server
  • EchoConfigurer: Function to configure Echo server and add custom routes
  • Shutdown: Custom shutdown handler

Health Checks

The server provides comprehensive health check endpoints:

  • GET /health - Overall health status
  • GET /health/ready - Readiness probe
  • GET /health/live - Liveness probe
Custom Health Checks
server, err := grpcserver.New(config)
if err != nil {
    log.Fatal(err)
}

healthManager := server.GetHealthManager()

// Register custom health check
healthManager.RegisterCheck("database", func() grpcserver.HealthCheckResult {
    // Check database connectivity
    return grpcserver.HealthCheckResult{
        Status: grpcserver.HealthStatusUp,
        Details: map[string]interface{}{
            "connection": "active",
            "latency": "5ms",
        },
    }
})

// Start the server
if err := server.Start(); err != nil {
    log.Fatal(err)
}

Metrics

Built-in Prometheus metrics include:

gRPC Metrics
  • grpc_server_grpc_requests_total - Total gRPC requests by method and status
  • grpc_server_grpc_request_duration_seconds - Request duration histogram
  • grpc_server_grpc_request_size_bytes - Request payload size histogram
  • grpc_server_grpc_response_size_bytes - Response payload size histogram
  • grpc_server_grpc_active_connections - Active gRPC connections
HTTP Metrics (Echo)
  • grpc_server_http_requests_total - Total HTTP requests by method, path, and status
  • grpc_server_http_request_duration_seconds - Request duration histogram
  • grpc_server_http_request_size_bytes - Request payload size histogram
  • grpc_server_http_response_size_bytes - Response payload size histogram
  • grpc_server_http_active_requests - Active HTTP requests
Server Metrics
  • grpc_server_uptime_seconds - Server uptime in seconds
  • grpc_server_start_time_seconds - Server start timestamp

Access metrics at http://localhost:{port}/metrics

API Reference

Quick Start Functions
Start(port string, serviceRegistrar func(*grpc.Server)) error

Starts a server in H2C mode with default configuration.

grpcserver.Start("8080", func(s *grpc.Server) {
    // Register services
})
StartH2C(port string, serviceRegistrar func(*grpc.Server)) error

Explicitly starts a server in H2C mode.

grpcserver.StartH2C("8080", serviceRegistrar)
StartSeparate(grpcPort, httpPort string, serviceRegistrar func(*grpc.Server)) error

Starts a server in separate mode with different ports for gRPC and HTTP.

grpcserver.StartSeparate("9090", "9091", serviceRegistrar)
StartWithConfig(config Config) error

Starts a server with custom configuration.

config := grpcserver.DefaultConfig()
// Configure...
grpcserver.StartWithConfig(config)
Advanced Usage Functions
New(config Config) (*Server, error)

Creates a new server instance without starting it. Useful for advanced control and testing.

server, err := grpcserver.New(config)
if err != nil {
    log.Fatal(err)
}

// Access managers before starting
healthManager := server.GetHealthManager()
metricsManager := server.GetMetricsManager()

// Start when ready
if err := server.Start(); err != nil {
    log.Fatal(err)
}
DefaultConfig() Config

Returns a configuration with sensible defaults.

config := grpcserver.DefaultConfig()
config.GRPCPort = "50051"
Types
Config

Main configuration struct with all server options. See Configuration Options section above.

Server

Server instance with methods:

  • Start() error - Start the server
  • Stop() error - Gracefully stop the server
  • GetHealthManager() *HealthManager - Get health check manager
  • GetMetricsManager() *MetricsManager - Get metrics manager
  • GetGRPCServer() *grpc.Server - Get underlying gRPC server
  • IsRunning() bool - Check if server is running
ServerMode

Server mode enumeration:

  • H2CMode - Single port HTTP/2 cleartext mode (gRPC + HTTP on same port)
  • SeparateMode - Separate ports for gRPC and HTTP

Architecture

H2C Mode Architecture
Client Request → Port 8080
                    ↓
            HTTP/2 Cleartext Handler
                    ↓
         ┌──────────┴──────────┐
         ↓                     ↓
    gRPC Server           Echo HTTP Server
    (application/grpc)    (HTTP/1.1 & HTTP/2)
         ↓                     ↓
    Your Services        - gRPC Gateway
                        - Health Checks
                        - Metrics
                        - Custom Routes
Separate Mode Architecture
Client Request
    ↓
    ├─→ Port 9090 → gRPC Server → Your Services
    │
    └─→ Port 9091 → Echo HTTP Server → - gRPC Gateway
                                       - Health Checks
                                       - Metrics
                                       - Custom Routes

Examples

The examples/ directory contains a complete calculator service demonstrating:

  • Unary RPC: Basic request-response operations (Add, Subtract, Multiply, Divide)
  • Server Streaming: Server sends multiple responses (Factorial)
  • Client Streaming: Client sends multiple requests (Sum)
  • Bidirectional Streaming: Both send multiple messages (RunningAverage)
  • Echo Integration: Custom HTTP routes alongside gRPC
  • Full Observability: Health checks and metrics
Running the Example
# Navigate to examples directory
cd examples

# Run the server
go run -tags examples cmd/server/main.go

# In another terminal, run the client
go run -tags examples cmd/client/main.go

# Test HTTP endpoints
curl http://localhost:50051/status
curl http://localhost:50051/health
curl http://localhost:50051/metrics
curl http://localhost:50051/calculator

Testing

The package includes comprehensive tests covering:

  • Server lifecycle (start, stop, restart)
  • Configuration validation
  • Health check functionality
  • Metrics collection
  • H2C and Separate modes
  • Graceful shutdown

Run tests:

go test ./...

Best Practices

  1. Use H2C Mode for Development: Simplifies local testing with a single port
  2. Use Separate Mode for Production: Better isolation and flexibility
  3. Enable All Observability Features: Metrics and health checks are essential for production
  4. Configure Timeouts: Set appropriate timeouts based on your service requirements
  5. Use gRPC Reflection in Development: Makes testing with tools like grpcurl easier
  6. Disable Reflection in Production: Security best practice
  7. Add Custom Health Checks: Monitor critical dependencies (database, cache, etc.)
  8. Use Echo Middleware: Leverage Echo's rich middleware ecosystem

Dependencies

  • google.golang.org/grpc - gRPC framework
  • github.com/grpc-ecosystem/grpc-gateway/v2 - gRPC gateway
  • github.com/labstack/echo/v4 - Echo HTTP framework
  • github.com/prometheus/client_golang - Prometheus metrics
  • golang.org/x/net - HTTP/2 support

License

This package is part of the jasoet/pkg project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateGatewayMux

func CreateGatewayMux() *runtime.ServeMux

CreateGatewayMux creates a new gateway mux with standard configuration

func DefaultHealthCheckers

func DefaultHealthCheckers() map[string]HealthChecker

DefaultHealthCheckers returns a set of default health checkers

func GatewayHealthMiddleware

func GatewayHealthMiddleware() echo.MiddlewareFunc

GatewayHealthMiddleware adds health monitoring for gateway routes

func LogGatewayRoutes

func LogGatewayRoutes(basePath string, services []string)

LogGatewayRoutes logs information about mounted gateway routes

func MountGatewayOnEcho

func MountGatewayOnEcho(e *echo.Echo, gatewayMux *runtime.ServeMux, basePath string)

MountGatewayOnEcho mounts a gRPC gateway mux onto Echo under a base path

func MountGatewayWithStripPrefix

func MountGatewayWithStripPrefix(e *echo.Echo, gatewayMux *runtime.ServeMux, mountPath, stripPrefix string)

MountGatewayWithStripPrefix mounts gateway with path prefix stripping

func SetupGatewayForH2C

func SetupGatewayForH2C(ctx context.Context, gatewayMux *runtime.ServeMux, serviceRegistrar func(*grpc.Server), grpcServer *grpc.Server) error

SetupGatewayForH2C sets up gateway for H2C mode (server-side registration)

func SetupGatewayForSeparate

func SetupGatewayForSeparate(ctx context.Context, gatewayMux *runtime.ServeMux, grpcEndpoint string) error

SetupGatewayForSeparate sets up gateway for separate mode (endpoint-based registration)

func Start

func Start(port string, serviceRegistrar func(*grpc.Server), opts ...Option) error

Start creates and starts a server with the given options

func StartH2C

func StartH2C(port string, serviceRegistrar func(*grpc.Server), opts ...Option) error

StartH2C creates and starts a server in H2C mode with custom service registrar

func StartSeparate

func StartSeparate(grpcPort, httpPort string, serviceRegistrar func(*grpc.Server), opts ...Option) error

StartSeparate creates and starts a server in separate mode with custom service registrar

Types

type GatewayRoute

type GatewayRoute struct {
	Path        string
	StripPrefix string
	Description string
}

GatewayRoute represents a single gateway route configuration

type HealthCheckResult

type HealthCheckResult struct {
	Status    HealthStatus           `json:"status"`
	Timestamp time.Time              `json:"timestamp"`
	Duration  time.Duration          `json:"duration"`
	Details   map[string]interface{} `json:"details,omitempty"`
	Error     string                 `json:"error,omitempty"`
}

HealthCheckResult represents the result of a health check

type HealthChecker

type HealthChecker func() HealthCheckResult

HealthChecker defines the interface for health check functions

type HealthManager

type HealthManager struct {
	// contains filtered or unexported fields
}

HealthManager manages health checks for the server

func NewHealthManager

func NewHealthManager() *HealthManager

NewHealthManager creates a new health manager

func (*HealthManager) CheckHealth

func (hm *HealthManager) CheckHealth() map[string]HealthCheckResult

CheckHealth runs all registered health checks

func (*HealthManager) CreateEchoHealthHandler

func (h *HealthManager) CreateEchoHealthHandler(checkName string) echo.HandlerFunc

CreateEchoHealthHandler creates a health check handler for specific checks

func (*HealthManager) CreateHealthHandlers

func (hm *HealthManager) CreateHealthHandlers(basePath string) map[string]http.HandlerFunc

CreateHealthHandlers creates HTTP handlers for health check endpoints

func (*HealthManager) EchoHealthCheckMiddleware

func (h *HealthManager) EchoHealthCheckMiddleware() echo.MiddlewareFunc

EchoHealthCheckMiddleware adds health check headers to responses

func (*HealthManager) GetOverallStatus

func (hm *HealthManager) GetOverallStatus() HealthStatus

GetOverallStatus returns the overall health status

func (*HealthManager) RegisterCheck

func (hm *HealthManager) RegisterCheck(name string, checker HealthChecker)

RegisterCheck registers a health check with the given name

func (*HealthManager) RegisterEchoHealthChecks

func (h *HealthManager) RegisterEchoHealthChecks(e *echo.Echo, basePath string)

RegisterEchoHealthChecks registers health check endpoints with Echo

func (*HealthManager) RegisterEchoIndividualHealthChecks

func (h *HealthManager) RegisterEchoIndividualHealthChecks(e *echo.Echo, basePath string)

RegisterEchoIndividualHealthChecks registers individual health check endpoints

func (*HealthManager) RemoveCheck

func (hm *HealthManager) RemoveCheck(name string)

RemoveCheck removes a health check

func (*HealthManager) SetEnabled

func (hm *HealthManager) SetEnabled(enabled bool)

SetEnabled enables or disables health checks

type HealthStatus

type HealthStatus string

HealthStatus represents the status of a health check

const (
	// HealthStatusUp indicates the service is healthy
	HealthStatusUp HealthStatus = "UP"
	// HealthStatusDown indicates the service is unhealthy
	HealthStatusDown HealthStatus = "DOWN"
	// HealthStatusUnknown indicates the health status is unknown
	HealthStatusUnknown HealthStatus = "UNKNOWN"
)

type MetricsManager

type MetricsManager struct {
	// contains filtered or unexported fields
}

MetricsManager manages Prometheus metrics for the gRPC server

func NewMetricsManager

func NewMetricsManager(namespace string) *MetricsManager

NewMetricsManager creates a new metrics manager

func (*MetricsManager) CreateMetricsHandler

func (mm *MetricsManager) CreateMetricsHandler() http.Handler

CreateMetricsHandler creates an HTTP handler for the metrics endpoint

func (*MetricsManager) DecrementGRPCConnections

func (mm *MetricsManager) DecrementGRPCConnections()

DecrementGRPCConnections decrements the active gRPC connections counter

func (*MetricsManager) DecrementHTTPRequests

func (mm *MetricsManager) DecrementHTTPRequests()

DecrementHTTPRequests decrements the active HTTP requests counter

func (*MetricsManager) EchoMetricsMiddleware

func (m *MetricsManager) EchoMetricsMiddleware() echo.MiddlewareFunc

EchoMetricsMiddleware returns Echo middleware that records HTTP metrics

func (*MetricsManager) EchoUptimeMiddleware

func (m *MetricsManager) EchoUptimeMiddleware() echo.MiddlewareFunc

EchoUptimeMiddleware can be used to track server uptime via Echo

func (*MetricsManager) GetRegistry

func (mm *MetricsManager) GetRegistry() *prometheus.Registry

GetRegistry returns the prometheus registry

func (*MetricsManager) HTTPMetricsMiddleware

func (mm *MetricsManager) HTTPMetricsMiddleware(next http.Handler) http.Handler

HTTPMetricsMiddleware creates middleware for recording HTTP metrics

func (*MetricsManager) IncrementGRPCConnections

func (mm *MetricsManager) IncrementGRPCConnections()

IncrementGRPCConnections increments the active gRPC connections counter

func (*MetricsManager) IncrementHTTPRequests

func (mm *MetricsManager) IncrementHTTPRequests()

IncrementHTTPRequests increments the active HTTP requests counter

func (*MetricsManager) RecordGRPCRequest

func (mm *MetricsManager) RecordGRPCRequest(method string, statusCode string, duration time.Duration, requestSize, responseSize int)

RecordGRPCRequest records metrics for a gRPC request

func (*MetricsManager) RecordHTTPRequest

func (mm *MetricsManager) RecordHTTPRequest(method, path string, statusCode int, duration time.Duration, requestSize, responseSize int)

RecordHTTPRequest records metrics for an HTTP request

func (*MetricsManager) RegisterEchoMetrics

func (m *MetricsManager) RegisterEchoMetrics(e *echo.Echo, path string)

RegisterEchoMetrics registers the Prometheus metrics endpoint with Echo

func (*MetricsManager) UpdateUptime

func (mm *MetricsManager) UpdateUptime()

UpdateUptime updates the server uptime metric

type Option

type Option func(*config)

Option is a functional option for configuring the server

func WithCORS

func WithCORS() Option

WithCORS enables CORS middleware

func WithConnectionTimeouts

func WithConnectionTimeouts(idle, age, grace time.Duration) Option

WithConnectionTimeouts sets all gRPC connection timeout values

func WithEchoConfigurer

func WithEchoConfigurer(fn func(*echo.Echo)) Option

WithEchoConfigurer sets the function to configure the Echo HTTP server

func WithGRPCConfigurer

func WithGRPCConfigurer(fn func(*grpc.Server)) Option

WithGRPCConfigurer sets the function to configure the gRPC server

func WithGRPCPort

func WithGRPCPort(port string) Option

WithGRPCPort sets the gRPC server port

func WithGatewayBasePath

func WithGatewayBasePath(path string) Option

WithGatewayBasePath sets the base path for gRPC gateway routes

func WithH2CMode

func WithH2CMode() Option

WithH2CMode sets the server to H2C mode (gRPC and HTTP on same port)

func WithHTTPPort

func WithHTTPPort(port string) Option

WithHTTPPort sets the HTTP gateway port (only used in SeparateMode)

func WithHealthCheck

func WithHealthCheck() Option

WithHealthCheck enables health check endpoints

func WithHealthPath

func WithHealthPath(path string) Option

WithHealthPath sets the health check base path

func WithIdleTimeout

func WithIdleTimeout(d time.Duration) Option

WithIdleTimeout sets the HTTP server idle timeout

func WithLogging

func WithLogging() Option

WithLogging enables request/response logging

func WithMaxConnectionAge

func WithMaxConnectionAge(d time.Duration) Option

WithMaxConnectionAge sets the maximum connection age for gRPC

func WithMaxConnectionAgeGrace

func WithMaxConnectionAgeGrace(d time.Duration) Option

WithMaxConnectionAgeGrace sets the connection age grace period for gRPC

func WithMaxConnectionIdle

func WithMaxConnectionIdle(d time.Duration) Option

WithMaxConnectionIdle sets the maximum connection idle time for gRPC

func WithMetrics

func WithMetrics() Option

WithMetrics enables Prometheus metrics endpoint

func WithMetricsPath

func WithMetricsPath(path string) Option

WithMetricsPath sets the metrics endpoint path

func WithMiddleware

func WithMiddleware(mw ...echo.MiddlewareFunc) Option

WithMiddleware adds custom Echo middleware

func WithOTelConfig

func WithOTelConfig(cfg *otel.Config) Option

WithOTelConfig sets the OpenTelemetry configuration for traces, metrics, and logs When set, the gRPC server will instrument with OTel instead of Prometheus

func WithRateLimit

func WithRateLimit(rps float64) Option

WithRateLimit enables rate limiting with the specified requests per second

func WithReadTimeout

func WithReadTimeout(d time.Duration) Option

WithReadTimeout sets the HTTP server read timeout

func WithReflection

func WithReflection() Option

WithReflection enables gRPC server reflection

func WithSeparateMode

func WithSeparateMode(grpcPort, httpPort string) Option

WithSeparateMode sets the server to separate mode with different ports for gRPC and HTTP

func WithServiceRegistrar

func WithServiceRegistrar(fn func(*grpc.Server)) Option

WithServiceRegistrar sets the function to register gRPC services

func WithShutdownHandler

func WithShutdownHandler(fn func() error) Option

WithShutdownHandler sets a custom shutdown handler

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout sets the graceful shutdown timeout

func WithTLS

func WithTLS(certFile, keyFile string) Option

WithTLS enables TLS with the specified certificate and key files

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout sets the HTTP server write timeout

func WithoutHealthCheck

func WithoutHealthCheck() Option

WithoutHealthCheck disables health check endpoints

func WithoutLogging

func WithoutLogging() Option

WithoutLogging disables request/response logging

func WithoutMetrics

func WithoutMetrics() Option

WithoutMetrics disables Prometheus metrics endpoint

func WithoutReflection

func WithoutReflection() Option

WithoutReflection disables gRPC server reflection

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the gRPC server and gateway

func New

func New(opts ...Option) (*Server, error)

New creates a new server instance with the given options

func (*Server) GetGRPCServer

func (s *Server) GetGRPCServer() *grpc.Server

GetGRPCServer returns the underlying gRPC server

func (*Server) GetHealthManager

func (s *Server) GetHealthManager() *HealthManager

GetHealthManager returns the health manager

func (*Server) GetMetricsManager

func (s *Server) GetMetricsManager() *MetricsManager

GetMetricsManager returns the metrics manager

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the server is running

func (*Server) Start

func (s *Server) Start() error

Start starts the server with the configured mode

func (*Server) Stop

func (s *Server) Stop() error

Stop gracefully stops the server

type ServerMode

type ServerMode string

ServerMode defines the server operation mode

const (
	// SeparateMode runs gRPC and HTTP servers on separate ports
	SeparateMode ServerMode = "separate"
	// H2CMode runs both gRPC and HTTP on a single port using HTTP/2 cleartext
	H2CMode ServerMode = "h2c"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL