Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Enable process level tags to be associated with the tracer #143

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions jaeger-core/src/main/java/com/uber/jaeger/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private Tracer(
Sampler sampler,
PropagationRegistry registry,
Clock clock,
Metrics metrics) {
Metrics metrics,
Map<String, Object> tags) {
this.serviceName = serviceName;
this.reporter = reporter;
this.sampler = sampler;
Expand All @@ -89,7 +90,6 @@ private Tracer(

this.version = loadVersion();

Map<String, Object> tags = new HashMap<String, Object>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to create a new map and add all tags from the parameter tags, rather than taking the ownership of the map created outside of the constructor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, never mind, the map is owned by the builder, so it's fine

tags.put("jaeger.version", this.version);
String hostname = getHostName();
if (hostname != null) {
Expand Down Expand Up @@ -326,6 +326,7 @@ public static final class Builder {
private Metrics metrics;
private String serviceName;
private Clock clock = new SystemClock();
private Map<String, Object> tags = new HashMap<String, Object>();

public Builder(String serviceName, Reporter reporter, Sampler sampler) {
if (serviceName == null || serviceName.trim().length() == 0) {
Expand Down Expand Up @@ -370,8 +371,23 @@ Builder withMetrics(Metrics metrics) {
return this;
}

Builder withTag(String key, String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be public. I will change it in #142 in order to test it when corverting to thrift model.

tags.put(key, value);
return this;
}

Builder withTag(String key, boolean value) {
tags.put(key, value);
return this;
}

Builder withTag(String key, Number value) {
tags.put(key, value);
return this;
}

public Tracer build() {
return new Tracer(this.serviceName, reporter, sampler, registry, clock, metrics);
return new Tracer(this.serviceName, reporter, sampler, registry, clock, metrics, tags);
}
}

Expand Down
15 changes: 14 additions & 1 deletion jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,27 @@ public static Collection<Object[]> data() {
Map<String, Object> rootTags = new HashMap<>();
rootTags.put("jaeger.version", tracer.getVersion());
rootTags.put("jaeger.hostname", hostname);
rootTags.put("tracer.tag.str", "y");
rootTags.put("tracer.tag.bool", true);
rootTags.put("tracer.tag.num", 1);
rootTags.put("sampler.type", "const");
rootTags.put("sampler.param", true);

Map<String, Object> childTags = new HashMap<>();
childTags.put("jaeger.version", SENTINEL);
childTags.put("jaeger.hostname", SENTINEL);
childTags.put("tracer.tag.str", SENTINEL);
childTags.put("tracer.tag.bool", SENTINEL);
childTags.put("tracer.tag.num", SENTINEL);
childTags.put("sampler.type", SENTINEL);
childTags.put("sampler.param", SENTINEL);

Map<String, Object> rpcTags = new HashMap<>();
rpcTags.put("jaeger.version", tracer.getVersion());
rpcTags.put("jaeger.hostname", hostname);
rpcTags.put("tracer.tag.str", "y");
rpcTags.put("tracer.tag.bool", true);
rpcTags.put("tracer.tag.num", 1);
rpcTags.put("sampler.type", SENTINEL);
rpcTags.put("sampler.param", SENTINEL);

Expand All @@ -94,7 +103,11 @@ public void setUp() throws Exception {}
@Test
public void testTracerTags() throws Exception {
InMemoryReporter spanReporter = new InMemoryReporter();
Tracer tracer = new Tracer.Builder("x", spanReporter, new ConstSampler(true)).build();
Tracer tracer = new Tracer.Builder("x", spanReporter, new ConstSampler(true))
.withTag("tracer.tag.str", "y")
.withTag("tracer.tag.bool", true)
.withTag("tracer.tag.num", 1)
.build();

Span span = (Span) tracer.buildSpan("root").start();
if (spanType == SpanType.CHILD) {
Expand Down