|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.opentelemetry; |
| 18 | + |
| 19 | +import io.opentelemetry.api.OpenTelemetry; |
| 20 | +import io.opentelemetry.api.common.Attributes; |
| 21 | +import io.opentelemetry.context.propagation.ContextPropagators; |
| 22 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 23 | +import io.opentelemetry.sdk.OpenTelemetrySdkBuilder; |
| 24 | +import io.opentelemetry.sdk.logs.SdkLoggerProvider; |
| 25 | +import io.opentelemetry.sdk.metrics.SdkMeterProvider; |
| 26 | +import io.opentelemetry.sdk.resources.Resource; |
| 27 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 28 | +import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; |
| 29 | + |
| 30 | +import org.springframework.beans.factory.ObjectProvider; |
| 31 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 35 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.core.env.Environment; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry. |
| 41 | + * |
| 42 | + * @author Moritz Halbritter |
| 43 | + * @since 3.2.0 |
| 44 | + */ |
| 45 | +@AutoConfiguration |
| 46 | +@ConditionalOnClass(OpenTelemetrySdk.class) |
| 47 | +@EnableConfigurationProperties(OpenTelemetryProperties.class) |
| 48 | +public class OpenTelemetryInfrastructureAutoConfiguration { |
| 49 | + |
| 50 | + /** |
| 51 | + * Default value for application name if {@code spring.application.name} is not set. |
| 52 | + */ |
| 53 | + private static final String DEFAULT_APPLICATION_NAME = "application"; |
| 54 | + |
| 55 | + @Bean |
| 56 | + @ConditionalOnMissingBean(OpenTelemetry.class) |
| 57 | + OpenTelemetrySdk openTelemetry(ObjectProvider<SdkTracerProvider> tracerProvider, |
| 58 | + ObjectProvider<ContextPropagators> propagators, ObjectProvider<SdkLoggerProvider> loggerProvider, |
| 59 | + ObjectProvider<SdkMeterProvider> meterProvider) { |
| 60 | + OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder(); |
| 61 | + tracerProvider.ifAvailable(builder::setTracerProvider); |
| 62 | + propagators.ifAvailable(builder::setPropagators); |
| 63 | + loggerProvider.ifAvailable(builder::setLoggerProvider); |
| 64 | + meterProvider.ifAvailable(builder::setMeterProvider); |
| 65 | + return builder.build(); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + @ConditionalOnMissingBean |
| 70 | + Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) { |
| 71 | + String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME); |
| 72 | + return Resource.getDefault() |
| 73 | + .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName))) |
| 74 | + .merge(properties.toResource()); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments