-
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
[WIP] Context Prop #325
Changes from 14 commits
5f84c2b
6bea6ec
bbb583d
c3f408b
8ec6106
9788f69
6a46a25
5ef40df
0442f29
622d787
317e937
64cfe9b
22a5c64
375b78e
5ca5328
b475933
c4829c4
c7610fa
7489eb5
a4c4150
0946846
033e27e
cc813bb
7391372
fa6d437
5fa8e02
164ef68
b37c3b0
4ddac90
f500132
93e88de
72c0dbd
f3c8076
d82e4c5
8927455
a4b7d0c
3b8bf5d
9efb6e4
5272bed
ea0905c
4c2c4de
1447d7f
1cd03c5
7c9597c
4ca46d9
48c2a7d
c7130a1
5169723
673224c
4f008a4
66d67b8
4442a04
17bb4b1
b82dc78
b850f99
33a5b78
be91061
f84c4d3
b1ba228
c3906ea
8d0b142
6965c33
c129e82
0a6c385
57ad2d2
cfdfc62
257627c
b210df8
73dbfab
b9be7bd
d70a47c
1e3ee56
47f521f
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,101 @@ | ||
# 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 | ||
from flask import request | ||
import requests | ||
|
||
from opentelemetry import propagation, trace | ||
from opentelemetry.distributedcontext import CorrelationContextManager | ||
from opentelemetry.sdk.context.propagation import b3_format | ||
from opentelemetry.sdk.trace import Tracer | ||
from opentelemetry.sdk.trace.export import ( | ||
BatchExportSpanProcessor, | ||
ConsoleSpanExporter, | ||
) | ||
from opentelemetry.baggage import BaggageManager | ||
|
||
|
||
def configure_opentelemetry(flask_app: flask.Flask): | ||
trace.set_preferred_tracer_implementation(lambda T: Tracer()) | ||
|
||
# Global initialization | ||
(baggage_extractor, baggage_injector) = BaggageManager.http_propagator() | ||
(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()) | ||
# flask_app.wsgi_app = OpenTelemetryMiddleware(flask_app.wsgi_app) | ||
|
||
|
||
def fetch_from_service_b() -> str: | ||
# Inject the contexts to be propagated. Note that there is no direct | ||
# reference to tracing or baggage. | ||
headers = {"Accept": "application/json"} | ||
propagation.inject(headers) | ||
print(headers) | ||
resp = requests.get("https://opentelemetry.io", headers=headers) | ||
return resp.text | ||
|
||
|
||
def fetch_from_service_c() -> str: | ||
# Inject the contexts to be propagated. Note that there is no direct | ||
# reference to tracing or baggage. | ||
headers = {"Accept": "application/json"} | ||
propagation.inject(headers) | ||
print(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() | ||
tracer.add_span_processor(BatchExportSpanProcessor(ConsoleSpanExporter())) | ||
with propagation.extract(request.headers): | ||
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. It looks like this differs from the example you put in the description. Is there one that you preferred? In my opinion, this method looks great, and reduces a lot of boilerplate in comparison with the other example posted. 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. right, this is the same as the "Simpler API" example for anyone that doesn't need to use the context explicitly. As I suspect this would be the more common case, I like how much simpler it is. |
||
# extract a baggage header | ||
with tracer.start_as_current_span("service-span"): | ||
with tracer.start_as_current_span("external-req-span"): | ||
headers = {"Accept": "application/json"} | ||
propagation.inject(headers) | ||
version = CorrelationContextManager.value("version") | ||
if version == "2.0": | ||
return fetch_from_service_c() | ||
|
||
return fetch_from_service_b() | ||
|
||
|
||
request_headers = { | ||
"Accept": "application/json", | ||
"x-b3-traceid": "038c3fb613811e30898424c863eeae5a", | ||
"x-b3-spanid": "6c7f9e56212a6ffa", | ||
"x-b3-sampled": "0", | ||
} | ||
|
||
if __name__ == "__main__": | ||
configure_opentelemetry(app) | ||
app.run(debug=True) |
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.