|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2012-2024 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.logging.opentelemetry.otlp; | 
|  | 18 | + | 
|  | 19 | +import java.util.Locale; | 
|  | 20 | + | 
|  | 21 | +import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter; | 
|  | 22 | +import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder; | 
|  | 23 | + | 
|  | 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | 
|  | 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | 
|  | 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 
|  | 27 | +import org.springframework.context.annotation.Bean; | 
|  | 28 | +import org.springframework.context.annotation.Configuration; | 
|  | 29 | + | 
|  | 30 | +/** | 
|  | 31 | + * Configurations imported by {@link OtlpLoggingAutoConfiguration}. | 
|  | 32 | + * | 
|  | 33 | + * @author Toshiaki Maki | 
|  | 34 | + */ | 
|  | 35 | +final class OtlpLoggingConfigurations { | 
|  | 36 | + | 
|  | 37 | +	private OtlpLoggingConfigurations() { | 
|  | 38 | +	} | 
|  | 39 | + | 
|  | 40 | +	@Configuration(proxyBeanMethods = false) | 
|  | 41 | +	static class ConnectionDetails { | 
|  | 42 | + | 
|  | 43 | +		@Bean | 
|  | 44 | +		@ConditionalOnMissingBean | 
|  | 45 | +		@ConditionalOnProperty(prefix = "management.otlp.logging", name = "endpoint") | 
|  | 46 | +		OtlpLoggingConnectionDetails otlpLogsConnectionDetails(OtlpLoggingProperties properties) { | 
|  | 47 | +			return new PropertiesOtlpLoggingConnectionDetails(properties); | 
|  | 48 | +		} | 
|  | 49 | + | 
|  | 50 | +		/** | 
|  | 51 | +		 * Adapts {@link OtlpLoggingProperties} to {@link OtlpLoggingConnectionDetails}. | 
|  | 52 | +		 */ | 
|  | 53 | +		static class PropertiesOtlpLoggingConnectionDetails implements OtlpLoggingConnectionDetails { | 
|  | 54 | + | 
|  | 55 | +			private final OtlpLoggingProperties properties; | 
|  | 56 | + | 
|  | 57 | +			PropertiesOtlpLoggingConnectionDetails(OtlpLoggingProperties properties) { | 
|  | 58 | +				this.properties = properties; | 
|  | 59 | +			} | 
|  | 60 | + | 
|  | 61 | +			@Override | 
|  | 62 | +			public String getEndpoint() { | 
|  | 63 | +				return this.properties.getEndpoint(); | 
|  | 64 | +			} | 
|  | 65 | + | 
|  | 66 | +		} | 
|  | 67 | + | 
|  | 68 | +	} | 
|  | 69 | + | 
|  | 70 | +	@Configuration(proxyBeanMethods = false) | 
|  | 71 | +	static class Exporters { | 
|  | 72 | + | 
|  | 73 | +		@ConditionalOnMissingBean(value = OtlpHttpLogRecordExporter.class, | 
|  | 74 | +				type = "io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter") | 
|  | 75 | +		@ConditionalOnBean(OtlpLoggingConnectionDetails.class) | 
|  | 76 | +		@Bean | 
|  | 77 | +		OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpLoggingProperties properties, | 
|  | 78 | +				OtlpLoggingConnectionDetails connectionDetails) { | 
|  | 79 | +			OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder() | 
|  | 80 | +				.setEndpoint(connectionDetails.getEndpoint()) | 
|  | 81 | +				.setCompression(properties.getCompression().name().toLowerCase(Locale.US)) | 
|  | 82 | +				.setTimeout(properties.getTimeout()); | 
|  | 83 | +			properties.getHeaders().forEach(builder::addHeader); | 
|  | 84 | +			return builder.build(); | 
|  | 85 | +		} | 
|  | 86 | + | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +} | 
0 commit comments