Skip to content

Commit

Permalink
opentracing: handle no parent in span end for lttng
Browse files Browse the repository at this point in the history
The information can be inferred by the start.

Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Change-Id: Ic7315aa4c189c54cab50ceaf8fe0a8cfeb7b4618
Reviewed-on: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/205400
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
  • Loading branch information
MatthewKhouzam committed Nov 10, 2023
1 parent 16f16ba commit 8586778
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Expand Up @@ -173,7 +173,7 @@ public SpanLifeDataProvider(ITmfTrace trace, SpanLifeAnalysis analysisModule) {
}

@Override
protected @Nullable TimeGraphModel getRowModel(@NonNull ITmfStateSystem ss, Map<String, Object> parameters, @Nullable IProgressMonitor monitor) throws StateSystemDisposedException {
protected @Nullable TimeGraphModel getRowModel(ITmfStateSystem ss, Map<String, Object> parameters, @Nullable IProgressMonitor monitor) throws StateSystemDisposedException {
TreeMultimap<Integer, ITmfStateInterval> intervals = TreeMultimap.create(Comparator.naturalOrder(),
Comparator.comparing(ITmfStateInterval::getStartTime));
SelectionTimeQueryFilter filter = FetchParametersUtils.createSelectionTimeQuery(parameters);
Expand Down Expand Up @@ -217,7 +217,7 @@ protected boolean isCacheable() {
}

@Override
protected @NonNull TmfTreeModel<@NonNull TimeGraphEntryModel> getTree(@NonNull ITmfStateSystem ss, Map<String, Object> parameters, @Nullable IProgressMonitor monitor) throws StateSystemDisposedException {
protected @NonNull TmfTreeModel<@NonNull TimeGraphEntryModel> getTree(ITmfStateSystem ss, Map<String, Object> parameters, @Nullable IProgressMonitor monitor) throws StateSystemDisposedException {
Builder<@NonNull TimeGraphEntryModel> builder = new Builder<>();
long rootId = getId(ITmfStateSystem.ROOT_ATTRIBUTE);
builder.add(new TimeGraphEntryModel(rootId, -1, Collections.singletonList(String.valueOf(getTrace().getName())), ss.getStartTime(), ss.getCurrentEndTime()));
Expand All @@ -237,25 +237,19 @@ private void addTrace(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryModel>
} catch (AttributeNotFoundException e) {
logsQuarks = new ArrayList<>();
}

int openTracingSpansQuark;
try {
openTracingSpansQuark = ss.getQuarkRelative(quark, SpanLifeStateProvider.OPEN_TRACING_ATTRIBUTE);
} catch (AttributeNotFoundException e) {
return;
}

long traceQuarkId = getId(quark);
builder.add(new TimeGraphEntryModel(traceQuarkId, parentId, Collections.singletonList(ss.getAttributeName(quark)), ss.getStartTime(), ss.getCurrentEndTime()));

int openTracingSpansQuark = ss.optQuarkRelative(quark, SpanLifeStateProvider.OPEN_TRACING_ATTRIBUTE);
builder.add(new TimeGraphEntryModel(traceQuarkId, parentId, Collections.singletonList(ss.getAttributeName(quark)), ss.getStartTime(), ss.getCurrentEndTime()));
int ustSpansQuark;
try {
ustSpansQuark = ss.getQuarkRelative(quark, SpanLifeStateProvider.UST_ATTRIBUTE);
logsQuarks = ss.getSubAttributes(ustSpansQuark, false);
} catch (AttributeNotFoundException e) {
addChildren(ss, builder, openTracingSpansQuark, traceQuarkId, logsQuarks);
return;
}
addUstChildren(ss, builder, openTracingSpansQuark, ustSpansQuark, traceQuarkId, logsQuarks);
addUstChildren(ss, builder, ustSpansQuark, traceQuarkId, logsQuarks);
}

private void addChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryModel> builder, int quark, long parentId, List<Integer> logsQuarks) {
Expand All @@ -280,8 +274,8 @@ private void addChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryMode
}
}

private void addUstChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryModel> builder, int openTracingQuark, int ustQuark, long parentId, List<Integer> logsQuarks) {
for (Integer child : ss.getSubAttributes(openTracingQuark, false)) {
private void addUstChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryModel> builder, int ustQuark, long parentId, List<Integer> logsQuarks) {
for (Integer child : ss.getSubAttributes(ustQuark, false)) {
String childName = ss.getAttributeName(child);

List<LogEvent> logs = new ArrayList<>();
Expand All @@ -293,6 +287,7 @@ private void addUstChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryM
}
}
} catch (IndexOutOfBoundsException | TimeRangeException | StateSystemDisposedException e) {
// do nothing. It will fail later
}

