Skip to content

Commit 8f43a55

Browse files
Adrian Coleabesto
Adrian Cole
authored andcommitted
Drops internal DependencyLinkSpan for Span2
This drops the internal type of DependencyLinkSpan in favor of the Span2 type coming in openzipkin#1499. Doing so now gives us practice, solves a few bugs along the way. When Span2 becomes non-internal, the change will be a simple package rename.
1 parent ae8ae76 commit 8f43a55

File tree

8 files changed

+411
-701
lines changed

8 files changed

+411
-701
lines changed

zipkin-storage/mysql/src/main/java/zipkin/storage/mysql/DependencyLinkSpanIterator.java renamed to zipkin-storage/mysql/src/main/java/zipkin/storage/mysql/DependencyLinkSpan2Iterator.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,32 @@
1616
import java.util.Iterator;
1717
import org.jooq.Record;
1818
import org.jooq.TableField;
19-
import zipkin.internal.DependencyLinkSpan;
19+
import zipkin.Endpoint;
2020
import zipkin.internal.Nullable;
2121
import zipkin.internal.PeekingIterator;
22+
import zipkin.internal.Span2;
2223
import zipkin.storage.mysql.internal.generated.tables.ZipkinSpans;
2324

2425
import static zipkin.Constants.CLIENT_ADDR;
2526
import static zipkin.Constants.CLIENT_SEND;
2627
import static zipkin.Constants.SERVER_ADDR;
2728
import static zipkin.Constants.SERVER_RECV;
29+
import static zipkin.internal.Util.equal;
2830
import static zipkin.storage.mysql.internal.generated.tables.ZipkinAnnotations.ZIPKIN_ANNOTATIONS;
2931

