diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 0d7d1dae..9de4078c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -24,12 +24,14 @@ jobs: run: cmake --build ${BUILD_DIR} -j --target config-inversion -v - name: Verify supported configurations metadata run: | + tool="./${BUILD_DIR}/tools/config-inversion/config-inversion" tmp_dir="$(mktemp -d)" trap 'rm -rf "$tmp_dir"' EXIT - ./${BUILD_DIR}/tools/config-inversion/config-inversion --output-file "${tmp_dir}/ci-supported-configurations.json" - if ! diff -q "$tmp_dir/ci-supported-configurations.json" "supported-configurations.json" >/dev/null 2>&1; then - echo "ERROR: supported-configurations.json got out of sync with implemented configurations. Please run `./config-inversion --output-file supported-configurations.json` locally." - diff -u "$tmp_dir/supported-configurations.json" supported-configurations.json || true + ci_generated_configurations="${tmp_dir}/ci-supported-configurations.json" + "$tool" --output-file "$ci_generated_configurations" + if ! diff -q "$ci_generated_configurations" supported-configurations.json >/dev/null 2>&1; then + echo "ERROR: supported-configurations.json got out of sync with implemented configurations. Run ${tool} --output-file supported-configurations.json from the repo root. See docs/configuration_keys.md." + diff -u "$ci_generated_configurations" supported-configurations.json || true exit 1 fi diff --git a/docs/README.md b/docs/README.md index 3967d2d0..598ab14c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,7 @@ This directory contains documentation of the Datadog C++ Tracer, including: +- [Adding or Changing a Configuration Key](configuration_keys.md) - [Conventions](conventions.md) - [Design](design.md) - [Development Processes](development.md) diff --git a/docs/configuration_keys.md b/docs/configuration_keys.md new file mode 100644 index 00000000..d7abcf97 --- /dev/null +++ b/docs/configuration_keys.md @@ -0,0 +1,161 @@ +# Adding or Changing a Configuration Key + +This guide explains how to add, rename or change a configuration key (an environment variable) in +`dd-trace-cpp`. Follow every step. CI checks parts of steps 2 to 4. + +## Overview + +- All configuration keys are listed in one place: `DD_LIST_ENVIRONMENT_VARIABLES` in + [include/datadog/environment.h](../include/datadog/environment.h). +- The `config-inversion` tool ([tools/config-inversion](../tools/config-inversion)) reads that list + and writes [supported-configurations.json](../supported-configurations.json) at the repo root. +- The JSON file is committed. CI checks that it matches the list. +- CI also checks every key in the JSON file against the central configuration registry (the + [Feature Parity Dashboard](https://feature-parity.us1.prod.dog/#/configurations?viewType=configurations)). + +## 1. Check the Central Registry + +Search the registry for the configuration key before you pick a name. If another tracer already has +it, reuse its name and behavior. If the key is new for all tracers, add it to the registry. + +Each registry entry has one or more implementations, named `A`, `B`, `C`, and so on. Each one has a +type, a default and a description. The registry description is what users see: the public +[C++ configuration page](https://docs.datadoghq.com/tracing/trace_collection/library_config/cpp/) +builds its list of keys from the registry. + +## 2. Add the Entry + +In [include/datadog/environment.h](../include/datadog/environment.h), add one line at the end of +`DD_LIST_ENVIRONMENT_VARIABLES`. Add a `\` to the line that was last. The new last line has no `\`: + +```cpp + MACRO(DD_EXTERNAL_ENV, STRING, nullptr) \ + MACRO(DD_MY_NEW_SETTING, BOOLEAN, false) +``` + +- `NAME`: the environment variable name. It also becomes the `environment::Variable` enum value. +- `TYPE`: one of `STRING`, `BOOLEAN`, `INT`, `DECIMAL`, `ARRAY`, `MAP`. +- `DEFAULT`: the default value. Use: + - a literal (`"localhost"`, `true`, `8126`, `5.0`) when the default is fixed; + - `nullptr` when there is no default (written as JSON `null`); + - `ENV_DEFAULT_RESOLVED_IN_CODE("[explanation]")` when the code computes the default at runtime. + For example, `DD_SERVICE` defaults to the process name. The string parameter is only for + readers of the header. The JSON `default` is then an empty string. Do not use it with + `DECIMAL`: the tool would write `""` (two quote characters) as the default. + +The type and default must match the registry implementation you use. The JSON keys are sorted, so +the order does not matter for the JSON. Add new entries at the end, as shown above: the list +also defines the public `environment::Variable` enum, and the existing values must not change. + +Run `bin/format` after editing the header. + +## 3. Read the Value in Code + +Never call `std::getenv`. Use `environment::lookup`. This is checked by the CI. + +Follow the `DD_SERVICE` pattern: + +1. Add a field to the public config struct, for example `TracerConfig` in + [include/datadog/tracer_config.h](../include/datadog/tracer_config.h). Note the environment + variable in the field comment. This is the user-facing doc for code users. +2. Read the variable in the `load_*_env_config` function of the same component, for example + `load_tracer_env_config` in [src/datadog/tracer_config.cpp](../src/datadog/tracer_config.cpp): + + ```cpp + if (auto service_env = lookup(environment::DD_SERVICE)) { + env_cfg.service = std::string{*service_env}; + } + ``` + + Parse the text with the helpers in [src/datadog/parse_util.h](../src/datadog/parse_util.h) + (`falsy`, `parse_uint64`, `parse_double`, `parse_list`, `parse_tags`). On a parse error, return + an `Error` that names the variable with `name(environment::...)`. +3. Add a `ConfigName` value at the end of the enum in + [include/datadog/config.h](../include/datadog/config.h), so existing values keep their numbers. + Map it to its telemetry name in `to_string` in + [src/datadog/telemetry/telemetry_impl.cpp](../src/datadog/telemetry/telemetry_impl.cpp). + Skip this step for telemetry settings (`telemetry/configuration.cpp`) because they record no + metadata. +4. Pick the final value in `finalize_config` with `resolve_and_record_config` from `config.h`. It + applies the precedence environment > code > default, and records the origin for telemetry: + + ```cpp + final_config.defaults.service = resolve_and_record_config( + env_config->service, user_config.service, &final_config.metadata, + ConfigName::SERVICE_NAME, get_process_name()); + ``` + + For a value that is not a string, pass a function that turns it into a string as the last + argument. See the `log_on_startup` call in `tracer_config.cpp`. + +Other components in `src/datadog` also have a `load_*_env_config` function. Some pick the final +value differently (for example with `resolve_and_record_config`, `pick` or `value_or`). Follow the +code next to your setting. + +## 4. Regenerate `supported-configurations.json` + +Build the tool and run it from the repo root: + +```shell +cmake -B build -DDD_TRACE_BUILD_TOOLS=1 . +cmake --build build -j --target config-inversion +./build/tools/config-inversion/config-inversion --output-file supported-configurations.json +``` + +The tool reads the existing `supported-configurations.json` in the current directory to keep each +key's `implementation`. In any other directory it aborts with +`parse error ... attempting to parse an empty input`. + +New keys get implementation `A`. If the registry uses another letter for this behavior, edit only +that `implementation` value in the JSON file, then run the tool again. The tool keeps it. + +Commit the updated `supported-configurations.json` with the header change. Do not edit other fields +by hand. + +## 5. Add Tests + +Use `EnvGuard` from [test/common/environment.h](../test/common/environment.h). It sets a variable +for one scope and restores it after. + +- Tracer, agent and sampler settings: [test/test_tracer_config.cpp](../test/test_tracer_config.cpp). + See the `DD_SERVICE overrides service` and `DD_TRACE_RATE_LIMIT` sections. +- Telemetry settings: [test/telemetry/test_configuration.cpp](../test/telemetry/test_configuration.cpp). + +Test at least: the default, the environment value overriding the code value, and an invalid value. +If you record metadata, check its origin and value (see the `PROPAGATION_BEHAVIOR_EXTRACT` tests). +See [development.md](development.md) to run the tests. + +## Renaming or Removing a Configuration Key + +The `config-inversion` tool does not emit aliases or deprecations. To rename, add the new +name as its own entry and keep the old one. Read both in code and decide which wins. See how +`DD_TRACE_PROPAGATION_STYLE_EXTRACT` and `DD_PROPAGATION_STYLE_EXTRACT` are handled in +`load_tracer_env_config`. Removing an entry removes the key from the JSON file. Users of that key +then lose support, so treat it as a breaking change. + +## Troubleshooting + +### GitHub: "supported-configurations.json got out of sync" + +The `Verify supported configurations metadata` step in +[.github/workflows/dev.yml](../.github/workflows/dev.yml) builds `config-inversion`, runs it, and +diffs the output with the committed file. To fix it, run step 4 and commit the result. + +The tool writes no final newline. If your editor adds one, the diff fails. Regenerate the file +instead of editing it. + +### GitLab: `validate_supported_configurations_v2_local_file` + +This job in [.gitlab-ci.yml](../.gitlab-ci.yml) sends `supported-configurations.json` to the +central registry. It fails when: + +- the key is not in the registry: add it to the registry; +- the `implementation` is not in the registry: use an existing letter, or add a new + implementation in the registry; +- the type or default does not match the registry implementation: fix the code and the header + entry to match. If another implementation matches, the job log lists it. Use that letter. + +Do not change the local values just to pass the check. They must describe what the code does. + +The `update_central_configurations_version_range_v2` job runs only on release tags. It records +which versions support each key. It needs no action in a PR. diff --git a/docs/development.md b/docs/development.md index c3072593..fb894227 100644 --- a/docs/development.md +++ b/docs/development.md @@ -37,3 +37,6 @@ C++ code is analyzed with Clang Tidy. Run it with the following command: bin/check-tidy ``` +## Configuration Keys + +Follow [configuration_keys.md](configuration_keys.md) when adding or changing a configuration key. diff --git a/include/datadog/environment.h b/include/datadog/environment.h index 4b4e8dbb..08fd87c1 100644 --- a/include/datadog/environment.h +++ b/include/datadog/environment.h @@ -25,7 +25,7 @@ namespace environment { // // This registry is the single source of truth for: // - env variable name allowlist (`include/datadog/environment.h`) -// - generated metadata (`metadata/supported-configurations.json`) +// - generated metadata (`supported-configurations.json` at the repo root) // // Each entry has: // - NAME: environment variable symbol (e.g. DD_SERVICE) @@ -35,11 +35,14 @@ namespace environment { // Marker tokens: // - ENV_DEFAULT_RESOLVED_IN_CODE("...description...") // The runtime default is resolved in C++ configuration finalization -// logic. The description is emitted as the "default" field in -// metadata/supported-configurations.json. +// logic. The "default" field in supported-configurations.json is an +// empty string. The description is only for readers of this file. +// Do not use it with DECIMAL: the generated default would be `""`. // - nullptr // There is no default value. This is emitted as JSON null in -// metadata/supported-configurations.json. +// supported-configurations.json. +// +// See docs/configuration_keys.md before adding or changing an entry. #define DD_LIST_ENVIRONMENT_VARIABLES(MACRO) \ MACRO(DD_AGENT_HOST, STRING, "localhost") \ MACRO(DD_ENV, STRING, "") \