Skip to content

Add support for OpenTelemetry's service.namespace #44499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

thecooldrop
Copy link
Contributor

@thecooldrop thecooldrop commented Mar 1, 2025

Add OTEL service attribute service.namespace. The value is set to same value as service.group currently. Since there is no specific environment variable to specify the service.namespace prescribed by OTEL, following chain is used to decide the value:

  • management.opentelemetry.resource-attributes configuration property
  • OTEL_RESOURCE_ATTRIBUTES environment variable
  • If service.group is configured, then set it to same name
  • Otherwise empty

Closes #44411

@thecooldrop thecooldrop changed the title Gh 44411 Add support for OpenTelemetry's service.namespace (#44411) Mar 1, 2025
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 1, 2025
Copy link
Contributor

@nosan nosan left a comment

Choose a reason for hiding this comment

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

Thank you so much, @thecooldrop
I've left some comments for your consideration.

Please don't do anything until the Spring Boot team had a chance to review it.

I’ve opened #44494 to eliminate the duplications in OpenTelemetryAutoConfiguration and OtlpMetricsPropertiesConfigAdapter, consolidating everything in one place.

@@ -80,6 +80,9 @@ private Resource toResource(Environment environment, OpenTelemetryProperties pro
.asMap();
attributes.computeIfAbsent("service.name", (key) -> getApplicationName(environment));
attributes.computeIfAbsent("service.group", (key) -> getApplicationGroup(environment));
// See https://github.com/spring-projects/spring-boot/issues/44411 for potential
// information about deprecation of "service.group" attribute
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that instead of reusing the existing getApplicationGroup method, a new method,
getServiceNamespace, can be introduced. This would make it possible to mark the
existing method with the @Deprecated annotation and include appropriate javadoc.

private String getServiceNamespace(Environment environment) {
    return environment.getProperty("spring.application.group");
}
/**
 * @deprecated ...
 */
@Deprecated(since = "3.5.0", forRemoval = true)
private String getApplicationGroup(Environment environment) {
}

Using the @Deprecated annotation would make it easier to ensure that the
service.group property is deleted in future versions.

The same could be done in OtlpMetricsPropertiesConfigAdapter

Copy link
Contributor Author

@thecooldrop thecooldrop Mar 2, 2025

Choose a reason for hiding this comment

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

Makes sense. As per your note, will wait for input from Spring team.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @nosan, i think that's a good idea.

@@ -163,6 +163,17 @@ void shouldUseDefaultApplicationGroupIfApplicationGroupIsNotSet() {
assertThat(createAdapter().resourceAttributes()).doesNotContainKey("service.group");
}

@Test
void shouldUseServiceGroupForServiceNamespaceIfServiceGroupIsSet() {
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 great to include a similar test in OpenTelemetryAutoConfigurationTests as well. Since OpenTelemetryAutoConfigurationTests already contains a test for checking service.group, you could potentially reuse that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was looking for that in exactly that class, but somehow I missed it in the mass.

Thanks for pointing this out. I will add this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that entire logic for determining the attributes, which are included is now contained in OpenTelemetryResourceAttributes, I will place the tests only once in this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a bit of a weird corner case. If service.group is populated via OTEL_RESOURCE_ATTRIBUTES env variable, and overrides spring.application.group configuration from Environment, then the question is if service.namespace should be populated from OTEL_RESOURCE_ATTRIBUTES as well, or from the service.application.group.

My expectation would be to populate it from service.application.group, as long as it is not listed in OTEL_RESOURCE_ATTRIBUTES

This choice is reflected in following test (note the similar, but different values for two entries):

	@Test
	void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
		this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.group=spring-boot");
		this.environment.setProperty("spring.application.group", "spring-boot-app");
		assertThat(getAttributes()).hasSize(3)
			.containsEntry("service.name", "unknown_service")
			.containsEntry("service.group", "spring-boot")
			.containsEntry("service.namespace", "spring-boot-app");
	}

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Mar 5, 2025
@mhalbritter
Copy link
Contributor

mhalbritter commented Mar 5, 2025

Could you please also rebase your changes on top of main? There has been some changes to the OpenTelemetry variables handling.

…to OpenTelemetryAutoconfiguration

Signed-off-by: Vanio Begic <[email protected]>
@thecooldrop
Copy link
Contributor Author

Could you please also rebase your changes on top of main? There has been some changes to the OpenTelemetry variables handling.

Done

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 5, 2025
@mhalbritter mhalbritter changed the title Add support for OpenTelemetry's service.namespace (#44411) Add support for OpenTelemetry's service.namespace Mar 6, 2025
@mhalbritter mhalbritter added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Mar 6, 2025
@mhalbritter mhalbritter added this to the 3.5.x milestone Mar 6, 2025
@mhalbritter mhalbritter self-assigned this Mar 6, 2025
mhalbritter pushed a commit that referenced this pull request Mar 6, 2025
@mhalbritter mhalbritter modified the milestones: 3.5.x, 3.5.0-M3 Mar 6, 2025
@mhalbritter
Copy link
Contributor

Thanks @thecooldrop !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for OpenTelemetry's service.namespace
4 participants