-
Notifications
You must be signed in to change notification settings - Fork 535
feat(internal/env): add env var config validation #3746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8f6c18f
62a8e72
b8004e8
ca152a7
3ceb6c0
1afe4a5
ea265eb
74ff37b
3baed7a
c9fce2b
e2be82a
d4d9802
d2031ff
f9c2805
0f2c54e
486c05b
90e735a
6d1684e
b16ea14
85c035c
b025ee7
77497b9
26e6e80
cc847c4
787a0db
d2e055c
ab715f1
89b3bfe
b4dc9e9
ebc6b77
e3fdd88
7d4f9f8
ccefe39
f81dcce
523140a
0cdca0d
d5e5a71
d3d98c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| name: 'Supported Configurations Diff Check' | ||
| description: 'Check for changes in supported_configurations.json file in order to detect unknown configurations' | ||
| runs: | ||
| using: "composite" | ||
| steps: | ||
| - name: Supported Configurations Diff Check | ||
| if: always() | ||
| shell: bash | ||
| run: |- | ||
| git diff internal/env/supported_configurations.json | ||
| go run ./scripts/configinverter/main.go check |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # This CI jobs are copied from libdatadog-build one-pipeline.yml gitlab template. | ||
| # This URL is available in the dd-gitlab/publish-content-addresable-templates job whenever | ||
| # a change is made on the one-pipeline template. | ||
| variables: | ||
| SCRIPTS_BASE_URL: https://gitlab-templates.ddbuild.io/libdatadog/one-pipeline/ca/50d49f6898ce86e93326856210e8ab6526895273cb6341ac2d7d0e6c1c14e31e/scripts | ||
|
|
||
| .download-scripts-from-template: &download-scripts-from-template | ||
| - mkdir -p scripts | ||
| - | | ||
| for script_file in "config-inversion-local-validation.sh" "config-inversion-update-supported-range.sh" | ||
| do | ||
| curl --location --fail --show-error --output "scripts/${script_file}" "${SCRIPTS_BASE_URL}/${script_file}" | ||
| if [[ $script_file == *sh ]] | ||
| then | ||
| chmod +x scripts/$script_file | ||
| fi | ||
| done | ||
|
|
||
| .validate_supported_configurations_local_file: | ||
| allow_failure: false | ||
| rules: | ||
| - when: on_success | ||
| variables: | ||
| LOCAL_JSON_PATH: "" | ||
| before_script: | ||
| - *download-scripts-from-template | ||
| script: | ||
| - scripts/config-inversion-local-validation.sh "$LOCAL_JSON_PATH" | ||
|
|
||
| .update_central_configurations_version_range: | ||
| allow_failure: false | ||
| rules: | ||
| - if: '$CI_COMMIT_TAG =~ /^v?[0-9]+\.[0-9]+\.[0-9]+$/' | ||
| when: on_success | ||
| variables: | ||
| LOCAL_JSON_PATH: "" | ||
| LANGUAGE_NAME: "" | ||
| MULTIPLE_RELEASE_LINES: "false" # expect "true" or "false" as a value to determine if a new "branch" identifier is needed to differentiate between multiple release lines | ||
| before_script: | ||
| - *download-scripts-from-template | ||
| - export FP_API_KEY=$(aws ssm get-parameter --region us-east-1 --name ci.$CI_PROJECT_NAME.FP_API_KEY --with-decryption --query "Parameter.Value" --out text) | ||
| script: | ||
| - scripts/config-inversion-update-supported-range.sh "$LOCAL_JSON_PATH" "$LANGUAGE_NAME" "$MULTIPLE_RELEASE_LINES" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -297,6 +297,54 @@ Sample PR: <https://github.com/DataDog/dd-trace-go/pull/3365> | |
|
|
||
| Please view our contrib [README.md](contrib/README.md) for information on integrations. If you need support for a new integration, please file an issue to discuss before opening a PR. | ||
|
|
||
| ### Working with environment variables | ||
|
|
||
| When working with environment variables, direct use of `os.Getenv` and `os.LookupEnv` is not permitted. Instead, all environment variables must be validated against an [allowed list](./internal/env/supported_configurations.gen.go) using `env.Get` and `env.Lookup` from the [`internal/env`](./internal/env.go) package (or [`instrumentation/env`](./instrumentation/env/env.go) when working on contrib packages). This validation system helps us automatically detect newly introduced variables and ensures they are properly documented and tracked. | ||
|
|
||
| Once a new environment variable is added to the codebase, Datadog maintainers will also add it to Datadog's internal configuration registry for tracking and documentation purposes. | ||
|
|
||
| Upon each tracer release, new configuration keys are automatically tagged by our [CI pipeline](./.gitlab/config-validation.yml) to track when they were introduced. | ||
|
|
||
| #### Adding new environment variables using configinverter | ||
|
|
||
| The `configinverter` tool provides a command to add new environment variable keys to the `supported_configurations.json` file and regenerate the corresponding Go code. | ||
|
|
||
| ```sh | ||
| go run ./scripts/configinverter/main.go add DD_MY_NEW_KEY | ||
| ``` | ||
|
Comment on lines
+308
to
+314
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I want to add a new environment variable, I only have to run this command, and that's it? Or, are there additional steps? Would be nice to see an explicit play by play, if it's anything more than this.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the other step needed when adding env var. This should be complete to have a play by play of what needs to be done. |
||
|
|
||
| After adding it to the codebase the key also needs to be added to the [registry](https://feature-parity.us1.prod.dog/#/configurations?viewType=configurations) by an **internal contributor**. | ||
| If the key already exists in the registry because another language already registred it this step can be skipped. | ||
| Not adding the key to the registry will fail the CI step in charge of checking the local file against the registry. | ||
|
|
||
| #### Auto-detection via tests | ||
|
|
||
| All environment variables should be read at least once by a test. When this happens, a helper automatically detects the usage and adds the variable to the [JSON configuration file](./internal/env/supported_configurations.json). Since the variable isn't yet present in the generated code, it won't read any actual environment values initially, but it will be recorded for code generation. | ||
|
|
||
| Note that CI jobs will fail if new keys are detected but not properly generated into the code. | ||
|
|
||
| You can check for keys that have been added to the JSON file but not yet generated into code: | ||
|
|
||
| ```sh | ||
| go run ./scripts/configinverter/main.go check | ||
| ``` | ||
|
|
||
| After the first test run that detects your new environment variable, regenerate the code: | ||
|
|
||
| ```sh | ||
| go run ./scripts/configinverter/main.go generate | ||
| ``` | ||
|
|
||
| After adding it to the codebase the key also needs to be added to the [registry](https://feature-parity.us1.prod.dog/#/configurations?viewType=configurations) by an **internal contributor**. | ||
| If the key already exists in the registry because another language already registred it this step can be skipped. | ||
| Not adding the key to the registry will fail the CI step in charge of checking the local file against the registry. | ||
|
|
||
| #### Handling related CI failures | ||
|
|
||
| The GitLab `validate_supported_configurations_local_file` job validates the JSON file content against Datadog's [configuration registry](https://feature-parity.us1.prod.dog/#/configurations?viewType=configurations) to ensure every configuration key is properly registered and documented. If keys are missing from the registry, the job will fail and display the list of missing keys in the output. These keys must be added to the internal registry by Datadog maintainers for the check to pass, the key will need to be documented before merging the PR onto main. | ||
|
|
||
| Additionally, multiple CI jobs include a [step](./.github/actions/supported_configurations_validation/action.yml) that checks for newly discovered environment variables during test execution and will fail if keys are missing from the generated list. To resolve this failure, use one of the two methods described above to add the key to the generated list. | ||
|
|
||
| ### Adding Go Modules | ||
|
|
||
| When adding a new dependency, especially for `contrib/` packages, prefer the minimum secure versions of any modules rather than the latest versions. This is to avoid forcing upgrades on downstream users for modules such as `google.golang.org/grpc` which often introduce breaking changes within minor versions. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.