Example #1
0
async def main():
    print(RuntimeContext)
    RuntimeContext.correlation_context['test'] = True
    print(RuntimeContext)
    await asyncio.gather(
        hello('foo'),
        hello('bar'),
        hello('baz'),
    )
    print(RuntimeContext)
    RuntimeContext.clear()
    print(RuntimeContext)
Example #2
0
    def test_with_current_context(self):
        from threading import Thread

        RuntimeContext.register_slot('operation_id')

        def work(name):
            self.assertEqual(RuntimeContext.operation_id, 'foo')
            RuntimeContext.operation_id = name
            self.assertEqual(RuntimeContext.operation_id, name)

        RuntimeContext.operation_id = 'foo'
        thread = Thread(
            target=RuntimeContext.with_current_context(work),
            args=('bar'),
        )
        thread.start()
        thread.join()

        self.assertEqual(RuntimeContext.operation_id, 'foo')
Example #3
0
# 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.

import asyncio
from opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('current_span', None)


class Span(object):
    def __init__(self, name):
        self.name = name
        self.parent = RuntimeContext.current_span

    def __repr__(self):
        return ('{}(name={}, parent={})'.format(
            type(self).__name__,
            self.name,
            self.parent,
        ))

    async def __aenter__(self):
Example #4
0
# Copyright 2018, OpenCensus 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 opencensus.common.runtime_context import RuntimeContext
from opencensus.tags.tag import Tag
from opencensus.tags.tag_key import TagKey
from opencensus.tags.tag_map import TagMap
from opencensus.tags.tag_value import TagValue

__all__ = ['Tag', 'TagContext', 'TagKey', 'TagValue', 'TagMap']

TagContext = RuntimeContext.register_slot('tag_context', None)
# 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 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."""
Example #6
0
 def test_clear(self):
     RuntimeContext.register_slot('baz')
     RuntimeContext.baz = 123
     self.assertEqual(RuntimeContext.baz, 123)
     RuntimeContext.clear()
     self.assertEqual(RuntimeContext.baz, None)
Example #7
0
 def test_register_duplicate(self):
     self.assertRaises(
         ValueError, lambda: [
             RuntimeContext.register_slot('dup'),
             RuntimeContext.register_slot('dup'),
         ])
Example #8
0
 def test_register_with_default(self):
     RuntimeContext.register_slot('bar', 123)
     self.assertEqual(RuntimeContext.bar, 123)
Example #9
0
    def test_register(self):
        RuntimeContext.register_slot('foo')
        self.assertIsNone(RuntimeContext.foo)

        RuntimeContext.foo = 123
        self.assertEqual(RuntimeContext.foo, 123)
Example #10
0
# 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.

import asyncio

from opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('correlation_context', lambda: dict())


async def hello(name):
    correlation_context = RuntimeContext.correlation_context.copy()
    correlation_context['name'] = name
    RuntimeContext.correlation_context = correlation_context

    for i in range(3):
        print('Hello {} {} {}'.format(
            name,
            i,
            RuntimeContext,
        ))
        await asyncio.sleep(0.1)
Example #11
0
#
# 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 opencensus.common.runtime_context import RuntimeContext

_measure_to_view_map_slot = RuntimeContext.register_slot(
    'measure_to_view_map', lambda: {})


def get_measure_to_view_map():
    return RuntimeContext.measure_to_view_map


def set_measure_to_view_map(measure_to_view_map):
    RuntimeContext.measure_to_view_map = measure_to_view_map


def clear():
    """Clear the context, used in test."""
    _measure_to_view_map_slot.clear()
Example #12
0
# 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 threading import Thread

from opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('operation_id', '<empty>')


def work(name):
    print('Entering worker:', RuntimeContext)
    RuntimeContext.operation_id = name
    print('Exiting worker:', RuntimeContext)


if __name__ == '__main__':
    print('Main thread:', RuntimeContext)
    RuntimeContext.operation_id = 'main'

    print('Main thread:', RuntimeContext)

    # by default context is not propagated to worker thread
Example #13
0
#
# 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 opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('correlation_context', lambda: {})


def hello(name):
    correlation_context = RuntimeContext.correlation_context.copy()
    correlation_context['name'] = name
    RuntimeContext.correlation_context = correlation_context

    print(RuntimeContext)


if __name__ == '__main__':
    print(RuntimeContext)
    RuntimeContext.correlation_context['test'] = True
    print(RuntimeContext)
    hello('hello')
Example #14
0
#
#     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.

import threading
import time
from multiprocessing.dummy import Pool as ThreadPool

from opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('operation_id', '<empty>')
_console_lock = threading.Lock()


def println(msg):
    with _console_lock:
        print(msg)


def work(name):
    println('Entering worker[{}]: {}'.format(name, RuntimeContext))
    RuntimeContext.operation_id = name
    time.sleep(0.01)
    println('Exiting worker[{}]: {}'.format(name, RuntimeContext))