Merge branch 'master' into 2.0.x
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.stream;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
@@ -26,11 +28,8 @@ import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
|
||||
/**
|
||||
* A {@link HostLocator} that retrieves:
|
||||
*
|
||||
@@ -51,17 +50,15 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar
|
||||
private static final String IP_ADDRESS_PROP_NAME = "spring.cloud.client.ipAddress";
|
||||
|
||||
private final ServerProperties serverProperties; // Nullable
|
||||
private final String appName;
|
||||
private final InetUtils inetUtils;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
private Integer port; // Lazy assigned
|
||||
private Environment environment;
|
||||
|
||||
public ServerPropertiesHostLocator(ServerProperties serverProperties, String appName,
|
||||
ZipkinProperties zipkinProperties, InetUtils inetUtils) {
|
||||
public ServerPropertiesHostLocator(ServerProperties serverProperties,
|
||||
Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
|
||||
this.serverProperties = serverProperties;
|
||||
this.appName = appName;
|
||||
Assert.notNull(this.appName, "appName");
|
||||
this.environment = environment;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
if (inetUtils == null) {
|
||||
this.inetUtils = new InetUtils(new InetUtilsProperties());
|
||||
@@ -102,7 +99,8 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar
|
||||
if (this.serverProperties != null && this.serverProperties.getAddress() != null) {
|
||||
address = this.serverProperties.getAddress().getHostAddress();
|
||||
}
|
||||
else if (this.environment != null) {
|
||||
else if (this.environment != null &&
|
||||
StringUtils.hasText(this.environment.getProperty(IP_ADDRESS_PROP_NAME, String.class))) {
|
||||
address = this.environment.getProperty(IP_ADDRESS_PROP_NAME, String.class);
|
||||
}
|
||||
else {
|
||||
@@ -119,7 +117,7 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar
|
||||
serviceName = span.getProcessId();
|
||||
}
|
||||
else {
|
||||
serviceName = this.appName;
|
||||
serviceName = this.environment.getProperty("spring.application.name", "unknown");
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Span will contain serviceName [" + serviceName + "]");
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -114,12 +113,12 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Autowired
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Bean
|
||||
public HostLocator zipkinEndpointLocator() {
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName, this.zipkinProperties,
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.environment, this.zipkinProperties,
|
||||
this.inetUtils);
|
||||
}
|
||||
|
||||
@@ -140,8 +139,8 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DiscoveryClient client;
|
||||
@@ -151,7 +150,7 @@ public class SleuthStreamAutoConfiguration {
|
||||
if (this.client != null) {
|
||||
return new DiscoveryClientHostLocator(this.client, this.zipkinProperties);
|
||||
}
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName, this.zipkinProperties,
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.environment, this.zipkinProperties,
|
||||
this.inetUtils);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,15 +16,16 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.stream;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -37,7 +38,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
@Test
|
||||
public void portDefaultsTo8080() throws UnknownHostException {
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(),
|
||||
new ServerProperties(), new MockEnvironment(), new ZipkinProperties(),
|
||||
localAddress(ADR1234));
|
||||
|
||||
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 8080);
|
||||
@@ -49,7 +50,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
properties.setPort(1234);
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown", new ZipkinProperties(),localAddress(ADR1234));
|
||||
new MockEnvironment(), new ZipkinProperties(),localAddress(ADR1234));
|
||||
|
||||
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 1234);
|
||||
}
|
||||
@@ -57,7 +58,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
@Test
|
||||
public void portDefaultsToLocalhost() throws UnknownHostException {
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(),
|
||||
new ServerProperties(), new MockEnvironment(), new ZipkinProperties(),
|
||||
localAddress(ADR1234));
|
||||
|
||||
assertThat(locator.locate(this.span).getAddress()).isEqualTo("1.2.3.4");
|
||||
@@ -69,7 +70,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
properties.setAddress(InetAddress.getByAddress(ADR1234));
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown", new ZipkinProperties(),localAddress(new byte[] { 1, 1, 1, 1 }));
|
||||
new MockEnvironment(), new ZipkinProperties(),localAddress(new byte[] { 1, 1, 1, 1 }));
|
||||
|
||||
assertThat(locator.locate(this.span).getAddress()).isEqualTo("1.2.3.4");
|
||||
}
|
||||
@@ -82,7 +83,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
zipkinProperties.getService().setName("foo");
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown", zipkinProperties,localAddress(ADR1234));
|
||||
new MockEnvironment(), zipkinProperties,localAddress(ADR1234));
|
||||
|
||||
assertThat(locator.locate(this.span).getServiceName()).isEqualTo("foo");
|
||||
}
|
||||
@@ -93,7 +94,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
properties.setPort(-1);
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown", new ZipkinProperties(),localAddress(ADR1234));
|
||||
new MockEnvironment(), new ZipkinProperties(),localAddress(ADR1234));
|
||||
|
||||
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 8080);
|
||||
}
|
||||
|
||||
@@ -16,21 +16,19 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.zipkin;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
import zipkin.Endpoint;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* {@link EndpointLocator} implementation that:
|
||||
*
|
||||
@@ -44,35 +42,26 @@ import java.nio.ByteBuffer;
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ServerPropertiesEndpointLocator implements EndpointLocator,
|
||||
EnvironmentAware {
|
||||
public class ServerPropertiesEndpointLocator implements EndpointLocator {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
|
||||
private static final String IP_ADDRESS_PROP_NAME = "spring.cloud.client.ipAddress";
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
private final String appName;
|
||||
private final InetUtils inetUtils;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
private final Environment environment;
|
||||
private Integer port;
|
||||
private Environment environment;
|
||||
|
||||
public ServerPropertiesEndpointLocator(ServerProperties serverProperties,
|
||||
String appName, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
|
||||
Environment environment, ZipkinProperties zipkinProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
this.appName = appName;
|
||||
this.environment = environment;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
if (inetUtils == null) {
|
||||
this.inetUtils = new InetUtils(new InetUtilsProperties());
|
||||
} else {
|
||||
this.inetUtils = inetUtils;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Endpoint local() {
|
||||
String serviceName = StringUtils.hasText(this.zipkinProperties.getService().getName()) ?
|
||||
this.zipkinProperties.getService().getName() : this.appName;
|
||||
String serviceName = serviceName();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Span will contain serviceName [" + serviceName + "]");
|
||||
}
|
||||
@@ -83,6 +72,13 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator,
|
||||
.build();
|
||||
}
|
||||
|
||||
private String serviceName() {
|
||||
if (StringUtils.hasText(this.zipkinProperties.getService().getName())) {
|
||||
return this.zipkinProperties.getService().getName();
|
||||
}
|
||||
return this.environment.getProperty("spring.application.name", "unknown");
|
||||
}
|
||||
|
||||
@EventListener(ServletWebServerInitializedEvent.class)
|
||||
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
|
||||
this.port = event.getSource().getPort();
|
||||
@@ -107,18 +103,8 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator,
|
||||
return ByteBuffer.wrap(this.serverProperties.getAddress().getAddress())
|
||||
.getInt();
|
||||
}
|
||||
else if (this.environment != null) {
|
||||
String ipAddress = this.environment
|
||||
.getProperty(IP_ADDRESS_PROP_NAME, String.class);
|
||||
return InetUtils.getIpAddressAsInt(ipAddress);
|
||||
}
|
||||
else {
|
||||
return ByteBuffer.wrap(this.inetUtils.findFirstNonLoopbackAddress().getAddress()).getInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
String ipAddress = this.environment
|
||||
.getProperty(IP_ADDRESS_PROP_NAME, String.class);
|
||||
return InetUtils.getIpAddressAsInt(ipAddress);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.List;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -34,7 +33,6 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
import org.springframework.cloud.sleuth.SpanAdjuster;
|
||||
import org.springframework.cloud.sleuth.SpanReporter;
|
||||
@@ -159,16 +157,13 @@ public class ZipkinAutoConfiguration {
|
||||
@Autowired
|
||||
private ZipkinProperties zipkinProperties;
|
||||
|
||||
@Autowired(required=false)
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
return new ServerPropertiesEndpointLocator(this.serverProperties, this.appName,
|
||||
this.zipkinProperties, this.inetUtils);
|
||||
return new ServerPropertiesEndpointLocator(this.serverProperties, this.environment,
|
||||
this.zipkinProperties);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -185,11 +180,8 @@ public class ZipkinAutoConfiguration {
|
||||
@Autowired
|
||||
private ZipkinProperties zipkinProperties;
|
||||
|
||||
@Autowired(required=false)
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired(required=false)
|
||||
private DiscoveryClient client;
|
||||
@@ -197,8 +189,8 @@ public class ZipkinAutoConfiguration {
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
return new FallbackHavingEndpointLocator(discoveryClientEndpointLocator(),
|
||||
new ServerPropertiesEndpointLocator(this.serverProperties, this.appName,
|
||||
this.zipkinProperties, this.inetUtils));
|
||||
new ServerPropertiesEndpointLocator(this.serverProperties, this.environment,
|
||||
this.zipkinProperties));
|
||||
}
|
||||
|
||||
private DiscoveryClientEndpointLocator discoveryClientEndpointLocator() {
|
||||
|
||||
@@ -11,8 +11,6 @@ import io.micrometer.core.instrument.simple.SimpleCounter;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.cloud.sleuth.DefaultSpanNamer;
|
||||
import org.springframework.cloud.sleuth.TraceKeys;
|
||||
import org.springframework.cloud.sleuth.Tracer;
|
||||
@@ -22,6 +20,7 @@ import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.cloud.sleuth.trace.DefaultTracer;
|
||||
import org.springframework.cloud.sleuth.util.ExceptionUtils;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import zipkin.Span;
|
||||
import zipkin.junit.HttpFailure;
|
||||
@@ -140,8 +139,8 @@ public class HttpZipkinSpanReporterTest {
|
||||
AtomicReference<Span> receivedSpan = new AtomicReference<>();
|
||||
Tracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(), new DefaultSpanNamer(),
|
||||
new NoOpSpanLogger(), new ZipkinSpanListener(receivedSpan::set,
|
||||
new ServerPropertiesEndpointLocator(new ServerProperties(), "foo",
|
||||
new ZipkinProperties(), new InetUtils(new InetUtilsProperties())),
|
||||
new ServerPropertiesEndpointLocator(new ServerProperties(), new MockEnvironment(),
|
||||
new ZipkinProperties()),
|
||||
null, new ArrayList<>()), new TraceKeys());
|
||||
// tag::service_name[]
|
||||
org.springframework.cloud.sleuth.Span newSpan = tracer.createSpan("redis");
|
||||
@@ -178,8 +177,8 @@ public class HttpZipkinSpanReporterTest {
|
||||
|
||||
Tracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(), new DefaultSpanNamer(),
|
||||
new NoOpSpanLogger(),new ZipkinSpanListener(httpZipkinSpanReporter,
|
||||
new ServerPropertiesEndpointLocator(new ServerProperties(), "foo",
|
||||
zipkinProperties, new InetUtils(new InetUtilsProperties())),
|
||||
new ServerPropertiesEndpointLocator(new ServerProperties(), new MockEnvironment(),
|
||||
zipkinProperties),
|
||||
null, Collections.emptyList()), new TraceKeys());
|
||||
|
||||
tracer.close(tracer.createSpan("foo"));
|
||||
|
||||
@@ -20,10 +20,8 @@ import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -34,8 +32,7 @@ public class ServerPropertiesEndpointLocatorTests {
|
||||
@Test
|
||||
public void portDefaultsTo8080() throws UnknownHostException {
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(),
|
||||
localAddress(ADDRESS1234));
|
||||
new ServerProperties(), new MockEnvironment(), new ZipkinProperties());
|
||||
|
||||
assertThat(locator.local().port).isEqualTo((short) 8080);
|
||||
}
|
||||
@@ -46,15 +43,17 @@ public class ServerPropertiesEndpointLocatorTests {
|
||||
properties.setPort(1234);
|
||||
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
properties, new MockEnvironment(), new ZipkinProperties());
|
||||
|
||||
assertThat(locator.local().port).isEqualTo((short) 1234);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void portDefaultsToLocalhost() throws UnknownHostException {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
environment.setProperty("spring.cloud.client.ipAddress", String.valueOf(1 << 24 | 2 << 16 | 3 << 8 | 4));
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(), localAddress(ADDRESS1234));
|
||||
new ServerProperties(), environment, new ZipkinProperties());
|
||||
|
||||
assertThat(locator.local().ipv4).isEqualTo(1 << 24 | 2 << 16 | 3 << 8 | 4);
|
||||
}
|
||||
@@ -65,8 +64,7 @@ public class ServerPropertiesEndpointLocatorTests {
|
||||
properties.setAddress(InetAddress.getByAddress(ADDRESS1234));
|
||||
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
properties, "unknown", new ZipkinProperties(),
|
||||
localAddress(new byte[] { 4, 4, 4, 4 }));
|
||||
properties, new MockEnvironment(), new ZipkinProperties());
|
||||
|
||||
assertThat(locator.local().ipv4).isEqualTo(1 << 24 | 2 << 16 | 3 << 8 | 4);
|
||||
}
|
||||
@@ -78,7 +76,7 @@ public class ServerPropertiesEndpointLocatorTests {
|
||||
zipkinProperties.getService().setName("foo");
|
||||
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
properties, "unknown", zipkinProperties,localAddress(ADDRESS1234));
|
||||
properties, new MockEnvironment(), zipkinProperties);
|
||||
|
||||
assertThat(locator.local().serviceName).isEqualTo("foo");
|
||||
}
|
||||
@@ -89,15 +87,8 @@ public class ServerPropertiesEndpointLocatorTests {
|
||||
properties.setPort(-1);
|
||||
|
||||
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
|
||||
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
properties, new MockEnvironment(), new ZipkinProperties());
|
||||
|
||||
assertThat(locator.local().port).isEqualTo((short) 8080);
|
||||
}
|
||||
|
||||
private InetUtils localAddress(byte[] address) throws UnknownHostException {
|
||||
InetUtils mocked = Mockito.spy(new InetUtils(new InetUtilsProperties()));
|
||||
Mockito.when(mocked.findFirstNonLoopbackAddress())
|
||||
.thenReturn(InetAddress.getByAddress(address));
|
||||
return mocked;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
import zipkin2.Endpoint;
|
||||
@@ -45,7 +44,7 @@ import zipkin2.Endpoint;
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware,
|
||||
public class DefaultEndpointLocator implements EndpointLocator,
|
||||
ApplicationListener<ServletWebServerInitializedEvent> {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
|
||||
@@ -53,17 +52,16 @@ public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware
|
||||
|
||||
private final Registration registration;
|
||||
private final ServerProperties serverProperties;
|
||||
private final String appName;
|
||||
private final Environment environment;
|
||||
private final InetUtils inetUtils;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
private Integer port;
|
||||
private Environment environment;
|
||||
|
||||
public DefaultEndpointLocator(Registration registration, ServerProperties serverProperties,
|
||||
String appName, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
|
||||
Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
|
||||
this.registration = registration;
|
||||
this.serverProperties = serverProperties;
|
||||
this.appName = appName;
|
||||
this.environment = environment;
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
if (inetUtils == null) {
|
||||
this.inetUtils = new InetUtils(new InetUtilsProperties());
|
||||
@@ -94,7 +92,7 @@ public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware
|
||||
log.warn("error getting service name from registration", e);
|
||||
}
|
||||
}
|
||||
return this.appName;
|
||||
return this.environment.getProperty("spring.application.name", "unknown");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,7 +119,7 @@ public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware
|
||||
&& builder.parseIp(this.serverProperties.getAddress())) {
|
||||
return builder;
|
||||
}
|
||||
else if (this.environment != null && this.environment.containsProperty(IP_ADDRESS_PROP_NAME)
|
||||
else if (this.environment.containsProperty(IP_ADDRESS_PROP_NAME)
|
||||
&& builder.parseIp(this.environment.getProperty(IP_ADDRESS_PROP_NAME, String.class))) {
|
||||
return builder;
|
||||
}
|
||||
@@ -129,9 +127,4 @@ public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware
|
||||
return builder.ip(this.inetUtils.findFirstNonLoopbackAddress());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -122,12 +121,12 @@ public class ZipkinAutoConfiguration {
|
||||
@Autowired(required=false)
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
return new DefaultEndpointLocator(null, this.serverProperties, this.appName,
|
||||
return new DefaultEndpointLocator(null, this.serverProperties, this.environment,
|
||||
this.zipkinProperties, this.inetUtils);
|
||||
}
|
||||
|
||||
@@ -148,15 +147,15 @@ public class ZipkinAutoConfiguration {
|
||||
@Autowired(required=false)
|
||||
private InetUtils inetUtils;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired(required=false)
|
||||
private Registration registration;
|
||||
|
||||
@Bean
|
||||
public EndpointLocator zipkinEndpointLocator() {
|
||||
return new DefaultEndpointLocator(this.registration, this.serverProperties, this.appName,
|
||||
return new DefaultEndpointLocator(this.registration, this.serverProperties, this.environment,
|
||||
this.zipkinProperties, this.inetUtils);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.springframework.cloud.sleuth.zipkin2;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -13,6 +14,8 @@ import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -103,11 +106,12 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
}
|
||||
}
|
||||
public static final byte[] ADDRESS1234 = { 1, 2, 3, 4 };
|
||||
Environment environment = new MockEnvironment();
|
||||
|
||||
@Test
|
||||
public void portDefaultsTo8080() throws UnknownHostException {
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(),
|
||||
new ServerProperties(), environment, new ZipkinProperties(),
|
||||
localAddress(ADDRESS1234));
|
||||
|
||||
assertThat(locator.local().port()).isEqualTo(8080);
|
||||
@@ -119,7 +123,7 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
properties.setPort(1234);
|
||||
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
properties, environment, new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
|
||||
assertThat(locator.local().port()).isEqualTo(1234);
|
||||
}
|
||||
@@ -127,7 +131,7 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
@Test
|
||||
public void portDefaultsToLocalhost() throws UnknownHostException {
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
new ServerProperties(), "unknown", new ZipkinProperties(), localAddress(ADDRESS1234));
|
||||
new ServerProperties(), environment, new ZipkinProperties(), localAddress(ADDRESS1234));
|
||||
|
||||
assertThat(locator.local().ipv4()).isEqualTo("1.2.3.4");
|
||||
}
|
||||
@@ -138,7 +142,7 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
properties.setAddress(InetAddress.getByAddress(ADDRESS1234));
|
||||
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
properties, "unknown", new ZipkinProperties(),
|
||||
properties, environment, new ZipkinProperties(),
|
||||
localAddress(new byte[] { 4, 4, 4, 4 }));
|
||||
|
||||
assertThat(locator.local().ipv4()).isEqualTo("1.2.3.4");
|
||||
@@ -151,7 +155,7 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
zipkinProperties.getService().setName("foo");
|
||||
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
properties, "unknown", zipkinProperties,localAddress(ADDRESS1234));
|
||||
properties, environment, zipkinProperties,localAddress(ADDRESS1234));
|
||||
|
||||
assertThat(locator.local().serviceName()).isEqualTo("foo");
|
||||
}
|
||||
@@ -162,7 +166,7 @@ public class DefaultEndpointLocatorConfigurationTest {
|
||||
properties.setPort(-1);
|
||||
|
||||
DefaultEndpointLocator locator = new DefaultEndpointLocator(null,
|
||||
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
properties, environment, new ZipkinProperties(),localAddress(ADDRESS1234));
|
||||
|
||||
assertThat(locator.local().port()).isEqualTo(8080);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user