def do_trace_request(self, request): if utils.disable_tracing_url(request.url, self.blacklist_paths): return span_context = self.propagator.from_headers(request.headers) tracer = noop_tracer_module.NoopTracer() if span_context.from_header and span_context.trace_options.enabled: tracer = tracer_module.ContextTracer(span_context=span_context, exporter=self.exporter) elif self.sampler.should_sample(span_context.trace_id): tracer = tracer_module.ContextTracer(exporter=self.exporter) span = tracer.start_span() route = request.app.router.get(request) # Set the span name as the name of the current module name span.name = '[sanic] {} {}'.format(request.method, route[3]) tracer.add_attribute_to_current_span('http.nethod', request.method) tracer.add_attribute_to_current_span('http.host', request.host) tracer.add_attribute_to_current_span('http.scheme', request.scheme) tracer.add_attribute_to_current_span('http.url', request.url) tracer.add_attribute_to_current_span('http.client.ip', request.ip) for header in ['user-agent', 'x-forwarded-for', 'x-real-ip']: if header in request.headers: tracer.add_attribute_to_current_span('http.headers.' + header, request.headers[header]) request['tracer'] = tracer asyncio_context.set_opencensus_tracer(tracer)
def get_tracer(self): """Return a tracer according to the sampling decision.""" sampled = self.should_sample() if sampled: return context_tracer.ContextTracer(exporter=self.exporter, span_context=self.span_context) else: return noop_tracer.NoopTracer()
def get_tracer(self): """Return a tracer according to the sampling decision.""" sampled = self.should_sample() if sampled: self.span_context.trace_options.set_enabled(True) return context_tracer.ContextTracer(exporter=self.exporter, span_context=self.span_context) return noop_tracer.NoopTracer()
def stop_trace(): """Stop the current trace.""" if not execution_context: return execution_context.set_current_span(None) tracer = execution_context.get_opencensus_tracer() tracer.tracer = noop_tracer.NoopTracer() tracer.span_context = SpanContext( trace_options=trace_options.TraceOptions(0)) tracer.sampler = always_off.AlwaysOffSampler()
def get_opencensus_tracer(): """Get the opencensus tracer from thread local.""" return getattr(_thread_local, 'tracer', noop_tracer.NoopTracer())
# # 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 opencensus.common.runtime_context import RuntimeContext from opencensus.trace.tracers import noop_tracer _attrs_slot = RuntimeContext.register_slot('attrs', lambda: {}) _current_span_slot = RuntimeContext.register_slot('current_span', None) _exporter_slot = RuntimeContext.register_slot('is_exporter', False) _tracer_slot = RuntimeContext.register_slot('tracer', noop_tracer.NoopTracer()) def is_exporter(): return RuntimeContext.is_exporter def set_is_exporter(is_exporter): RuntimeContext.is_exporter = is_exporter def get_opencensus_tracer(): """Get the opencensus tracer from runtime context.""" return RuntimeContext.tracer
import aiotask_context as context import logging from opencensus.trace.tracers import noop_tracer log = logging.getLogger(__name__) _TRACER_KEY = 'opencensus.io/trace' _ATTRS_KEY = 'opencensus.io/attrs' _CURRENT_SPAN_KEY = 'opencensus.io/current-span' from opencensus.trace.tracers import ( noop_tracer as noop_tracer_module ) default_tracer = noop_tracer.NoopTracer() def get_opencensus_tracer(): try: return context.get(_TRACER_KEY, default=default_tracer) except Exception: return default_tracer def set_opencensus_tracer(tracer): """Add the tracer to thread local.""" context.set(key=_TRACER_KEY, value=tracer) def set_opencensus_attr(attr_key, attr_value): attrs = context.get(_ATTRS_KEY, {})
def test_list_collected_spans(self): tracer = noop_tracer.NoopTracer() spans = tracer.list_collected_spans() self.assertIsNone(spans)