msp/runtime: add -help handling for config options (#61050)

```
$ go build -o .bin/telemetry-gateway github.com/sourcegraph/sourcegraph/cmd/telemetry-gateway
$ .bin/telemetry-gateway -help                                                                                   
SERVICE: telemetry-gateway
VERSION: 0.0.0+dev
CONFIGURATION OPTIONS:
- 'TELEMETRY_GATEWAY_EVENTS_PUBSUB_ENABLED': If false, logs Pub/Sub messages instead of actually sending them (default: "true")
- 'TELEMETRY_GATEWAY_EVENTS_STREAM_PUBLISH_CONCURRENCY': Per-stream concurrent publishing limit. (default: "250")
- 'MSP': indicates if we are running in a MSP environment (default: "false")
- 'ENVIRONMENT_ID': MSP Service Environment ID (default: "unknown")
- 'PORT': service port (required)
- 'OTEL_SDK_DISABLED': disable OpenTelemetry SDK (default: "false")
```
This commit is contained in:
Robert Lin 2024-03-13 22:25:09 +08:00 committed by GitHub
parent 223ef4f1b5
commit 61f59e06e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 2 deletions

View File

@ -9,9 +9,19 @@ import (
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type requestedEnvVar struct {
name string
defaultValue string
description string
}
type Env struct {
errs []error
env map[string]string
// requestedEnvVars are only available after ConfigLoader is used on this
// Env instance.
requestedEnvVars []requestedEnvVar
}
func newEnv() (*Env, error) {
@ -30,8 +40,13 @@ func newEnv() (*Env, error) {
}, nil
}
// TODO: Try to use third param description to generate docs.
func (e *Env) get(name, defaultValue, _ string) string {
func (e *Env) get(name, defaultValue, description string) string {
e.requestedEnvVars = append(e.requestedEnvVars, requestedEnvVar{
name: name,
defaultValue: defaultValue,
description: description,
})
v, ok := e.env[name]
if !ok {
return defaultValue

View File

@ -2,6 +2,9 @@ package runtime
import (
"context"
"flag"
"fmt"
"os"
"cloud.google.com/go/profiler"
"github.com/getsentry/sentry-go"
@ -29,6 +32,8 @@ type Service[ConfigT any] interface {
) (background.Routine, error)
}
var showHelp = flag.Bool("help", false, "Show service help text")
// Start handles the entire lifecycle of the program running Service, and should
// be the only thing called in a MSP program's main package, for example:
//
@ -40,6 +45,7 @@ func Start[
ConfigT any,
LoaderT ConfigLoader[ConfigT],
](service Service[ConfigT]) {
flag.Parse()
passSanityCheck(service)
// Resource representing the service
@ -70,6 +76,23 @@ func Start[
config.Load(env)
contract := newContract(log.Scoped("msp.contract"), env, service)
// Fast-exit with configuration facts if requested
if *showHelp {
fmt.Printf("SERVICE: %s\nVERSION: %s\n",
service.Name(), service.Version())
fmt.Printf("CONFIGURATION OPTIONS:\n")
for _, v := range env.requestedEnvVars {
fmt.Printf("- '%s': %s", v.name, v.description)
if v.defaultValue != "" {
fmt.Printf(" (default: %q)", v.defaultValue)
} else {
fmt.Printf(" (required)")
}
fmt.Println()
}
os.Exit(0)
}
// Enable Sentry error log reporting
var sentryEnabled bool
if contract.internal.sentryDSN != nil {