How to use the opencensus.trace.samplers.ProbabilitySampler 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 / samplers / test_probability.py View on Github external
def test_constructor_default(self):
        sampler = samplers.ProbabilitySampler()
        self.assertEqual(sampler.rate, samplers.DEFAULT_SAMPLING_RATE)
github census-instrumentation / opencensus-python / tests / unit / trace / ext / flask / test_flask_middleware.py View on Github external
def test_init_app_config_zipkin_exporter(self):
        app = mock.Mock()
        app.config = {
            'OPENCENSUS_TRACE': {
                'SAMPLER': ProbabilitySampler,
                'EXPORTER': zipkin_exporter.ZipkinExporter,
                'PROPAGATOR': google_cloud_format.GoogleCloudFormatPropagator,
            },
            'OPENCENSUS_TRACE_PARAMS': {
                'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
                'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
                'ZIPKIN_EXPORTER_PORT': 9411,
            },
        }

        middleware = flask_middleware.FlaskMiddleware()
        middleware.init_app(app)

        self.assertIs(middleware.app, app)
        self.assertTrue(app.before_request.called)
        self.assertTrue(app.after_request.called)
github census-instrumentation / opencensus-python / contrib / opencensus-ext-azure / examples / logs / correlated.py View on Github external
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer

config_integration.trace_integrations(['logging'])

logger = logging.getLogger(__name__)

# TODO: you need to specify the instrumentation key in the
# APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
handler = AzureLogHandler()
logger.addHandler(handler)

tracer = Tracer(exporter=AzureExporter(), sampler=ProbabilitySampler(1.0))

logger.warning('Before the span')
with tracer.span(name='test'):
    logger.warning('In the span')
logger.warning('After the span')
github census-instrumentation / opencensus-python / opencensus / trace / tracer.py View on Github external
def __init__(
            self,
            span_context=None,
            sampler=None,
            exporter=None,
            propagator=None):
        if span_context is None:
            span_context = SpanContext()

        if sampler is None:
            sampler = samplers.ProbabilitySampler()

        if exporter is None:
            exporter = print_exporter.PrintExporter()

        if propagator is None:
            propagator = \
                trace_context_http_header_format.TraceContextPropagator()

        self.span_context = span_context
        self.sampler = sampler
        self.exporter = exporter
        self.propagator = propagator
        self.tracer = self.get_tracer()
        self.store_tracer()
github census-instrumentation / opencensus-python / contrib / opencensus-ext-pyramid / examples / simple.py View on Github external
def run_app():
    settings = {
        'OPENCENSUS': {
            'TRACE': {
                'EXPORTER': print_exporter.PrintExporter(),
                'SAMPLER': samplers.ProbabilitySampler(rate=1),
            }
        }
    }

    app = main({}, **settings)
    server = make_server('localhost', 8080, app)
    server.serve_forever()
github census-instrumentation / opencensus-python / contrib / opencensus-ext-azure / examples / traces / server.py View on Github external
import requests
from flask import Flask

from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler

# TODO: you need to specify the instrumentation key in a connection string
# and place it in the APPLICATIONINSIGHTS_CONNECTION_STRING
# environment variable.
app = Flask(__name__)
middleware = FlaskMiddleware(
    app,
    exporter=AzureExporter(),
    sampler=ProbabilitySampler(rate=1.0),
)


@app.route('/')
def hello():
    requests.get('https://www.wikipedia.org/wiki/Rabbit')
    return 'Hello, World!'


if __name__ == '__main__':
    import logging
    logger = logging.getLogger('werkzeug')
    logger.setLevel(logging.ERROR)
    config_integration.trace_integrations(['requests'])
    app.run(host='localhost', port=8080, threaded=True)
github census-instrumentation / opencensus-python / contrib / opencensus-ext-flask / opencensus / ext / flask / flask_middleware.py View on Github external
def init_app(self, app):
        self.app = app

        # get settings from app config
        settings = self.app.config.get('OPENCENSUS', {})
        settings = settings.get('TRACE', {})

        if self.sampler is None:
            self.sampler = (settings.get('SAMPLER', None)
                            or samplers.ProbabilitySampler())
            if isinstance(self.sampler, six.string_types):
                self.sampler = configuration.load(self.sampler)

        if self.exporter is None:
            self.exporter = settings.get('EXPORTER', None) or \
                print_exporter.PrintExporter()
            if isinstance(self.exporter, six.string_types):
                self.exporter = configuration.load(self.exporter)

        if self.propagator is None:
            self.propagator = settings.get('PROPAGATOR', None) or \
                trace_context_http_header_format.TraceContextPropagator()
            if isinstance(self.propagator, six.string_types):
                self.propagator = configuration.load(self.propagator)

        self.blacklist_paths = settings.get(BLACKLIST_PATHS,
github microsoft / AIforEarth-API-Development / Containers / base-py / ai4e_api_tools / ai4e_service.py View on Github external
from functools import wraps
from werkzeug.exceptions import HTTPException

from opencensus.trace.tracer import Tracer
from opencensus.trace.samplers import ProbabilitySampler

if not getenv('APPINSIGHTS_INSTRUMENTATIONKEY', None):
    tracer = Tracer()
else:
    sampling_rate = getenv('TRACE_SAMPLING_RATE', None)
    if not sampling_rate:
        sampling_rate = 1.0

    tracer = Tracer(
        exporter=AzureExporter(),
        sampler=ProbabilitySampler(float(sampling_rate)),
    )

disable_request_metric = getenv('DISABLE_CURRENT_REQUEST_METRIC', False)

MAX_REQUESTS_KEY_NAME = 'max_requests'
CONTENT_TYPE_KEY_NAME = 'content_type'
CONTENT_MAX_KEY_NAME = 'content_max_length'

APP_INSIGHTS_REQUESTS_KEY_NAME = 'CURRENT_REQUESTS'

class Task(Resource):
    def __init__(self, **kwargs):
        self.task_mgr = kwargs['task_manager']

    def get(self, id):
        st = self.task_mgr.GetTaskStatus(str(id))
github census-instrumentation / opencensus-python / contrib / opencensus-ext-django / opencensus / ext / django / middleware.py View on Github external
def __init__(self, get_response=None):
        self.get_response = get_response
        settings = getattr(django.conf.settings, 'OPENCENSUS', {})
        settings = settings.get('TRACE', {})

        self.sampler = (settings.get('SAMPLER', None)
                        or samplers.ProbabilitySampler())
        if isinstance(self.sampler, six.string_types):
            self.sampler = configuration.load(self.sampler)

        self.exporter = settings.get('EXPORTER', None) or \
            print_exporter.PrintExporter()
        if isinstance(self.exporter, six.string_types):
            self.exporter = configuration.load(self.exporter)

        self.propagator = settings.get('PROPAGATOR', None) or \
            trace_context_http_header_format.TraceContextPropagator()
        if isinstance(self.propagator, six.string_types):
            self.propagator = configuration.load(self.propagator)

        self.blacklist_paths = settings.get(BLACKLIST_PATHS, None)

        self.blacklist_hostnames = settings.get(BLACKLIST_HOSTNAMES, None)