-
Notifications
You must be signed in to change notification settings - Fork 327
Context propagation refactorings #3206
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
Changes from all commits
58b022a
fd45dab
71ca343
8f32172
78a2c8c
0d485db
d9e1fcd
1845d12
da5b3a6
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 |
|---|---|---|
|
|
@@ -69,9 +69,9 @@ Transaction currentTransaction() { | |
| } | ||
|
|
||
| /** | ||
| * @return the currently active context, {@literal null} if there is none. | ||
| * @return the current context, potentially empty when no span, transaction or baggage is currently active. | ||
| */ | ||
| @Nullable | ||
|
|
||
| public ElasticContext<?> currentContext() { | ||
| ElasticContext<?> current = activeContextStack.peek(); | ||
|
|
||
|
|
@@ -80,7 +80,7 @@ public ElasticContext<?> currentContext() { | |
| if (current instanceof ElasticContextWrapper) { | ||
| return ((ElasticContextWrapper<?>) current).getWrappedContext(); | ||
| } | ||
| return current; | ||
| return current != null ? current : EmptyElasticContext.INSTANCE; | ||
| } | ||
|
|
||
| boolean activate(ElasticContext<?> context, List<ActivationListener> activationListeners) { | ||
|
|
@@ -99,9 +99,9 @@ boolean activate(ElasticContext<?> context, List<ActivationListener> activationL | |
| return false; | ||
| } | ||
|
|
||
| context.incrementReferences(); | ||
|
Member
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. here I am not sure why we increment references in the first place, but I think it's because the span reference is being used in the activation listeners, so might be worth checking if that does not mean systematically incrementing the context references count even when not needed.
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. We do increment here to make sure that the |
||
| AbstractSpan<?> span = context.getSpan(); | ||
| if (span != null) { | ||
| span.incrementReferences(); | ||
| triggerActivationListeners(span, true, activationListeners); | ||
| } | ||
|
|
||
|
|
@@ -122,22 +122,15 @@ boolean deactivate(ElasticContext<?> context, List<ActivationListener> activatio | |
| ElasticContext<?> activeContext = currentContext(); | ||
| activeContextStack.remove(); | ||
|
|
||
| AbstractSpan<?> span = context.getSpan(); | ||
|
|
||
| if (activeContext != context && activeContext instanceof ElasticContextWrapper) { | ||
|
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. This unwrapping was unnecessary: |
||
| // when context has been wrapped, we need to get the underlying context | ||
| activeContext = ((ElasticContextWrapper<?>) activeContext).getWrappedContext(); | ||
| } | ||
|
|
||
| try { | ||
| assertIsActive(context, activeContext, assertionsEnabled); | ||
|
|
||
| AbstractSpan<?> span = context.getSpan(); | ||
| if (null != span) { | ||
| triggerActivationListeners(span, false, activationListeners); | ||
| } | ||
| } finally { | ||
| if (null != span) { | ||
| span.decrementReferences(); | ||
| } | ||
| context.decrementReferences(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.impl; | ||
|
|
||
| import co.elastic.apm.agent.impl.transaction.AbstractSpan; | ||
| import co.elastic.apm.agent.impl.transaction.ElasticContext; | ||
| import co.elastic.apm.agent.tracer.Scope; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| class EmptyElasticContext extends ElasticContext<EmptyElasticContext> { | ||
SylvainJuge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| static final ElasticContext<?> INSTANCE = new EmptyElasticContext(); | ||
|
|
||
| private EmptyElasticContext() { | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public AbstractSpan<?> getSpan() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public EmptyElasticContext activate() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public EmptyElasticContext deactivate() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Scope activateInScope() { | ||
| return NoopScope.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public void incrementReferences() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void decrementReferences() { | ||
|
|
||
| } | ||
|
|
||
| } | ||
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.
This change has been to simplify plugins: Instead of returning null when there is no context, we return an empty context. This avoids a lot of null-checks in the plugins.
If one really needs to know wheter a context is active or not, it is possible to do so by calling
ElasticContext.isEmpty(). This is for example useful to optimize intra-process context propagation: No need to propagate empty contexts here.