Added property for overriding the service name in Zipkin
without this only either service discovery service id / spring.application.name can be chosen as a service name for zipkin
with this change you can pass spring.zipkin.service.name property to change override that both for HTTP and Stream collectors
fixes #324
This commit is contained in:
@@ -22,27 +22,39 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An {@link HostLocator} that tries to find local service information from a
|
||||
* {@link DiscoveryClient}.
|
||||
*
|
||||
* You can override the value of service id by {@link ZipkinProperties#setName(String)}
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DiscoveryClientHostLocator implements HostLocator {
|
||||
|
||||
private DiscoveryClient client;
|
||||
private final DiscoveryClient client;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
|
||||
@Deprecated
|
||||
public DiscoveryClientHostLocator(DiscoveryClient client) {
|
||||
this(client, new ZipkinProperties());
|
||||
}
|
||||
|
||||
public DiscoveryClientHostLocator(DiscoveryClient client, ZipkinProperties zipkinProperties) {
|
||||
this.client = client;
|
||||
Assert.notNull(this.client, "client");
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Host locate(Span span) {
|
||||
ServiceInstance instance = this.client.getLocalServiceInstance();
|
||||
return new Host(instance.getServiceId(), getIpAddress(instance),
|
||||
String serviceId = StringUtils.hasText(this.zipkinProperties.getName()) ?
|
||||
this.zipkinProperties.getName() : instance.getServiceId();
|
||||
return new Host(serviceId, getIpAddress(instance),
|
||||
instance.getPort());
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerInitial
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link HostLocator} that retrieves:
|
||||
@@ -31,6 +32,8 @@ import org.springframework.util.Assert;
|
||||
* <li><b>port</b> - from lazily assigned port or {@link ServerProperties}</li>
|
||||
* </ul>
|
||||
*
|
||||
* You can override the value of service id by {@link ZipkinProperties#setName(String)}
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@@ -38,13 +41,15 @@ public class ServerPropertiesHostLocator implements HostLocator {
|
||||
|
||||
private final ServerProperties serverProperties; // Nullable
|
||||
private final String appName;
|
||||
private final ZipkinProperties zipkinProperties;
|
||||
private Integer port; // Lazy assigned
|
||||
|
||||
public ServerPropertiesHostLocator(ServerProperties serverProperties,
|
||||
String appName) {
|
||||
String appName, ZipkinProperties zipkinProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
this.appName = appName;
|
||||
Assert.notNull(this.appName, "appName");
|
||||
this.zipkinProperties = zipkinProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,7 +92,9 @@ public class ServerPropertiesHostLocator implements HostLocator {
|
||||
|
||||
private String getServiceName(Span span) {
|
||||
String serviceName;
|
||||
if (span.getProcessId() != null) {
|
||||
if (StringUtils.hasText(this.zipkinProperties.getName())) {
|
||||
serviceName = this.zipkinProperties.getName();
|
||||
} else if (span.getProcessId() != null) {
|
||||
serviceName = span.getProcessId();
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -54,7 +54,7 @@ import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({ SleuthStreamProperties.class, SamplerProperties.class })
|
||||
@EnableConfigurationProperties({ SleuthStreamProperties.class, SamplerProperties.class, ZipkinProperties.class })
|
||||
@AutoConfigureAfter(TraceMetricsAutoConfiguration.class)
|
||||
@AutoConfigureBefore(ChannelBindingAutoConfiguration.class)
|
||||
@EnableBinding(SleuthSource.class)
|
||||
@@ -96,12 +96,15 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Autowired
|
||||
private ZipkinProperties zipkinProperties;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
|
||||
@Bean
|
||||
public HostLocator zipkinEndpointLocator() {
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName);
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName, this.zipkinProperties);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -113,6 +116,9 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Autowired
|
||||
private ZipkinProperties zipkinProperties;
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appName;
|
||||
|
||||
@@ -122,9 +128,9 @@ public class SleuthStreamAutoConfiguration {
|
||||
@Bean
|
||||
public HostLocator zipkinEndpointLocator() {
|
||||
if (this.client != null) {
|
||||
return new DiscoveryClientHostLocator(this.client);
|
||||
return new DiscoveryClientHostLocator(this.client, this.zipkinProperties);
|
||||
}
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName);
|
||||
return new ServerPropertiesHostLocator(this.serverProperties, this.appName, this.zipkinProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2013-2016 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
|
||||
*
|
||||
* http://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.cloud.sleuth.stream;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Zipkin settings for Zipkin Stream client
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.12
|
||||
*/
|
||||
@ConfigurationProperties("spring.zipkin.service")
|
||||
public class ZipkinProperties {
|
||||
|
||||
/** The name of the service, from which the Span was sent via Stream, that should appear in Zipkin */
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -34,11 +34,11 @@ import static org.mockito.BDDMockito.given;
|
||||
public class DiscoveryClientHostLocatorTest {
|
||||
DiscoveryClient discoveryClient = Mockito.mock(DiscoveryClient.class);
|
||||
DiscoveryClientHostLocator discoveryClientHostLocator =
|
||||
new DiscoveryClientHostLocator(this.discoveryClient);
|
||||
new DiscoveryClientHostLocator(this.discoveryClient, new ZipkinProperties());
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void should_throw_exception_when_no_discovery_client_is_present() throws Exception {
|
||||
new DiscoveryClientHostLocator(null);
|
||||
new DiscoveryClientHostLocator(null, new ZipkinProperties());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,6 +63,20 @@ public class DiscoveryClientHostLocatorTest {
|
||||
then(host.getIpv4()).isEqualTo(InetUtils.getIpAddressAsInt("localhost"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_override_the_service_name_from_properties() throws Exception {
|
||||
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
|
||||
ZipkinProperties zipkinProperties = new ZipkinProperties();
|
||||
zipkinProperties.setName("foo");
|
||||
this.discoveryClientHostLocator = new DiscoveryClientHostLocator(this.discoveryClient, zipkinProperties);
|
||||
|
||||
Host host = this.discoveryClientHostLocator.locate(null);
|
||||
|
||||
then(host.getServiceName()).isEqualTo("foo");
|
||||
then(host.getPort()).isEqualTo((short)8_000);
|
||||
then(host.getIpv4()).isEqualTo(InetUtils.getIpAddressAsInt("localhost"));
|
||||
}
|
||||
|
||||
private ServiceInstance serviceInstanceWithInvalidHost() {
|
||||
return new ServiceInstance() {
|
||||
@Override public String getServiceId() {
|
||||
|
||||
@@ -33,7 +33,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
@Test
|
||||
public void portDefaultsTo8080() {
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(
|
||||
new ServerProperties(), "unknown");
|
||||
new ServerProperties(), "unknown", new ZipkinProperties());
|
||||
|
||||
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 8080);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
properties.setPort(1234);
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown");
|
||||
"unknown", new ZipkinProperties());
|
||||
|
||||
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 1234);
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class ServerPropertiesHostLocatorTests {
|
||||
@Test
|
||||
public void portDefaultsToLocalhost() {
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(
|
||||
new ServerProperties(), "unknown");
|
||||
new ServerProperties(), "unknown", new ZipkinProperties());
|
||||
|
||||
assertThat(locator.locate(this.span).getAddress()).isEqualTo("127.0.0.1");
|
||||
}
|
||||
@@ -63,8 +63,21 @@ public class ServerPropertiesHostLocatorTests {
|
||||
properties.setAddress(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }));
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown");
|
||||
"unknown", new ZipkinProperties());
|
||||
|
||||
assertThat(locator.locate(this.span).getAddress()).isEqualTo("1.2.3.4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameTakenFromProperties() throws UnknownHostException {
|
||||
ServerProperties properties = new ServerProperties();
|
||||
properties.setAddress(InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }));
|
||||
ZipkinProperties zipkinProperties = new ZipkinProperties();
|
||||
zipkinProperties.setName("foo");
|
||||
|
||||
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
|
||||
"unknown", zipkinProperties);
|
||||
|
||||
assertThat(locator.locate(this.span).getServiceName()).isEqualTo("foo");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user