String spanId = getSpanId(childName);
Expand All @@ -305,7 +300,6 @@ private void addUstChildren(ITmfStateSystem ss, Builder<@NonNull TimeGraphEntryM
}
long childId = getId(ustSpan);
builder.add(new SpanLifeEntryModel(childId, parentId, Collections.singletonList(getSpanName(childName)), ss.getStartTime(), ss.getCurrentEndTime(), logs, getErrorTag(childName), getProcessName(childName)));
addUstChildren(ss, builder, child, ustQuark, childId, logsQuarks);
}
}

Expand All @@ -327,12 +321,21 @@ private static String getSpanName(String attributeName) {

private static String getSpanId(String attributeName) {
String[] attributeInfo = attributeName.split("/"); //$NON-NLS-1$
return attributeInfo[attributeInfo.length - 3];
int length = attributeInfo.length;
if (length == 1) {
return attributeInfo[0];
}
return attributeInfo[length - 3];
}

private static Boolean getErrorTag(String attributeName) {
String[] attributeInfo = attributeName.split("/"); //$NON-NLS-1$
return attributeInfo[attributeInfo.length - 2].equals("true"); //$NON-NLS-1$
int length = attributeInfo.length;
String attribute = attributeInfo[0];
if (length >= 3) {
attribute = attributeInfo[length - 2];
}
return attribute.equals("true");//$NON-NLS-1$
}

private static String getProcessName(String attributeName) {
Expand Down
Expand Up @@ -45,6 +45,7 @@ public class SpanLifeStateProvider extends AbstractTmfStateProvider {
public static final String UST_ATTRIBUTE = "ustSpans"; //$NON-NLS-1$

private final Map<String, Integer> fSpanMap;
private final Map<String, Integer> fSpanQuarkMap;

private final Map<String, BiConsumer<ITmfEvent, ITmfStateSystemBuilder>> fHandlers;

Expand All @@ -58,6 +59,7 @@ public SpanLifeStateProvider(ITmfTrace trace) {
super(trace, SpanLifeAnalysis.ID);
fSpanMap = new HashMap<>();
fHandlers = new HashMap<>();
fSpanQuarkMap = new HashMap<>();
fHandlers.put("OpenTracingSpan", this::handleSpan); //$NON-NLS-1$
fHandlers.put("jaeger_ust:start_span", this::handleStart); //$NON-NLS-1$
fHandlers.put("jaeger_ust:end_span", this::handleEnd); //$NON-NLS-1$
Expand Down Expand Up @@ -152,22 +154,29 @@ private void handleStart(ITmfEvent event, ITmfStateSystemBuilder ss) {
String spanId = event.getContent().getFieldValue(String.class, "span_id"); //$NON-NLS-1$
spanId = Long.toHexString(Long.decode(spanId));
int spanQuark = ss.getQuarkRelativeAndAdd(ustSpansQuark, spanId);

fSpanQuarkMap.put(spanId, spanQuark);
long timestamp = event.getTimestamp().toNanos();
String name = event.getContent().getFieldValue(String.class, "op_name"); //$NON-NLS-1$
ss.modifyAttribute(timestamp, name, spanQuark);
}

private void handleEnd(ITmfEvent event, ITmfStateSystemBuilder ss) {
String traceId = event.getContent().getFieldValue(String.class, "trace_id_low"); //$NON-NLS-1$
traceId = Long.toHexString(Long.decode(traceId));
int traceQuark = ss.getQuarkAbsoluteAndAdd(traceId);

int ustSpansQuark = ss.getQuarkRelativeAndAdd(traceQuark, UST_ATTRIBUTE);

String spanId = event.getContent().getFieldValue(String.class, "span_id"); //$NON-NLS-1$
if (spanId == null) {
return;
}
spanId = Long.toHexString(Long.decode(spanId));
int spanQuark = ss.getQuarkRelativeAndAdd(ustSpansQuark, spanId);
Integer spanQuark = fSpanQuarkMap.remove(spanId);
if (spanQuark == null) {
if (traceId == null) {
return;
}
traceId = Long.toHexString(Long.decode(traceId));
int traceQuark = ss.getQuarkAbsoluteAndAdd(traceId);
int ustSpansQuark = ss.getQuarkRelativeAndAdd(traceQuark, UST_ATTRIBUTE);
spanQuark = ss.getQuarkRelativeAndAdd(ustSpansQuark, spanId);
}

long timestamp = event.getTimestamp().toNanos();
ss.modifyAttribute(timestamp, (Object) null, spanQuark);
Expand Down

0 comments on commit 8586778

Please sign in to comment.