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

Conditionally create server spans for falcon #867

Merged
merged 8 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 @@ -108,8 +108,8 @@ def response_hook(span, req, resp):
)
from opentelemetry.instrumentation.utils import (
extract_attributes_from_object,
get_token_context_span_kind,
http_status_to_status_code,
start_internal_or_server_span,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status
Expand Down Expand Up @@ -195,15 +195,12 @@ def __call__(self, env, start_response):

start_time = _time_ns()

token, ctx, span_kind = get_token_context_span_kind(
env, getter=otel_wsgi.wsgi_getter
)

span = self._tracer.start_span(
otel_wsgi.get_default_span_name(env),
context=ctx,
kind=span_kind,
span, token = start_internal_or_server_span(
tracer=self._tracer,
span_name=otel_wsgi.get_default_span_name(env),
start_time=start_time,
env=env,
context_getter=otel_wsgi.wsgi_getter,
)

if span.is_recording():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,40 @@ def unwrap(obj, attr: str):
setattr(obj, attr, func.__wrapped__)


def get_token_context_span_kind(env, getter):
"""Based on presence of active span, extracts context and initializes token and span_kind
def start_internal_or_server_span(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private function so prefix with _

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

tracer, span_name, start_time, env, context_getter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env should be called context

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to context_carrier as context is already imported module and context is extracted from env object.
Let me know if you want me to rename it.

):
"""Returns internal or server span along with the token which can be used by caller to reset context


Args:
tracer : tracer in use by given instrumentation library
name (string): name of the span
start_time : start time of the span
env : object which contains values that are
used to construct a Context. This object
must be paired with an appropriate getter
which understands how to extract a value from it.
getter : an object which contains a get function that can retrieve zero
context_getter : an object which contains a get function that can retrieve zero
or more values from the carrier and a keys function that can get all the keys
from carrier.
"""

token = ctx = span_kind = None

if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(env, getter=getter)
ctx = extract(env, getter=context_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER
else:
ctx = context.get_current()
span_kind = trace.SpanKind.INTERNAL

return token, ctx, span_kind
span = tracer.start_span(
name=span_name,
context=ctx,
kind=span_kind,
start_time=start_time,
)

return span, token