Skip to content

Commit 7af5fd9

Browse files
committed
Merge pull request #52878 from anqorithm
Closes gh-52878 * pr/52878: Polish 'Fix startup failure when an OpenTelemetry timeout is set to 0' Fix startup failure when an OpenTelemetry timeout is set to 0
2 parents cfdacec + 4b4b48e commit 7af5fd9

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryEnvironmentVariables.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
* Reads and transforms OpenTelemetry environment variables.
2323
*
2424
* @author Moritz Halbritter
25+
* @author Abdullah Alqahtani
2526
*/
2627
class OpenTelemetryEnvironmentVariables {
2728

29+
// The largest duration that can be converted to nanoseconds
30+
private static final String MAX_TIMEOUT = Duration.ofNanos(Long.MAX_VALUE).toString();
31+
2832
private final Log logger;
2933

3034
private final Function<String, @Nullable String> envLookup;
@@ -113,7 +117,7 @@ EnvVariable getTimeout(String name) {
113117
return EnvVariable.none();
114118
}
115119
if (intValue == 0) {
116-
return EnvVariable.of(name, Duration.ofSeconds(Long.MAX_VALUE).toString());
120+
return EnvVariable.of(name, MAX_TIMEOUT);
117121
}
118122
return EnvVariable.of(name, Duration.ofMillis(intValue).toString());
119123
}

‎module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryEnvironmentVariableEnvironmentPostProcessorTests.java‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Tests for {@link OpenTelemetryEnvironmentVariableEnvironmentPostProcessor}.
3333
*
3434
* @author Moritz Halbritter
35+
* @author Abdullah Alqahtani
3536
*/
3637
@ExtendWith(OutputCaptureExtension.class)
3738
class OpenTelemetryEnvironmentVariableEnvironmentPostProcessorTests {
@@ -718,20 +719,16 @@ void fallbackEndpointWithTrailingSlashShouldAppendPathCorrectly() {
718719
@Test
719720
void timeoutZeroShouldBeInterpretedAsInfinite() {
720721
Environment environment = runProcessor(Map.of("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "0"));
721-
assertThat(environment.getProperty("management.opentelemetry.tracing.export.otlp.timeout"))
722-
.isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
722+
String max = Duration.ofNanos(Long.MAX_VALUE).toString();
723+
assertThat(environment.getProperty("management.opentelemetry.tracing.export.otlp.timeout")).isEqualTo(max);
723724
environment = runProcessor(Map.of("OTEL_EXPORTER_OTLP_LOGS_TIMEOUT", "0"));
724-
assertThat(environment.getProperty("management.opentelemetry.logging.export.otlp.timeout"))
725-
.isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
725+
assertThat(environment.getProperty("management.opentelemetry.logging.export.otlp.timeout")).isEqualTo(max);
726726
environment = runProcessor(Map.of("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT", "0"));
727-
assertThat(environment.getProperty("management.otlp.metrics.export.read-timeout"))
728-
.isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
727+
assertThat(environment.getProperty("management.otlp.metrics.export.read-timeout")).isEqualTo(max);
729728
environment = runProcessor(Map.of("OTEL_BSP_EXPORT_TIMEOUT", "0"));
730-
assertThat(environment.getProperty("management.opentelemetry.tracing.export.timeout"))
731-
.isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
729+
assertThat(environment.getProperty("management.opentelemetry.tracing.export.timeout")).isEqualTo(max);
732730
environment = runProcessor(Map.of("OTEL_BLRP_EXPORT_TIMEOUT", "0"));
733-
assertThat(environment.getProperty("management.opentelemetry.logging.export.timeout"))
734-
.isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
731+
assertThat(environment.getProperty("management.opentelemetry.logging.export.timeout")).isEqualTo(max);
735732
}
736733

737734
@Test

‎module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetryEnvironmentVariablesTests.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void getTimeoutShouldTreatZeroAsInfinity() {
209209
OpenTelemetryEnvironmentVariables env = createEnv(Map.of("VAR", "0"));
210210
EnvVariable result = env.getTimeout("VAR");
211211
assertThat(result.isPresent()).isTrue();
212-
assertThat(result.value()).isEqualTo(Duration.ofSeconds(Long.MAX_VALUE).toString());
212+
assertThat(Duration.parse(result.value()).toNanos()).isEqualTo(Long.MAX_VALUE);
213213
}
214214

215215
@Test

‎module/spring-boot-opentelemetry/src/test/java/org/springframework/boot/opentelemetry/autoconfigure/logging/OpenTelemetryLoggingAutoConfigurationTests.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Tests for {@link OpenTelemetryLoggingAutoConfiguration}.
3737
*
3838
* @author Moritz Halbritter
39+
* @author Abdullah Alqahtani
3940
*/
4041
class OpenTelemetryLoggingAutoConfigurationTests {
4142

@@ -164,6 +165,14 @@ void shouldConfigureBatchLogRecordProcessorWithProperties() {
164165
});
165166
}
166167

168+
@Test
169+
void shouldConfigureBatchLogRecordProcessorWithInfiniteExportTimeout() {
170+
this.contextRunner
171+
.withPropertyValues("management.opentelemetry.logging.export.timeout=" + Duration.ofNanos(Long.MAX_VALUE))
172+
.run((context) -> assertThat(context.getBean(BatchLogRecordProcessor.class)).extracting("worker")
173+
.hasFieldOrPropertyWithValue("exporterTimeoutNanos", Long.MAX_VALUE));
174+
}
175+
167176
@Configuration(proxyBeanMethods = false)
168177
static class UserConfiguration {
169178

0 commit comments

Comments
 (0)