Skip to content

Commit

Permalink
Allow setting an explicit service name (#39)
Browse files Browse the repository at this point in the history
Fixes: #38
  • Loading branch information
geoand authored Mar 11, 2019
1 parent 81eace0 commit f64cbc0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Beware to use the correct syntax for properties that are camel-case in `JaegerCo
If no configuration options are changed and the user does not manually provide any of the beans that the
auto-configuration process provides, the following defaults are used:

* `unknown-spring-boot` Will be used as the service-name if no value has been specified to the property `spring.application.name`.
* `unknown-spring-boot` Will be used as the service-name if no value has been specified to the property `spring.application.name` or `opentracing.jaeger.service-name` (which has the highest priority).
* `CompositeReporter` is provided which contains the following delegates:
- `LoggingReporter` for reporting spans to the console
- `RemoteReporter` that contains a `UdpSender` that sends spans to `localhost:6831`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,14 @@ public class JaegerAutoConfiguration {
@Autowired(required = false)
private List<TracerBuilderCustomizer> tracerCustomizers = Collections.emptyList();

@Value("${spring.application.name:unknown-spring-boot}")
private String serviceName;

@Bean
public io.opentracing.Tracer tracer(Sampler sampler,
Reporter reporter,
Metrics metrics,
JaegerConfigurationProperties properties) {

final JaegerTracer.Builder builder =
new JaegerTracer.Builder(serviceName)
new JaegerTracer.Builder(properties.getServiceName())
.withReporter(reporter)
.withSampler(sampler)
.withTags(properties.determineTags())
Expand Down Expand Up @@ -187,7 +184,7 @@ public Sampler sampler(JaegerConfigurationProperties properties, Metrics metrics
JaegerConfigurationProperties.RemoteControlledSampler samplerProperties
= properties.getRemoteControlledSampler();

return new RemoteControlledSampler.Builder(serviceName)
return new RemoteControlledSampler.Builder(properties.getServiceName())
.withSamplingManager(new HttpSamplingManager(samplerProperties.getHostPort()))
.withInitialSampler(
new ProbabilisticSampler(samplerProperties.getSamplingRate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.jaegertracing.Configuration;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("opentracing.jaeger")
Expand All @@ -28,11 +30,21 @@ public class JaegerConfigurationProperties {
private final ProbabilisticSampler probabilisticSampler = new ProbabilisticSampler();
private final RateLimitingSampler rateLimitingSampler = new RateLimitingSampler();
private final RemoteControlledSampler remoteControlledSampler = new RemoteControlledSampler();


/**
* Enable Jaeger Tracer
*/
private boolean enabled = true;

/**
* Service name to be used
* By default it will be the value of the 'spring.application.name' property name
* and if that is not set, it falls back to 'unknown-spring-boot'
*/
@Value("${spring.application.name:unknown-spring-boot}")
private String serviceName;

/**
* Whether spans should be logged to the console
*/
Expand Down Expand Up @@ -60,6 +72,14 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getServiceName() {
return serviceName;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public boolean isLogSpans() {
return logSpans;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2018-2019 The OpenTracing 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 io.opentracing.contrib.java.spring.jaeger.starter.basic;

import static org.assertj.core.api.Java6Assertions.assertThat;

import io.opentracing.contrib.java.spring.jaeger.starter.AbstractTracerSpringTest;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(
properties = {
"spring.main.banner-mode=off",
"opentracing.jaeger.enabled=true",
"opentracing.jaeger.service-name=foo",
"spring.application.name=bar"
}
)
public class JaegerTracerServiceNameSetExplicitTest extends AbstractTracerSpringTest {

@Test
public void testNameIsAsExpected() {
assertThat(tracer).isNotNull();
assertThat(tracer).isInstanceOf(io.jaegertracing.internal.JaegerTracer.class);

// 'opentracing.jaeger.service-name' should take priority over 'spring.application.name'
assertThat((getTracer()).getServiceName()).isEqualTo("foo");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2018-2019 The OpenTracing 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 io.opentracing.contrib.java.spring.jaeger.starter.basic;

import static org.assertj.core.api.Java6Assertions.assertThat;

import io.opentracing.contrib.java.spring.jaeger.starter.AbstractTracerSpringTest;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(
properties = {
"spring.main.banner-mode=off",
"opentracing.jaeger.enabled=true",
"spring.application.name=foo",
"app.env=stage",
"opentracing.jaeger.service-name=${spring.application.name}-${app.env}"
}
)
public class JaegerTracerServiceNameSetExplicitWithPropsTest extends AbstractTracerSpringTest {

@Test
public void testNameIsAsExpected() {
assertThat(tracer).isNotNull();
assertThat(tracer).isInstanceOf(io.jaegertracing.internal.JaegerTracer.class);

assertThat((getTracer()).getServiceName()).isEqualTo("foo-stage");
}
}

0 comments on commit f64cbc0

Please sign in to comment.