Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
36e5dac
[WIP] delegate all tracing calls to `tracing-java`
qinfchen Sep 6, 2018
f321e2c
cleanup
qinfchen Sep 6, 2018
c78a24b
Use published versions of libraries
iamdanfox Sep 6, 2018
edfa0d6
Everything compiles
iamdanfox Sep 6, 2018
6a8471a
Convert#span actually works now
iamdanfox Sep 6, 2018
68d893a
Implement Convert#toRemotingTrace
iamdanfox Sep 6, 2018
420c0e8
All converters propagate nulls
iamdanfox Sep 6, 2018
6ec78ae
Tracer#subscribe returns a remoting span observer
iamdanfox Sep 6, 2018
d4dc7d6
Delete redundant fully-qualified names in TracersTest
iamdanfox Sep 6, 2018
332bbe1
Move longToPaddedHex function out of Tracers
iamdanfox Sep 6, 2018
9e6effd
Delete an asConjure method
iamdanfox Sep 6, 2018
1beda24
@Ignore one TracerTest
iamdanfox Sep 6, 2018
6a02d6f
Thanks checkstyle
iamdanfox Sep 6, 2018
f3724a3
Reduce visibility of Convert class
iamdanfox Sep 6, 2018
9c45298
Fix one Convert method
iamdanfox Sep 6, 2018
13fb17c
Remove mavenLocal()
iamdanfox Sep 6, 2018
0a5b297
Move Utilities to test source set
iamdanfox Sep 6, 2018
8a06a46
Better javadoc and warnings
iamdanfox Sep 6, 2018
bb252e2
Restore CloseableTracerTest
iamdanfox Sep 6, 2018
badb51c
Delete some unused fields and point to their new locations
iamdanfox Sep 6, 2018
b20a71e
rearrange method orders and remove broken javadoc links
qinfchen Sep 6, 2018
1bd0d56
remove Tracers.TRACE_ID_KEY and use java-tracing TRACE_ID_KEY
qinfchen Sep 6, 2018
0ac9f54
thanks checkstyle
qinfchen Sep 6, 2018
e9c0d1e
Revert "remove Tracers.TRACE_ID_KEY and use java-tracing TRACE_ID_KEY"
iamdanfox Sep 7, 2018
adf0f74
Don't make Trace public
iamdanfox Sep 7, 2018
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
Prev Previous commit
Next Next commit
Everything compiles
  • Loading branch information
iamdanfox committed Sep 6, 2018
commit edfa0d63364318defedbc5747c1dc2c2e44bbf9e
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,11 @@

