Skip to content

Commit

Permalink
Merge pull request #20 from go-coldbrew/feat/maxprocs
Browse files Browse the repository at this point in the history
adding auto max procs
  • Loading branch information
ankurs authored Nov 8, 2023
2 parents 0006f9a + 7aecda8 commit 05f3496
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 105 deletions.
79 changes: 50 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/github.com/go-coldbrew/core)



# core

```go
Expand All @@ -12,7 +14,7 @@ package core is the core module for cold brew and provides the base implementati

The idea behind cold brew is simple, we want to reuse as many good components that we can by providing only a thin wrapper around them if needed.

### The components in use by cold brew currently are
The components in use by cold brew currently are

```
github.com/grpc-ecosystem/grpc-gateway
Expand All @@ -24,7 +26,6 @@ github.com/newrelic/go-agent

The core module provides the base implementation for Cold Brew. It provides the following features

```
- A base implementation for a gRPC server
- A base implementation for a gRPC gateway
- A base implementation for health check
Expand All @@ -34,91 +35,109 @@ The core module provides the base implementation for Cold Brew. It provides the
- A base implementation for a new relic
- A base implementation for a logger
- A base implementation for a gRPC server reflection
```

The core module is the base module for cold brew and provides the base implementation for Cold Brew. It works in conjunction with the other modules to provide the full functionality of Cold Brew. To get started with Cold Brew, you can use cookiecutter to generate a new project from the template. The template can be found at https://github.com/go-coldbrew/cookiecutter-coldbrew

## Index

