From 90abfbf3238aa927ad82fe68f0e06c01fac44475 Mon Sep 17 00:00:00 2001 From: "matthew.li" Date: Fri, 20 Nov 2026 12:57:46 -0800 Subject: [PATCH 1/3] docs: add a guide for adding or changing a configuration Link it from AGENTS.md and docs/README.md. Fix the environment.h comment: the generated JSON is at the repo root, and ENV_DEFAULT_RESOLVED_IN_CODE emits an empty default. --- AGENTS.md | 4 + docs/README.md | 1 + docs/add_configurations.md | 168 ++++++++++++++++++++++++++++++++++ include/datadog/environment.h | 11 ++- 4 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 docs/add_configurations.md diff --git a/AGENTS.md b/AGENTS.md index efeb79ac..adabad33 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,6 +12,10 @@ See [docs/development.md](docs/development.md) for build, test, and formatting i See [docs/design.md](docs/design.md) for the library's architecture and design rationale. +## Configurations + +Follow [docs/add_configurations.md](docs/add_configurations.md) when adding or changing a configuration. + ## Agent-Only Instructions Write short comments, with simple words and short sentences. diff --git a/docs/README.md b/docs/README.md index 3967d2d0..d27f467b 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](add_configurations.md) - [Conventions](conventions.md) - [Design](design.md) - [Development Processes](development.md) diff --git a/docs/add_configurations.md b/docs/add_configurations.md new file mode 100644 index 00000000..078bc22f --- /dev/null +++ b/docs/add_configurations.md @@ -0,0 +1,168 @@ +# Adding or Changing a Configuration + +This guide explains how to add, rename or change a configuration (an environment variable) in +`dd-trace-cpp`. Follow every step. CI checks parts of steps 2 to 4. + +## Overview + +- All configurations 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 setting 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 + +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("...")` when the code computes the default at runtime. For + example, `DD_SERVICE` defaults to the process name. The JSON `default` is then an empty string. + The text is only for readers of the header. 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. CI runs `bin/check-format`. + +## 3. Read the Value in Code + +Never call `std::getenv`. CI runs `bin/check-environment-variables` and fails if `std::getenv` is +used outside `src/datadog/environment.cpp` and `src/datadog/curl.cpp`. Use `environment::lookup`. + +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 in [include/datadog/config.h](../include/datadog/config.h). Map it to + its telemetry name in `to_string` in + [src/datadog/telemetry/telemetry_impl.cpp](../src/datadog/telemetry/telemetry_impl.cpp). +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 of them pick the +final value differently: + +- `span_sampler_config.cpp` uses `resolve_and_record_config`. +- `trace_sampler_config.cpp` uses `resolve_and_record_config`, and sets the sampling rules metadata + by hand. +- `datadog_agent_config.cpp` mostly uses `value_or`. It uses `pick` for the agent URL and sets the + `AGENT_URL` metadata by hand. +- `telemetry/configuration.cpp` uses `pick` and records no `ConfigName` metadata. Do not add a + `ConfigName` there. + +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 +``` + +Run it from the repo root. 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 + +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/include/datadog/environment.h b/include/datadog/environment.h index 4b4e8dbb..fa016122 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/add_configurations.md before adding or changing an entry. #define DD_LIST_ENVIRONMENT_VARIABLES(MACRO) \ MACRO(DD_AGENT_HOST, STRING, "localhost") \ MACRO(DD_ENV, STRING, "") \ From 4b186f9d0de04e79e90d73202a32ec563387e099 Mon Sep 17 00:00:00 2001 From: "matthew.li" Date: Fri, 20 Nov 2026 13:02:55 -0800 Subject: [PATCH 2/3] docs: link configuration guide from development.md Move the configuration guide link out of AGENTS.md into docs/development.md, which AGENTS.md already points to. Note that a new ConfigName goes at the end of the enum. --- AGENTS.md | 4 ---- docs/add_configurations.md | 4 +++- docs/development.md | 4 ++++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index adabad33..efeb79ac 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,10 +12,6 @@ See [docs/development.md](docs/development.md) for build, test, and formatting i See [docs/design.md](docs/design.md) for the library's architecture and design rationale. -## Configurations - -Follow [docs/add_configurations.md](docs/add_configurations.md) when adding or changing a configuration. - ## Agent-Only Instructions Write short comments, with simple words and short sentences. diff --git a/docs/add_configurations.md b/docs/add_configurations.md index 078bc22f..e89eeb74 100644 --- a/docs/add_configurations.md +++ b/docs/add_configurations.md @@ -71,7 +71,9 @@ Follow the `DD_SERVICE` pattern: 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 in [include/datadog/config.h](../include/datadog/config.h). Map it to +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). 4. Pick the final value in `finalize_config` with `resolve_and_record_config` from `config.h`. It diff --git a/docs/development.md b/docs/development.md index c3072593..f6e68015 100644 --- a/docs/development.md +++ b/docs/development.md @@ -29,6 +29,10 @@ To check formatting without writing files: bin/check-format ``` +## Configuration + +Follow [add_configurations.md](add_configurations.md) when adding or changing a configuration. + ## Static Analysis C++ code is analyzed with Clang Tidy. Run it with the following command: From 4f77ad6c47913e827175a1fd72e71ac076eb0795 Mon Sep 17 00:00:00 2001 From: "matthew.li" Date: Mon, 23 Nov 2026 10:53:22 -0800 Subject: [PATCH 3/3] docs: address review feedback on configuration key guide Rename the guide to docs/configuration_keys.md and use the term "configuration key" from the public documentation. Simplify the per-component notes, move the development.md section to the bottom, and fix the error message and diff path in the supported configurations CI step. --- .github/workflows/dev.yml | 10 +-- docs/README.md | 2 +- ...onfigurations.md => configuration_keys.md} | 63 ++++++++----------- docs/development.md | 7 +-- include/datadog/environment.h | 2 +- 5 files changed, 38 insertions(+), 46 deletions(-) rename docs/{add_configurations.md => configuration_keys.md} (74%) 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 d27f467b..598ab14c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ This directory contains documentation of the Datadog C++ Tracer, including: -- [Adding or Changing a Configuration](add_configurations.md) +- [Adding or Changing a Configuration Key](configuration_keys.md) - [Conventions](conventions.md) - [Design](design.md) - [Development Processes](development.md) diff --git a/docs/add_configurations.md b/docs/configuration_keys.md similarity index 74% rename from docs/add_configurations.md rename to docs/configuration_keys.md index e89eeb74..d7abcf97 100644 --- a/docs/add_configurations.md +++ b/docs/configuration_keys.md @@ -1,11 +1,11 @@ -# Adding or Changing a Configuration +# Adding or Changing a Configuration Key -This guide explains how to add, rename or change a configuration (an environment variable) in +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 configurations are listed in one place: `DD_LIST_ENVIRONMENT_VARIABLES` in +- 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. @@ -15,8 +15,8 @@ This guide explains how to add, rename or change a configuration (an environment ## 1. Check the Central Registry -Search the registry for the setting 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. +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 @@ -25,8 +25,8 @@ builds its list of keys from the registry. ## 2. Add the Entry -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 `\`: +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) \ @@ -38,21 +38,20 @@ The new last line has no `\`: - `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("...")` when the code computes the default at runtime. For - example, `DD_SERVICE` defaults to the process name. The JSON `default` is then an empty string. - The text is only for readers of the header. Do not use it with `DECIMAL`: the tool would write - `""` (two quote characters) as the default. + - `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. CI runs `bin/check-format`. +Run `bin/format` after editing the header. ## 3. Read the Value in Code -Never call `std::getenv`. CI runs `bin/check-environment-variables` and fails if `std::getenv` is -used outside `src/datadog/environment.cpp` and `src/datadog/curl.cpp`. Use `environment::lookup`. +Never call `std::getenv`. Use `environment::lookup`. This is checked by the CI. Follow the `DD_SERVICE` pattern: @@ -72,10 +71,11 @@ Follow the `DD_SERVICE` pattern: (`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 + [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: @@ -88,31 +88,22 @@ Follow the `DD_SERVICE` pattern: 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 of them pick the -final value differently: +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. -- `span_sampler_config.cpp` uses `resolve_and_record_config`. -- `trace_sampler_config.cpp` uses `resolve_and_record_config`, and sets the sampling rules metadata - by hand. -- `datadog_agent_config.cpp` mostly uses `value_or`. It uses `pick` for the agent URL and sets the - `AGENT_URL` metadata by hand. -- `telemetry/configuration.cpp` uses `pick` and records no `ConfigName` metadata. Do not add a - `ConfigName` there. - -Follow the code next to your setting. - -## 4. Regenerate supported-configurations.json +## 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 +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 ``` -Run it from the repo root. 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 +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 @@ -134,7 +125,7 @@ Test at least: the default, the environment value overriding the code value, and 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 +## 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 diff --git a/docs/development.md b/docs/development.md index f6e68015..fb894227 100644 --- a/docs/development.md +++ b/docs/development.md @@ -29,10 +29,6 @@ To check formatting without writing files: bin/check-format ``` -## Configuration - -Follow [add_configurations.md](add_configurations.md) when adding or changing a configuration. - ## Static Analysis C++ code is analyzed with Clang Tidy. Run it with the following command: @@ -41,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 fa016122..08fd87c1 100644 --- a/include/datadog/environment.h +++ b/include/datadog/environment.h @@ -42,7 +42,7 @@ namespace environment { // There is no default value. This is emitted as JSON null in // supported-configurations.json. // -// See docs/add_configurations.md before adding or changing an entry. +// 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, "") \