Skip to content

Commit

Permalink
feat: add automaxprocs
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 11, 2023
1 parent 23f8b0c commit a0305c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ WIP SDK from go-faster for instrumentation.

Metrics and pprof can be served from same address if needed, set both addresses to the same value.

| Name | Description | Example | Default |
|---------------------------------------|---------------------------------|--------------------|------------------------|
| `OTEL_RESOURCE_ATTRIBUTES` | OTEL Resource attributes | `service.name=app` | |
| `OTEL_SERVICE_NAME` | OTEL Service name | `app` | `unknown_service` |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | OTLP protocol to use | `http` | `grpc` |
| `OTEL_PROPAGATORS` | OTEL Propagators | `none` | `tracecontext,baggage` |
| `PPROF_ROUTES` | List of enabled pprof routes | `cmdline,profile` | See below |
| `PPROF_ADDR` | Enable pprof and listen on addr | `0.0.0.0:9010` | N/A |
| `OTEL_LOG_LEVEL` | Log level | `debug` | `info` |
| `METRICS_ADDR` | Prometheus addr (fallback) | `localhost:9464` | Prometheus addr |
| `OTEL_METRICS_EXPORTER` | Metrics exporter to use | `prometheus` | `otlp` |
| `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL` | Metrics OTLP protocol to use | `http` | `grpc` |
| `OTEL_EXPORTER_PROMETHEUS_HOST` | Host of prometheus addr | `0.0.0.0` | `localhost` |
| `OTEL_EXPORTER_PROMETHEUS_PORT` | Port of prometheus addr | `9090` | `9464` |
| `OTEL_TRACES_EXPORTER` | Traces exporter to use | `otlp` | `otlp` |
| `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` | Traces OTLP protocol to use | `http` | `grpc` |


| Name | Description | Example | Default |
|---------------------------------------|----------------------------------|--------------------|------------------------|
| `AUTOMAXPROCS` | Use [automaxprocs][automaxprocs] | `0` | `1` |
| `AUTOMAXPROCS_MIN` | Minimum `GOMAXPROCS` to use | `2` | `1` |
| `OTEL_RESOURCE_ATTRIBUTES` | OTEL Resource attributes | `service.name=app` | |
| `OTEL_SERVICE_NAME` | OTEL Service name | `app` | `unknown_service` |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | OTLP protocol to use | `http` | `grpc` |
| `OTEL_PROPAGATORS` | OTEL Propagators | `none` | `tracecontext,baggage` |
| `PPROF_ROUTES` | List of enabled pprof routes | `cmdline,profile` | See below |
| `PPROF_ADDR` | Enable pprof and listen on addr | `0.0.0.0:9010` | N/A |
| `OTEL_LOG_LEVEL` | Log level | `debug` | `info` |
| `METRICS_ADDR` | Prometheus addr (fallback) | `localhost:9464` | Prometheus addr |
| `OTEL_METRICS_EXPORTER` | Metrics exporter to use | `prometheus` | `otlp` |
| `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL` | Metrics OTLP protocol to use | `http` | `grpc` |
| `OTEL_EXPORTER_PROMETHEUS_HOST` | Host of prometheus addr | `0.0.0.0` | `localhost` |
| `OTEL_EXPORTER_PROMETHEUS_PORT` | Port of prometheus addr | `9090` | `9464` |
| `OTEL_TRACES_EXPORTER` | Traces exporter to use | `otlp` | `otlp` |
| `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` | Traces OTLP protocol to use | `http` | `grpc` |

[automaxprocs]: https://github.com/uber-go/automaxprocs

### Metrics exporters

Expand Down
22 changes: 22 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"time"

"github.com/go-faster/errors"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -67,6 +69,26 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
panic(err)
}

{
// Automatically setting GOMAXPROCS.
set := true // enabled by default
if v, err := strconv.ParseBool(os.Getenv("AUTOMAXPROCS")); err == nil {
set = v
}
minProcs := 1
if v, err := strconv.Atoi(os.Getenv("AUTOMAXPROCS_MIN")); err == nil {
minProcs = v
}
if set {
if _, err := maxprocs.Set(
maxprocs.Logger(lg.Sugar().Infof),
maxprocs.Min(minProcs),
); err != nil {
lg.Warn("Failed to set GOMAXPROCS", zap.Error(err))
}
}
}

g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
defer lg.Info("Shutting down")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
go.opentelemetry.io/otel/sdk v1.20.0
go.opentelemetry.io/otel/sdk/metric v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.26.0
golang.org/x/sync v0.5.0
)
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
Expand Down Expand Up @@ -83,6 +84,8 @@ go.opentelemetry.io/otel/trace v1.20.0 h1:+yxVAPZPbQhbC3OfAkeIVTky6iTFpcr4SiY9om
go.opentelemetry.io/otel/trace v1.20.0/go.mod h1:HJSK7F/hA5RlzpZ0zKDCHCDHm556LCDtKaAo6JmBFUU=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down

0 comments on commit a0305c6

Please sign in to comment.