Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Add generic typed setTag/withTag #311

Merged
merged 2 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions opentracing-api/src/main/java/io/opentracing/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.opentracing;

import io.opentracing.tag.AbstractTag;
import java.util.Map;

/**
Expand Down Expand Up @@ -44,6 +45,9 @@ public interface Span {
/** Same as {@link #setTag(String, String)}, but for numeric values. */
Span setTag(String key, Number value);

/** Same as {@link #setTag(String, String)}, but with typed value. */
<T> Span setTag(AbstractTag<T> key, T value);

/**
* Log key:value pairs to the Span with the current walltime timestamp.
*
Expand Down
4 changes: 4 additions & 0 deletions opentracing-api/src/main/java/io/opentracing/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.opentracing;

import io.opentracing.propagation.Format;
import io.opentracing.tag.AbstractTag;

/**
* Tracer is a simple, thin interface for Span creation and propagation across arbitrary transports.
Expand Down Expand Up @@ -162,6 +163,9 @@ interface SpanBuilder {
/** Same as {@link Span#setTag(String, Number)}, but for the span being built. */
SpanBuilder withTag(String key, Number value);

/** Same as {@link AbstractTag#set(Span, T)}, but for the span being built. */
<T> SpanBuilder withTag(AbstractTag<T> tag, T value);

/** Specify a timestamp of when the Span was started, represented in microseconds since epoch. */
SpanBuilder withStartTimestamp(long microseconds);

Expand Down
11 changes: 8 additions & 3 deletions opentracing-mock/src/main/java/io/opentracing/mock/MockSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package io.opentracing.mock;

import io.opentracing.References;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.tag.AbstractTag;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -22,9 +25,6 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import io.opentracing.Span;
import io.opentracing.SpanContext;

/**
* MockSpans are created via MockTracer.buildSpan(...), but they are also returned via calls to
* MockTracer.finishedSpans(). They provide accessors to all Span state.
Expand Down Expand Up @@ -136,6 +136,11 @@ public MockSpan setTag(String key, Number value) {
return setObjectTag(key, value);
}

@Override
public <T> Span setTag(AbstractTag<T> key, T value) {
return setObjectTag(key.getKey(), value);
}

private synchronized MockSpan setObjectTag(String key, Object value) {
finishedCheck("Adding tag {%s:%s} to already finished span", key, value);
tags.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.opentracing.mock;

import io.opentracing.tag.AbstractTag;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -258,6 +259,12 @@ public SpanBuilder withTag(String key, Number value) {
return this;
}

@Override
public <T> Tracer.SpanBuilder withTag(AbstractTag<T> tag, T value) {
this.initialTags.put(tag.getKey(), value);
return this;
}

@Override
public SpanBuilder withStartTimestamp(long microseconds) {
this.startMicros = microseconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.tag.AbstractTag;

import java.util.Map;

Expand Down Expand Up @@ -42,6 +43,11 @@ public void finish(long finishMicros) {}
@Override
public NoopSpan setTag(String key, Number value) { return this; }

@Override
public <T> Span setTag(AbstractTag<T> key, T value) {
return this;
}

@Override
public NoopSpan log(Map<String, ?> fields) { return this; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;

import java.util.Collections;
import java.util.Map;
import io.opentracing.tag.AbstractTag;

public interface NoopSpanBuilder extends Tracer.SpanBuilder {
NoopSpanBuilder INSTANCE = new NoopSpanBuilderImpl();
Expand Down Expand Up @@ -60,6 +58,11 @@ public Tracer.SpanBuilder withTag(String key, Number value) {
return this;
}

@Override
public <T> Tracer.SpanBuilder withTag(AbstractTag<T> key, T value) {
return this;
}

@Override
public Tracer.SpanBuilder withStartTimestamp(long microseconds) {
return this;
Expand Down