How to use the opencensus.trace.span_context.SpanContext function in opencensus

To help you get started, we’ve selected a few opencensus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github census-instrumentation / opencensus-python / tests / unit / trace / exporters / test_stackdriver_exporter.py View on Github external
def test_emit(self, monitor_resource_mock):
        trace_id = '6e0c63257de34c92bf9efcd03927272e'
        span_datas = [
            span_data_module.SpanData(
                name='span',
                context=span_context.SpanContext(trace_id=trace_id),
                span_id='1111',
                parent_span_id=None,
                attributes=None,
                start_time=None,
                end_time=None,
                child_span_count=None,
                stack_trace=None,
                time_events=None,
                links=None,
                status=None,
                same_process_as_parent_span=None,
                span_kind=0,
            )
        ]

        stackdriver_spans = {
github census-instrumentation / opencensus-python / tests / unit / trace / exporters / test_logging_exporter.py View on Github external
def test_emit(self):
        exporter = logging_exporter.LoggingExporter()
        logger = mock.Mock()
        exporter.logger = logger

        span_datas = [
            span_data_module.SpanData(
                name='span',
                context=span_context.SpanContext(trace_id='1'),
                span_id='1111',
                parent_span_id=None,
                attributes=None,
                start_time=None,
                end_time=None,
                child_span_count=None,
                stack_trace=None,
                annotations=None,
                message_events=None,
                links=None,
                status=None,
                same_process_as_parent_span=None,
                span_kind=0,
            )
        ]
        exporter.emit(span_datas)
github census-instrumentation / opencensus-python / tests / unit / trace / exporters / ocagent / test_trace_exporter_utils.py View on Github external
def test_translate_server_span_kindt(self):
        server_span_data = span_data_module.SpanData(
            context=span_context_module.SpanContext(
                trace_id='6e0c63257de34c92bf9efcd03927272e'),
            span_id='6e0c63257de34c92',
            span_kind=span_module.SpanKind.SERVER,
            child_span_count=1,
            start_time=None,
            end_time=None,
            name=None,
            parent_span_id=None,
            attributes=None,
            same_process_as_parent_span=False,
            stack_trace=None,
            time_events=None,
            links=None,
            status=None)

        pb_span = utils.translate_to_trace_proto(server_span_data)
github census-instrumentation / opencensus-python / tests / unit / trace / tracers / test_context_tracer.py View on Github external
def test_constructor_defaults(self):
        from opencensus.trace import print_exporter
        from opencensus.trace import span_context

        tracer = context_tracer.ContextTracer()

        assert isinstance(tracer.span_context, span_context.SpanContext)
        assert isinstance(tracer.exporter, print_exporter.PrintExporter)
        self.assertEqual(tracer._spans_list, [])
        self.assertEqual(tracer.root_span_id, tracer.span_context.span_id)
github census-instrumentation / opencensus-python / tests / unit / trace / exporters / ocagent / test_trace_exporter_utils.py View on Github external
def test_basic_span_translation(self):
        hex_encoder = codecs.getencoder('hex')

        span_data = span_data_module.SpanData(
            name="name",
            context=span_context_module.SpanContext(
                trace_id='6e0c63257de34c92bf9efcd03927272e'),
            span_id='6e0c63257de34c92',
            parent_span_id='6e0c63257de34c93',
            attributes={
                'test_str_key': 'test_str_value',
                'test_int_key': 1,
                'test_bool_key': False,
                'test_double_key': 567.89
            },
            start_time='2017-08-15T18:02:26.071158Z',
            end_time='2017-08-15T18:02:36.071158Z',
            child_span_count=None,
            stack_trace=None,
            time_events=None,
            links=None,
            status=None,
github census-instrumentation / opencensus-python / tests / unit / trace / test_tracer.py View on Github external
def test_constructor_default(self):
        from opencensus.trace import print_exporter
        from opencensus.trace.propagation \
            import trace_context_http_header_format
        from opencensus.trace.samplers import ProbabilitySampler
        from opencensus.trace.span_context import SpanContext
        from opencensus.trace.tracers import noop_tracer

        tracer = tracer_module.Tracer()

        assert isinstance(tracer.span_context, SpanContext)
        assert isinstance(tracer.sampler, ProbabilitySampler)
        assert isinstance(tracer.exporter, print_exporter.PrintExporter)
        assert isinstance(
            tracer.propagator,
            trace_context_http_header_format.TraceContextPropagator)
        assert isinstance(tracer.tracer, noop_tracer.NoopTracer)
github census-instrumentation / opencensus-python / opencensus / trace / propagation / b3_format.py View on Github external
def from_headers(self, headers):
        """Generate a SpanContext object from B3 propagation headers.

        :type headers: dict
        :param headers: HTTP request headers.

        :rtype: :class:`~opencensus.trace.span_context.SpanContext`
        :returns: SpanContext generated from B3 propagation headers.
        """
        if headers is None:
            return SpanContext(from_header=False)

        trace_id, span_id, sampled = None, None, None

        state = headers.get(_STATE_HEADER_KEY)
        if state:
            fields = state.split('-', 4)

            if len(fields) == 1:
                sampled = fields[0]
            elif len(fields) == 2:
                trace_id, span_id = fields
            elif len(fields) == 3:
                trace_id, span_id, sampled = fields
            elif len(fields) == 4:
                trace_id, span_id, sampled, _parent_span_id = fields
            else:
github census-instrumentation / opencensus-python / opencensus / trace / propagation / google_cloud_format.py View on Github external
def from_header(self, header):
        """Generate a SpanContext object using the trace context header.
        The value of enabled parsed from header is int. Need to convert to
        bool.

        :type header: str
        :param header: Trace context header which was extracted from the HTTP
                       request headers.

        :rtype: :class:`~opencensus.trace.span_context.SpanContext`
        :returns: SpanContext generated from the trace context header.
        """
        if header is None:
            return SpanContext()

        try:
            match = re.search(_TRACE_CONTEXT_HEADER_RE, header)
        except TypeError:
            logging.warning(
                'Header should be str, got %s. Cannot parse the header.',
                header.__class__.__name__)
            raise

        if match:
            trace_id = match.group(1)
            span_id = match.group(3)
            trace_options = match.group(5)

            if trace_options is None:
                trace_options = 1
github criteo / biggraphite / biggraphite / tracing.py View on Github external
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()