refactor: load configuration from env struct tags - #784
Merged
Conversation
Every setting is now a struct field with an `env` tag parsed by caarlos0/env, replacing the hand-rolled getEnv/getEnvBool helpers and the per-field strconv blocks. Defaults live next to the field, the AMQP block uses a nested envPrefix struct, and the OTLP/Datadog fallbacks are expressed as expanding defaults instead of nested getEnv calls. What tags cannot express is split into normalize (values derived from other settings) and validate (range and cross-field checks). Load now returns an error instead of calling os.Exit, and unparseable or out-of-range values fail startup rather than silently falling back to the default, so a typo cannot run an instance with settings nobody chose. telemetry.Setup takes a config struct instead of five positional strings and no longer reads OTEL_TRACES_SAMPLER_RATIO itself, so the API reads no environment variable outside the config package. Also fixes .env.example, which documented SMTP_FROM — a variable nothing reads; the sender address is MAIL_FROM. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
Greptile SummaryThe PR replaces imperative environment lookup and parsing with tagged configuration structs, centralized normalization and validation, and explicit configuration-load error propagation.
Confidence Score: 5/5The PR appears safe to merge because no blocking failure remains in the eligible follow-up scope. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| api/internal/config/config.go | Replaces imperative environment reads with typed tags, normalization, validation, and returned errors; no eligible follow-up finding was established. |
| api/internal/config/config_test.go | Expands coverage for fail-fast parsing, derived values, aliases, SMTP assembly, proxy trimming, and typed defaults. |
| api/internal/telemetry/telemetry.go | Changes telemetry initialization to consume a typed configuration and removes direct sampler environment parsing. |
| api/telemetry.go | Adds the adapter translating application configuration into telemetry configuration. |
| api/server.go | Propagates configuration-load errors and uses the typed telemetry adapter. |
| api/worker.go | Propagates configuration-load errors while preserving the worker-specific telemetry service name. |
| api/migrate.go | Updates all migration commands to propagate configuration-load errors. |
| api/fixtures.go | Updates fixture loading to propagate configuration errors before initializing dependencies. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
Env[Environment and .env] --> Parse[caarlos0/env Parse]
Parse --> Normalize[normalize derived values]
Normalize --> Validate[validate ranges and cross-field rules]
Validate -->|valid| Config[Config]
Validate -->|invalid| Error[Return startup error]
Config --> Server[Server]
Config --> Worker[Worker]
Config --> Migrate[Migrate commands]
Config --> Fixtures[Fixtures]
Server --> Telemetry[Typed telemetry config]
Worker --> Telemetry
Reviews (2): Last reviewed commit: "chore: merge main into branch, resolve g..." | Re-trigger Greptile
Member
Author
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: shyim <6224096+shyim@users.noreply.github.com>
Contributor
Resolved. The only conflict was in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
internal/configwas a wall ofgetEnv("KEY", "default")calls, with every non-string setting hand-parsed in its ownstrconvblock after the struct literal. This makes it declarative: each setting is a struct field with anenvtag, parsed by caarlos0/env.What changed
envDefault:"5m"), and types parse themselves —time.Duration,int,bool,float64,[]string. The three post-literal parse blocks (scrape delay, rate limit, prefetch) and thegetEnv/getEnvBool/parseCommaListhelpers are gone.envPrefix:"QUEUE_AMQP_"instead of repeating the prefix six times.env:"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,expand" envDefault:"${OTEL_EXPORTER_OTLP_ENDPOINT}", same for the logs endpoint and theDD_ENV/DD_VERSIONaliases, replacing the nestedgetEnv(getEnv(...))calls.normalize()for derived values (WebAuthn RP fromFRONTEND_URL, mail DSN from the legacySMTP_*vars,OtelEnabled, sampler clamp, proxy-list trimming) andvalidate()for range and cross-field checks, joined into one error.Load()returns(*Config, error)instead ofos.Exit(1)on a badAPP_SECRET. The six call sites (server, worker, 3× migrate, fixtures) return the error from theirRunE.telemetry.Setuptakes a config struct instead of five positional strings, andOTEL_TRACES_SAMPLER_RATIOmoved into config — the API now reads no environment variable outside the config package.api/.env.example, which documentedSMTP_FROM. Nothing reads that variable; the sender address isMAIL_FROM.Behavior change
Unparseable or out-of-range values now fail startup instead of warning and using the default.
QUEUE_AMQP_PREFETCH=many,QUEUE_AMQP_DELAYED_EXCHANGE=flaseandDEPLOYMENT_SCRAPE_DELAY=-1mpreviously logged a warning and ran with the default; they now abort with a clear error naming the variable. That holds the line the old code was aiming at ("a typo cannot silently flip a flag that defaults to on") more strictly. Documented inSELF_HOSTING.md.Tests
Config coverage goes from 4 to 11 tests. The existing ones were updated for the fail-fast semantics; new ones cover
APP_SECRETvalidation, the SMTP DSN assembly, the OTLP endpoint fallbacks, trusted-proxy trimming, the sampler clamp, and the WebAuthn derivation — none of which were tested before.mise run lintandmise run testpass.🤖 Generated with Claude Code