GoFr Framework

repository·development·Indexed 12 days ago

https://github.com/gofr-dev/gofr

An opinionated microservice development framework for Go designed to simplify the creation of scalable services. It features built-in support for Kubernetes deployment, out-of-the-box observability, gRPC (unary and streaming), and various datasources including Redis, MySQL, and Google Cloud SQL. GoFr provides utilities for automatic CRUD generation via AddRESTHandlers, background task scheduling with AddCronJob, and OTLP metrics exporting to backends like Datadog, Grafana Cloud, and New Relic.

Tokens
272.3K
Snippets
880
Records
1.2K
Agent score
97%

What's inside GoFr

  1. Overview of GoFr features

    development

    GoFr is an opinionated microservice development framework optimized for Kubernetes deployment and observability. Key features include:

    • Observability: Built-in support for Logs, Traces, and Metrics.
    • Communication: REST standards, gRPC support, HTTP services with Circuit Breaker, Websockets, and Pub/Sub.
    • Middleware: Inbuilt Auth middleware and support for custom middleware.
    • Data Management: Database migrations, health checks for all datasources, and abstracted file systems.
    • Operational Tools: Cron jobs, Swagger rendering, and the ability to change log levels remotely without restarting the service.
  2. Get started with GoFr

    development
    GoFr is an opinionated Go (Golang) framework designed for production microservice development. It provides built-in support for observability, various datasource clients, gRPC, GraphQL, WebSockets, Pub/Sub, and zero-boilerplate REST handlers. The framework focuses on simplicity, scalability, and providing a user-friendly abstraction for developers.
  3. Core features included in GoFr

    development

    GoFr provides a comprehensive set of built-in capabilities for microservices:

    • Protocols: HTTP, gRPC, GraphQL, WebSockets, and CLI.
    • Auto CRUD: app.AddRESTHandlers(&Entity{}) generates Create, Get, GetAll, Update, and Delete endpoints from a struct.
    • Observability: Built-in OpenTelemetry traces (OTLP/Jaeger), Prometheus metrics, and structured contextual logging with configurable sampling and remote log-level changes.
    • Datasources (15+): Auto-instrumented support for MySQL, PostgreSQL, Oracle, SQLite, MongoDB, Redis, Cassandra, ScyllaDB, ClickHouse, CockroachDB, Couchbase, DGraph, SurrealDB, ArangoDB, Elasticsearch, Solr, and InfluxDB. KV-store backends include Badger, DynamoDB, and NATS.
    • Pub/Sub: Kafka, NATS JetStream, Google Pub/Sub, AWS SQS, MQTT, and Azure Event Hub.
    • File Storage: Unified interface for local filesystem, Amazon S3, Google Cloud Storage, Azure Blob, FTP, and SFTP.
    • Resilience: Service-to-service HTTP client with circuit breakers, retries, rate limits, and connection pooling.
    • Migrations: Versioned migrations for SQL, MongoDB, Redis, DGraph, and more.
    • Auth & RBAC: Basic, API key, and OAuth (JWKS-validated JWT) with config-driven role/permission mappings.
    • Developer Tools: Built-in Swagger UI (renders openapi.json from static/ or /.well-known/swagger) and Cron jobs with auto-instrumented spans.
  4. Use GoFr Observability Features

    development

    Unlike Rails, where observability is often wired manually, GoFr provides built-in observability:

    • Traces: Automatically emits OpenTelemetry traces.
    • Metrics: Prometheus metrics are available at the /metrics endpoint.
    • Logs: Structured JSON logs including trace IDs.
    • Health Checks: Available at /.well-known/health.
    • Runtime Log Control: Log levels can be changed at runtime via a remote log-level endpoint.
  5. Key features of GoFr

    development

    GoFr includes several built-in capabilities for production-ready microservices:

    • Logging: Level-based logging support for effective debugging and monitoring.
    • Response Types: Support for various response types, including JSON and FILE.
    • Monitoring: Built-in Health check and Readiness monitoring to ensure continuous service availability.
    • Metrics: Metrics exposure via Prometheus for monitoring and analysis.
    • Tracing: Tracing capabilities to track user request progress using traceable spans.
  6. Comparing GoFr and Fiber

    development

    GoFr and Fiber are both open-source frameworks with different design philosophies:

    • Fiber is an Express-inspired HTTP framework built on fasthttp. It is optimized for high raw HTTP throughput and is ideal for developers coming from a Node.js background. However, because it uses fasthttp, it is not natively compatible with the standard net/http library without using an adapter.
    • GoFr is built on the standard net/http library, ensuring full compatibility with the Go ecosystem. It is a broader production stack that bundles HTTP routing with built-in support for gRPC, GraphQL, Pub/Sub, cron jobs, migrations, and circuit breakers. It also provides out-of-the-box observability (OpenTelemetry and Prometheus) and auto-instrumented datasource clients.
  7. What is Publisher-Subscriber in GoFr

    development

    The Publisher-Subscriber pattern in GoFr is used for asynchronous communication between decoupled entities (different applications or instances of the same application). This pattern allows components to exchange messages without knowing each other's identities, enhancing system flexibility and scalability.

    GoFr supports multiple message brokers to implement this pattern, including:

    • Apache Kafka
    • Google PubSub
    • MQTT
    • NATS JetStream
    • Redis Pub/Sub
    • Azure Event Hubs
    • Amazon SQS
  8. What is the GoFr Unified File Store API?

    development

    GoFr provides a uniform API for interacting with various storage backends, allowing you to read and write files using the same set of methods regardless of the underlying system. This abstraction hides the implementation details of different storage providers.

    Supported Backends:

    • Local Disk: Initialized by default and accessible via the context.
    • FTP/SFTP: For traditional file transfer protocols.
    • Cloud Storage: AWS S3, Google Cloud Storage (GCS), and Azure File Storage.
    • S3-Compatible: Cloudflare R2, MinIO, DigitalOcean Spaces, and others.
  9. Use built-in Health Checks in GoFr gRPC Services and Clients

    development

    GoFr provides built-in health checks for gRPC services to enable observability, monitoring, and inter-service health verification.

    Client-side

    When using a GoFr-generated gRPC client, the client interface includes a health interface. This allows you to perform health checks on the remote service using Check and Watch methods.

    Server-side

    GoFr's gRPC server implementation includes a healthServer that supports standard gRPC health check methods. You can manage the service status using SetServingStatus, and control the health check lifecycle with Shutdown and Resume.

    // Client Interface snippet
    type <SERVICE_NAME>GoFrClient interface {
        SayHello(*gofr.Context, *HelloRequest, ...grpc.CallOption) (*HelloResponse, error)
        health
    }
    
    type health interface {
        Check(ctx *gofr.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error)
        Watch(ctx *gofr.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[grpc_health_v1.HealthCheckResponse], error)
    }
    
    // Server Implementation snippet
    type <SERVICE_NAME>GoFrServer struct {
        health *healthServer
    }
    
    // Supported Server Methods
    func (h *healthServer) Check(ctx *gofr.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error)
    func (h *healthServer) Watch(ctx *gofr.Context, in *grpc_health_v1.HealthCheckRequest, stream grpc_health_v1.Health_WatchServer) error
    func (h *healthServer) SetServingStatus(ctx *gofr.Context, service string, status grpc_health_v1.HealthCheckResponse_ServingStatus)
    func (h *healthServer) Shutdown(ctx *gofr.Context)
    func (h *healthServer) Resume(ctx *gofr.Context)
  10. Observe GraphQL performance with Tracing and Metrics

    development

    GoFr provides built-in observability for GraphQL via OpenTelemetry and Prometheus metrics.

    Tracing

    • Root Span: graphql-request for every request.
    • Resolver Spans: Nested spans for each field (e.g., graphql-resolver-user).
    • Attributes: graphql.operation_name and graphql.operation_type are automatically attached.

    Metrics

    Metrics are tagged by operation_name, type (query/mutation), and status (success or error).

    • app_graphql_operations_total: Total operations received.
    • app_graphql_error_total: Total operations resulting in errors (validation or resolver errors).
    • app_graphql_request_duration: Histogram of request lifecycle in seconds.

    Note on Status Labels: In GraphQL metrics, success means the request returned no errors in the errors array, even if the HTTP status was 200 OK.

  11. Handle Graceful Shutdown in Kubernetes

    development

    When Kubernetes terminates a pod, it sends a SIGTERM signal. GoFr's app.Run() listens for this signal and begins a graceful shutdown by stopping the acceptance of new requests while allowing in-flight requests to complete.

    To ensure GoFr has enough time to drain these requests, you must set the terminationGracePeriodSeconds in your Pod specification to a value higher than your longest expected in-flight request. A value of 45 seconds is a common starting point for standard APIs.

    Deployment Configuration:

    spec:
      template:
        spec:
          terminationGracePeriodSeconds: 45
          containers:
            - name: orders
              # ...
    spec:
      template:
        spec:
          terminationGracePeriodSeconds: 45