Skip to content
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

Update instrumentations to use tracer_provider for creating tracer if given, otherwise use global tracer provider #402

Merged
merged 45 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
bb5570c
Update instrumentations to use tracer_provider for creating tracer if…
srikanthccv Apr 4, 2021
ba977dd
Update instrumentations; fix lint
srikanthccv Apr 4, 2021
848a3b0
Merge branch 'main' into issue-401
srikanthccv Apr 4, 2021
9f91e91
Merge remote-tracking branch 'upstream/main' into issue-402
srikanthccv Apr 8, 2021
f908c87
Use same tracer for each call
srikanthccv Apr 8, 2021
f82ccc9
Merge branch 'issue-401' of github.com:lonewolf3739/opentelemetry-pyt…
srikanthccv Apr 8, 2021
c0daf28
Update aiopg
srikanthccv Apr 8, 2021
004e5c5
Fix lint
srikanthccv Apr 9, 2021
9e27104
resolve merge conflicts
srikanthccv Apr 9, 2021
d4bd7fd
Remove old test
srikanthccv Apr 9, 2021
89bd65c
Add test for aiopg inregration
srikanthccv Apr 9, 2021
baa70ec
Add tracer provider test
srikanthccv Apr 9, 2021
e47cdbb
Black format
srikanthccv Apr 9, 2021
7c121df
Remove unused code
srikanthccv Apr 9, 2021
43dc357
Update dbapi tests
srikanthccv Apr 9, 2021
0980c5f
Add django test
srikanthccv Apr 9, 2021
415ba22
Update instrumentations
srikanthccv Apr 9, 2021
3073cf9
Add missing params
srikanthccv Apr 9, 2021
5bf13f9
update instrumentations
srikanthccv Apr 10, 2021
8e5a6df
Update docstring
srikanthccv Apr 10, 2021
6df43ab
Fix lint
srikanthccv Apr 10, 2021
0f3fa3f
Update asgi
srikanthccv Apr 10, 2021
8c27e16
Fix sphnix docs
srikanthccv Apr 13, 2021
791a23c
Add CHANGELOG
srikanthccv Apr 13, 2021
ea5a8a7
Revert tox.ini change
srikanthccv Apr 13, 2021
bf33f17
Invalidate old cache
srikanthccv Apr 13, 2021
5df680a
Revert "Invalidate old cache"
srikanthccv Apr 13, 2021
f3ebf2c
Address review comments
srikanthccv Apr 13, 2021
b5d614b
Resolve merge conflicts
srikanthccv Apr 13, 2021
7ee6846
Recreate tox env
srikanthccv Apr 13, 2021
a025c2e
Resolve merge conflicts
srikanthccv Apr 15, 2021
c537619
Fix lint
srikanthccv Apr 15, 2021
eb23f94
Fix CI
srikanthccv Apr 15, 2021
8e3decc
Remove docstring
srikanthccv Apr 15, 2021
90b62ca
Update docstring
srikanthccv Apr 15, 2021
fc0fb70
Revert test.yml
srikanthccv Apr 15, 2021
04eb555
Revert tox.ini
srikanthccv Apr 15, 2021
d90b2e0
Merge branch 'main' into issue-401
lzchen Apr 19, 2021
25f5035
Resolve merge conflicts
srikanthccv Apr 19, 2021
842ba1a
Merge branch 'issue-401' of github.com:lonewolf3739/opentelemetry-pyt…
srikanthccv Apr 19, 2021
9906fd3
Resolve merge conflicts
srikanthccv Apr 23, 2021
2be24c5
Merge branch 'main' into issue-401
lzchen Apr 26, 2021
42d8351
Fix lint
srikanthccv Apr 27, 2021
5219873
Merge branch 'main' into issue-401
lzchen Apr 27, 2021
66ae5e1
Merge branch 'main' into issue-401
srikanthccv Apr 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,15 @@ class OpenTelemetryMiddleware:
Optional: Defaults to get_default_span_details.
"""

def __init__(self, app, excluded_urls=None, span_details_callback=None):
def __init__(
self,
app,
excluded_urls=None,
span_details_callback=None,
tracer_provider=None,
):
self.app = guarantee_single_callable(app)
self.tracer = trace.get_tracer(__name__, __version__)
self.tracer = trace.get_tracer(__name__, __version__, tracer_provider)
self.span_details_callback = (
span_details_callback or get_default_span_details
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ def __init__(self, capture_parameters=False):
self.capture_parameters = capture_parameters

def _instrument(self, **kwargs):
tracer_provider = kwargs.get(
"tracer_provider", trace.get_tracer_provider()
)
tracer_provider = kwargs.get("tracer_provider")
setattr(
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
asyncpg,
_APPLIED,
tracer_provider.get_tracer("asyncpg", __version__),
trace.get_tracer(__name__, __version__, tracer_provider),
)

for method in [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _instrument(self, **kwargs):
if environ.get(OTEL_PYTHON_DJANGO_INSTRUMENT) == "False":
return

_DjangoMiddleware._tracer_provider = kwargs.get("tracer_provider")

# This can not be solved, but is an inherent problem of this approach:
# the order of middleware entries matters, and here you have no control
# on that:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class _DjangoMiddleware(MiddlewareMixin):

_traced_request_attrs = get_traced_request_attrs("DJANGO")
_excluded_urls = get_excluded_urls("DJANGO")
_tracer_provider = None

@staticmethod
def _get_span_name(request):
Expand Down Expand Up @@ -99,7 +100,11 @@ def process_request(self, request):

token = attach(extract(request_meta, getter=wsgi_getter))

tracer = get_tracer(__name__, __version__)
tracer = get_tracer(
__name__,
__version__,
tracer_provider=_DjangoMiddleware._tracer_provider,
)

span = tracer.start_span(
self._get_span_name(request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,26 @@ class FalconInstrumentor(BaseInstrumentor):

def _instrument(self, **kwargs):
self._original_falcon_api = falcon.API
_InstrumentedFalconAPI._tracer_provider = kwargs.get("tracer_provider")
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
falcon.API = _InstrumentedFalconAPI

def _uninstrument(self, **kwargs):
falcon.API = self._original_falcon_api


class _InstrumentedFalconAPI(falcon.API):
_tracer_provider = None

def __init__(self, *args, **kwargs):
middlewares = kwargs.pop("middleware", [])
if not isinstance(middlewares, (list, tuple)):
middlewares = [middlewares]

self._tracer = trace.get_tracer(__name__, __version__)
self._tracer = trace.get_tracer(
__name__,
__version__,
tracer_provider=_InstrumentedFalconAPI._tracer_provider,
)
trace_middleware = _TraceMiddleware(
self._tracer, kwargs.get("traced_request_attributes")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@ class FastAPIInstrumentor(BaseInstrumentor):
_original_fastapi = None

@staticmethod
def instrument_app(app: fastapi.FastAPI):
def instrument_app(app: fastapi.FastAPI, tracer_provider=None):
"""Instrument an uninstrumented FastAPI application.
"""
if not getattr(app, "is_instrumented_by_opentelemetry", False):
app.add_middleware(
OpenTelemetryMiddleware,
excluded_urls=_excluded_urls,
span_details_callback=_get_route_details,
tracer_provider=tracer_provider,
)
app.is_instrumented_by_opentelemetry = True

def _instrument(self, **kwargs):
self._original_fastapi = fastapi.FastAPI
_InstrumentedFastAPI._tracer_provider = kwargs.get("tracer_provider")
fastapi.FastAPI = _InstrumentedFastAPI

def _uninstrument(self, **kwargs):
fastapi.FastAPI = self._original_fastapi


class _InstrumentedFastAPI(fastapi.FastAPI):
_tracer_provider = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_middleware(
OpenTelemetryMiddleware,
excluded_urls=_excluded_urls,
span_details_callback=_get_route_details,
tracer_provider=_InstrumentedFastAPI._tracer_provider,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _start_response(status, response_headers, *args, **kwargs):
return _wrapped_app


def _wrapped_before_request(name_callback):
def _wrapped_before_request(name_callback, tracer_provider=None):
def _before_request():
if _excluded_urls.url_disabled(flask.request.url):
return
Expand All @@ -120,7 +120,7 @@ def _before_request():
extract(flask_request_environ, getter=otel_wsgi.wsgi_getter)
)

tracer = trace.get_tracer(__name__, __version__)
tracer = trace.get_tracer(__name__, __version__, tracer_provider)

span = tracer.start_span(
span_name,
Expand Down Expand Up @@ -171,6 +171,7 @@ def _teardown_request(exc):
class _InstrumentedFlask(flask.Flask):

name_callback = get_default_span_name
_tracer_provider = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -179,7 +180,8 @@ def __init__(self, *args, **kwargs):
self.wsgi_app = _rewrapped_app(self.wsgi_app)

_before_request = _wrapped_before_request(
_InstrumentedFlask.name_callback
_InstrumentedFlask.name_callback,
tracer_provider=_InstrumentedFlask._tracer_provider,
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
)
self._before_request = _before_request
self.before_request(_before_request)
Expand All @@ -196,12 +198,14 @@ class FlaskInstrumentor(BaseInstrumentor):
def _instrument(self, **kwargs):
self._original_flask = flask.Flask
name_callback = kwargs.get("name_callback")
tracer_provider = kwargs.get("tracer_provider")
if callable(name_callback):
_InstrumentedFlask.name_callback = name_callback
_InstrumentedFlask._tracer_provider = tracer_provider
flask.Flask = _InstrumentedFlask

def instrument_app(
self, app, name_callback=get_default_span_name
self, app, name_callback=get_default_span_name, tracer_provider=None
): # pylint: disable=no-self-use
if not hasattr(app, "_is_instrumented"):
app._is_instrumented = False
Expand All @@ -210,7 +214,9 @@ def instrument_app(
app._original_wsgi_app = app.wsgi_app
app.wsgi_app = _rewrapped_app(app.wsgi_app)

_before_request = _wrapped_before_request(name_callback)
_before_request = _wrapped_before_request(
name_callback, tracer_provider=tracer_provider
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
)
app._before_request = _before_request
app.before_request(_before_request)
app.teardown_request(_teardown_request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ class GrpcInstrumentorServer(BaseInstrumentor):

def _instrument(self, **kwargs):
self._original_func = grpc.server
tracer_provider = kwargs.get("tracer_provider")

def server(*args, **kwargs):
if "interceptors" in kwargs:
# add our interceptor as the first
kwargs["interceptors"].insert(0, server_interceptor())
kwargs["interceptors"].insert(
0, server_interceptor(tracer_provider=tracer_provider)
)
else:
kwargs["interceptors"] = [server_interceptor()]
kwargs["interceptors"] = [
server_interceptor(tracer_provider=tracer_provider)
]
return self._original_func(*args, **kwargs)

grpc.server = server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ class RedisInstrumentor(BaseInstrumentor):
"""

def _instrument(self, **kwargs):
tracer_provider = kwargs.get(
"tracer_provider", trace.get_tracer_provider()
)
tracer_provider = kwargs.get("tracer_provider")
setattr(
redis,
"_opentelemetry_tracer",
tracer_provider.get_tracer(_DEFAULT_SERVICE, __version__),
trace.get_tracer(
__name__, __version__, tracer_provider=tracer_provider,
),
)

if redis.VERSION < (3, 0, 0):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def _normalize_vendor(vendor):


def _get_tracer(engine, tracer_provider=None):
if tracer_provider is None:
tracer_provider = trace.get_tracer_provider()
return tracer_provider.get_tracer(
_normalize_vendor(engine.name), __version__
return trace.get_tracer(
_normalize_vendor(engine.name),
__version__,
tracer_provider=tracer_provider,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@ class StarletteInstrumentor(BaseInstrumentor):
_original_starlette = None

@staticmethod
def instrument_app(app: applications.Starlette):
def instrument_app(app: applications.Starlette, tracer_provider=None):
"""Instrument an uninstrumented Starlette application.
"""
if not getattr(app, "is_instrumented_by_opentelemetry", False):
app.add_middleware(
OpenTelemetryMiddleware,
excluded_urls=_excluded_urls,
span_details_callback=_get_route_details,
tracer_provider=tracer_provider,
)
app.is_instrumented_by_opentelemetry = True

def _instrument(self, **kwargs):
self._original_starlette = applications.Starlette
_InstrumentedStarlette._tracer_provider = kwargs.get("tracer_provider")
applications.Starlette = _InstrumentedStarlette

def _uninstrument(self, **kwargs):
applications.Starlette = self._original_starlette


class _InstrumentedStarlette(applications.Starlette):
_tracer_provider = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_middleware(
OpenTelemetryMiddleware,
excluded_urls=_excluded_urls,
span_details_callback=_get_route_details,
tracer_provider=_InstrumentedStarlette._tracer_provider,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ class OpenTelemetryMiddleware:
Optional: Defaults to get_default_span_name.
"""

def __init__(self, wsgi, name_callback=get_default_span_name):
def __init__(
self, wsgi, name_callback=get_default_span_name, tracer_provider=None
):
self.wsgi = wsgi
self.tracer = trace.get_tracer(__name__, __version__)
self.tracer = trace.get_tracer(__name__, __version__, tracer_provider)
self.name_callback = name_callback

@staticmethod
Expand Down