|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.logging.otlp;
|
18 | 18 |
|
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import io.opentelemetry.api.metrics.MeterProvider; |
19 | 22 | import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
|
| 23 | +import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder; |
20 | 24 | import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
|
| 25 | +import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder; |
21 | 26 | import io.opentelemetry.sdk.logs.export.LogRecordExporter;
|
22 | 27 | import okhttp3.HttpUrl;
|
23 | 28 | import org.junit.jupiter.api.Test;
|
|
30 | 35 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
31 | 36 | import org.springframework.context.annotation.Bean;
|
32 | 37 | import org.springframework.context.annotation.Configuration;
|
| 38 | +import org.springframework.test.util.ReflectionTestUtils; |
33 | 39 |
|
34 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
35 | 41 |
|
36 | 42 | /**
|
37 | 43 | * Tests for {@link OtlpLoggingAutoConfiguration}.
|
38 | 44 | *
|
39 | 45 | * @author Toshiaki Maki
|
| 46 | + * @author Moritz Halbritter |
40 | 47 | */
|
41 | 48 | class OtlpLoggingAutoConfigurationTests {
|
42 | 49 |
|
@@ -118,7 +125,6 @@ void shouldBackOffWhenCustomOtlpLoggingConnectionDetailsIsDefined() {
|
118 | 125 | assertThat(otlpHttpLogRecordExporter).extracting("delegate.httpSender.url")
|
119 | 126 | .isEqualTo(HttpUrl.get("https://otel.example.com/v1/logs"));
|
120 | 127 | });
|
121 |
| - |
122 | 128 | }
|
123 | 129 |
|
124 | 130 | @Test
|
@@ -155,31 +161,74 @@ void shouldUseGrpcExporterIfTransportIsSetToGrpc() {
|
155 | 161 | });
|
156 | 162 | }
|
157 | 163 |
|
| 164 | + @Test |
| 165 | + @SuppressWarnings("unchecked") |
| 166 | + void httpShouldUseMeterProviderIfSet() { |
| 167 | + this.contextRunner.withUserConfiguration(MeterProviderConfiguration.class) |
| 168 | + .withPropertyValues("management.otlp.logging.endpoint=http://localhost:4318/v1/logs") |
| 169 | + .run((context) -> { |
| 170 | + OtlpHttpLogRecordExporter otlpHttpLogRecordExporter = context.getBean(OtlpHttpLogRecordExporter.class); |
| 171 | + OtlpHttpLogRecordExporterBuilder builder = otlpHttpLogRecordExporter.toBuilder(); |
| 172 | + Supplier<MeterProvider> meterProviderSupplier = (Supplier<MeterProvider>) ReflectionTestUtils |
| 173 | + .getField(ReflectionTestUtils.getField(builder, "delegate"), "meterProviderSupplier"); |
| 174 | + assertThat(meterProviderSupplier).isNotNull(); |
| 175 | + assertThat(meterProviderSupplier.get()).isSameAs(MeterProviderConfiguration.meterProvider); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @SuppressWarnings("unchecked") |
| 181 | + void grpcShouldUseMeterProviderIfSet() { |
| 182 | + this.contextRunner.withUserConfiguration(MeterProviderConfiguration.class) |
| 183 | + .withPropertyValues("management.otlp.logging.endpoint=http://localhost:4318/v1/logs", |
| 184 | + "management.otlp.logging.transport=grpc") |
| 185 | + .run((context) -> { |
| 186 | + OtlpGrpcLogRecordExporter otlpGrpcLogRecordExporter = context.getBean(OtlpGrpcLogRecordExporter.class); |
| 187 | + OtlpGrpcLogRecordExporterBuilder builder = otlpGrpcLogRecordExporter.toBuilder(); |
| 188 | + Supplier<MeterProvider> meterProviderSupplier = (Supplier<MeterProvider>) ReflectionTestUtils |
| 189 | + .getField(ReflectionTestUtils.getField(builder, "delegate"), "meterProviderSupplier"); |
| 190 | + assertThat(meterProviderSupplier).isNotNull(); |
| 191 | + assertThat(meterProviderSupplier.get()).isSameAs(MeterProviderConfiguration.meterProvider); |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + @Configuration(proxyBeanMethods = false) |
| 196 | + private static final class MeterProviderConfiguration { |
| 197 | + |
| 198 | + static final MeterProvider meterProvider = (instrumentationScopeName) -> null; |
| 199 | + |
| 200 | + @Bean |
| 201 | + MeterProvider meterProvider() { |
| 202 | + return meterProvider; |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
158 | 207 | @Configuration(proxyBeanMethods = false)
|
159 |
| - public static class CustomHttpExporterConfiguration { |
| 208 | + private static final class CustomHttpExporterConfiguration { |
160 | 209 |
|
161 | 210 | @Bean
|
162 |
| - public OtlpHttpLogRecordExporter customOtlpHttpLogRecordExporter() { |
| 211 | + OtlpHttpLogRecordExporter customOtlpHttpLogRecordExporter() { |
163 | 212 | return OtlpHttpLogRecordExporter.builder().build();
|
164 | 213 | }
|
165 | 214 |
|
166 | 215 | }
|
167 | 216 |
|
168 | 217 | @Configuration(proxyBeanMethods = false)
|
169 |
| - public static class CustomGrpcExporterConfiguration { |
| 218 | + private static final class CustomGrpcExporterConfiguration { |
170 | 219 |
|
171 | 220 | @Bean
|
172 |
| - public OtlpGrpcLogRecordExporter customOtlpGrpcLogRecordExporter() { |
| 221 | + OtlpGrpcLogRecordExporter customOtlpGrpcLogRecordExporter() { |
173 | 222 | return OtlpGrpcLogRecordExporter.builder().build();
|
174 | 223 | }
|
175 | 224 |
|
176 | 225 | }
|
177 | 226 |
|
178 | 227 | @Configuration(proxyBeanMethods = false)
|
179 |
| - public static class CustomOtlpLoggingConnectionDetails { |
| 228 | + private static final class CustomOtlpLoggingConnectionDetails { |
180 | 229 |
|
181 | 230 | @Bean
|
182 |
| - public OtlpLoggingConnectionDetails customOtlpLoggingConnectionDetails() { |
| 231 | + OtlpLoggingConnectionDetails customOtlpLoggingConnectionDetails() { |
183 | 232 | return (transport) -> "https://otel.example.com/v1/logs";
|
184 | 233 | }
|
185 | 234 |
|
|
0 commit comments