Skip to content

Commit

Permalink
changing the getter function with getter objects in instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nprajilesh committed Oct 9, 2020
1 parent b0de4f8 commit c1e203d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from opentelemetry.instrumentation.wsgi import (
add_response_attributes,
collect_request_attributes,
get_header_from_environ,
getter,
)
from opentelemetry.propagators import extract
from opentelemetry.trace import SpanKind, get_tracer
Expand Down Expand Up @@ -98,7 +98,7 @@ def process_request(self, request):

environ = request.META

token = attach(extract(get_header_from_environ, environ))
token = attach(extract(getter, environ))

tracer = get_tracer(__name__, __version__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def __call__(self, env, start_response):

start_time = time_ns()

token = context.attach(
propagators.extract(otel_wsgi.get_header_from_environ, env)
)
token = context.attach(propagators.extract(otel_wsgi.getter, env))
attributes = otel_wsgi.collect_request_attributes(env)
span = self._tracer.start_span(
otel_wsgi.get_default_span_name(env),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def _before_request():
span_name = flask.request.endpoint or otel_wsgi.get_default_span_name(
environ
)
token = context.attach(
propagators.extract(otel_wsgi.get_header_from_environ, environ)
)
token = context.attach(propagators.extract(otel_wsgi.getter, environ))

tracer = trace.get_tracer(__name__, __version__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ def _before_traversal(event):

start_time = environ.get(_ENVIRON_STARTTIME_KEY)

token = context.attach(
propagators.extract(otel_wsgi.get_header_from_environ, environ)
)
token = context.attach(propagators.extract(otel_wsgi.getter, environ))
tracer = trace.get_tracer(__name__, __version__)

if request.matched_route:
Expand Down
21 changes: 17 additions & 4 deletions opentelemetry-api/src/opentelemetry/trace/propagation/textmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import abc
import typing
from typing import Optional

from opentelemetry.context.context import Context

Expand All @@ -22,6 +23,20 @@
Setter = typing.Callable[[TextMapPropagatorT, str, str], None]


def default_get(carrier: TextMapPropagatorT, key: str) -> typing.List[str]:
return [carrier.get(key)] if carrier.get(key) else []


def default_keys(carrier: TextMapPropagatorT) -> typing.List[str]:
return list(carrier.keys())


GetterGetFunction = typing.Callable[
[TextMapPropagatorT, str], typing.List[str]
]
GetterKeysFunction = typing.Callable[[TextMapPropagatorT], typing.List[str]]


class Getter:
"""This class implements a Getter that enables extracting propagated
fields from a carrier
Expand All @@ -30,10 +45,8 @@ class Getter:

def __init__(
self,
get=lambda carrier, key: [carrier.get(key)]
if carrier.get(key)
else [],
keys=lambda carrier: list(carrier.keys()),
get: GetterGetFunction[TextMapPropagatorT] = default_get,
keys: GetterKeysFunction[TextMapPropagatorT] = default_keys,
):
self._get = get
self._keys = keys
Expand Down

0 comments on commit c1e203d

Please sign in to comment.