forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add properties to enable/disable tracing per exporter
There are now three new properties, which control the trace exporting on a more fine-grained level: - management.otlp.tracing.export.enabled - management.wavefront.tracing.export.enabled - management.zipkin.tracing.export.enabled They default to null, and if set, take precedence over the global management.metrics.enabled property. Closes spring-projectsgh-34620
- Loading branch information
1 parent
8a9feb0
commit 5bcd3ff
Showing
13 changed files
with
307 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ava/org/springframework/boot/actuate/autoconfigure/tracing/OnEnabledTracingCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.tracing; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionMessage; | ||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | ||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* {@link SpringBootCondition} to check whether tracing is enabled. | ||
* | ||
* @author Moritz Halbritter | ||
* @see ConditionalOnEnabledTracing | ||
*/ | ||
class OnEnabledTracingCondition extends SpringBootCondition { | ||
|
||
private static final String GLOBAL_PROPERTY = "management.tracing.enabled"; | ||
|
||
private static final String EXPORTER_PROPERTY = "management.%s.tracing.export.enabled"; | ||
|
||
@Override | ||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
Boolean globalTracingEnabled = context.getEnvironment().getProperty(GLOBAL_PROPERTY, Boolean.class); | ||
String tracingExporter = getExporterName(metadata); | ||
Boolean exporterTracingEnabled = null; | ||
if (StringUtils.hasLength(tracingExporter)) { | ||
exporterTracingEnabled = context.getEnvironment() | ||
.getProperty(EXPORTER_PROPERTY.formatted(tracingExporter), Boolean.class); | ||
} | ||
if (exporterTracingEnabled != null) { | ||
return new ConditionOutcome(exporterTracingEnabled, | ||
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class) | ||
.because(EXPORTER_PROPERTY.formatted(tracingExporter) + " is " + exporterTracingEnabled)); | ||
} | ||
if (globalTracingEnabled != null) { | ||
return new ConditionOutcome(globalTracingEnabled, | ||
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class) | ||
.because(GLOBAL_PROPERTY + " is " + globalTracingEnabled)); | ||
} | ||
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledTracing.class) | ||
.because("tracing is enabled by default")); | ||
} | ||
|
||
private static String getExporterName(AnnotatedTypeMetadata metadata) { | ||
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName()); | ||
if (attributes == null) { | ||
return null; | ||
} | ||
return (String) attributes.get("value"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...rg/springframework/boot/actuate/autoconfigure/tracing/OnEnabledTracingConditionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.tracing; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link OnEnabledTracingCondition}. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
class OnEnabledTracingConditionTests { | ||
|
||
@Test | ||
void shouldMatchIfNoPropertyIsSet() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(), mockMetaData("")); | ||
assertThat(outcome.isMatch()).isTrue(); | ||
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing tracing is enabled by default"); | ||
} | ||
|
||
@Test | ||
void shouldNotMatchIfGlobalPropertyIsFalse() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition | ||
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "false")), mockMetaData("")); | ||
assertThat(outcome.isMatch()).isFalse(); | ||
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is false"); | ||
} | ||
|
||
@Test | ||
void shouldMatchIfGlobalPropertyIsTrue() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition | ||
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "true")), mockMetaData("")); | ||
assertThat(outcome.isMatch()).isTrue(); | ||
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is true"); | ||
} | ||
|
||
@Test | ||
void shouldNotMatchIfExporterPropertyIsFalse() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition.getMatchOutcome( | ||
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "false")), | ||
mockMetaData("zipkin")); | ||
assertThat(outcome.isMatch()).isFalse(); | ||
assertThat(outcome.getMessage()) | ||
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false"); | ||
} | ||
|
||
@Test | ||
void shouldMatchIfExporterPropertyIsTrue() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition.getMatchOutcome( | ||
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "true")), | ||
mockMetaData("zipkin")); | ||
assertThat(outcome.isMatch()).isTrue(); | ||
assertThat(outcome.getMessage()) | ||
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true"); | ||
} | ||
|
||
@Test | ||
void exporterPropertyShouldOverrideGlobalPropertyIfTrue() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext( | ||
Map.of("management.tracing.enabled", "false", "management.zipkin.tracing.export.enabled", "true")), | ||
mockMetaData("zipkin")); | ||
assertThat(outcome.isMatch()).isTrue(); | ||
assertThat(outcome.getMessage()) | ||
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true"); | ||
} | ||
|
||
@Test | ||
void exporterPropertyShouldOverrideGlobalPropertyIfFalse() { | ||
OnEnabledTracingCondition condition = new OnEnabledTracingCondition(); | ||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext( | ||
Map.of("management.tracing.enabled", "true", "management.zipkin.tracing.export.enabled", "false")), | ||
mockMetaData("zipkin")); | ||
assertThat(outcome.isMatch()).isFalse(); | ||
assertThat(outcome.getMessage()) | ||
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false"); | ||
} | ||
|
||
private ConditionContext mockConditionContext() { | ||
return mockConditionContext(Collections.emptyMap()); | ||
} | ||
|
||
private ConditionContext mockConditionContext(Map<String, String> properties) { | ||
ConditionContext context = mock(ConditionContext.class); | ||
MockEnvironment environment = new MockEnvironment(); | ||
properties.forEach(environment::setProperty); | ||
given(context.getEnvironment()).willReturn(environment); | ||
return context; | ||
} | ||
|
||
private AnnotatedTypeMetadata mockMetaData(String exporter) { | ||
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class); | ||
given(metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName())) | ||
.willReturn(Map.of("value", exporter)); | ||
return metadata; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.