Migrate Config system to environment component by PerfectSlayer · Pull Request #9191 · DataDog/dd-trace-java · GitHub
Skip to content

Migrate Config system to environment component#9191

Merged
PerfectSlayer merged 3 commits into
masterfrom
bbujon/environment-step3
Jul 29, 2025
Merged

Migrate Config system to environment component#9191
PerfectSlayer merged 3 commits into
masterfrom
bbujon/environment-step3

Conversation

@PerfectSlayer

@PerfectSlayer PerfectSlayer commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

What Does This Do

This PR migrates the config system to the environment component.
It additionally improves the environment component with:

  • the addition of SystemProperties.clear()
  • better null handling on SystemProperties for the set and clear methods.

Motivation

Safely interact with the env var and system properties by handling security manager exception and possible NPE.

Additional Notes

Environment component related stacked PRs:

Contributor Checklist

Jira ticket: LANGPLAT-458

@PerfectSlayer PerfectSlayer added the type: enhancement Enhancements and improvements label Jul 17, 2025
@PerfectSlayer PerfectSlayer requested a review from a team as a July 17, 2025 06:57
@PerfectSlayer PerfectSlayer added the comp: core Tracer core label Jul 17, 2025
@PerfectSlayer PerfectSlayer requested a review from mcculls July 17, 2025 06:57
@PerfectSlayer PerfectSlayer added the tag: no release notes Changes to exclude from release notes label Jul 17, 2025
@pr-commenter

pr-commenter Bot commented Jul 17, 2025

Copy link
Copy Markdown

@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step2 branch from 78ad76f to 9d287cb Compare July 17, 2025 13:42
@PerfectSlayer PerfectSlayer requested review from a team and bric3 and removed request for a team July 17, 2025 13:42
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from b6c331b to 2512a49 Compare July 17, 2025 13:43
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step2 branch from 9d287cb to d374062 Compare July 17, 2025 15:50
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from 2512a49 to fe762c4 Compare July 18, 2025 08:37
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step2 branch from 3cca925 to 8647cfd Compare July 18, 2025 08:51
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from fe762c4 to 41de4a2 Compare July 18, 2025 08:55
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step2 branch from 8647cfd to e085472 Compare July 18, 2025 09:24
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from 41de4a2 to 42d109e Compare July 18, 2025 09:24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor question: why getOrDefault ? is it possible not to have user.home?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the security manager prevents access to this property, SystemProperties.get() will return null.
So using getOrDefault(key, '') should avoid NPE or "null" in path.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. But just curious, how previous code worked?
And would it make sense to build a path that will be not correct?
I have a feeling that we need method in SystemProperties class that will throw IllegalStateException in case when we must have a value.
Here instead of NPE we will introduce logic error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. But just curious, how previous code worked?

I think the whole OTel config source init fails, then config init fails.

And would it make sense to build a path that will be not correct?

That would be a question for @mcculls 😉

I have a feeling that we need method in SystemProperties class that will throw IllegalStateException in case when we must have a value.

We can still check if a value is missing using null check and no default value. The question stays: how should we deal with this case then if we don't want to have a logic error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I finally went with a safer approach: only do the ~ substitution if env.home property is set and retrieved.
I applied the same logic to the ConfigProvider (around line 475)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OTel code doesn't handle this condition: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigurationFile.java#L57 - so we can choose.

TBH I doubt that anyone except maybe developers uses the ~ prefix for OTel configuration files, instead preferring the more stable absolute path. This scenario is specific to someone who wants to use an external config file, declares the path to that file using ~, and has a security manager installed that disallows system property access.

This is so unlikely I think using "" is ok as a fallback - but TBH I would also be fine with letting it throw an exception, because this falls into the category of an exceptional situation where it might be safer to not try to second guess...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. But just curious, how previous code worked?

I doubt the tracer would have got this far - if such a strict security manager was installed and the tracer hadn't been granted this permission, then it would have bailed out earlier.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I revert to an IllegalStateException then?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with your current approach of just not doing the substitution

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. But just curious, how previous code worked?
And would it make sense to build a path that will be not correct?
I have a feeling that we need method in SystemProperties class that will throw IllegalStateException in case when we must have a value.
Here instead of NPE we will introduce logic error.

@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from 42d109e to 68fc6ca Compare July 18, 2025 15:25
Base automatically changed from bbujon/environment-step2 to master July 21, 2025 08:04
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch 2 times, most recently from e7cb973 to a3d54e9 Compare July 21, 2025 08:48
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from a3d54e9 to 9ae7f6a Compare July 21, 2025 11:22
@PerfectSlayer PerfectSlayer enabled auto-merge (squash) July 21, 2025 15:37
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from 9ae7f6a to d61ab39 Compare July 28, 2025 11:47
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from d61ab39 to d83f869 Compare July 28, 2025 12:36

@mhlidd mhlidd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 ! I'm wondering if it's worthwhile to have a followup PR add a ForbiddenAPIs rule to prevent the usages of System.setProperty() and System.clearProperty() to encourage the usage of the Environment component. This is something I'm already doing for System.getenv() for Config Inversion :).

@PerfectSlayer

Copy link
Copy Markdown
Contributor Author

Yes, this is something we should be doing.
I have 15+ stacked PRs that continues the migration and expect to put it in place only when fully migrated.
Will you be handling it then?

@mhlidd

mhlidd commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

@mcculls mcculls left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 ... just a query about the behaviour change in addByteBuddyRawSetting

@PerfectSlayer PerfectSlayer disabled auto-merge July 28, 2025 15:07
@PerfectSlayer PerfectSlayer force-pushed the bbujon/environment-step3 branch from d83f869 to 476cb5f Compare July 28, 2025 15:37
@PerfectSlayer PerfectSlayer enabled auto-merge (squash) July 28, 2025 15:47
@PerfectSlayer PerfectSlayer merged commit 24db888 into master Jul 29, 2025
504 checks passed
@PerfectSlayer PerfectSlayer deleted the bbujon/environment-step3 branch July 29, 2025 07:33
@github-actions github-actions Bot added this to the 1.52.0 milestone Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core tag: no release notes Changes to exclude from release notes type: enhancement Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants