-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Add Traceable interface #80788
Add Traceable interface #80788
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ | |
import org.elasticsearch.common.component.AbstractLifecycleComponent; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
import org.elasticsearch.plugins.TracingPlugin; | ||
import org.elasticsearch.tasks.Task; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
@@ -34,7 +33,7 @@ public class APMTracer extends AbstractLifecycleComponent implements TracingPlug | |
|
||
public static final CapturingSpanExporter CAPTURING_SPAN_EXPORTER = new CapturingSpanExporter(); | ||
|
||
private final Map<Long, Span> taskSpans = ConcurrentCollections.newConcurrentMap(); | ||
private final Map<String, Span> spans = ConcurrentCollections.newConcurrentMap(); | ||
|
||
private volatile Tracer tracer; | ||
|
||
|
@@ -60,20 +59,37 @@ protected void doStop() {} | |
protected void doClose() {} | ||
|
||
@Override | ||
public void onTaskRegistered(Task task) { | ||
public void onRegistered(TracingPlugin.Traceable traceable) { | ||
final Tracer tracer = this.tracer; | ||
if (tracer != null) { | ||
taskSpans.computeIfAbsent(task.getId(), taskId -> { | ||
final Span span = tracer.spanBuilder(task.getAction()).startSpan(); | ||
span.setAttribute("es.task.id", task.getId()); | ||
spans.computeIfAbsent(traceable.getSpanId(), spanId -> { | ||
final Span span = tracer.spanBuilder(traceable.getSpanName()).startSpan(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When setting attributes while creating the span, it's common to use SpanBuilder spanBuilder = tracer.spanBuilder(traceable.getSpanName());
for (Map.Entry<String, Object> entry : traceable.getAttributes().entrySet()) {
Object value = entry.getValue();
if (value instanceof String) {
spanBuilder.setAttribute(entry.getKey(), (String) value);
} else if (value instanceof Long) {
spanBuilder.setAttribute(entry.getKey(), (Long) value);
} else if (value instanceof Integer) {
spanBuilder.setAttribute(entry.getKey(), (Integer) value);
} else if (value instanceof Double) {
spanBuilder.setAttribute(entry.getKey(), (Double) value);
} else if (value instanceof Boolean) {
spanBuilder.setAttribute(entry.getKey(), (Boolean) value);
} else {
throw new IllegalArgumentException(
"span attributes do not support value type of [" + value.getClass().getCanonicalName() + "]"
);
}
return spanBuilder.startSpan(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks I updated as suggested. |
||
for (Map.Entry<String, Object> entry : traceable.getAttributes().entrySet()) { | ||
final Object value = entry.getValue(); | ||
if (value instanceof String) { | ||
span.setAttribute(entry.getKey(), (String) value); | ||
} else if (value instanceof Long) { | ||
span.setAttribute(entry.getKey(), (Long) value); | ||
} else if (value instanceof Integer) { | ||
span.setAttribute(entry.getKey(), (Integer) value); | ||
} else if (value instanceof Double) { | ||
span.setAttribute(entry.getKey(), (Double) value); | ||
} else if (value instanceof Boolean) { | ||
span.setAttribute(entry.getKey(), (Boolean) value); | ||
} else { | ||
throw new IllegalArgumentException( | ||
"span attributes do not support value type of [" + value.getClass().getCanonicalName() + "]" | ||
); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not pretty. But it does the job for now and keep other parts unware of the opentelemetry dependencies. We can improve it once we know more about what other things needed by the new |
||
return span; | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onTaskUnregistered(Task task) { | ||
final Span span = taskSpans.remove(task.getId()); | ||
public void onUnregistered(TracingPlugin.Traceable traceable) { | ||
final Span span = spans.remove(traceable.getSpanId()); | ||
if (span != null) { | ||
span.end(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe registered/unregistered is still related to the task naming.
Should generic methods be called
onSpanStarted
/onStarted
or something similar?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I changed the method names to
onTraceStarted
andonTraceStopped
.