spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Auto-configure the Postgres application_name when using Docker Compose

Open nosan opened this issue 1 year ago • 5 comments

gh-40772

I added since 3.4.0 everywhere but maybe it could be merged earlier if everything is ok.

Thanks in advance

nosan avatar Sep 26 '24 22:09 nosan

Should I also configure ApplicationName in PostgresR2dbcDockerComposeConnectionDetailsFactory?

nosan avatar Sep 27 '24 07:09 nosan

Should I also configure ApplicationName in PostgresR2dbcDockerComposeConnectionDetailsFactory?

Auto-configure for r2dbc as well.

nosan avatar Sep 27 '24 07:09 nosan

Thank you for your feedback @snicoll and @wilkinsona

  1. I can add unit tests for PostgresJdbcDockerComposeConnectionDetails and PostgresDbR2dbcDockerComposeConnectionDetails without involving docker, to verify that all options are set correctly.
  2. I can add a docker integration test for PostgresJdbcDockerComposeConnectionDetailsFactory and PostgresR2dbcDockerComposeConnectionDetailsFactory to verify the application name is correctly set.
	@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL,
			properties = "spring.application.name=my-app")
	void runCreatesConnectionDetailsWithAutoConfiguredApplicationName(JdbcConnectionDetails connectionDetails)
			throws ClassNotFoundException {
		assertThat(connectionDetails.getUsername()).isEqualTo("myuser");
		assertThat(connectionDetails.getPassword()).isEqualTo("secret");
		assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("?ApplicationName=my-app");
		assertThat(getApplicationName(connectionDetails)).isEqualTo("my-app");
	}

	private String getApplicationName(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
		JdbcTemplate template = getJdbcTemplate(connectionDetails);
		return template.queryForObject("select current_setting('application_name')", String.class);
	}

and

@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL,
			properties = "spring.application.name=my-app")
	void runCreatesConnectionDetailsWithAutoConfiguredApplicationName(R2dbcConnectionDetails connectionDetails) {
		assertConnectionDetails(connectionDetails);
		assertThat(getApplicationName(connectionDetails)).isEqualTo("my-app");
	}

	private String getApplicationName(R2dbcConnectionDetails connectionDetails) {
		ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
		return DatabaseClient.create(ConnectionFactories.get(connectionFactoryOptions))
			.sql("select current_setting('application_name')")
			.map((row, metadata) -> row.get(0))
			.first()
			.map(Objects::toString)
			.block(Duration.ofSeconds(30));
	}

Does it make sense?

nosan avatar Sep 30 '24 14:09 nosan

Also, should I revert my DockerComposeTestExtension changes? I can test the application name is set via docker-compose labels.

nosan avatar Sep 30 '24 16:09 nosan

PR has been updated.

  • I reverted my DockerComposeTestExtension and JdbcUrlBuilder changes.
  • I added unit tests for both ConnectionDetails and removed docker-compose integration tests.
  • I left only two docker-compose integration tests for checking that application_name had the desired effect on the Postgres side.

nosan avatar Sep 30 '24 19:09 nosan