Resolve service name at runtime when sending a span to Zipkin; fixes #749

This commit is contained in:
Marcin Grzejszczak
2017-10-18 16:18:41 +02:00
parent 70821f4dc3
commit d02c62bb44
9 changed files with 75 additions and 55 deletions

View File

@@ -57,6 +57,7 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar
private Integer port; // Lazy assigned
private Environment environment;
@Deprecated
public ServerPropertiesHostLocator(ServerProperties serverProperties, String appName,
ZipkinProperties zipkinProperties, InetUtils inetUtils) {
this.serverProperties = serverProperties;
@@ -70,6 +71,12 @@ public class ServerPropertiesHostLocator implements HostLocator, EnvironmentAwar
}
}
public ServerPropertiesHostLocator(ServerProperties serverProperties,
Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
this(serverProperties, "", zipkinProperties, inetUtils);
this.environment = environment;
}
@Override
public Host locate(Span span) {
String serviceName = getServiceName(span);
@@ -119,7 +126,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 + "]");

View File

@@ -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);
}

View File

@@ -51,12 +51,14 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator,
private static final String IP_ADDRESS_PROP_NAME = "spring.cloud.client.ipAddress";
private final ServerProperties serverProperties;
// TODO: Remove this in Finchley
private final String appName;
private final InetUtils inetUtils;
private final ZipkinProperties zipkinProperties;
private Integer port;
private Environment environment;
@Deprecated
public ServerPropertiesEndpointLocator(ServerProperties serverProperties,
String appName, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
this.serverProperties = serverProperties;
@@ -69,10 +71,15 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator,
}
}
public ServerPropertiesEndpointLocator(ServerProperties serverProperties,
Environment environment, ZipkinProperties zipkinProperties, InetUtils inetUtils) {
this(serverProperties, "", zipkinProperties, inetUtils);
this.environment = environment;
}
@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 +90,16 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator,
.build();
}
private String serviceName() {
if (StringUtils.hasText(this.zipkinProperties.getService().getName())) {
return this.zipkinProperties.getService().getName();
}
if (this.environment != null) {
return this.environment.getProperty("spring.application.name", "unknown");
}
return "unknown";
}
@EventListener(EmbeddedServletContainerInitializedEvent.class)
public void grabPort(EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();

View File

@@ -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;
@@ -162,12 +161,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 ServerPropertiesEndpointLocator(this.serverProperties, this.appName,
return new ServerPropertiesEndpointLocator(this.serverProperties, this.environment,
this.zipkinProperties, this.inetUtils);
}
@@ -188,8 +187,8 @@ 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 DiscoveryClient client;
@@ -197,7 +196,7 @@ public class ZipkinAutoConfiguration {
@Bean
public EndpointLocator zipkinEndpointLocator() {
return new FallbackHavingEndpointLocator(discoveryClientEndpointLocator(),
new ServerPropertiesEndpointLocator(this.serverProperties, this.appName,
new ServerPropertiesEndpointLocator(this.serverProperties, this.environment,
this.zipkinProperties, this.inetUtils));
}

View File

@@ -1,5 +1,10 @@
package org.springframework.cloud.sleuth.zipkin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
@@ -14,16 +19,12 @@ 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.reporter.Encoding;
import zipkin.Span;
import zipkin.junit.HttpFailure;
import zipkin.junit.ZipkinRule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import zipkin.reporter.Encoding;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
@@ -133,7 +134,7 @@ 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 ServerPropertiesEndpointLocator(new ServerProperties(), new MockEnvironment(),
new ZipkinProperties(), new InetUtils(new InetUtilsProperties())),
null, new ArrayList<>()), new TraceKeys());
// tag::service_name[]
@@ -171,7 +172,7 @@ public class HttpZipkinSpanReporterTest {
Tracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(), new DefaultSpanNamer(),
new NoOpSpanLogger(),new ZipkinSpanListener(httpZipkinSpanReporter,
new ServerPropertiesEndpointLocator(new ServerProperties(), "foo",
new ServerPropertiesEndpointLocator(new ServerProperties(), new MockEnvironment(),
zipkinProperties, new InetUtils(new InetUtilsProperties())),
null, Collections.emptyList()), new TraceKeys());

View File

@@ -24,6 +24,7 @@ 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,7 +35,7 @@ public class ServerPropertiesEndpointLocatorTests {
@Test
public void portDefaultsTo8080() throws UnknownHostException {
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
new ServerProperties(), "unknown", new ZipkinProperties(),
new ServerProperties(), new MockEnvironment(), new ZipkinProperties(),
localAddress(ADDRESS1234));
assertThat(locator.local().port).isEqualTo((short) 8080);
@@ -46,7 +47,7 @@ public class ServerPropertiesEndpointLocatorTests {
properties.setPort(1234);
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
properties, new MockEnvironment(), new ZipkinProperties(),localAddress(ADDRESS1234));
assertThat(locator.local().port).isEqualTo((short) 1234);
}
@@ -65,7 +66,7 @@ public class ServerPropertiesEndpointLocatorTests {
properties.setAddress(InetAddress.getByAddress(ADDRESS1234));
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
properties, "unknown", new ZipkinProperties(),
properties, new MockEnvironment(), new ZipkinProperties(),
localAddress(new byte[] { 4, 4, 4, 4 }));
assertThat(locator.local().ipv4).isEqualTo(1 << 24 | 2 << 16 | 3 << 8 | 4);
@@ -78,7 +79,7 @@ public class ServerPropertiesEndpointLocatorTests {
zipkinProperties.getService().setName("foo");
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
properties, "unknown", zipkinProperties,localAddress(ADDRESS1234));
properties, new MockEnvironment(), zipkinProperties,localAddress(ADDRESS1234));
assertThat(locator.local().serviceName).isEqualTo("foo");
}
@@ -89,7 +90,7 @@ public class ServerPropertiesEndpointLocatorTests {
properties.setPort(-1);
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
properties, new MockEnvironment(), new ZipkinProperties(),localAddress(ADDRESS1234));
assertThat(locator.local().port).isEqualTo((short) 8080);
}

View File

@@ -25,7 +25,6 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerInitial
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.EnvironmentAware;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
@@ -45,24 +44,23 @@ import zipkin2.Endpoint;
* @author Dave Syer
* @since 1.0.0
*/
public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware {
public class DefaultEndpointLocator 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 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());
@@ -93,7 +91,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");
}
@EventListener(EmbeddedServletContainerInitializedEvent.class)
@@ -128,9 +126,4 @@ public class DefaultEndpointLocator implements EndpointLocator, EnvironmentAware
return builder.ip(this.inetUtils.findFirstNonLoopbackAddress());
}
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}