Skip to content

Avoid usage of static getters when accessing configuration parameters#863

Merged
labbati merged 11 commits intomasterfrom
labbati/non-static-config
Jun 13, 2019
Merged

Avoid usage of static getters when accessing configuration parameters#863
labbati merged 11 commits intomasterfrom
labbati/non-static-config

Conversation

@labbati
Copy link
Member

@labbati labbati commented Jun 5, 2019

There were many places in our code where we directly accessed configuration properties directly using static getter instead of reading those values from the singleton configuration instance.

While this can be seen as the first step into a large project to improve our configuration mechanism, for now we just want to avoid usage of those static method from outside the Config class.

@labbati labbati added tag: breaking change Breaking changes comp: core Tracer core labels Jun 5, 2019
@labbati labbati added this to the 0.30.0 milestone Jun 5, 2019
@labbati labbati changed the title Labbati/non static config Avoid usage of static getters when accessing configuration parameters Jun 6, 2019
@labbati labbati marked this pull request as ready for review June 6, 2019 15:29
@labbati labbati requested a review from a team as a code owner June 6, 2019 15:29
Copy link
Contributor

@tylerbenson tylerbenson left a comment

Choose a reason for hiding this comment

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

Bunch of comments. If you'd rather leave as is, LMK and I'll approve.

when:
BaseDecorator dec = withSystemProperty("dd.${integName}.analytics.enabled", "true") {
withSystemProperty("dd.${integName}.analytics.sample-rate", "$sampleRate") {
ConfigUtils.resetConfig()
Copy link
Contributor

Choose a reason for hiding this comment

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

Want to take this opportunity to remove withSystemProperty and migrate to withConfigOverride?

// loads opentracing before bootstrap classpath is setup
// so we declare tracer as an object and cast when needed.
protected static final Object TEST_TRACER;
protected static Object TEST_TRACER;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer if we could avoid this somehow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately we are accessing the global config from within the tracer constructor so I fear that this is necessary until we proceed with our future plan: i.e.

  • injecting the config into the tracer
  • always use that one from within the tracer
  • when you need to access it from a static method then use the tracer to retrieve it

So basically the tracer will become our source of truth for the config object, so please see this as temporarily until we have time to implement the second part of the plan :)

Moreover, I would notice that here we are in a test, not in code, so having a non final variable it is acceptable IMHO.

Copy link
Contributor

Choose a reason for hiding this comment

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

I realize we are in a test, but I think it leaves opportunity for confusing bugs down the road. If we can't work around it, it's fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just realized this should not be necessary. Looking into it.

* tot he new config instance.
*/
static void resetConfig() {
static void resetConfig(preserveRuntimeId = false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make preserveRuntimeId always true? Not sure we really need to test with the false case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this makes sense. Let me see what the consequences are.

static {
ConfigUtils.makeConfigInstanceModifiable()
System.setProperty("dd.trace.methods", "package.ClassName[method1,method2];${ConfigTracedCallable.name}[call]")
ConfigUtils.resetConfig()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: add a check inside makeConfigInstanceModifiable to ensure it is only run once (it won't hurt anything to run again... it's just unnecessary).

Then change this sequence to something more like this:

ConfigUtils.updateConfig {
  System.setProperty()
}

This would implicitly apply the makeConfigInstanceModifiable if necessary and resetConfig.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the idea. 👍 much better


traceEnabled = getPropertyBooleanValue(properties, TRACE_ENABLED, parent.traceEnabled);
integrationsEnabled =
getPropertyBooleanValue(properties, INTEGRATIONS_ENABLED, parent.integrationsEnabled);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if we could restructure this class so we didn't have to duplicate all this between each constructor. Perhaps we should move to a builder instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've tried that in the past but could not find a good way to do that, unfortunately. I do not think builder would help here: you would still have to handle two different config sources separately listing all fields (or builder methods) - it probably would work, but it would still be very verbose...

Copy link
Member Author

Choose a reason for hiding this comment

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

I would call it out of scope from this PR. And yes I totally agree with your concern. Let's wrap up our minds if we find a better approach. I would not block this PR tough.

}
}

static void makeConfigInstanceModifiable() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want a check to prevent this from being run multiple times?

*/
static void resetConfig() {
// Ensure the class was retransformed properly in AgentTestRunner.makeConfigInstanceModifiable()
static void resetConfig(preserveRuntimeId = true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems this argument is no longer needed.

// loads opentracing before bootstrap classpath is setup
// so we declare tracer as an object and cast when needed.
protected static final Object TEST_TRACER;
protected static Object TEST_TRACER;
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize we are in a test, but I think it leaves opportunity for confusing bugs down the road. If we can't work around it, it's fine.

@labbati labbati removed the tag: breaking change Breaking changes label Jun 13, 2019
@labbati
Copy link
Member Author

labbati commented Jun 13, 2019

@tylerbenson ack to you just in case you like to take a final look after your latest comments.

Copy link
Contributor

@tylerbenson tylerbenson left a comment

Choose a reason for hiding this comment

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

Nice work!

@labbati labbati merged commit 075b300 into master Jun 13, 2019
@tylerbenson tylerbenson deleted the labbati/non-static-config branch June 13, 2019 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants