Jon Thoms
Jon Thoms

Reputation: 10767

Dropwizard Enviornment Variable Substitution Type Check Error

I'm a Dropwizard newbie and am using an existing Dropwizard service, where I am attempting to create YML values that are overridable depending on the environment that my service is deployed to (i.e. Dev, QA, Prod). I tried Googling my error message, but really didn't find a whole lot for this specific error, so I thought that I would post a new question for this.

When I run my script that starts the service, I get the following error:

my-host:my-service jthoms$ ./start.sh 3.0.0
Starting my-service...
src/main/resources/configuration.yml has an error:
  * Incorrect type of value at: downstream_service.loggingEnabled; is of type: String, expected: boolean

Note that I have already created a MyServiceConfiguration class which extends Dropwizard's main Configuration class and delegates to a DownstreamServiceConfiguration class that has a boolean field for the loggingEnabled property. I did this in accordance with the Dropwizard Core doc.

My start.sh script is as follows:

#!/bin/sh

if [ "$#" -ne 1 ]; then
    echo "Please pass jar version! Usage: ./start.sh <jar_version>"
    exit 1
fi

echo "Starting my-service with version '$1'"

java $DEBUG_ARGS -Xmx128m -jar target/my-service-$1.jar server src/main/resources/configuration.yml

My configuration.yml is as follows:

server:
  ...
downstream_service:
  loggingEnabled: ${DW_DOWNSTREAM_SERVICE_LOGGING_ENABLED}
  ...
logging:
  ...

I don't understand what's causing this type safety error. How can I import my environment variables to Dropwizard as a non-string type?

Upvotes: 1

Views: 2758

Answers (1)

Jon Thoms
Jon Thoms

Reputation: 10767

After some Googling around, I found the solution(s) to my issue, and thought that I would share my answer here for others with the same error message I was getting.

The problem wasn't that my boolean DW_DOWNSTREAM_SERVICE_LOGGING_ENABLED environment was being treated as a string, the problem was the boolean YML property just wasn't being wired up correctly, so the string value it was finding wasn't the string literal, true. Rather, it was the string literal ${DW_DOWNSTREAM_SERVICE_LOGGING_ENABLED}. Basically, Dropwizard just wasn't substituting ${DW_DOWNSTREAM_SERVICE_LOGGING_ENABLED} for true in the first place.

The solution involved two things:

  1. Call the script that actually creates the environment variables from my start.sh script. I had actually forgotten to do this. Doh!
  2. Configure the application's bootstrap to use an EnvironmentVariableSubstitutor as per this answer. Note that you don't need to set the EnvironmentVariableSubstitutor to use non-strict checking, the shell variables aren't being unconditionally interpreted as strings by Dropwizard. The following code needs to be added to your main Application class.

    @Override
    public void initialize(Bootstrap<MyServiceConfiguration> bootstrap) {
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
                        new EnvironmentVariableSubstitutor()));
    }
    

Those two things combined solved the problem.

Upvotes: 1

Related Questions