Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2547 Implement MicroProfile OpenTracing 1.0 #2776

Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0af6260
PAYARA-2547 Initial implementation - No MP or injectable tracer
Pandrex247 Apr 6, 2018
d2d6800
PAYARA-2547 Remove old traced annotation
Pandrex247 May 8, 2018
6a69dc6
PAYARA-2547 Missed API
Pandrex247 May 8, 2018
7ff8e1d
PAYARA-2547 Add mock tracer
Pandrex247 May 9, 2018
462207d
PAYARA-2547 Some fixes
Pandrex247 May 10, 2018
ce019cb
PAYARA-2547 Remove old container tracing, and add exception mapper
Pandrex247 May 17, 2018
b000ed6
PAYARA-2547 Remove import
Pandrex247 May 17, 2018
60199fb
PAYARA-2547 Switch order and add span error logs
Pandrex247 May 17, 2018
f7783ae
PAYARA-2547 Remove junk, update jsonb, and fix mocktracer deactivation
Pandrex247 May 21, 2018
c657fb2
PAYARA-2547 Fix internal deactivation
Pandrex247 May 21, 2018
1084d1d
PAYARA-2547 Better error handling
Pandrex247 May 21, 2018
d6659be
PAYARA-2547 Remove junk
Pandrex247 May 21, 2018
0544e3b
PAYARA-2547 Change to use inheritable thread local
Pandrex247 May 21, 2018
f3adc53
PAYARA-2547 Inheritable thread local doesn't work, revert
Pandrex247 May 22, 2018
776e9a8
PAYARA-2547 Switch to using decorated jaxrs client - passing TCK
Pandrex247 May 24, 2018
01a4be7
PAYARA-2547 Add to embedded
Pandrex247 May 24, 2018
964cec8
PAYARA-2547 Add comments, Javadoc, and do some cleanup
Pandrex247 May 24, 2018
660ba80
PAYARA-2547 Remove swap file, and add concrete yasson version
Pandrex247 May 25, 2018
347d594
PAYARA-2547 Revert NetBeans formatting
Pandrex247 May 25, 2018
6ff731a
PAYARA-2547 Remove vestiges of old requesttracing api
Pandrex247 May 25, 2018
80d9df4
PAYARA-2547 Remove NetBeans formatting and changes that should be in …
Pandrex247 May 25, 2018
1b9ac3b
PAYARA-2547 Further redaction of things that shouldn't be in this PR
Pandrex247 May 25, 2018
54df4fa
PAYARA-2547 Correct some time conversions and settings to prevent a null
Pandrex247 May 28, 2018
47ee92f
Merge branch 'master' into PAYARA-2547-Implement-MicroProfile-OpenTra…
Pandrex247 May 29, 2018
b435892
PAYARA-2547 Dodgy fix for Jaxrs fileupload test
Pandrex247 May 29, 2018
5482134
Merge branch 'PAYARA-2547-Implement-MicroProfile-OpenTracing-1.0' of …
Pandrex247 May 29, 2018
3bd9ae0
PAYARA-2547 Extra javadoc
Pandrex247 May 29, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/payara-api/pom.xml
Expand Up @@ -175,5 +175,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>${opentracing.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -86,43 +86,94 @@ public void addEvent(RequestTraceSpan span) {
if (null != span.getEventType()) {
switch (span.getEventType()) {
case TRACE_START:
trace.clear();
startTime = span.getStartInstant();
trace.add(span);
started = true;
completed = false;
handleTraceStart(span);
break;
case PROPAGATED_TRACE:
trace.clear();
startTime = span.getStartInstant();
trace.add(span);
started = true;
completed = false;
handlePropagatedTrace(span);
break;
case REQUEST_EVENT: {
if (!started) {
return;
}
RequestTraceSpan rootSpan = trace.getFirst();
span.setTraceId(rootSpan.getTraceId());
span.setSpanDuration(span.getStartInstant().until(Instant.now(), ChronoUnit.NANOS));
span.setTraceEndTime(Instant.now());
trace.add(span);
handleRequestEvent(span);
break;
}
default:
break;
}
}
}

public void endTrace() {

public void addEvent(RequestTraceSpan span, long timestampMillis) {
// Do not add trace events if completed
if (completed
&& span.getEventType() != TRACE_START
&& span.getEventType() != PROPAGATED_TRACE) {
return;
}

if (null != span.getEventType()) {
switch (span.getEventType()) {
case TRACE_START:
handleTraceStart(span);
break;
case PROPAGATED_TRACE:
handlePropagatedTrace(span);
break;
case REQUEST_EVENT: {
handleRequestEvent(span, timestampMillis);
break;
}
default:
break;
}
}
}

private void handleTraceStart(RequestTraceSpan span) {
trace.clear();
startTime = span.getStartInstant();
trace.add(span);
started = true;
completed = false;
}

private void handlePropagatedTrace(RequestTraceSpan span) {
trace.clear();
startTime = span.getStartInstant();
trace.add(span);
started = true;
completed = false;
}

private void handleRequestEvent(RequestTraceSpan span) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could the first function call the overloaded one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not straight-forwardly as you'd lose the precision of nanoTime converting to milliseconds.

if (!started) {
return;
}

Collections.sort(trace);
RequestTraceSpan rootSpan = trace.getFirst();
span.setTraceId(rootSpan.getTraceId());
span.setSpanDuration(span.getStartInstant().until(Instant.now(), ChronoUnit.NANOS));
span.setTraceEndTime(Instant.now());
trace.add(span);
}

private void handleRequestEvent(RequestTraceSpan span, long timestampMillis) {
if (!started) {
return;
}

RequestTraceSpan rootSpan = trace.getFirst();
span.setTraceId(rootSpan.getTraceId());
span.setSpanDuration(span.getStartInstant().until(Instant.ofEpochMilli(timestampMillis), ChronoUnit.NANOS));
span.setTraceEndTime(Instant.ofEpochMilli(timestampMillis));
trace.add(span);
}

public void endTrace() {
if (!started) {
return;
}

Collections.sort(trace);

RequestTraceSpan startSpan = trace.getFirst();
endTime = Instant.now();
startSpan.setSpanDuration(startTime.until(endTime, ChronoUnit.NANOS));
Expand Down Expand Up @@ -171,16 +222,16 @@ public boolean isStarted() {

/**
* Returns a list of all the events that make up the trace.
* @return
* @return
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be corrected to be described in the @return?

*/
public LinkedList<RequestTraceSpan> getTraceSpans() {
return trace;
}

/**
* Gets the Instant when the span was started
* Gets the Instant when the span was started
* See {@link java.time.Instant#now()} for how this time is generated.
* @return
* @return
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

*/
public Instant getStartTime() {
return startTime;
Expand Down Expand Up @@ -250,8 +301,8 @@ private void assignReferences() {
if (trace.indexOf(span) != 0) {
RequestTraceSpan bestMatchingParent = null;
for (RequestTraceSpan comparisonSpan : trace) {
if (span.getTimeOccured() > comparisonSpan.getTimeOccured() &&
span.getTraceEndTime().compareTo(comparisonSpan.getTraceEndTime()) < 0) {
if (span.getTimeOccured() > comparisonSpan.getTimeOccured()
&& span.getTraceEndTime().compareTo(comparisonSpan.getTraceEndTime()) < 0) {
if (bestMatchingParent == null) {
bestMatchingParent = comparisonSpan;
} else {
Expand Down Expand Up @@ -291,8 +342,8 @@ public boolean equals(Object o) {

RequestTrace that = (RequestTrace) o;

return elapsedTime == that.elapsedTime && (this.toString() != null ?
this.toString().equals(that.toString()) : that.toString() == null);
return elapsedTime == that.elapsedTime && (this.toString() != null
? this.toString().equals(that.toString()) : that.toString() == null);
}

@Override
Expand Down
Expand Up @@ -42,7 +42,7 @@
import java.io.Serializable;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -57,21 +57,28 @@
public class RequestTraceSpan implements Serializable, Comparable<RequestTraceSpan> {

private final SpanContext spanContext;
private final Instant startTime;
private Instant startTime;
private Instant endTime;
private long spanDuration;
private final EventType eventType;
private Map<String, String> spanTags;
private List<RequestTraceSpanLog> spanLogs;
private EventType eventType;
private final Map<String, String> spanTags;
private final List<RequestTraceSpanLog> spanLogs;
private String eventName;
private final List<SpanReference> spanReferences;

protected RequestTraceSpan() {
this.spanContext = new SpanContext();
this.spanTags = new HashMap<>();
this.spanLogs = new ArrayList<>();
this.spanReferences = new ArrayList<>();
}

public RequestTraceSpan(String eventName) {
this(EventType.REQUEST_EVENT, eventName);
}

public RequestTraceSpan(EventType eventType, String eventName) {
this.spanContext = new SpanContext();;
this.spanContext = new SpanContext();
this.startTime = Instant.now();
this.eventType = eventType;
this.eventName = eventName;
Expand Down Expand Up @@ -109,17 +116,20 @@ public SpanContext getSpanContext() {
return spanContext;
}

public Instant getStartInstant(){
public Instant getStartInstant() {
return startTime;
}


public void setStartInstant(Instant startTime) {
this.startTime = startTime;
}

/**
* Gets the time in milliseconds since the epoch (midnight, January 1st 1970)
* when the the request event occurred.
*
* @return the time the trace occurred
*/
public long getTimeOccured(){
public long getTimeOccured() {
return startTime.toEpochMilli();
}

Expand All @@ -146,17 +156,17 @@ public EventType getEventType() {
public String getEventName() {
return eventName;
}


public void setEventName(String eventName) {
this.eventName = eventName;
}

/**
* Adds more information about a span
* @param name
* @param value
*/
public void addSpanTag(String name, String value) {
if (spanTags == null) {
spanTags = new HashMap<>();
}

if (value != null) {
// Escape any quotes
spanTags.put(name, value.replaceAll("\"", "\\\\\""));
Expand Down Expand Up @@ -184,10 +194,13 @@ public Instant getTraceEndTime() {
public void setTraceEndTime(Instant endTime) {
this.endTime = endTime;
}


public void setEventType(EventType spanType) {
this.eventType = spanType;
}

@Override
public String toString() {

StringBuilder result = new StringBuilder("\n{");
result.append("\"operationName\":\"").append(eventName).append("\",")
.append("\"spanContext\":{")
Expand Down Expand Up @@ -260,6 +273,7 @@ public int compareTo(RequestTraceSpan span) {
}

public class SpanContext implements Serializable {

private final UUID spanId;
private UUID traceId;
private final Map<String, String> baggageItems;
Expand Down Expand Up @@ -297,18 +311,20 @@ public void setTraceId(UUID traceId) {
public void addBaggageItem(String name, String value) {
if (value != null) {
// Escape any quotes
spanTags.put(name, value.replaceAll("\"", "\\\""));
addSpanTag(name, value.replaceAll("\"", "\\\""));
} else {
spanTags.put(name, value);
addSpanTag(name, value);
}
}

public Map<String, String> getBaggageItems() {
return baggageItems;
}

}

public class SpanReference implements Serializable {

private final SpanContext referenceSpanContext;
private final SpanContextRelationshipType relationshipType;

Expand All @@ -324,10 +340,12 @@ public SpanContext getReferenceSpanContext() {
public SpanContextRelationshipType getSpanContextRelationshipType() {
return relationshipType;
}

}

public enum SpanContextRelationshipType {
ChildOf,
FollowsFrom;
}

}
Expand Up @@ -52,12 +52,28 @@ public class RequestTraceSpanLog implements Serializable {
private final long timeMillis;
private final Map<String, String> logEntries;

public RequestTraceSpanLog() {
timeMillis = System.currentTimeMillis();
logEntries = new LinkedHashMap<>();
}

public RequestTraceSpanLog(long timestampMillis) {
timeMillis = timestampMillis;
logEntries = new LinkedHashMap<>();
}

public RequestTraceSpanLog(String logName) {
timeMillis = System.currentTimeMillis();
logEntries = new LinkedHashMap<>();
logEntries.put("logEvent", logName);
}

public RequestTraceSpanLog(long timestampMillis, String logName) {
timeMillis = timestampMillis;
logEntries = new LinkedHashMap<>();
logEntries.put("logEvent", logName);
}

public long getTimeMillis() {
return timeMillis;
}
Expand Down