3032
/**
31-
* Convenience that lazy converts rows into {@linkplain DependencyLinkSpan} objects.
33+
* Lazy converts rows into {@linkplain Span2} objects suitable for dependency links. This takes
34+
* short-cuts to require less data. For example, it folds shared RPC spans into one, and doesn't
35+
* include tags, non-core annotations or time units.
3236
*
3337
* <p>Out-of-date schemas may be missing the trace_id_high field. When present, this becomes {@link
34-
* DependencyLinkSpan.TraceId#hi} used as the left-most 16 characters of the traceId in logging
38+
* Span2#traceIdHigh()} used as the left-most 16 characters of the traceId in logging
3539
* statements.
3640
*/
37-
final class DependencyLinkSpanIterator implements Iterator<DependencyLinkSpan> {
41+
final class DependencyLinkSpan2Iterator implements Iterator<Span2> {
3842

3943
/** Assumes the input records are sorted by trace id, span id */
40-
static final class ByTraceId implements Iterator<Iterator<DependencyLinkSpan>> {
44+
static final class ByTraceId implements Iterator<Iterator<Span2>> {
4145
final PeekingIterator<Record> delegate;
4246
final boolean hasTraceIdHigh;
4347

@@ -53,10 +57,10 @@ static final class ByTraceId implements Iterator<Iterator<DependencyLinkSpan>> {
5357
return delegate.hasNext();
5458
}
5559

56-
@Override public Iterator<DependencyLinkSpan> next() {
60+
@Override public Iterator<Span2> next() {
5761
currentTraceIdHi = hasTraceIdHigh ? traceIdHigh(delegate) : null;
5862
currentTraceIdLo = delegate.peek().getValue(ZipkinSpans.ZIPKIN_SPANS.TRACE_ID);
59-
return new DependencyLinkSpanIterator(delegate, currentTraceIdHi, currentTraceIdLo);
63+
return new DependencyLinkSpan2Iterator(delegate, currentTraceIdHi, currentTraceIdLo);
6064
}
6165

6266
@Override public void remove() {
@@ -68,7 +72,7 @@ static final class ByTraceId implements Iterator<Iterator<DependencyLinkSpan>> {
6872
@Nullable final Long traceIdHi;
6973
final long traceIdLo;
7074

71-
DependencyLinkSpanIterator(PeekingIterator<Record> delegate, Long traceIdHi, long traceIdLo) {
75+
DependencyLinkSpan2Iterator(PeekingIterator<Record> delegate, Long traceIdHi, long traceIdLo) {
7276
this.delegate = delegate;
7377
this.traceIdHi = traceIdHi;
7478
this.traceIdLo = traceIdLo;
@@ -83,17 +87,11 @@ public boolean hasNext() {
8387
}
8488

8589
@Override
86-
public DependencyLinkSpan next() {
90+
public Span2 next() {
8791
Record row = delegate.peek();
8892

8993
long spanId = row.getValue(ZipkinSpans.ZIPKIN_SPANS.ID);
90-
DependencyLinkSpan.Builder result = DependencyLinkSpan.builder(
91-
traceIdHi != null ? traceIdHi : 0L,
92-
traceIdLo,
93-
row.getValue(ZipkinSpans.ZIPKIN_SPANS.PARENT_ID),
94-
spanId
95-
);
96-
94+
String srService = null, csService = null, caService = null, saService = null;
9795
while (hasNext()) { // there are more values for this trace
9896
if (spanId != delegate.peek().getValue(ZipkinSpans.ZIPKIN_SPANS.ID)) {
9997
break; // if we are in a new span
@@ -105,18 +103,48 @@ public DependencyLinkSpan next() {
105103
if (key == null || value == null) continue; // neither client nor server
106104
switch (key) {
107105
case CLIENT_ADDR:
108-
result.caService(value);
106+
caService = value;
109107
break;
110108
case CLIENT_SEND:
111-
result.csService(value);
109+
csService = value;
112110
break;
113111
case SERVER_ADDR:
114-
result.saService(value);
112+
saService = value;
115113
break;
116114
case SERVER_RECV:
117-
result.srService(value);
115+
srService = value;
118116
}
119117
}
118+
119+
// The client address is more authoritative than the client send owner.
120+
if (caService == null) caService = csService;
121+
122+
// Finagle labels two sides of the same socket ("ca", "sa") with the same name.
123+
// Skip the client side, so it isn't mistaken for a loopback request
124+
if (equal(saService, caService)) caService = null;
125+
126+
Span2.Builder result = Span2.builder()
127+
.traceIdHigh(traceIdHi != null ? traceIdHi : 0L)
128+
.traceId(traceIdLo)
129+
.parentId(row.getValue(ZipkinSpans.ZIPKIN_SPANS.PARENT_ID))
130+
.id(spanId);
131+
132+
if (srService != null) {
133+
return result.kind(Span2.Kind.SERVER)
134+
.localEndpoint(ep(srService))
135+
.remoteEndpoint(ep(caService))
136+
.build();
137+
} else if (saService != null) {
138+
return result
139+
.kind(csService != null ? Span2.Kind.CLIENT : null)
140+
.localEndpoint(ep(caService))
141+
.remoteEndpoint(ep(saService))
142+
.build();
143+
} else if (csService != null) {
144+
return result.kind(Span2.Kind.SERVER)
145+
.localEndpoint(ep(caService))
146+
.build();
147+
}
120148
return result.build();
121149
}
122150

@@ -129,8 +157,12 @@ static long traceIdHigh(PeekingIterator<Record> delegate) {
129157
return delegate.peek().getValue(ZipkinSpans.ZIPKIN_SPANS.TRACE_ID_HIGH);
130158
}
131159

132-
static String emptyToNull(Record next, TableField<Record, String> field) {
160+
static @Nullable String emptyToNull(Record next, TableField<Record, String> field) {
133161
String result = next.getValue(field);
134162
return result != null && !"".equals(result) ? result : null;
135163
}
164+
165+
static Endpoint ep(@Nullable String serviceName) {
166+
return serviceName != null ? Endpoint.builder().serviceName(serviceName).build() : null;
167+
}
136168
}

zipkin-storage/mysql/src/main/java/zipkin/storage/mysql/MySQLSpanStore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
import zipkin.DependencyLink;
4141
import zipkin.Endpoint;
4242
import zipkin.Span;
43-
import zipkin.internal.DependencyLinkSpan;
4443
import zipkin.internal.DependencyLinker;
4544
import zipkin.internal.GroupByTraceId;
4645
import zipkin.internal.Nullable;
4746
import zipkin.internal.Pair;
47+
import zipkin.internal.Span2;
4848
import zipkin.storage.QueryRequest;
4949
import zipkin.storage.SpanStore;
5050
import zipkin.storage.mysql.internal.generated.tables.ZipkinAnnotations;
@@ -324,8 +324,8 @@ List<DependencyLink> aggregateDependencies(long endTs, @Nullable Long lookback,
324324
// Grouping so that later code knows when a span or trace is finished.
325325
.groupBy(schema.dependencyLinkGroupByFields).fetchLazy();
326326

327-
Iterator<Iterator<DependencyLinkSpan>> traces =
328-
new DependencyLinkSpanIterator.ByTraceId(cursor.iterator(), schema.hasTraceIdHigh);
327+
Iterator<Iterator<Span2>> traces =
328+
new DependencyLinkSpan2Iterator.ByTraceId(cursor.iterator(), schema.hasTraceIdHigh);
329329

330330
if (!traces.hasNext()) return Collections.emptyList();
331331

0 commit comments

Comments
 (0)