Skip to content

Commit 644bb0f

Browse files
author
Adrian Cole
committed
Revert "Adds CollectedSpanHandler"
This reverts commit 3e9c457.
1 parent 3e9c457 commit 644bb0f

File tree

10 files changed

+89
-327
lines changed

10 files changed

+89
-327
lines changed

zipkin-collector/activemq/src/main/java/zipkin2/collector/activemq/ActiveMQCollector.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import zipkin2.collector.CollectorComponent;
2323
import zipkin2.collector.CollectorMetrics;
2424
import zipkin2.collector.CollectorSampler;
25-
import zipkin2.collector.handler.CollectedSpanHandler;
2625
import zipkin2.storage.StorageComponent;
2726

2827
/** This collector consumes encoded binary messages from a ActiveMQ queue. */
@@ -39,18 +38,13 @@ public static final class Builder extends CollectorComponent.Builder {
3938
String queue = "zipkin";
4039
int concurrency = 1;
4140

42-
@Override public Builder sampler(CollectorSampler sampler) {
43-
this.delegate.sampler(sampler);
44-
return this;
45-
}
46-
47-
@Override public Builder addCollectedSpanHandler(CollectedSpanHandler collectedSpanHandler) {
48-
this.delegate.addCollectedSpanHandler(collectedSpanHandler);
41+
@Override public Builder storage(StorageComponent storage) {
42+
this.delegate.storage(storage);
4943
return this;
5044
}
5145

52-
@Override public Builder storage(StorageComponent storage) {
53-
this.delegate.storage(storage);
46+
@Override public Builder sampler(CollectorSampler sampler) {
47+
this.delegate.sampler(sampler);
5448
return this;
5549
}
5650

zipkin-collector/core/src/main/java/zipkin2/collector/Collector.java

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515

1616
import java.nio.ByteBuffer;
1717
import java.util.ArrayList;
18-
import java.util.Arrays;
1918
import java.util.Iterator;
2019
import java.util.List;
2120
import java.util.concurrent.Executor;
2221
import java.util.function.Supplier;
2322
import java.util.logging.Logger;
24-
import zipkin2.Call;
2523
import zipkin2.Callback;
2624
import zipkin2.Span;
2725
import zipkin2.SpanBytesDecoderDetector;
2826
import zipkin2.codec.BytesDecoder;
2927
import zipkin2.codec.SpanBytesDecoder;
30-
import zipkin2.collector.handler.CollectedSpanHandler;
3128
import zipkin2.storage.StorageComponent;
3229

3330
import static java.lang.String.format;
@@ -36,13 +33,21 @@
3633

3734
/**
3835
* This component takes action on spans received from a transport. This includes deserializing,
39-
* sampling, invoking any handlers, and scheduling for storage.
36+
* sampling and scheduling for storage.
4037
*
4138
* <p>Callbacks passed do not propagate to the storage layer. They only return success or failures
4239
* before storage is attempted. This ensures that calling threads are disconnected from storage
4340
* threads.
4441
*/
4542
public class Collector { // not final for mock
43+
static final Callback<Void> NOOP_CALLBACK = new Callback<Void>() {
44+
@Override public void onSuccess(Void value) {
45+
}
46+
47+
@Override public void onError(Throwable t) {
48+
}
49+
};
50+
4651
/** Needed to scope this to the correct logging category */
4752
public static Builder newBuilder(Class<?> loggingClass) {
4853
if (loggingClass == null) throw new NullPointerException("loggingClass == null");
@@ -51,35 +56,14 @@ public static Builder newBuilder(Class<?> loggingClass) {
5156

5257
public static final class Builder {
5358
final Logger logger;
54-
final ArrayList<CollectedSpanHandler> collectedSpanHandlers = new ArrayList<>();
5559
StorageComponent storage;
60+
CollectorSampler sampler;
5661
CollectorMetrics metrics;
5762

5863
Builder(Logger logger) {
5964
this.logger = logger;
6065
}
6166

62-
/** @see {@link CollectorComponent.Builder#sampler(CollectorSampler)} */
63-
public Builder sampler(CollectorSampler sampler) {
64-
if (sampler == null) throw new NullPointerException("sampler == null");
65-
this.collectedSpanHandlers.add(0, sampler); // sample first
66-
return this;
67-
}
68-
69-
/**
70-
* @see {@link CollectorComponent.Builder#addCollectedSpanHandler(CollectedSpanHandler)}
71-
* @since 2.17
72-
*/
73-
public Builder addCollectedSpanHandler(CollectedSpanHandler collectedSpanHandler) {
74-
if (collectedSpanHandler == null) {
75-
throw new NullPointerException("collectedSpanHandler == null");
76-
}
77-
if (collectedSpanHandler != CollectedSpanHandler.NOOP) { // lenient on config bug
78-
this.collectedSpanHandlers.add(collectedSpanHandler);
79-
}
80-
return this;
81-
}
82-
8367
/** @see {@link CollectorComponent.Builder#storage(StorageComponent)} */
8468
public Builder storage(StorageComponent storage) {
8569
if (storage == null) throw new NullPointerException("storage == null");
@@ -94,14 +78,21 @@ public Builder metrics(CollectorMetrics metrics) {
9478
return this;
9579
}
9680

81+
/** @see {@link CollectorComponent.Builder#sampler(CollectorSampler)} */
82+
public Builder sampler(CollectorSampler sampler) {
83+
if (sampler == null) throw new NullPointerException("sampler == null");
84+
this.sampler = sampler;
85+
return this;
86+
}
87+
9788
public Collector build() {
9889
return new Collector(this);
9990
}
10091
}
10192

10293
final Logger logger;
10394
final CollectorMetrics metrics;
104-
final CollectedSpanHandler handler;
95+
final CollectorSampler sampler;
10596
final StorageComponent storage;
10697

10798
Collector(Builder builder) {
@@ -110,7 +101,7 @@ public Collector build() {
110101
this.metrics = builder.metrics == null ? CollectorMetrics.NOOP_METRICS : builder.metrics;
111102
if (builder.storage == null) throw new NullPointerException("storage == null");
112103
this.storage = builder.storage;
113-
this.handler = consolidate(builder.collectedSpanHandlers);
104+
this.sampler = builder.sampler == null ? CollectorSampler.ALWAYS_SAMPLE : builder.sampler;
114105
}
115106

116107
public void accept(List<Span> spans, Callback<Void> callback) {
@@ -128,25 +119,24 @@ public void accept(List<Span> spans, Callback<Void> callback, Executor executor)
128119
callback.onSuccess(null);
129120
return;
130121
}
122+
metrics.incrementSpans(spans.size());
131123

132-
try {
133-
metrics.incrementSpans(spans.size());
134-
135-
List<Span> handledSpans = handle(spans);
136-
if (handledSpans.isEmpty()) {
137-
callback.onSuccess(null);
138-
return;
139-
}
124+
List<Span> sampledSpans = sample(spans);
125+
if (sampledSpans.isEmpty()) {
126+
callback.onSuccess(null);
127+
return;
128+
}
140129

141-
// In order to ensure callers are not blocked, we swap callbacks when we get to the storage
142-
// phase of this process. Here, we create a callback whose sole purpose is classifying later
143-
// errors on this bundle of spans in the same log category. This allows people to only turn on
144-
// debug logging in one place.
145-
executor.execute(new StoreSpans(handledSpans));
130+
// In order to ensure callers are not blocked, we swap callbacks when we get to the storage
131+
// phase of this process. Here, we create a callback whose sole purpose is classifying later
132+
// errors on this bundle of spans in the same log category. This allows people to only turn on
133+
// debug logging in one place.
134+
try {
135+
executor.execute(new StoreSpans(sampledSpans));
146136
callback.onSuccess(null);
147137
} catch (Throwable unexpected) { // ensure if a future is supplied we always set value or error
148-
Call.propagateIfFatal(unexpected);
149138
callback.onError(unexpected);
139+
throw unexpected;
150140
}
151141
}
152142

@@ -214,15 +204,17 @@ String idString(Span span) {
214204
return span.traceId() + "/" + span.id();
215205
}
216206

217-
List<Span> handle(List<Span> input) {
218-
List<Span> handled = new ArrayList<>(input.size());
207+
List<Span> sample(List<Span> input) {
208+
List<Span> sampled = new ArrayList<>(input.size());
219209
for (int i = 0, length = input.size(); i < length; i++) {
220-
Span s = handler.handle(input.get(i));
221-
if (s != null) handled.add(s);
210+
Span s = input.get(i);
211+
if (sampler.isSampled(s.traceId(), Boolean.TRUE.equals(s.debug()))) {
212+
sampled.add(s);
213+
}
222214
}
223-
int dropped = input.size() - handled.size();
215+
int dropped = input.size() - sampled.size();
224216
if (dropped > 0) metrics.incrementSpansDropped(dropped);
225-
return handled;
217+
return sampled;
226218
}
227219

228220
class StoreSpans implements Callback<Void>, Runnable {
@@ -247,7 +239,7 @@ class StoreSpans implements Callback<Void>, Runnable {
247239
}
248240

249241
@Override public void onError(Throwable t) {
250-
handleStorageError(spans, t, NOOP_VOID);
242+
handleStorageError(spans, t, NOOP_CALLBACK);
251243
}
252244

253245
@Override public String toString() {
@@ -301,30 +293,4 @@ String appendSpanIds(List<Span> spans, StringBuilder message) {
301293

302294
return message.append("]").toString();
303295
}
304-
305-
static CollectedSpanHandler consolidate(List<CollectedSpanHandler> handlers) {
306-
if (handlers.isEmpty()) return CollectedSpanHandler.NOOP;
307-
if (handlers.size() == 1) return handlers.get(0);
308-
return new MultipleHandler(handlers);
309-
}
310-
311-
static final class MultipleHandler implements CollectedSpanHandler {
312-
final CollectedSpanHandler[] handlers; // Array ensures no iterators are created at runtime
313-
314-
MultipleHandler(List<CollectedSpanHandler> handlers) {
315-
this.handlers = handlers.toArray(new CollectedSpanHandler[0]);
316-
}
317-
318-
@Override public Span handle(Span span) {
319-
for (CollectedSpanHandler handler : handlers) {
320-
span = handler.handle(span);
321-
if (span == null) return null;
322-
}
323-
return span;
324-
}
325-
326-
@Override public String toString() {
327-
return Arrays.toString(handlers);
328-
}
329-
}
330296
}

zipkin-collector/core/src/main/java/zipkin2/collector/CollectorComponent.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import java.util.List;
1717
import zipkin2.Component;
18-
import zipkin2.collector.handler.CollectedSpanHandler;
1918
import zipkin2.storage.SpanConsumer;
2019
import zipkin2.storage.StorageComponent;
2120

@@ -36,31 +35,7 @@ public abstract class CollectorComponent extends Component {
3635

3736
public abstract static class Builder {
3837
/**
39-
* {@link CollectorSampler#isSampled(String, boolean) samples spans} to reduce load on the
40-
* storage system. Defaults to always sample.
41-
*
42-
* <p>Sampling happens before {@link #addCollectedSpanHandler(CollectedSpanHandler) handlers}.
43-
*
44-
* @deprecated since 2.17, use {@link #addCollectedSpanHandler(CollectedSpanHandler)}
45-
*/
46-
@Deprecated public abstract Builder sampler(CollectorSampler sampler);
47-
48-
/**
49-
* Triggered on each collected span, before storage. This allows the ability to mutate or drop
50-
* spans for reasons including remapping tags.
51-
*
52-
* <p>Handlers execute after {@link #sampler(CollectorSampler) sampling} and before {@link
53-
* #storage(StorageComponent) storage}.
54-
*
55-
* @since 2.17
56-
*/
57-
// empty implementation as this method was added late
58-
public Builder addCollectedSpanHandler(CollectedSpanHandler collectedSpanHandler) {
59-
return this;
60-
}
61-
62-
/**
63-
* Once spans are handled, they are {@link SpanConsumer#accept(List)} queued for storage} using
38+
* Once spans are sampled, they are {@link SpanConsumer#accept(List)} queued for storage} using
6439
* this component.
6540
*/
6641
public abstract Builder storage(StorageComponent storage);
@@ -71,6 +46,12 @@ public Builder addCollectedSpanHandler(CollectedSpanHandler collectedSpanHandler
7146
*/
7247
public abstract Builder metrics(CollectorMetrics metrics);
7348

49+
/**
50+
* {@link CollectorSampler#isSampled(String, boolean) samples spans} to reduce load on the
51+
* storage system. Defaults to always sample.
52+
*/
53+
public abstract Builder sampler(CollectorSampler sampler);
54+
7455
public abstract CollectorComponent build();
7556
}
7657
}

zipkin-collector/core/src/main/java/zipkin2/collector/CollectorSampler.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package zipkin2.collector;
1515

1616
import zipkin2.Span;
17-
import zipkin2.collector.handler.CollectedSpanHandler;
1817
import zipkin2.internal.HexCodec;
1918

2019
/**
@@ -27,21 +26,21 @@
2726
* <p>Accepts a percentage of trace ids by comparing their absolute value against a potentially
2827
* dynamic boundary. eg {@code isSampled == abs(traceId) <= boundary}
2928
*
30-
* <p>While idempotent, this implementation's sample rate won't exactly match the input rate
31-
* because trace ids are not perfectly distributed across 64bits. For example, tests have shown an
32-
* error rate of 3% when 100K trace ids are {@link java.util.Random#nextLong random}.
29+
* <p>While idempotent, this implementation's sample rate won't exactly match the input rate because
30+
* trace ids are not perfectly distributed across 64bits. For example, tests have shown an error
31+
* rate of 3% when 100K trace ids are {@link java.util.Random#nextLong random}.
3332
*/
34-
public abstract class CollectorSampler implements CollectedSpanHandler {
33+
public abstract class CollectorSampler {
3534
public static final CollectorSampler ALWAYS_SAMPLE = CollectorSampler.create(1.0f);
3635

3736
/** @param rate minimum sample rate is 0.0001, or 0.01% of traces */
3837
public static CollectorSampler create(float rate) {
39-
if (rate < 0 || rate > 1) {
38+
if (rate < 0 || rate > 1)
4039
throw new IllegalArgumentException("rate should be between 0 and 1: was " + rate);
41-
}
4240
final long boundary = (long) (Long.MAX_VALUE * rate); // safe cast as less <= 1
4341
return new CollectorSampler() {
44-
@Override protected long boundary() {
42+
@Override
43+
protected long boundary() {
4544
return boundary;
4645
}
4746
};
@@ -60,29 +59,20 @@ public static CollectorSampler create(float rate) {
6059
*
6160
* @param hexTraceId the lower 64 bits of the span's trace ID are checked against the boundary
6261
* @param debug when true, always passes sampling
63-
* @deprecated since 2.17, use {@link #handle(Span)}
6462
*/
65-
@Deprecated public boolean isSampled(String hexTraceId, boolean debug) {
66-
if (debug) return true;
63+
public boolean isSampled(String hexTraceId, boolean debug) {
64+
if (Boolean.TRUE.equals(debug)) return true;
6765
long traceId = HexCodec.lowerHexToUnsignedLong(hexTraceId);
6866
// The absolute value of Long.MIN_VALUE is larger than a long, so Math.abs returns identity.
6967
// This converts to MAX_VALUE to avoid always dropping when traceId == Long.MIN_VALUE
7068
long t = traceId == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(traceId);
7169
return t <= boundary();
7270
}
7371

74-
/** Returns the input when the input is {@link Span#debug() debug} or its trace ID was sampled. */
75-
@Override public Span handle(Span span) {
76-
if (isSampled(span.traceId(), Boolean.TRUE.equals(span.debug()))) {
77-
return span;
78-
}
79-
return null;
80-
}
81-
82-
@Override public String toString() {
72+
@Override
73+
public String toString() {
8374
return "CollectorSampler(" + boundary() + ")";
8475
}
8576

86-
protected CollectorSampler() {
87-
}
77+
protected CollectorSampler() {}
8878
}

0 commit comments

Comments
 (0)