- [func ConfigureInterceptors(DoNotLogGRPCReflection bool, traceHeaderName string)](<#func-configureinterceptors>)
- [func SetupEnvironment(env string)](<#func-setupenvironment>)
- [func SetupHystrixPrometheus()](<#func-setuphystrixprometheus>)
- [func SetupLogger(logLevel string, jsonlogs bool) error](<#func-setuplogger>)
- [func SetupNROpenTelemetry(serviceName, license, version string, ratio float64) error](<#func-setupnropentelemetry>)
- [func SetupNewRelic(serviceName, apiKey string, tracing bool) error](<#func-setupnewrelic>)
- [func SetupReleaseName(rel string)](<#func-setupreleasename>)
- [func SetupSentry(dsn string)](<#func-setupsentry>)
- [type CB](<#type-cb>)
- [func New(c config.Config) CB](<#func-new>)
- [type CBGracefulStopper](<#type-cbgracefulstopper>)
- [type CBService](<#type-cbservice>)
- [type CBStopper](<#type-cbstopper>)


## func [ConfigureInterceptors](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L200>)
- [func ConfigureInterceptors\(DoNotLogGRPCReflection bool, traceHeaderName string\)](<#ConfigureInterceptors>)
- [func SetupAutoMaxProcs\(\)](<#SetupAutoMaxProcs>)
- [func SetupEnvironment\(env string\)](<#SetupEnvironment>)
- [func SetupHystrixPrometheus\(\)](<#SetupHystrixPrometheus>)
- [func SetupLogger\(logLevel string, jsonlogs bool\) error](<#SetupLogger>)
- [func SetupNROpenTelemetry\(serviceName, license, version string, ratio float64\) error](<#SetupNROpenTelemetry>)
- [func SetupNewRelic\(serviceName, apiKey string, tracing bool\) error](<#SetupNewRelic>)
- [func SetupReleaseName\(rel string\)](<#SetupReleaseName>)
- [func SetupSentry\(dsn string\)](<#SetupSentry>)
- [type CB](<#CB>)
- [func New\(c config.Config\) CB](<#New>)
- [type CBGracefulStopper](<#CBGracefulStopper>)
- [type CBService](<#CBService>)
- [type CBStopper](<#CBStopper>)


<a name="ConfigureInterceptors"></a>
## func [ConfigureInterceptors](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L201>)

```go
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 [SetupEnvironment](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L89>)
<a name="SetupAutoMaxProcs"></a>
## func [SetupAutoMaxProcs](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L212>)

```go
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

<a name="SetupEnvironment"></a>
## func [SetupEnvironment](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L91>)

```go
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](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L192>)
<a name="SetupHystrixPrometheus"></a>
## func [SetupHystrixPrometheus](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L193>)

```go
func SetupHystrixPrometheus()
```

SetupHystrixPrometheus sets up the hystrix metrics This is a workaround for hystrix\-go not supporting the prometheus registry

## func [SetupLogger](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L65>)
<a name="SetupLogger"></a>
## func [SetupLogger](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L67>)

```go
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](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L135>)
<a name="SetupNROpenTelemetry"></a>
## func [SetupNROpenTelemetry](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L137>)

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

setupOpenTelemetry sets up the OpenTelemetry tracing It uses the New Relic OTLP exporter to send traces to New Relic One APM and Insights serviceName is the name of the service license is the New Relic license key version is the version of the service ratio is the sampling ratio to use for traces

## func [SetupNewRelic](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L40>)
<a name="SetupNewRelic"></a>
## func [SetupNewRelic](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L42>)

```go
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 [SetupReleaseName](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L98>)
<a name="SetupReleaseName"></a>
## func [SetupReleaseName](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L100>)

```go
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](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L80>)
<a name="SetupSentry"></a>
## func [SetupSentry](<https://github.com/go-coldbrew/core/blob/main/initializers.go#L82>)

```go
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

<a name="CB"></a>
## type [CB](<https://github.com/go-coldbrew/core/blob/main/types.go#L42-L54>)

CB is the interface that wraps coldbrew methods.
Expand All @@ -139,14 +158,16 @@ type CB interface {
}
```

### func [New](<https://github.com/go-coldbrew/core/blob/main/core.go#L364>)
<a name="New"></a>
### func [New](<https://github.com/go-coldbrew/core/blob/main/core.go#L369>)

```go
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

<a name="CBGracefulStopper"></a>
## type [CBGracefulStopper](<https://github.com/go-coldbrew/core/blob/main/types.go#L28-L32>)

CBGracefulStopper is the interface that wraps the graceful stop method.
Expand All @@ -159,6 +180,7 @@ type CBGracefulStopper interface {
}
```

<a name="CBService"></a>
## type [CBService](<https://github.com/go-coldbrew/core/blob/main/types.go#L16-L25>)

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.
Expand All @@ -176,6 +198,7 @@ type CBService interface {
}
```

<a name="CBStopper"></a>
## type [CBStopper](<https://github.com/go-coldbrew/core/blob/main/types.go#L35-L39>)

CBStopper is the interface that wraps the stop method.
Expand All @@ -188,6 +211,4 @@ type CBStopper interface {
}
```



Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
13 changes: 9 additions & 4 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/github.com/go-coldbrew/core)



# config

```go
Expand All @@ -10,10 +12,11 @@ import "github.com/go-coldbrew/core/config"

## Index

- [type Config](<#type-config>)
- [type Config](<#Config>)


## type [Config](<https://github.com/go-coldbrew/core/blob/main/config/config.go#L6-L80>)
<a name="Config"></a>
## type [Config](<https://github.com/go-coldbrew/core/blob/main/config/config.go#L6-L84>)

Config is the configuration for the Coldbrew server It is populated from environment variables and has sensible defaults for all fields so that you can just use it as is without any configuration The following environment variables are supported and can be used to override the defaults for the fields

Expand Down Expand Up @@ -92,9 +95,11 @@ type Config struct {
// which the connection will be forcibly closed.
// https://github.com/grpc/grpc-go/blob/v1.48.0/keepalive/keepalive.go#L50
GRPCServerMaxConnectionAgeGraceInSeconds int `envconfig:"GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_IN_SECONDS"`

// DisableAutoMaxProcs disables the automatic setting of GOMAXPROCS
// This is useful when running in a container where the container runtime sets GOMAXPROCS for you already
DisableAutoMaxProcs bool `envconfig:"DISABLE_AUTO_MAX_PROCS" default:"false"`
}
```



Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ type Config struct {
// which the connection will be forcibly closed.
// https://github.com/grpc/grpc-go/blob/v1.48.0/keepalive/keepalive.go#L50
GRPCServerMaxConnectionAgeGraceInSeconds int `envconfig:"GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_IN_SECONDS"`

// DisableAutoMaxProcs disables the automatic setting of GOMAXPROCS
// This is useful when running in a container where the container runtime sets GOMAXPROCS for you already
DisableAutoMaxProcs bool `envconfig:"DISABLE_AUTO_MAX_PROCS" default:"false"`
}
15 changes: 10 additions & 5 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/opentracing/opentracing-go/ext"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
)
Expand Down Expand Up @@ -62,6 +63,9 @@ func (c *cb) processConfig() {
if nrName == "" {
nrName = c.config.AppName
}
if !c.config.DisableAutoMaxProcs {
SetupAutoMaxProcs()
}
SetupNewRelic(nrName, c.config.NewRelicLicenseKey, c.config.NewRelicDistributedTracing)
SetupSentry(c.config.SentryDSN)
SetupEnvironment(c.config.Environment)
Expand Down Expand Up @@ -154,7 +158,8 @@ func (c *cb) initHTTP(ctx context.Context) (*http.Server, error) {

mux := runtime.NewServeMux(muxOpts...)

opts := []grpc.DialOption{grpc.WithInsecure(),
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(
interceptors.DefaultClientInterceptor(
grpc_opentracing.WithTraceHeaderName(c.config.TraceHeaderName),
Expand Down Expand Up @@ -199,7 +204,7 @@ func (c *cb) initHTTP(ctx context.Context) (*http.Server, error) {
gziphandler.GzipHandler(tracingWrapper(mux)).ServeHTTP(w, r)
}),
}
log.Info(ctx, "Starting HTTP server on ", gatewayAddr)
log.Info(ctx, "msg", "Starting HTTP server", "address", gatewayAddr)
return gwServer, nil
}

Expand All @@ -208,7 +213,7 @@ func (c *cb) runHTTP(ctx context.Context, svr *http.Server) error {
}

func (c *cb) getGRPCServerOptions() []grpc.ServerOption {
so := make([]grpc.ServerOption, 0, 0)
so := make([]grpc.ServerOption, 0)
so = append(so,
grpc.ChainUnaryInterceptor(interceptors.DefaultInterceptors()...),
grpc.ChainStreamInterceptor(interceptors.DefaultStreamInterceptors()...),
Expand Down Expand Up @@ -250,7 +255,7 @@ func (c *cb) runGRPC(ctx context.Context, svr *grpc.Server) error {
if !c.config.DisableGRPCReflection {
reflection.Register(svr)
}
log.Info(ctx, "Starting GRPC server on ", grpcServerEndpoint)
log.Info(ctx, "msg", "Starting GRPC server", "address", grpcServerEndpoint)
return svr.Serve(lis)
}

Expand Down Expand Up @@ -364,7 +369,7 @@ func timedCall(ctx context.Context, f func()) {
func New(c config.Config) CB {
impl := &cb{
config: c,
svc: make([]CBService, 0, 0),
svc: make([]CBService, 0),
}
impl.processConfig()
return impl
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.15

require (
github.com/NYTimes/gziphandler v1.1.1
github.com/Unleash/unleash-client-go/v3 v3.7.0 // indirect
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/go-coldbrew/errors v0.1.1
github.com/go-coldbrew/feature-flags v0.1.3
github.com/go-coldbrew/hystrixprometheus v0.1.0
github.com/go-coldbrew/interceptors v0.1.4
github.com/go-coldbrew/log v0.1.0
Expand All @@ -20,18 +18,17 @@ require (
github.com/jaegertracing/jaeger-lib v2.4.1+incompatible
github.com/newrelic/go-agent/v3 v3.18.0
github.com/opentracing/opentracing-go v1.2.0
github.com/princjef/gomarkdoc v0.4.1 // indirect
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/twmb/murmur3 v1.1.6 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/bridge/opentracing v1.9.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.9.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.9.0
go.opentelemetry.io/otel/sdk v1.9.0
go.uber.org/automaxprocs v1.5.3
golang.org/x/net v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20220802133213-ce4fa296bf78 // indirect
google.golang.org/grpc v1.48.0
Expand Down
Loading

0 comments on commit 05f3496

Please sign in to comment.