Skip to content

Use uppercase letters for environment variable naming.#809

Open
kai8406 wants to merge 2 commits into
FrankChen021:masterfrom
kai8406:uppercase-letters
Open

Use uppercase letters for environment variable naming.#809
kai8406 wants to merge 2 commits into
FrankChen021:masterfrom
kai8406:uppercase-letters

Conversation

@kai8406
Copy link
Copy Markdown
Collaborator

@kai8406 kai8406 commented Jun 30, 2024

Use uppercase letters for environment variable naming.

#808

@FrankChen021 FrankChen021 added Component/Agent Incompatible agent and servers are not incompatible with old versions labels Jul 1, 2024
.replace('_', '.');
// Here we need to convert these characters back.
// Additionally, as the env variables are in uppercase, we need to convert them to lowercase.
.replace('_', '.').toLowerCase(Locale.ENGLISH);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Use toLowerCase to convert environment variable values from upper case to lower case may be not always correct.

Currently, any configuration class can be deserialized from external configuration source like file or environment variables, these classes may contain fields that are using camel style naming conventions.

For example, in org.bithon.agent.observability.tracing.config.TraceConfig, there's a field naming as traceResponseHeader. Also existing test case does not cover such scenario.

So to support the uppcase of the environment variables is a little bit tricky now. I have not come up with any idea to support it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

After reading the relevant code, I had a general understanding that the scope involved exceeded my previous estimation.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can add a property like camelNamingStyleProp to the TestProp class in the test file and improve the test case test_Environment to verify the deserialization for such field.

This makes sure new changes in this PR is compatible with such case. The comment below may be one way to resolve such problem.

@FrankChen021
Copy link
Copy Markdown
Owner

jackson supports to deserialize objects in case insensitive way. Maybe this is the simplest way.
We can configure the ObjectMapper inside the org.bithon.agent.configuration.Binder.

Here's the sample code

//
// Illustration of deserialization from uppercase 'Age' to lower case property 'age'
//
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // Enable case-insensitive property matching
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        String json = "{\"name\": \"John\", \"Age\": 30}";

        Person person = mapper.readValue(json, Person.class);
        System.out.println(person);
    }
}

class Person {
    private String name;
    private int age;

    // getters and setters
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}
//
// Illustration of same properties in different cases
//
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // Enable case-insensitive property matching
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        String json = "{\"name\": \"John\", \"Name\": \"Doe\"}";

        Person person = mapper.readValue(json, Person.class);
        System.out.println(person);
    }
}

class Person {
    private String name;

    // getters and setters
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "'}";
    }
}

ExternalSource.build(),
CommandLineArgsSource.build("bithon."),
EnvironmentSource.build("bithon_"));
EnvironmentSource.build("BITHON_"));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We can keep the lower case for compatibility.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree that the case of environment variables should not be strictly enforced. It is only necessary to ensure that the environment variables can be recognized, regardless of whether they are in uppercase or lowercase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component/Agent Incompatible agent and servers are not incompatible with old versions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants