forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split OpenTelemetry auto-configuration
The OpenTelemetry bean is now configured in the OpenTelemetryAutoConfiguration. This method also applies SdkLoggerProvider and SdkMeterProvider. Additionally, the OpenTelemetry Resource is now a bean. Resource attributes can now be configured through properties The resourceAttributes in OtlpProperties have been deprecated in favor of the new one in OpenTelemetryProperties. Closes spring-projectsgh-36544 Closes spring-projectsgh-36545
- Loading branch information
1 parent
346ebbc
commit 6d14596
Showing
15 changed files
with
413 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...oot/actuate/autoconfigure/opentelemetry/OpenTelemetryInfrastructureAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.opentelemetry; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder; | ||
import io.opentelemetry.sdk.logs.SdkLoggerProvider; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.env.Environment; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry. | ||
* | ||
* @author Moritz Halbritter | ||
* @since 3.2.0 | ||
*/ | ||
@AutoConfiguration | ||
@ConditionalOnClass(OpenTelemetrySdk.class) | ||
@EnableConfigurationProperties(OpenTelemetryProperties.class) | ||
public class OpenTelemetryInfrastructureAutoConfiguration { | ||
|
||
/** | ||
* Default value for application name if {@code spring.application.name} is not set. | ||
*/ | ||
private static final String DEFAULT_APPLICATION_NAME = "application"; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(OpenTelemetry.class) | ||
OpenTelemetrySdk openTelemetry(ObjectProvider<SdkTracerProvider> tracerProvider, | ||
ObjectProvider<ContextPropagators> propagators, ObjectProvider<SdkLoggerProvider> loggerProvider, | ||
ObjectProvider<SdkMeterProvider> meterProvider) { | ||
OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder(); | ||
tracerProvider.ifAvailable(builder::setTracerProvider); | ||
propagators.ifAvailable(builder::setPropagators); | ||
loggerProvider.ifAvailable(builder::setLoggerProvider); | ||
meterProvider.ifAvailable(builder::setMeterProvider); | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) { | ||
String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME); | ||
return Resource.getDefault() | ||
.merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName))) | ||
.merge(properties.toResource()); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...org/springframework/boot/actuate/autoconfigure/opentelemetry/OpenTelemetryProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.opentelemetry; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.resources.ResourceBuilder; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties for OpenTelemetry. | ||
* | ||
* @author Moritz Halbritter | ||
* @since 3.2.0 | ||
*/ | ||
@ConfigurationProperties(prefix = "management.opentelemetry") | ||
public class OpenTelemetryProperties { | ||
|
||
/** | ||
* Resource attributes. | ||
*/ | ||
private Map<String, String> resourceAttributes = new HashMap<>(); | ||
|
||
public Map<String, String> getResourceAttributes() { | ||
return this.resourceAttributes; | ||
} | ||
|
||
public void setResourceAttributes(Map<String, String> resourceAttributes) { | ||
this.resourceAttributes = resourceAttributes; | ||
} | ||
|
||
Resource toResource() { | ||
ResourceBuilder builder = Resource.builder(); | ||
for (Entry<String, String> entry : this.resourceAttributes.entrySet()) { | ||
builder.put(entry.getKey(), entry.getValue()); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
.../main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Auto-configuration for OpenTelemetry. | ||
*/ | ||
package org.springframework.boot.actuate.autoconfigure.opentelemetry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.