Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Add --metrics-port flag. (#87)
Browse files Browse the repository at this point in the history
* Add --metrics-port flag.

Metrics listeners on a random port is unlikely to be useful.

* Bind to frontend IP for metrics so they can be scraped.
  • Loading branch information
robholland authored Jul 7, 2022
1 parent dcd539d commit 80ecd27
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
36 changes: 22 additions & 14 deletions cmd/temporalite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ var (
)

const (
ephemeralFlag = "ephemeral"
dbPathFlag = "filename"
portFlag = "port"
uiPortFlag = "ui-port"
headlessFlag = "headless"
ipFlag = "ip"
logFormatFlag = "log-format"
logLevelFlag = "log-level"
namespaceFlag = "namespace"
pragmaFlag = "sqlite-pragma"
configFlag = "config"
ephemeralFlag = "ephemeral"
dbPathFlag = "filename"
portFlag = "port"
metricsPortFlag = "metrics-port"
uiPortFlag = "ui-port"
headlessFlag = "headless"
ipFlag = "ip"
logFormatFlag = "log-format"
logLevelFlag = "log-level"
namespaceFlag = "namespace"
pragmaFlag = "sqlite-pragma"
configFlag = "config"
)

func init() {
Expand Down Expand Up @@ -93,6 +94,11 @@ func buildCLI() *cli.App {
Usage: "port for the temporal-frontend GRPC service",
Value: liteconfig.DefaultFrontendPort,
},
&cli.IntFlag{
Name: metricsPortFlag,
Usage: "port for the metrics listener",
Value: liteconfig.DefaultMetricsPort,
},
&cli.IntFlag{
Name: uiPortFlag,
Usage: "port for the temporal web UI",
Expand Down Expand Up @@ -171,9 +177,10 @@ func buildCLI() *cli.App {
},
Action: func(c *cli.Context) error {
var (
ip = c.String(ipFlag)
serverPort = c.Int(portFlag)
uiPort = serverPort + 1000
ip = c.String(ipFlag)
serverPort = c.Int(portFlag)
metricsPort = c.Int(metricsPortFlag)
uiPort = serverPort + 1000
)

if c.IsSet(uiPortFlag) {
Expand All @@ -196,6 +203,7 @@ func buildCLI() *cli.App {
opts := []temporalite.ServerOption{
temporalite.WithDynamicPorts(),
temporalite.WithFrontendPort(serverPort),
temporalite.WithMetricsPort(metricsPort),
temporalite.WithFrontendIP(ip),
temporalite.WithDatabaseFilePath(c.String(dbPathFlag)),
temporalite.WithNamespaces(c.StringSlice(namespaceFlag)...),
Expand Down
31 changes: 19 additions & 12 deletions internal/liteconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
broadcastAddress = "127.0.0.1"
PersistenceStoreName = "sqlite-default"
DefaultFrontendPort = 7233
DefaultMetricsPort = 0
)

// UIServer abstracts the github.com/temporalio/ui-server project to
Expand All @@ -48,6 +49,7 @@ type Config struct {
Ephemeral bool
DatabaseFilePath string
FrontendPort int
MetricsPort int
DynamicPorts bool
Namespaces []string
SQLitePragmas map[string]string
Expand Down Expand Up @@ -83,6 +85,7 @@ func NewDefaultConfig() (*Config, error) {
Ephemeral: false,
DatabaseFilePath: filepath.Join(userConfigDir, "temporalite/db/default.db"),
FrontendPort: 0,
MetricsPort: 0,
UIServer: noopUIServer{},
DynamicPorts: false,
Namespaces: nil,
Expand Down Expand Up @@ -122,18 +125,22 @@ func Convert(cfg *Config) *config.Config {
sqliteConfig.ConnectAttributes["_"+k] = v
}

var metricsPort, pprofPort int
var pprofPort int
if cfg.DynamicPorts {
if cfg.FrontendPort == 0 {
cfg.FrontendPort = cfg.portProvider.mustGetFreePort()
}
metricsPort = cfg.portProvider.mustGetFreePort()
if cfg.MetricsPort == 0 {
cfg.MetricsPort = cfg.portProvider.mustGetFreePort()
}
pprofPort = cfg.portProvider.mustGetFreePort()
} else {
if cfg.FrontendPort == 0 {
cfg.FrontendPort = DefaultFrontendPort
}
metricsPort = cfg.FrontendPort + 200
if cfg.MetricsPort == 0 {
cfg.MetricsPort = cfg.FrontendPort + 200
}
pprofPort = cfg.FrontendPort + 201
}

Expand All @@ -144,7 +151,7 @@ func Convert(cfg *Config) *config.Config {
}
baseConfig.Global.Metrics = &metrics.Config{
Prometheus: &metrics.PrometheusConfig{
ListenAddress: fmt.Sprintf("%s:%d", broadcastAddress, metricsPort),
ListenAddress: fmt.Sprintf("%s:%d", cfg.FrontendIP, cfg.MetricsPort),
HandlerPath: "/metrics",
},
}
Expand Down Expand Up @@ -207,28 +214,28 @@ func Convert(cfg *Config) *config.Config {
return baseConfig
}

func (o *Config) mustGetService(frontendPortOffset int) config.Service {
func (cfg *Config) mustGetService(frontendPortOffset int) config.Service {
svc := config.Service{
RPC: config.RPC{
GRPCPort: o.FrontendPort + frontendPortOffset,
MembershipPort: o.FrontendPort + 100 + frontendPortOffset,
GRPCPort: cfg.FrontendPort + frontendPortOffset,
MembershipPort: cfg.FrontendPort + 100 + frontendPortOffset,
BindOnLocalHost: true,
BindOnIP: "",
},
}

// Assign any open port when configured to use dynamic ports
if o.DynamicPorts {
if cfg.DynamicPorts {
if frontendPortOffset != 0 {
svc.RPC.GRPCPort = o.portProvider.mustGetFreePort()
svc.RPC.GRPCPort = cfg.portProvider.mustGetFreePort()
}
svc.RPC.MembershipPort = o.portProvider.mustGetFreePort()
svc.RPC.MembershipPort = cfg.portProvider.mustGetFreePort()
}

// Optionally bind frontend to IPv4 address
if frontendPortOffset == 0 && o.FrontendIP != "" {
if frontendPortOffset == 0 && cfg.FrontendIP != "" {
svc.RPC.BindOnLocalHost = false
svc.RPC.BindOnIP = o.FrontendIP
svc.RPC.BindOnIP = cfg.FrontendIP
}

return svc
Expand Down
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func WithFrontendPort(port int) ServerOption {
})
}

// WithMetricsPort sets the listening port for metrics.
//
// When unspecified, the port will be system-chosen.
func WithMetricsPort(port int) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.MetricsPort = port
})
}

// WithFrontendIP binds the temporal-frontend GRPC service to a specific IP (eg. `0.0.0.0`)
// Check net.ParseIP for supported syntax; only IPv4 is supported.
//
Expand Down

0 comments on commit 80ecd27

Please sign in to comment.