Skip to content

go-coldbrew/core

ColdBrew

CI Go Report Card GoDoc License: MIT

A Go microservice framework for building production-grade gRPC services with built-in observability, resilience, and HTTP gateway support.

ColdBrew powers 100+ microservices serving 70k+ QPS each in production. It provides a batteries-included foundation so you can focus on business logic instead of boilerplate.

Packages

ColdBrew is a collection of composable packages:

Package Description
core gRPC server, HTTP gateway, health checks, Prometheus metrics, graceful shutdown
interceptors Chained gRPC interceptors: logging, tracing, Prometheus, circuit breaking, retries
errors Enhanced errors with stack traces, gRPC status codes, error notification
log Structured logging with pluggable backends (zap, logrus, go-kit)
tracing Distributed tracing: OpenTelemetry, OpenTracing, NewRelic
options Request-scoped key-value metadata via context
grpcpool Round-robin gRPC connection pool
data-builder Dependency injection with automatic resolution and parallel execution

Quick Start

# Generate a new service from the template
pip install cookiecutter
cookiecutter gh:go-coldbrew/cookiecutter-coldbrew

# Build and run
cd YourApp
make run

Your service starts with gRPC on :9090, HTTP gateway on :9091, Prometheus metrics at /metrics, and health checks at /healthcheck and /readycheck.

Documentation


API Reference

core

import "github.com/go-coldbrew/core"

Package core is the main entry point for the ColdBrew microservice framework. It creates a gRPC server with an HTTP gateway (via grpc-gateway), wires health checks, Prometheus metrics, pprof endpoints, signal handling, graceful shutdown, and all interceptors. Services implement the CBService interface to register their gRPC and HTTP handlers.

ColdBrew builds on proven open-source libraries:

Usage

cb := core.New(config.Config{
    GRPCPort:    "9090",
    HTTPPort:    "9091",
    ServiceName: "my-service",
})
cb.SetService(myService)
cb.Run()

For full documentation, visit https://docs.coldbrew.cloud

Index

func ConfigureInterceptors(DoNotLogGRPCReflection bool, traceHeaderName string)

ConfigureInterceptors configures the interceptors package with the provided DoNotLogGRPCReflection is a boolean that indicates whether to log the grpc.reflection.v1alpha.ServerReflection service calls in logs traceHeaderName is the name of the header to use for tracing (e.g. X-Trace-Id) - if empty, defaults to X-Trace-Id

func InitializeVTProto()

InitializeVTProto initializes the vtproto package for use with the service

https://github.com/planetscale/vtprotobuf?tab=readme-ov-file#mixing-protobuf-implementations-with-grpc

func SetupAutoMaxProcs()

SetupAutoMaxProcs sets up the GOMAXPROCS to match Linux container CPU quota This is used to set the GOMAXPROCS to the number of CPUs allocated to the container

func SetupEnvironment(env string)

SetupEnvironment sets the environment This is used to identify the environment in Sentry and New Relic env is the environment to set for the service (e.g. prod, staging, dev)

func SetupHystrixPrometheus()

SetupHystrixPrometheus sets up the hystrix metrics This is a workaround for hystrix-go not supporting the prometheus registry It uses sync.Once to ensure the Prometheus collectors are only registered once, since duplicate registration panics.

func SetupLogger(logLevel string, jsonlogs bool) error

SetupLogger sets up the logger It uses the coldbrew logger to log messages to stdout logLevel is the log level to set for the logger jsonlogs is a boolean to enable or disable json logs

func SetupNROpenTelemetry(serviceName, license, version string, ratio float64) error

SetupNROpenTelemetry sets up OpenTelemetry tracing with New Relic

This function configures OpenTelemetry to send traces to New Relic's OTLP endpoint. It's a convenience wrapper around SetupOpenTelemetry with New Relic-specific configuration.

Parameters:

  • serviceName: the name of the service
  • license: the New Relic license key
  • version: the version of the service
  • ratio: the sampling ratio to use for traces (0.0 to 1.0)

func SetupNewRelic(serviceName, apiKey string, tracing bool) error

SetupNewRelic sets up the New Relic tracing and monitoring agent for the service It uses the New Relic Go Agent to send traces to New Relic One APM and Insights serviceName is the name of the service apiKey is the New Relic license key tracing is a boolean to enable or disable tracing

func SetupOpenTelemetry(config OTLPConfig) error

SetupOpenTelemetry sets up OpenTelemetry tracing with a generic OTLP exporter

