-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce TraceContextHolder, unifying context propagation (#417)
Introducing `TraceContextHolder` in the internal API as a common superclass of both `TraceContext` and `AbstractSpan`. Given an instance of this class, you can create child spans, capture exceptions and manage activations/scopes. This abstraction reliefs clients from having to differ between the case when the current activation is a `TraceContext` vs an `AbstractSpan`. A `TraceContext` would be active when the current thread does not own the lifecycle of the parent span. Otherwise an `AbstractSpan` would be active. There is rarely a case where a client would need to get the currently active span to set the name or tags. This propagation is normally done with `@Advice.Local` variables from enter to exit advices. Should the need for that arise however, and the client can ensure that the current activation is actually an `AbstractSpan`, all they need to do is to cast `tracer.getActive()` from `TraceContextHolder` to `AbstractSpan`. The public API does not differ between `Span` and `TraceContextHolder`. `ElasticApm.currentSpan()` always returns a `Span`. In case the current activation is a `TraceContextHolder`, methods like `Span#setTag` are silent noops. Calling those methods on a `Span` whose lifecycle is not owned by the caller is illegal anyway. However, calling `ElasticApm.currentSpan().createSpan()` is always allowed. Taking away `ElasticApm.currentSpan()` is not an option, as a common use case is to rename the spans created by the auto instrumentation or to add custom tags. Also, `ElasticApm.currentSpan().createSpan()` makes for a much nicer and more object-oriented API than starting a span on a tracer, passing in the parent as a start option. In preparation of #145
- Loading branch information
1 parent
5beca3b
commit b1c30b6
Showing
44 changed files
with
448 additions
and
427 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
50 changes: 50 additions & 0 deletions
50
apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ActivationListener.java
This file contains 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,50 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.impl; | ||
|
||
import co.elastic.apm.agent.impl.transaction.AbstractSpan; | ||
import co.elastic.apm.agent.impl.transaction.TraceContextHolder; | ||
|
||
/** | ||
* A callback for {@link TraceContextHolder} activation and deactivaiton events | ||
*/ | ||
public interface ActivationListener { | ||
|
||
void init(ElasticApmTracer tracer); | ||
|
||
/** | ||
* A callback for {@link TraceContextHolder#activate()} | ||
* | ||
* @param context the {@link TraceContextHolder} which is being activated | ||
* @throws Throwable if there was an error while calling this method | ||
*/ | ||
void onActivate(TraceContextHolder<?> context) throws Throwable; | ||
|
||
/** | ||
* A callback for {@link TraceContextHolder#deactivate()} | ||
* <p> | ||
* Note: the corresponding span may already be {@link AbstractSpan#end() ended} and {@link AbstractSpan#resetState() recycled}. | ||
* That's why there is no {@link TraceContextHolder} parameter. | ||
* </p> | ||
* | ||
* @throws Throwable if there was an error while calling this method | ||
*/ | ||
void onDeactivate() throws Throwable; | ||
} |
This file contains 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 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 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
31 changes: 0 additions & 31 deletions
31
apm-agent-core/src/main/java/co/elastic/apm/agent/impl/SpanListener.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.