-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Utility to create spans for synchronous blocks of code #109467
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.telemetry; | ||
|
|
||
| import org.elasticsearch.common.UUIDs; | ||
| import org.elasticsearch.core.Releasable; | ||
| import org.elasticsearch.telemetry.tracing.Traceable; | ||
| import org.elasticsearch.telemetry.tracing.Tracer; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class Trace { | ||
|
|
||
| public static Releasable trace(ThreadPool threadPool, Tracer tracer, String name) { | ||
| return trace(threadPool, tracer, name, Map.of()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new trace for a synchronous block of code. | ||
| * @return a span that needs to be released once block of code is completed | ||
| */ | ||
| public static Releasable trace(ThreadPool threadPool, Tracer tracer, String name, Map<String, Object> attributes) { | ||
| if (tracer.isEnabled() == false) { | ||
| return () -> {}; | ||
| } | ||
| var span = Span.create(); | ||
| var ctx = threadPool.getThreadContext().newTraceContext(); | ||
| tracer.startTrace(threadPool.getThreadContext(), span, name, attributes); | ||
|
Comment on lines
+34
to
+35
Contributor
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. First off: this is great and allows to create sub-spans of the current thread context very easily. However, I wonder if we should use It might be sufficient to copy the logic from This would allow create interleaved traces in the same thread which would provide more flexibility; my use case for this is seeing when individual ESQL operators start and stop, even though they are executed concurrently on the same thread.
Contributor
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. I am worried about the use of threadpool & threadcontext here.
Contributor
Author
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.
Not sure I follow,
Contributor
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.
as part of a method signature. |
||
|
|
||
| return () -> { | ||
| tracer.stopTrace(span); | ||
| ctx.restore(); | ||
| }; | ||
| } | ||
|
|
||
| private record Span(String spanId) implements Traceable { | ||
|
|
||
| @Override | ||
| public String getSpanId() { | ||
| return spanId; | ||
| } | ||
|
|
||
| public static Span create() { | ||
| return new Span(UUIDs.randomBase64UUID()); | ||
| } | ||
| } | ||
| } | ||
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.
so I am planning to move the telemetry package into its own
:libthis import will make it impossible.