Documentation
¶
Overview ¶
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:
- github.com/grpc-ecosystem/grpc-gateway — REST gateway for gRPC services
- github.com/prometheus/client_golang — Prometheus metrics
- go.opentelemetry.io/otel — Distributed tracing via OpenTelemetry
- github.com/newrelic/go-agent — New Relic APM integration
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 ¶
- Constants
- func InitializeVTProto()
- func OTELMeterProvider() otelmetric.MeterProvider
- func SetOTELGRPCClientOptions(opts ...otelgrpc.Option)deprecated
- func SetOTELGRPCServerOptions(opts ...otelgrpc.Option)deprecated
- func SetOTELOptions(opts grpcotel.Options)
- func SetupAutoMaxProcs()
- func SetupEnvironment(env string)
- func SetupHystrixPrometheus()
- func SetupLogger(logLevel string, jsonlogs bool) error
- func SetupNROpenTelemetry(serviceName, license, version string, ratio float64) error
- func SetupNewRelic(serviceName, apiKey string, tracing bool) error
- func SetupOTELMetrics(config OTLPConfig, interval time.Duration) (*sdkmetric.MeterProvider, error)
- func SetupOpenTelemetry(config OTLPConfig) error
- func SetupReleaseName(rel string)
- func SetupSentry(dsn string)
- type CB
- type CBGracefulStopper
- type CBPostStarter
- type CBPostStopper
- type CBPreStarter
- type CBPreStopper
- type CBService
- type CBStopper
- type CBWorkerProvider
- type OTLPConfig
Constants ¶
const SupportPackageIsVersion1 = true
SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages reference this to enforce version compatibility.
Variables ¶
This section is empty.
Functions ¶
func InitializeVTProto ¶ added in v0.1.23
func InitializeVTProto()
InitializeVTProto initializes the vtproto package for use with the service
func OTELMeterProvider ¶ added in v0.1.46
func OTELMeterProvider() otelmetric.MeterProvider
OTELMeterProvider returns the global OTel MeterProvider. This is a convenience accessor for code that needs the interface type.
func SetOTELGRPCClientOptions
deprecated
added in
v0.1.36
func SetOTELGRPCServerOptions
deprecated
added in
v0.1.36
func SetOTELOptions ¶ added in v0.1.46
SetOTELOptions configures the native gRPC stats/opentelemetry integration. Must be called during init, before the gRPC server starts. When set, processConfig() will NOT overwrite these with auto-built options.
func SetupAutoMaxProcs ¶ added in v0.1.19
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 ¶ added in v0.1.18
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 ¶ added in v0.1.18
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 ¶ added in v0.1.18
SetupLogger sets up the logger using ColdBrew's slog-native Handler. It calls log.SetDefault which also wires slog.SetDefault, so native slog.LogAttrs calls automatically get ColdBrew context fields. logLevel is the log level to set for the logger jsonlogs is a boolean to enable or disable json logs
func SetupNROpenTelemetry ¶ added in v0.1.18
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 ¶ added in v0.1.18
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 SetupOTELMetrics ¶ added in v0.1.46
func SetupOTELMetrics(config OTLPConfig, interval time.Duration) (*sdkmetric.MeterProvider, error)
SetupOTELMetrics creates a MeterProvider with an OTLP gRPC exporter that reuses the same resource as the TracerProvider (set by SetupOpenTelemetry). The MeterProvider is set as the global OTel MeterProvider.
Call this after SetupOpenTelemetry so the shared resource is available.
func SetupOpenTelemetry ¶ added in v0.1.28
func SetupOpenTelemetry(config OTLPConfig) error
SetupOpenTelemetry sets up OpenTelemetry tracing with a generic OTLP exporter.
It configures a TracerProvider with the given sampling ratio and OTLP backend, sets it as the global provider, and stores it for graceful shutdown.
Example usage with Jaeger:
config := OTLPConfig{
Endpoint: "localhost:4317",
ServiceName: "my-service",
ServiceVersion: "v1.0.0",
SamplingRatio: 0.1,
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 ¶ added in v0.1.18
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 ¶ added in v0.1.18
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
Types ¶
type CB ¶
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
}
CB is the interface that wraps coldbrew methods.
func New ¶
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
type CBGracefulStopper ¶ added in v0.1.7
type CBGracefulStopper interface {
// FailCheck set if the service is ready to stop.
// FailCheck is called by the core package.
FailCheck(bool)
}
CBGracefulStopper is the interface that wraps the graceful stop method.
type CBPostStarter ¶ added in v0.2.0
CBPostStarter is implemented by services that need to act after servers are listening. Use this for registering with service discovery, logging startup banners, or notifying external systems.
type CBPostStopper ¶ added in v0.2.0
CBPostStopper is implemented by services that need final cleanup after all servers and workers have stopped. Use this for closing audit logs, pushing final metrics, or any cleanup that must happen after all in-flight work is complete.
type CBPreStarter ¶ added in v0.2.0
CBPreStarter is implemented by services that need setup before servers start. Called during Run(), before initGRPC/initHTTP. If PreStart returns an error, startup is aborted. Use this for connecting to databases, message brokers, configuring interceptors, or any setup that must complete before the service accepts traffic.
type CBPreStopper ¶ added in v0.2.0
CBPreStopper is implemented by services that need to act before graceful shutdown begins. Use this for deregistering from load balancers, flushing buffers, or notifying external systems of impending shutdown.
type CBService ¶
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
}
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 CBStopper ¶ added in v0.1.1
type CBStopper interface {
// Stop stops the service.
// Stop is called by the core package.
Stop()
}
CBStopper is the interface that wraps the stop method.
type CBWorkerProvider ¶ added in v0.2.0
CBWorkerProvider is implemented by services that run background workers. Workers are started after initGRPC/initHTTP and stopped during graceful shutdown. Called once during Run(). Workers are managed by the go-coldbrew/workers package with automatic panic recovery, configurable restart, and structured shutdown via suture supervisor trees.
type OTLPConfig ¶ added in v0.1.28
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
// Insecure disables TLS verification for the connection
// Only use this for local development or testing
Insecure bool
}
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.)