Skip to content

Commit 3768d22

Browse files
For Gateway will set reactor instrumentation to manual; fixes gh-1710
1 parent ed68a10 commit 3768d22

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2013-2019 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.cloud.sleuth.instrument.web.client;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.env.EnvironmentPostProcessor;
27+
import org.springframework.core.env.ConfigurableEnvironment;
28+
import org.springframework.core.env.MapPropertySource;
29+
import org.springframework.core.env.MutablePropertySources;
30+
import org.springframework.core.env.PropertySource;
31+
import org.springframework.util.ClassUtils;
32+
import org.springframework.util.StringUtils;
33+
34+
/**
35+
* Adds default properties for a gateway application.
36+
*
37+
* @author Marcin Grzejszczak
38+
* @since 3.0.0
39+
*/
40+
class TraceGatewayEnvironmentPostProcessor implements EnvironmentPostProcessor {
41+
42+
private static final Log log = LogFactory.getLog(TraceGatewayEnvironmentPostProcessor.class);
43+
44+
private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
45+
46+
@Override
47+
public void postProcessEnvironment(ConfigurableEnvironment environment,
48+
SpringApplication application) {
49+
Map<String, Object> map = new HashMap<>();
50+
if (sleuthEnabled(environment) && isGatewayOnTheClasspath()) {
51+
String instrumentationType = environment
52+
.getProperty("spring.sleuth.reactor.instrumentation-type");
53+
if (log.isDebugEnabled()) {
54+
log.debug("Found the following instrumentation type [" + instrumentationType + "]");
55+
}
56+
if (StringUtils.isEmpty(instrumentationType)) {
57+
instrumentationType = "manual";
58+
if (log.isDebugEnabled()) {
59+
log.debug("No instrumentation type passed, will force it to [" + instrumentationType + "]");
60+
}
61+
}
62+
map.put("spring.sleuth.reactor.instrumentation-type", instrumentationType);
63+
}
64+
addOrReplace(environment.getPropertySources(), map);
65+
}
66+
67+
private boolean sleuthEnabled(ConfigurableEnvironment environment) {
68+
return Boolean
69+
.parseBoolean(environment.getProperty("spring.sleuth.enabled", "true"));
70+
}
71+
72+
private boolean isGatewayOnTheClasspath() {
73+
try {
74+
ClassUtils.forName("org.springframework.cloud.gateway.filter.GatewayFilter",
75+
null);
76+
return true;
77+
}
78+
catch (ClassNotFoundException e) {
79+
return false;
80+
}
81+
}
82+
83+
private void addOrReplace(MutablePropertySources propertySources,
84+
Map<String, Object> map) {
85+
MapPropertySource target = null;
86+
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
87+
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
88+
if (source instanceof MapPropertySource) {
89+
target = (MapPropertySource) source;
90+
for (String key : map.keySet()) {
91+
if (!target.containsProperty(key)) {
92+
target.getSource().put(key, map.get(key));
93+
}
94+
}
95+
}
96+
}
97+
if (target == null) {
98+
target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
99+
}
100+
if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
101+
propertySources.addLast(target);
102+
}
103+
}
104+
105+
}

spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ org.springframework.cloud.sleuth.instrument.mongodb.TraceMongoDbAutoConfiguratio
3030

3131
# Environment Post Processor
3232
org.springframework.boot.env.EnvironmentPostProcessor=\
33-
org.springframework.cloud.sleuth.autoconfig.TraceEnvironmentPostProcessor
33+
org.springframework.cloud.sleuth.autoconfig.TraceEnvironmentPostProcessor,\
34+
org.springframework.cloud.sleuth.instrument.web.client.TraceGatewayEnvironmentPostProcessor

0 commit comments

Comments
 (0)