-
Notifications
You must be signed in to change notification settings - Fork 650
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
[WIP] Context Prop #325
Closed
Closed
[WIP] Context Prop #325
Changes from all commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
5f84c2b
stopgap PR to start the discussion on context propagation.
toumorokoshi 6bea6ec
Merge remote-tracking branch 'origin/master' into context-prop
bbb583d
adding Baggage API
c3f408b
no-op implementations of baggage injector/extractor
8ec6106
cleaning up baggage prop interface
9788f69
make extract/inject static
6a46a25
rename DistributedContext to CorrelationContext
5ef40df
adding ContextKeys for trace and distributedcontext
0442f29
moving api/propagators -> api/propagation. adding more to context api
622d787
add from_context/with_span_context helpers
317e937
splitting propagator into extractor/injector
64cfe9b
checkpoint checkin, this PR will not leave draft mode
22a5c64
implement context scoping
375b78e
minor cleanup
5ca5328
clean up example code
b475933
pass context where needed
c4829c4
removing baggage module
c7610fa
small lint improvements
7489eb5
more lint fixes
a4c4150
fixing a few more lint issues
0946846
fixing inject/extract signatures and tracecontext tests
033e27e
fixing tests
cc813bb
rename current -> snapshot
7391372
fix remaining sdk tests
fa6d437
small context cleanup, return obj, remove unused __getattr__
5fa8e02
store both current span and extracted span context
164ef68
test cleanup
b37c3b0
fix ot shim tests
4ddac90
fixing http_requests tests
f500132
refactoring common getter/setter for propagation
93e88de
add convenience methods to set/get span from context
72c0dbd
fix wsgi and flask tests
f3c8076
add parameter to extract to support custom getters
d82e4c5
changing to copy for now, still need better mechanism here
8927455
enable wsgi/request in example
a4b7d0c
fix tracecontext tests
3b8bf5d
move distributedcontext to correlationcontext
9efb6e4
lint fixes
5272bed
add default injector/extractor. move tracecontext propagator to sdk
ea0905c
use default propagator to avoid installing sdk
4c2c4de
moving context implementation to sdk
1447d7f
tests fixed after context move
1cd03c5
lint fixes
7c9597c
Revert "lint fixes"
4ca46d9
Revert "tests fixed after context move"
48c2a7d
Revert "moving context implementation to sdk"
c7130a1
lint fix
5169723
fix tracecontext tests
673224c
mypy cleanup
4f008a4
Merge 'origin/master' into context-prop
66d67b8
fix context prop example
4442a04
mypy cleanup. adding some ignores for now, will review later
17bb4b1
adding context merge method
b82dc78
adding documentation
b850f99
Adding tests for concurrency, fixing context
33a5b78
Clean up tests and context
be91061
rename from_context to span_context_from_context
f84c4d3
Fix example
b1ba228
Context cleanup
c3906ea
Revert behaviour of no context to INVALID_SPAN_CONTEXT
8d0b142
More refactors of Context
6965c33
Removing context correlation propagators
c129e82
Lint changes
0a6c385
Adding test and fixing restore behaviour
57ad2d2
adding restore test
cfdfc62
Pass context to set_value
257627c
Rename HTTPInjector HTTPExtractor to Injector Extractor
b210df8
Moving propagation api into propagation/__init__.py
73dbfab
Fix example
b9be7bd
Context refactor
d70a47c
Adding set_in_carrier parameter, updating docs
1e3ee56
rename dctx_api
47f521f
Gutting Context class
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
98 changes: 98 additions & 0 deletions
98
...es/opentelemetry-example-app/src/opentelemetry_example_app/context_propagation_example.py
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,98 @@ | ||
# Copyright 2019, OpenTelemetry Authors | ||
# | ||
# 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. | ||
# | ||
""" | ||
This module serves as an example for baggage, which exists | ||
to pass application-defined key-value pairs from service to service. | ||
""" | ||
# import opentelemetry.ext.http_requests | ||
# from opentelemetry.ext.wsgi import OpenTelemetryMiddleware | ||
|
||
import flask | ||
import requests | ||
from flask import request | ||
|
||
import opentelemetry.ext.http_requests | ||
from opentelemetry import propagation, trace | ||
from opentelemetry.correlationcontext import CorrelationContextManager | ||
from opentelemetry.ext.wsgi import OpenTelemetryMiddleware | ||
from opentelemetry.sdk.context.propagation import b3_format | ||
from opentelemetry.sdk.trace import TracerSource | ||
from opentelemetry.sdk.trace.export import ( | ||
BatchExportSpanProcessor, | ||
ConsoleSpanExporter, | ||
) | ||
|
||
|
||
def configure_opentelemetry(flask_app: flask.Flask): | ||
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) | ||
trace.tracer_source().add_span_processor( | ||
BatchExportSpanProcessor(ConsoleSpanExporter()) | ||
) | ||
|
||
# Global initialization | ||
(b3_extractor, b3_injector) = b3_format.http_propagator() | ||
# propagation.set_http_extractors([b3_extractor, baggage_extractor]) | ||
# propagation.set_http_injectors([b3_injector, baggage_injector]) | ||
propagation.set_http_extractors([b3_extractor]) | ||
propagation.set_http_injectors([b3_injector]) | ||
|
||
opentelemetry.ext.http_requests.enable(trace.tracer_source()) | ||
flask_app.wsgi_app = OpenTelemetryMiddleware(flask_app.wsgi_app) | ||
|
||
|
||
def fetch_from_service_b() -> str: | ||
with trace.tracer_source().get_tracer(__name__).start_as_current_span( | ||
"fetch_from_service_b" | ||
): | ||
# Inject the contexts to be propagated. Note that there is no direct | ||
# reference to tracing or baggage. | ||
headers = {"Accept": "text/html"} | ||
propagation.inject(headers) | ||
resp = requests.get("https://opentelemetry.io", headers=headers) | ||
return resp.text | ||
|
||
|
||
def fetch_from_service_c() -> str: | ||
with trace.tracer_source().get_tracer(__name__).start_as_current_span( | ||
"fetch_from_service_c" | ||
): | ||
# Inject the contexts to be propagated. Note that there is no direct | ||
# reference to tracing or baggage. | ||
headers = {"Accept": "application/json"} | ||
propagation.inject(headers) | ||
resp = requests.get("https://opentelemetry.io", headers=headers) | ||
return resp.text | ||
|
||
codeboten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
app = flask.Flask(__name__) | ||
|
||
|
||
@app.route("/") | ||
def hello(): | ||
tracer = trace.tracer_source().get_tracer(__name__) | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# extract a baggage header | ||
propagation.extract(request.headers) | ||
|
||
with tracer.start_as_current_span("service-span"): | ||
with tracer.start_as_current_span("external-req-span"): | ||
version = CorrelationContextManager.correlation("version") | ||
if version == "2.0": | ||
return fetch_from_service_c() | ||
return fetch_from_service_b() | ||
|
||
|
||
if __name__ == "__main__": | ||
configure_opentelemetry(app) | ||
app.run(debug=True) |
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
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
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.
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 whole function looks good to me.