This function provides a flexible way to configure OpenTelemetry tracing with any OTLP-compatible backend. It sets up the trace provider, configures sampling, and optionally sets up an OpenTracing bridge for compatibility.

Example usage with Jaeger:

config := OTLPConfig{
    Endpoint:             "localhost:4317",
    ServiceName:          "my-service",
    ServiceVersion:       "v1.0.0",
    SamplingRatio:        0.1,
    UseOpenTracingBridge: true,
    Insecure:            true,  // for local development
}
err := SetupOpenTelemetry(config)

Example usage with Honeycomb:

config := OTLPConfig{
    Endpoint:       "api.honeycomb.io:443",
    Headers:        map[string]string{"x-honeycomb-team": "your-api-key"},
    ServiceName:    "my-service",
    ServiceVersion: "v1.0.0",
    SamplingRatio:  0.2,
}
err := SetupOpenTelemetry(config)

func SetupReleaseName(rel string)

SetupReleaseName sets the release name This is used to identify the release in Sentry rel is the release name to set for the service (e.g. v1.0.0)

func SetupSentry(dsn string)

SetupSentry sets up the Sentry notifier It uses the Sentry HTTP Transport to send errors to Sentry server dsn is the Sentry DSN to use for sending errors

type CB

CB is the interface that wraps coldbrew methods.

type CB interface {
    // SetService sets the service.
    SetService(CBService) error
    // Run runs the service.
    // Run is blocking. It returns an error if the service fails. Otherwise, it returns nil.
    Run() error
    // SetOpenAPIHandler sets the OpenAPI handler.
    SetOpenAPIHandler(http.Handler)
    // Stop stops the service.
    // Stop is blocking. It returns an error if the service fails. Otherwise, it returns nil.
    // duration is the duration to wait for the service to stop.
    Stop(time.Duration) error
}

func New

func New(c config.Config) CB

New creates a new ColdBrew object It takes a config object and returns a CB interface The CB interface is used to start and stop the server The CB interface also provides a way to add services to the server The services are added using the AddService method The services are started and stopped in the order they are added

CBGracefulStopper is the interface that wraps the graceful stop method.

type CBGracefulStopper interface {
    // FailCheck set if the service is ready to stop.
    // FailCheck is called by the core package.
    FailCheck(bool)
}

CBService is the interface that wraps service methods used in ColdBrew. InitHTTP initializes the HTTP server. InitGRPC initializes the gRPC server. InitHTTP and InitGRPC are called by the core package.

type CBService interface {
    // InitHTTP initializes the HTTP server
    // mux is the HTTP server mux to register the service.
    // endpoint is the gRPC endpoint to connect.
    // opts is the gRPC dial options used to connect to the endpoint.
    InitHTTP(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error
    // InitGRPC initializes the gRPC server
    // server is the gRPC server to register the service.
    InitGRPC(ctx context.Context, server *grpc.Server) error
}

CBStopper is the interface that wraps the stop method.

type CBStopper interface {
    // Stop stops the service.
    // Stop is called by the core package.
    Stop()
}

OTLPConfig holds configuration for OpenTelemetry OTLP exporter

This struct provides a flexible way to configure OpenTelemetry tracing with any OTLP-compatible backend (e.g., Jaeger, Honeycomb, New Relic, etc.)

type OTLPConfig struct {
    // Endpoint is the OTLP gRPC endpoint to send traces to
    // Examples: "localhost:4317", "otlp.nr-data.net:4317", "api.honeycomb.io:443"
    Endpoint string

    // Headers are custom headers to send with each request
    // Examples:
    //   New Relic: {"api-key": "your-license-key"}
    //   Honeycomb: {"x-honeycomb-team": "your-api-key"}
    Headers map[string]string

    // ServiceName is the name of the service sending traces
    ServiceName string

    // ServiceVersion is the version of the service
    ServiceVersion string

    // SamplingRatio is the ratio of traces to sample (0.0 to 1.0)
    // 1.0 means sample all traces, 0.1 means sample 10% of traces
    SamplingRatio float64

    // Compression specifies the compression type (e.g., "gzip", "none")
    // If empty, defaults to "gzip"
    Compression string

    // UseOpenTracingBridge determines whether to set up OpenTracing compatibility bridge
    // This allows using OpenTracing instrumentation with OpenTelemetry
    UseOpenTracingBridge bool

    // Insecure disables TLS verification for the connection
    // Only use this for local development or testing
    Insecure bool
}

Generated by gomarkdoc

Contributors