forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_handler.py
More file actions
51 lines (40 loc) · 1.7 KB
/
request_handler.py
File metadata and controls
51 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright The 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.
from opentracing.ext import tags
# pylint: disable=import-error
from ..utils import get_logger # noqa: TID252
logger = get_logger(__name__)
class RequestHandler:
def __init__(self, tracer, context=None, ignore_active_span=True):
self.tracer = tracer
self.context = context
self.ignore_active_span = ignore_active_span
def before_request(self, request, request_context):
logger.info("Before request %s", request)
# If we should ignore the active Span, use any passed SpanContext
# as the parent. Else, use the active one.
if self.ignore_active_span:
span = self.tracer.start_span(
"send", child_of=self.context, ignore_active_span=True
)
else:
span = self.tracer.start_span("send")
span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT)
request_context["span"] = span
def after_request(self, request, request_context):
# pylint: disable=no-self-use
logger.info("After request %s", request)
span = request_context.get("span")
if span is not None:
span.finish()