|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | + |
| 17 | +from opencensus.trace.ext import utils |
| 18 | +from opencensus.trace.ext.pyramid.config import PyramidTraceSettings |
| 19 | + |
| 20 | +from opencensus.trace import attributes_helper |
| 21 | +from opencensus.trace import execution_context |
| 22 | +from opencensus.trace import tracer as tracer_module |
| 23 | + |
| 24 | + |
| 25 | +HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES['HTTP_METHOD'] |
| 26 | +HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL'] |
| 27 | +HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE'] |
| 28 | + |
| 29 | +_PYRAMID_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT' |
| 30 | + |
| 31 | +BLACKLIST_PATHS = 'BLACKLIST_PATHS' |
| 32 | + |
| 33 | + |
| 34 | +log = logging.getLogger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +class OpenCensusTweenFactory(object): |
| 38 | + """Pyramid tweens are like wsgi middleware, but have access to things |
| 39 | + like the request, response, and application registry. |
| 40 | +
|
| 41 | + The tween factory is a globally importable callable whose |
| 42 | + constructor takes a request handler and application registry. It |
| 43 | + will be called with a pyramid request object. |
| 44 | +
|
| 45 | + For details on pyramid tweens, see |
| 46 | + https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#creating-a-tween |
| 47 | + """ |
| 48 | + def __init__(self, handler, registry): |
| 49 | + """Constructor for the pyramid tween |
| 50 | +
|
| 51 | + :param handler: Either the main Pyramid request handling |
| 52 | + function or another tween |
| 53 | + :type handler: function |
| 54 | + :param registry: The pyramid application registry |
| 55 | + :type registry: :class:`pyramid.registry.Registry` |
| 56 | + """ |
| 57 | + self.handler = handler |
| 58 | + self.registry = registry |
| 59 | + settings = PyramidTraceSettings(registry) |
| 60 | + |
| 61 | + self.sampler = settings.SAMPLER |
| 62 | + self.exporter = settings.EXPORTER |
| 63 | + self.propagator = settings.PROPAGATOR |
| 64 | + |
| 65 | + self._blacklist_paths = settings.params.get(BLACKLIST_PATHS) |
| 66 | + |
| 67 | + def __call__(self, request): |
| 68 | + self._before_request(request) |
| 69 | + |
| 70 | + response = self.handler(request) |
| 71 | + |
| 72 | + self._after_request(request, response) |
| 73 | + |
| 74 | + return response |
| 75 | + |
| 76 | + def _before_request(self, request): |
| 77 | + if utils.disable_tracing_url(request.path, self._blacklist_paths): |
| 78 | + return |
| 79 | + |
| 80 | + try: |
| 81 | + header = get_context_header(request) |
| 82 | + span_context = self.propagator.from_header(header) |
| 83 | + |
| 84 | + tracer = tracer_module.Tracer( |
| 85 | + span_context=span_context, |
| 86 | + sampler=self.sampler, |
| 87 | + exporter=self.exporter, |
| 88 | + propagator=self.propagator) |
| 89 | + |
| 90 | + span = tracer.start_span() |
| 91 | + |
| 92 | + # Set the span name as the name of the current module name |
| 93 | + span.name = '[{}]{}'.format( |
| 94 | + request.method, |
| 95 | + request.path) |
| 96 | + |
| 97 | + tracer.add_attribute_to_current_span( |
| 98 | + attribute_key=HTTP_METHOD, |
| 99 | + attribute_value=request.method) |
| 100 | + tracer.add_attribute_to_current_span( |
| 101 | + attribute_key=HTTP_URL, |
| 102 | + attribute_value=request.path) |
| 103 | + except Exception: # pragma: NO COVER |
| 104 | + log.error('Failed to trace request', exc_info=True) |
| 105 | + |
| 106 | + def _after_request(self, request, response): |
| 107 | + if utils.disable_tracing_url(request.path, self._blacklist_paths): |
| 108 | + return |
| 109 | + |
| 110 | + try: |
| 111 | + tracer = execution_context.get_opencensus_tracer() |
| 112 | + tracer.add_attribute_to_current_span( |
| 113 | + HTTP_STATUS_CODE, |
| 114 | + str(response.status_code)) |
| 115 | + |
| 116 | + tracer.end_span() |
| 117 | + tracer.finish() |
| 118 | + except Exception: # pragma: NO COVER |
| 119 | + log.error('Failed to trace request', exc_info=True) |
| 120 | + |
| 121 | + |
| 122 | +def get_context_header(request): |
| 123 | + """Get trace context header from pyramid request headers.""" |
| 124 | + return request.headers.get(_PYRAMID_TRACE_HEADER) |
0 commit comments