package com.palantir.remoting3.tracing;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Lists;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.remoting.api.tracing.Span;
import com.palantir.remoting3.ext.jackson.ObjectMappers;
import com.palantir.remoting.api.tracing.SpanObserver;
import com.palantir.tracing.AsyncSpanObserver;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import org.immutables.value.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,12 +34,7 @@ public final class AsyncSlf4jSpanObserver implements SpanObserver {

@Override
public void consume(Span span) {
this.delegate.doConsume(span.asConjure());
}

@Override
public com.palantir.tracing.api.SpanObserver asConjure() {
return this.delegate;
this.delegate.doConsume(Convert.span(span));
}

private AsyncSlf4jSpanObserver(String serviceName, InetAddress ip, Logger logger, ExecutorService executorService) {
Expand Down
104 changes: 104 additions & 0 deletions tracing/src/main/java/com/palantir/remoting3/tracing/Convert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* 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 com.palantir.remoting3.tracing;

import com.palantir.remoting.api.tracing.OpenSpan;
import com.palantir.tracing.api.Span;
import com.palantir.tracing.api.SpanObserver;
import com.palantir.tracing.api.SpanType;

/** Utility functions to convert old remoting-api classes to the new tracing-java ones and vice-versa. */
public final class Convert {

private Convert() {}

static SpanType spanType(com.palantir.remoting.api.tracing.SpanType old) {
switch (old) {
case SERVER_INCOMING:
return SpanType.SERVER_INCOMING;
case CLIENT_OUTGOING:
return SpanType.CLIENT_OUTGOING;
case LOCAL:
return SpanType.LOCAL;
}
throw new IllegalStateException("Unable to convert: " + old);
}

static SpanObserver spanObserver(com.palantir.remoting.api.tracing.SpanObserver old) {
return new SpanObserver() {
@Override
public void consume(Span span) {
old.consume(toRemotingSpan(span));
}
};
}

static com.palantir.remoting.api.tracing.Span toRemotingSpan(Span span) {
return com.palantir.remoting.api.tracing.Span.builder()
.traceId(span.getTraceId())
.parentSpanId(span.getParentSpanId())
.spanId(span.getSpanId())
.type(toRemotingSpanType(span.type()))
.operation(span.getOperation())
.startTimeMicroSeconds(span.getStartTimeMicroSeconds())
.durationNanoSeconds(span.getDurationNanoSeconds())
.build();
}

static OpenSpan toRemotingOpenSpan(com.palantir.tracing.api.OpenSpan openSpan) {
return OpenSpan.builder()
.parentSpanId(openSpan.getParentSpanId())
.spanId(openSpan.getSpanId())
.type(toRemotingSpanType(openSpan.type()))
.operation(openSpan.getOperation())
.startTimeMicroSeconds(openSpan.getStartTimeMicroSeconds())
.startClockNanoSeconds(openSpan.getStartClockNanoSeconds())
.build();
}

static com.palantir.remoting.api.tracing.SpanType toRemotingSpanType(SpanType type) {
switch (type) {
case CLIENT_OUTGOING:
return com.palantir.remoting.api.tracing.SpanType.CLIENT_OUTGOING;
case SERVER_INCOMING:
return com.palantir.remoting.api.tracing.SpanType.SERVER_INCOMING;
case LOCAL:
return com.palantir.remoting.api.tracing.SpanType.LOCAL;
}

throw new UnsupportedOperationException("Unable to convert to Remoting SpanType");
}

public static com.palantir.remoting.api.tracing.SpanObserver toRemotingSpanObserver(SpanObserver unsubscribe) {
return new com.palantir.remoting.api.tracing.SpanObserver() {
@Override
public void consume(com.palantir.remoting.api.tracing.Span span) {
unsubscribe.consume(Convert.span(span));
}
};
}

public static Trace toRemotingTrace(com.palantir.tracing.Trace andClearTrace) {
// TODO
return null;
}

public static Span span(com.palantir.remoting.api.tracing.Span span) {
// TODO
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,10 @@ private Observers() {}
*/
public static SpanObserver asyncDecorator(
final SpanObserver observer, ExecutorService executorService, int maxInflights) {
return new SpanObserver() {
private final com.palantir.tracing.AsyncSpanObserver delegate =
new com.palantir.tracing.AsyncSpanObserver(executorService, maxInflights) {
@Override
public void doConsume(com.palantir.tracing.api.Span span) {
observer.asConjure().consume(span);
}
};

@Override
public void consume(Span span) {
this.delegate.doConsume(span.asConjure());
}

return new AsyncSpanObserver(executorService, maxInflights) {
@Override
public com.palantir.tracing.api.SpanObserver asConjure() {
return this.delegate;
public void doConsume(Span span) {
observer.consume(span);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Represents a trace as an ordered list of non-completed spans. Supports adding and removing of spans. This class is
* not thread-safe and is intended to be used in a thread-local context.
*/
final class Trace {
public final class Trace {
Copy link
Contributor

Choose a reason for hiding this comment

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

does this need to become public?

Copy link
Contributor

Choose a reason for hiding this comment

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

Without this being public, one of the test utils fails to compile.

screen shot 2018-09-06 at 17 26 13

I find an alternative that involved some objects and casting too!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is also kind of odd that we have a public static method that returns Trace and Trace is private.
https://github.com/palantir/http-remoting/pull/799/files#diff-7ee517b85058e51de91b9bf446ff7585L246

Copy link
Contributor

Choose a reason for hiding this comment

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

@qinfchen I don't have the original context, but one possible justification could be that this Trace class is useful for testing but shouldn't be used by consumers??


private final Deque<OpenSpan> stack;
private final boolean isObservable;
Expand Down
68 changes: 9 additions & 59 deletions tracing/src/main/java/com/palantir/remoting3/tracing/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ public static void initTrace(Optional<Boolean> isObservable, String traceId) {
* when the current trace is empty.
*/
public static OpenSpan startSpan(String operation, String parentSpanId, SpanType type) {
return toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(
operation, parentSpanId, type.asConjure()));
return Convert.toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(
operation, parentSpanId, Convert.spanType(type)));
}

/**
* Like {@link #startSpan(String)}, but opens a span of the explicitly given {@link SpanType span type}.
*/
public static OpenSpan startSpan(String operation, SpanType type) {
return toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(operation, type.asConjure()));
return Convert.toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(operation, Convert.spanType(type)));
}

/**
* Opens a new {@link SpanType#LOCAL LOCAL} span for this thread's call trace, labeled with the provided operation.
*/
public static OpenSpan startSpan(String operation) {
return toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(operation));
return Convert.toRemotingOpenSpan(com.palantir.tracing.Tracer.startSpan(operation));
}


Expand All @@ -100,14 +100,14 @@ public static void fastCompleteSpan(Map<String, String> metadata) {
* completed span.
*/
public static Optional<Span> completeSpan() {
return com.palantir.tracing.Tracer.completeSpan().map(Tracer::toRemotingSpan);
return com.palantir.tracing.Tracer.completeSpan().map(Convert::toRemotingSpan);
}

/**
* Like {@link #completeSpan()}, but adds {@code metadata} to the current span being completed.
*/
public static Optional<Span> completeSpan(Map<String, String> metadata) {
return com.palantir.tracing.Tracer.completeSpan(metadata).map(Tracer::toRemotingSpan);
return com.palantir.tracing.Tracer.completeSpan(metadata).map(Convert::toRemotingSpan);
}

/**
Expand All @@ -117,15 +117,15 @@ public static Optional<Span> completeSpan(Map<String, String> metadata) {
* with the given name, or null if there is no such observer.
*/
public static synchronized SpanObserver subscribe(String name, SpanObserver observer) {
return toRemotingSpanObserver(com.palantir.tracing.Tracer.subscribe(name, observer.asConjure()));
return Convert.toRemotingSpanObserver(com.palantir.tracing.Tracer.subscribe(name, Convert.spanObserver(observer)));
}

/**
* The inverse of {@link #subscribe}: removes the observer registered for the given name. Returns the removed
* observer if it existed, or null otherwise.
*/
public static synchronized SpanObserver unsubscribe(String name) {
return toRemotingSpanObserver(com.palantir.tracing.Tracer.unsubscribe(name));
return Convert.toRemotingSpanObserver(com.palantir.tracing.Tracer.unsubscribe(name));
}

/** Sets the sampler (for all threads). */
Expand All @@ -140,7 +140,7 @@ public static String getTraceId() {

/** Clears the current trace id and returns (a copy of) it. */
public static Trace getAndClearTrace() {
return com.palantir.tracing.Tracer.getAndClearTrace();
return Convert.toRemotingTrace(com.palantir.tracing.Tracer.getAndClearTrace());
}

/**
Expand All @@ -150,54 +150,4 @@ public static Trace getAndClearTrace() {
public static boolean isTraceObservable() {
return com.palantir.tracing.Tracer.isTraceObservable();
}

private static Span toRemotingSpan(com.palantir.tracing.api.Span span) {
return Span.builder()
.traceId(span.getTraceId())
.parentSpanId(span.getParentSpanId())
.spanId(span.getSpanId())
.type(toRemotingSpanType(span.type()))
.operation(span.getOperation())
.startTimeMicroSeconds(span.getStartTimeMicroSeconds())
.durationNanoSeconds(span.getDurationNanoSeconds())
.build();
}

private static OpenSpan toRemotingOpenSpan(com.palantir.tracing.api.OpenSpan openSpan) {
return OpenSpan.builder()
.parentSpanId(openSpan.getParentSpanId())
.spanId(openSpan.getSpanId())
.type(toRemotingSpanType(openSpan.type()))
.operation(openSpan.getOperation())
.startTimeMicroSeconds(openSpan.getStartTimeMicroSeconds())
.startClockNanoSeconds(openSpan.getStartClockNanoSeconds())
.build();
}

private static SpanType toRemotingSpanType(com.palantir.tracing.api.SpanType type) {
switch (type) {
case CLIENT_OUTGOING:
return SpanType.CLIENT_OUTGOING;
case SERVER_INCOMING:
return SpanType.SERVER_INCOMING;
case LOCAL:
return SpanType.LOCAL;
}

throw new UnsupportedOperationException("Unable to convert to Remoting SpanType");
}

private static SpanObserver toRemotingSpanObserver(com.palantir.tracing.api.SpanObserver spanObserver) {
return new SpanObserver() {
@Override
public void consume(Span span) {
spanObserver.consume(span.asConjure());
}

@Override
public com.palantir.tracing.api.SpanObserver asConjure() {
return spanObserver;
}
};
}
}
Loading