-
Notifications
You must be signed in to change notification settings - Fork 42
Unregister Stalled Traces #2320
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
Closed
Closed
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
a0d487f
Add TraceRegistry interface.
tduncan e4c0258
Have TraceRegistry and its extenders implement the ITraceRegistry int…
tduncan 0f35d76
Replace references to TraceRegistry with the ITraceRegistry interface.
tduncan 2b0700a
Remove TraceRegistry inheritence from TogglableTraceRegistry.
tduncan 2600c75
Remove TraceRegistry inheritence from RecordingTraceRegistry.
tduncan 32c296e
Rename TraceRegistry to SimpleTraceRegistry.
tduncan 466173f
Rename ITraceRegistry to TraceRegistry.
tduncan 7602fe7
Add @Override annotations in SimpleTraceRegistry.
tduncan 60dc92e
Add basic TraceRegistry implementation for unregistering stalled traces.
tduncan a52393c
Add test verifying that stalled traces are continually detected.
tduncan d06cf23
Stop stack trace sampling when stalled trace is automatically unregis…
tduncan 88e7681
Add configuration property for snapshot profiling stalled trace timeout.
tduncan c818959
Expand the TraceRegistry interface extend AutoCloseable.
tduncan 4e170f2
Unregister all traces when closed.
tduncan 099096e
Close encapsulated TraceRegistry delegate when StalledTraceDetectingT…
tduncan 43db5f5
Define a noop TraceRegistry and ConfigurableSupplier.
tduncan 4924bb2
Access TraceRegistry via a ConfigurableSupplier.
tduncan b6e8b76
Use a daemon thread rather than a ScheduledExecutorService for detect…
tduncan a534305
Apply spotless code formatting.
tduncan 8858d7a
If if/else rather than switch.
tduncan 1054b75
Close TraceRegistry when OpenTelemetry SDK shutdown.
tduncan af1beab
Detect orphaned traces using WeakReferences and a ReferenceQueue rath…
tduncan 221b5d6
Introduce the TraceProfilingContext in ScheduledExecutorStackTraceSam…
tduncan 0a5e890
Rename class.
tduncan e805635
Extract inner class to first class citizen.
tduncan a9bce31
Use ProfilingSpanContext reference within ScheduledExecutorStackTrace…
tduncan c373d11
Use ProfilingSpanContext in ActiveSpanTracker.
tduncan d462bdc
Define an invalid ProfilingSpanContext.
tduncan 204e9ab
Add test requiring that stack trace sampling be stopped when an orpha…
tduncan c8fac10
Apply spotless code formatting.
tduncan 9b229d7
Update javadoc comment.
tduncan 0cc5af7
Avoid creating weak references to a SpanContext when a trace is being…
tduncan 8d4b2ad
Add missing equals method to correctly remove entries from the set in…
tduncan 87db84a
Verify that sampling starts and is later stopped when an orphaned spa…
tduncan 77679ff
Apply spotless code formatting.
tduncan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
.../java/com/splunk/opentelemetry/profiler/snapshot/OrphanedTraceDetectingTraceRegistry.java
tduncan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* 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.splunk.opentelemetry.profiler.snapshot; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.lang.ref.WeakReference; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
class OrphanedTraceDetectingTraceRegistry implements TraceRegistry { | ||
private final Set<Key> traces = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
private final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>(); | ||
|
||
private final TraceRegistry delegate; | ||
private final Supplier<StackTraceSampler> sampler; | ||
private final Thread thread; | ||
|
||
OrphanedTraceDetectingTraceRegistry(TraceRegistry delegate, Supplier<StackTraceSampler> sampler) { | ||
this.delegate = delegate; | ||
this.sampler = sampler; | ||
|
||
thread = new Thread(this::unregisterOrphanedTraces); | ||
thread.setName("orphaned-trace-detector"); | ||
thread.setDaemon(true); | ||
thread.start(); | ||
} | ||
|
||
@Override | ||
public void register(SpanContext spanContext) { | ||
delegate.register(spanContext); | ||
traces.add(new WeakSpanContext(spanContext, referenceQueue)); | ||
} | ||
|
||
@Override | ||
public boolean isRegistered(SpanContext spanContext) { | ||
return delegate.isRegistered(spanContext); | ||
} | ||
|
||
@Override | ||
public void unregister(SpanContext spanContext) { | ||
delegate.unregister(spanContext); | ||
traces.remove(new LookupKey(spanContext)); | ||
tduncan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void unregisterOrphanedTraces() { | ||
while (!Thread.interrupted()) { | ||
try { | ||
Object reference = referenceQueue.remove(); | ||
if (reference != null) { | ||
Key key = (Key) reference; | ||
traces.remove(key); | ||
delegate.unregister(key.getSpanContext()); | ||
sampler.get().stop(key.getSpanContext()); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
thread.interrupt(); | ||
} | ||
|
||
interface Key { | ||
SpanContext getSpanContext(); | ||
} | ||
|
||
private static class LookupKey implements Key { | ||
private final SpanContext spanContext; | ||
|
||
private LookupKey(SpanContext spanContext) { | ||
this.spanContext = spanContext; | ||
} | ||
|
||
@Override | ||
public SpanContext getSpanContext() { | ||
return spanContext; | ||
} | ||
} | ||
|
||
private static class WeakSpanContext extends WeakReference<SpanContext> implements Key { | ||
private final SpanContext spanContext; | ||
|
||
public WeakSpanContext(SpanContext referent, ReferenceQueue<Object> q) { | ||
super(referent, q); | ||
this.spanContext = | ||
SpanContext.create( | ||
referent.getTraceId(), | ||
referent.getSpanId(), | ||
referent.getTraceFlags(), | ||
referent.getTraceState()); | ||
} | ||
|
||
@Override | ||
public SpanContext getSpanContext() { | ||
return spanContext; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(spanContext); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
WeakSpanContext that = (WeakSpanContext) o; | ||
return Objects.equals(spanContext, that.spanContext); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
profiler/src/main/java/com/splunk/opentelemetry/profiler/snapshot/ProfilingSpanContext.java
tduncan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* 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.splunk.opentelemetry.profiler.snapshot; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import java.util.Objects; | ||
|
||
class ProfilingSpanContext { | ||
static final ProfilingSpanContext INVALID = from(SpanContext.getInvalid()); | ||
|
||
static ProfilingSpanContext from(SpanContext spanContext) { | ||
return new ProfilingSpanContext(spanContext.getTraceId(), spanContext.getSpanId()); | ||
} | ||
|
||
private final String traceId; | ||
private final String spanId; | ||
|
||
private ProfilingSpanContext(String traceId, String spanId) { | ||
this.traceId = traceId; | ||
this.spanId = spanId; | ||
} | ||
|
||
public String getTraceId() { | ||
return traceId; | ||
} | ||
|
||
public String getSpanId() { | ||
return spanId; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(traceId, spanId); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ProfilingSpanContext that = (ProfilingSpanContext) o; | ||
return Objects.equals(traceId, that.traceId) && Objects.equals(spanId, that.spanId); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.