def run(): parser = argparse.ArgumentParser() parser.add_argument( '--log_payloads', action='store_true', help='log request/response objects to open-tracing spans') args = parser.parse_args() config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='trivial-client') tracer = config.initialize_tracer() tracer_interceptor = open_tracing_client_interceptor( tracer, log_payloads=args.log_payloads) channel = grpc.insecure_channel('localhost:50051') channel = intercept_channel(channel, tracer_interceptor) stub = command_line_pb2.CommandLineStub(channel) response = stub.Echo(command_line_pb2.CommandRequest(text='Hello, hello')) print(response.text) time.sleep(2) tracer.close() time.sleep(2)
def serve(): parser = argparse.ArgumentParser() parser.add_argument( '--log_payloads', action='store_true', help='log request/response objects to open-tracing spans') args = parser.parse_args() config = Config(config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='integration-server') tracer = config.initialize_tracer() tracer_interceptor = open_tracing_server_interceptor( tracer, log_payloads=args.log_payloads) server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) server = intercept_server(server, tracer_interceptor) command_line_pb2.add_CommandLineServicer_to_server(CommandLine(tracer), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) time.sleep(2) tracer.close() time.sleep(2)
def setup_opentracing(app): """ Helper function to setup opentracing with Jaeger client during setup. Use during app startup as follows: .. code-block:: python app = FastAPI() @app.on_event('startup') async def startup(): setup_opentracing(app) :param app: app object, instance of FastAPI :return: None """ config = Config(config={ "local_agent": { "reporting_host": settings.jaeger_host, "reporting_port": settings.jaeger_port }, "sampler": { "type": settings.jaeger_sampler_type, "param": settings.jaeger_sampler_rate, }, "trace_id_header": settings.trace_id_header }, service_name=settings.service_name, validate=True, scope_manager=AsyncioScopeManager()) # this call also sets opentracing.tracer app.tracer = config.initialize_tracer()
def run(): parser = argparse.ArgumentParser() parser.add_argument( '--log_payloads', action='store_true', help='log request/response objects to open-tracing spans') args = parser.parse_args() config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='integration-client') tracer = config.initialize_tracer() active_span_source = FixedActiveSpanSource() tracer_interceptor = open_tracing_client_interceptor( tracer, active_span_source=active_span_source, log_payloads=args.log_payloads) channel = grpc.insecure_channel('localhost:50051') channel = intercept_channel(channel, tracer_interceptor) stub = command_line_pb2.CommandLineStub(channel) echo(tracer, active_span_source, stub) time.sleep(2) tracer.close() time.sleep(2)
def test_rate_limiting_sampler(self): with self.assertRaises(Exception): cfg = {'sampler': {'type': 'rate_limiting', 'param': 'xx'}} Config(cfg, service_name='x').sampler c = Config({'sampler': {'type': 'rate_limiting', 'param': 1234}}, service_name='x') assert type(c.sampler) is RateLimitingSampler assert c.sampler.traces_per_second == 1234
def test_probabilistic_sampler(self): with self.assertRaises(Exception): cfg = {'sampler': {'type': 'probabilistic', 'param': 'xx'}} Config(cfg, service_name='x').sampler c = Config({'sampler': {'type': 'probabilistic', 'param': 0.5}}, service_name='x') assert type(c.sampler) is ProbabilisticSampler assert c.sampler.rate == 0.5
def __init__(self, *args, **kwargs): # start - JAEGER config = Config(config={'sampler': {'type': 'const', 'param': 1}, 'logging': True }, service_name=__name__) config.initialize_tracer() super().__init__(*args, **kwargs)
def test_const_sampler(self): c = Config({'sampler': {'type': 'const', 'param': True}}, service_name='x') assert type(c.sampler) is ConstSampler assert c.sampler.decision c = Config({'sampler': {'type': 'const', 'param': False}}, service_name='x') assert type(c.sampler) is ConstSampler assert not c.sampler.decision
def test_agent_reporting_host(self): c = Config({}, service_name='x') assert c.local_agent_reporting_host == 'localhost' c = Config({'local_agent': { 'reporting_host': 'jaeger.local' }}, service_name='x') assert c.local_agent_reporting_host == 'jaeger.local'
def test_tchannel_factory(self): c = Config({}, service_name='x') assert callable(c.tchannel_factory) assert c.tchannel_factory() is None c = Config( {'tchannel_factory': 'tchannel.singleton.TChannel.get_instance'}, service_name='x') assert callable(c.tchannel_factory)
def test_max_tag_value_length(self): c = Config({}, service_name='x') assert c.max_tag_value_length == constants.MAX_TAG_VALUE_LENGTH c = Config({'max_tag_value_length': 333}, service_name='x') assert c.max_tag_value_length == 333 t = c.create_tracer(NullReporter(), ConstSampler(True)) assert t.max_tag_value_length == 333
def _init_tracing(self): if getattr(settings, 'OPENTRACING_TRACER', None) is not None: # Backwards compatibility. tracing = settings.OPENTRACING_TRACER elif getattr(settings, 'OPENTRACING_TRACING', None) is not None: tracing = settings.OPENTRACING_TRACING elif getattr(settings, 'OPENTRACING_TRACER_CALLABLE', None) is not None: tracer_callable = settings.OPENTRACING_TRACER_CALLABLE tracer_parameters = getattr(settings, 'OPENTRACING_TRACER_PARAMETERS', {}) if not callable(tracer_callable): tracer_callable = import_string(tracer_callable) tracer = tracer_callable(**tracer_parameters) tracing = DjangoTracing(tracer) else: # Rely on the global Tracer. # tracing = DjangoTracing() # override the DjangoTracing used with jaeger tracer log.info("using default jaeger tracer") jaeger_config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': 'jaeger', 'reporting_port': '6831', }, 'logging': True, }, service_name='viper', validate=True, ) tracing = DjangoTracing(jaeger_config.initialize_tracer()) # trace_all defaults to True when used as middleware. tracing._trace_all = getattr(settings, 'OPENTRACING_TRACE_ALL', True) # set the start_span_cb hook, if any. tracing._start_span_cb = getattr(settings, 'OPENTRACING_START_SPAN_CB', None) # Normalize the tracing field in settings, including the old field. settings.OPENTRACING_TRACING = tracing settings.OPENTRACING_TRACER = tracing # Potentially set the global Tracer (unless we rely on it already). if getattr(settings, 'OPENTRACING_SET_GLOBAL_TRACER', False): initialize_global_tracer(tracing) return tracing
def test_agent_reporting_host(self): c = Config({}, service_name='x') assert c.local_agent_reporting_host == 'localhost' c = Config({'local_agent': {'reporting_host': 'jaeger.local'}}, service_name='x') assert c.local_agent_reporting_host == 'jaeger.local' os.environ['JAEGER_AGENT_HOST'] = 'jaeger-env.local' c = Config({}, service_name='x') assert c.local_agent_reporting_host == 'jaeger-env.local'
def create_tracer(access_token=None, set_global=True, config=None, *args, **kwargs): """ Creates a jaeger_client.Tracer via Config().initialize_tracer(). Default config argument will consist of service name of 'SignalFx-Tracing' value, B3 span propagation, and a ConstSampler. These are tunable by env vars. """ try: from jaeger_client import Config from jaeger_client import constants except ImportError: raise RuntimeError( 'create_tracer() is only for environments with jaeger-client installed.' ) config = config or {} if 'service_name' not in config and 'service_name' not in kwargs: config['service_name'] = _get_env_var('SIGNALFX_SERVICE_NAME', 'SignalFx-Tracing') if 'jaeger_endpoint' not in config: config['jaeger_endpoint'] = _get_env_var( 'SIGNALFX_INGEST_URL', 'https://ingest.signalfx.com/v1/trace') access_token = access_token or _get_env_var('SIGNALFX_ACCESS_TOKEN') if 'jaeger_user' not in config and access_token is not None: config['jaeger_user'] = '******' if 'jaeger_password' not in config and access_token is not None: config['jaeger_password'] = access_token if 'sampler' not in config: sampler_type = _get_env_var('SIGNALFX_SAMPLER_TYPE', 'const') sampler_param = _get_env_var('SIGNALFX_SAMPLER_PARAM', 1) if sampler_type == constants.SAMPLER_TYPE_CONST: sampler_param = int(float(sampler_param)) elif sampler_type in (constants.SAMPLER_TYPE_PROBABILISTIC, constants.SAMPLER_TYPE_RATE_LIMITING, constants.SAMPLER_TYPE_LOWER_BOUND): sampler_param = float(sampler_param) config['sampler'] = dict(type=sampler_type, param=sampler_param) if 'propagation' not in config: propagation = _get_env_var('SIGNALFX_PROPAGATION', 'b3') config['propagation'] = propagation jaeger_config = Config(config, *args, **kwargs) tracer = jaeger_config.initialize_tracer() if set_global: import opentracing opentracing.tracer = tracer return tracer
def initialize_global_tracer(): _config = Config( config=conf.CONFIG, service_name=settings.SERVICE_NAME, validate=True, ) if _config.initialized(): tracer = opentracing.tracer else: tracer = _config.initialize_tracer() return tracer
def __init__(self, tracer_config): if (Tracer.instance is not None): Tracer.instance.close() config = tracer_config.get('config') service_name = tracer_config.get('service_name') tracerConfig = Config(config=config, service_name=service_name, validate=True) self.tracer = tracerConfig.new_tracer() Tracer.instance = self
def init_opentracing_tracer(tracer, **kwargs): if tracer == OPENTRACING_BASIC: from basictracer import BasicTracer # noqa recorder = kwargs.get('recorder') sampler = kwargs.get('sampler') opentracing.tracer = BasicTracer(recorder=recorder, sampler=sampler) elif tracer == OPENTRACING_INSTANA: import instana # noqa elif tracer == OPENTRACING_LIGHTSTEP: import lightstep # Get Lightstep specific config vars component_name = kwargs.pop( 'component_name', os.environ.get('OPENTRACING_LIGHTSTEP_COMPONENT_NAME')) access_token = kwargs.pop( 'access_token', os.environ.get('OPENTRACING_LIGHTSTEP_ACCESS_TOKEN')) collector_host = kwargs.pop( 'collector_host', os.environ.get('OPENTRACING_LIGHTSTEP_COLLECTOR_HOST', 'collector.lightstep.com')) collector_port = kwargs.pop( 'collector_port', int(os.environ.get('OPENTRACING_LIGHTSTEP_COLLECTOR_PORT', 443))) verbosity = kwargs.pop( 'verbosity', int(os.environ.get('OPENTRACING_LIGHTSTEP_VERBOSITY', 0))) if not access_token: logger.warning( 'Initializing LighStep tracer with no access_token!') opentracing.tracer = lightstep.Tracer(component_name=component_name, access_token=access_token, collector_host=collector_host, collector_port=collector_port, verbosity=verbosity, **kwargs) elif tracer == OPENTRACING_JAEGER: from jaeger_client import Config service_name = kwargs.pop( 'service_name', os.environ.get('OPENTRACING_JAEGER_SERVICE_NAME')) config = kwargs.pop('config', {}) jaeger_config = Config(config=config, service_name=service_name) opentracing.tracer = jaeger_config.initialize_tracer() else: opentracing.tracer = opentracing.Tracer() return opentracing.tracer
def init_jaeger_tracer(service_name='proto.example.lifecycle_events'): config = Config(config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name=service_name, validate=True) return config.initialize_tracer()
def initialize_tracer(): config = Config( config={ # 'sampler': {'type': 'const', 'param': 1} 'local_agent': { 'reporting_host': 'jaeger-agent', }, 'logging': True }, service_name='hello-world') return config.initialize_tracer() # also sets opentracing.tracer
def tracer(): from jaeger_client import Config config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='example') return config.initialize_tracer()
def new_tracer(service_name: str): """Create a new tracer.""" new_config = JaegerConfig( config={ # usually read from some yaml config 'sampler': {'type': 'const', 'param': 1}, 'logging': True, 'reporter_batch_size': 1, 'trace_id_header': 'registries-trace-id', }, service_name=service_name, ) return new_config.new_tracer()
async def notify_server_started(app, loop): config = Config(config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name="inventory", validate=True, scope_manager=ContextVarsScopeManager()) config.initialize_tracer()
def init(): config = Config(service_name='test', config={ 'trace_id_header': 'Trace_ID', 'baggage_header_prefix': 'Trace-Attr-', }) reporter = InMemoryReporter() tracer = config.create_tracer( reporter=reporter, sampler=ConstSampler(True), ) tracing = FlaskTracing(tracer, True, app)
def initialize_tracer(): config = Config( config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True }, service_name='flask-service' ) return config.initialize_tracer() # also sets opentracing.tracer
def init_jaeger_tracer(): """This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_ :param service_name: the name of your application to register in the tracer :return: opentracing.Tracer """ config = Config(config={ 'propagation': 'b3', 'logging': True, }, service_name="data-connection") return config.initialize_tracer()
def initialize_tracer(): logging.getLogger('').handlers = [] logging.basicConfig(format='%(message)s', level=logging.INFO) config = Config( config={ 'sampler': {'type': 'const', 'param': 1}, 'local_agent': {'reporting_host': 'jaeger'}, 'logging': True, }, service_name='app-backend' ) return config.initialize_tracer()
def initialize_tracer(service_name): _jaeger_config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, } }, service_name='rpc-{}'.format(service_name) ) tracer = _jaeger_config.initialize_tracer() return tracer
def init_jaeger_tracer(service_name='your-app-name'): c = { # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, } config = Config(config=c, service_name=service_name, validate=True) tracer = config.initialize_tracer() return tracer
def init_tracer(service): config = Config(config={ 'sampler': { 'type': 'const', 'param': 1 }, 'local_agent': { 'reporting_host': JAEGER_HOST, }, 'logging': True, }, service_name=service) return config.initialize_tracer()
def init_jaeger_tracer(service_name=""): config = Config( config={ "sampler": { "type": "const", "param": 1 }, "logging": True }, service_name="fake provider", validate=True, ) return config.initialize_tracer()
def tracer(): from jaeger_client import Config config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': False, }, # metrics_factory=PrometheusMetricsFactory(namespace='hue-api'), service_name='hue-api') return config.initialize_tracer()
def init_tracer(service): config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name=service, ) return config.initialize_tracer()
def init_tracer(service): logging.getLogger('').handlers = [] logging.basicConfig(format='%(message)s', level=logging.DEBUG) config = Config( config={ # usually read from some yaml config 'sampler': {'type': 'const', 'param': 1, }, 'logging': True, 'reporter_batch_size': 1, }, service_name=service, ) return config.initialize_tracer()
def init_jaeger_tracer(): logger.info('Initializing Jaeger tracer') config = Config({ 'service_name': JAEGER_SERVICE_NAME, 'sampler': { 'type': JAEGER_SAMPLING_TYPE, 'param': JAEGER_SAMPLING_PARAM, }, 'local_agent': { 'reporting_host': JAEGER_AGENT_HOST, }, 'logging': JAEGER_LOGGING_ENABLED, }, validate=True) global JAEGER_TRACER JAEGER_TRACER = config.initialize_tracer()
def serve(): parser = argparse.ArgumentParser() parser.add_argument( '--log_payloads', action='store_true', help='log request/response objects to open-tracing spans') parser.add_argument( '--include_grpc_tags', action='store_true', help='set gRPC-specific tags on spans') args = parser.parse_args() config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='store-server') tracer = config.initialize_tracer() span_decorator = None if args.include_grpc_tags: span_decorator = StoreSpanDecorator() tracer_interceptor = open_tracing_server_interceptor( tracer, log_payloads=args.log_payloads, span_decorator=span_decorator) server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) server = intercept_server(server, tracer_interceptor) store_pb2.add_StoreServicer_to_server(Store(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) time.sleep(2) tracer.close() time.sleep(2)
def test_factory(self): c = Config({}, service_name='x') assert callable(c.tchannel_factory) assert c.tchannel_factory() is None with self.assertRaises(ValueError): c = Config({'tchannel_factory': 'opentracing.tracer'}, service_name='x') c.tchannel_factory() cfg = {'tchannel_factory': 'tchannel.singleton.TChannel.get_instance'} c = Config(cfg, service_name='x') assert callable(c.tchannel_factory) with self.assertRaises(ImportError): cfg = {'tchannel_factory': 'tchannel.singleton.WrongTChannel.get_instance'} Config(cfg, service_name='x')
def test_throttler(self): c = Config({ 'throttler': {} }, service_name='x') assert not c.throttler_group() assert c.throttler_port == DEFAULT_THROTTLER_PORT assert c.throttler_refresh_interval == constants.DEFAULT_THROTTLER_REFRESH_INTERVAL c = Config({ 'throttler': { 'port': '1234', 'refresh_interval': '10' } }, service_name='x') assert c.throttler_group() assert c.throttler_port == 1234 assert c.throttler_refresh_interval == 10 c = Config({}, service_name='x') assert c.throttler_group() is None assert c.throttler_port is None assert c.throttler_refresh_interval is None
import requests import time from opentracing_instrumentation.client_hooks import install_all_patches from jaeger_client import Config from os import getenv JAEGER_HOST = getenv('JAEGER_HOST', 'localhost') WEBSERVER_HOST = getenv('WEBSERVER_HOST', 'localhost') # Create configuration object with enabled logging and sampling of all requests. config = Config(config={'sampler': {'type': 'const', 'param': 1}, 'logging': True, 'local_agent': {'reporting_host': JAEGER_HOST}}, service_name="jaeger_opentracing_example") tracer = config.initialize_tracer() # Automatically trace all requests made with 'requests' library. install_all_patches() url = "http://{}:5000/log".format(WEBSERVER_HOST) # Make the actual request to webserver. requests.get(url) # allow tracer to flush the spans - https://github.com/jaegertracing/jaeger-client-python/issues/50 time.sleep(2) tracer.close()
def test_initialize_tracer(self): c = Config({}, service_name='x') tracer = c.initialize_tracer() assert opentracing.tracer == tracer
def test_tags(self): os.environ['JAEGER_TAGS'] = 'a=b,c=d' c = Config({'tags': {'e': 'f'}}, service_name='x') assert c.tags == {'a': 'b', 'c': 'd', 'e': 'f'} c.create_tracer(NullReporter(), ConstSampler(True))