|
| 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.logs.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 OtlpLogsAutoConfiguration}. |
| 32 | + * |
| 33 | + * @author Toshiaki Maki |
| 34 | + * @since 3.4.0 |
| 35 | + */ |
| 36 | +public class OtlpLogsConfigurations { |
| 37 | + |
| 38 | + @Configuration(proxyBeanMethods = false) |
| 39 | + static class ConnectionDetails { |
| 40 | + |
| 41 | + @Bean |
| 42 | + @ConditionalOnMissingBean |
| 43 | + @ConditionalOnProperty(prefix = "management.otlp.logs", name = "endpoint") |
| 44 | + OtlpLogsConnectionDetails otlpLogsConnectionDetails(OtlpProperties properties) { |
| 45 | + return new PropertiesOtlpLogsConnectionDetails(properties); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Adapts {@link OtlpProperties} to {@link OtlpLogsConnectionDetails}. |
| 50 | + */ |
| 51 | + static class PropertiesOtlpLogsConnectionDetails implements OtlpLogsConnectionDetails { |
| 52 | + |
| 53 | + private final OtlpProperties properties; |
| 54 | + |
| 55 | + PropertiesOtlpLogsConnectionDetails(OtlpProperties properties) { |
| 56 | + this.properties = properties; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getUrl() { |
| 61 | + return this.properties.getEndpoint(); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Configuration(proxyBeanMethods = false) |
| 69 | + static class Exporters { |
| 70 | + |
| 71 | + @ConditionalOnMissingBean(value = OtlpHttpLogRecordExporter.class, |
| 72 | + type = "io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter") |
| 73 | + @ConditionalOnBean(OtlpLogsConnectionDetails.class) |
| 74 | + @Bean |
| 75 | + OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpProperties properties, |
| 76 | + OtlpLogsConnectionDetails connectionDetails) { |
| 77 | + OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder() |
| 78 | + .setEndpoint(connectionDetails.getUrl()) |
| 79 | + .setCompression(properties.getCompression().name().toLowerCase(Locale.US)) |
| 80 | + .setTimeout(properties.getTimeout()); |
| 81 | + properties.getHeaders().forEach(builder::addHeader); |
| 82 | + return builder.build(); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments