def test_missing_service_name(self): with self.assertRaises(ValueError): Config({})
""" import logging import time from jaeger_client import Config if __name__ == "__main__": log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='your-app-name', validate=True, ) # this call also sets opentracing.tracer tracer = config.initialize_tracer() with tracer.start_span('TestSpan') as span: span.log_kv({'event': 'test message', 'life': 42}) with tracer.start_span('ChildSpan', child_of=span) as child_span: span.log_kv({'event': 'down below'}) time.sleep(
import opentracing import logging import re from jaeger_client import Config from flask_opentracing import FlaskTracer app = Flask(__name__) app.secret_key = "super secret key" config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': "jaeger-agent", 'reporting_port': 5775, }, 'logging': True, }, service_name='cybermonday', ) opentracing_tracer = config.initialize_tracer() tracer = FlaskTracer(opentracing_tracer) def inject_as_headers(tracer, span): text_carrier = {} tracer.inject(span.context, opentracing.Format.TEXT_MAP, text_carrier) return text_carrier
def test_reporter_batch_size(self): c = Config({'reporter_batch_size': 12345}, service_name='x') assert c.reporter_batch_size == 12345 c = Config({}, service_name='x') assert c.reporter_batch_size == 10
def test_no_sampler(self): c = Config({}, service_name='x') assert c.sampler is None
def test_propagation(self): c = Config({}, service_name='x') assert c.propagation == {} c = Config({'propagation': 'b3'}, service_name='x') assert len(c.propagation) == 1
def test_initialize_tracer(self): c = Config({}, service_name='x') tracer = c.initialize_tracer() assert opentracing.tracer == tracer
def main(): LOG_FORMAT = '%(asctime)s - %(name)s:%(funcName)s:%(lineno)s - %(levelname)s: %(message)s' logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) logger.info('Starting microservice.py:main') sys.path.append(os.getcwd()) parser = argparse.ArgumentParser() parser.add_argument("interface_name", type=str, help="Name of the user interface.") parser.add_argument("api_type", type=str, choices=["REST", "GRPC", "FBS"]) parser.add_argument("--service-type", type=str, choices=[ "MODEL", "ROUTER", "TRANSFORMER", "COMBINER", "OUTLIER_DETECTOR"], default="MODEL") parser.add_argument("--persistence", nargs='?', default=0, const=1, type=int) parser.add_argument("--parameters", type=str, default=os.environ.get(PARAMETERS_ENV_NAME, "[]")) parser.add_argument("--log-level", type=str, default="INFO") parser.add_argument("--tracing", nargs='?', default=int(os.environ.get("TRACING", "0")), const=1, type=int) args = parser.parse_args() parameters = parse_parameters(json.loads(args.parameters)) # set up log level log_level_num = getattr(logging, args.log_level.upper(), None) if not isinstance(log_level_num, int): raise ValueError('Invalid log level: %s', args.log_level) logger.setLevel(log_level_num) logger.debug("Log level set to %s:%s", args.log_level, log_level_num) DEBUG = False if parameters.get(DEBUG_PARAMETER): parameters.pop(DEBUG_PARAMETER) DEBUG = True annotations = load_annotations() logger.info("Annotations: %s", annotations) interface_file = importlib.import_module(args.interface_name) user_class = getattr(interface_file, args.interface_name) if args.persistence: logger.info('Restoring persisted component') user_object = persistence.restore(user_class, parameters, debug=DEBUG) persistence.persist(user_object, parameters.get("push_frequency")) else: user_object = user_class(**parameters) if args.service_type == "MODEL": import seldon_core.model_microservice as seldon_microservice elif args.service_type == "ROUTER": import seldon_core.router_microservice as seldon_microservice elif args.service_type == "TRANSFORMER": import seldon_core.transformer_microservice as seldon_microservice elif args.service_type == "COMBINER": import seldon_core.combiner_microservice as seldon_microservice elif args.service_type == "OUTLIER_DETECTOR": import seldon_core.outlier_detector_microservice as seldon_microservice # set log level for the imported microservice type seldon_microservice.logger.setLevel(log_level_num) port = int(os.environ.get(SERVICE_PORT_ENV_NAME, DEFAULT_PORT)) if args.tracing: logger.info("Initializing tracing") from jaeger_client import Config jaeger_serv = os.environ.get("JAEGER_AGENT_HOST","0.0.0.0") jaeger_port = os.environ.get("JAEGER_AGENT_PORT", 5775) jaeger_config = os.environ.get("JAEGER_CONFIG_PATH",None) if jaeger_config is None: logger.info("Using default tracing config") config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': jaeger_serv, 'reporting_port': jaeger_port, }, 'logging': True, }, service_name=args.interface_name, validate=True, ) else: logger.info("Loading tracing config from %s",jaeger_config) import yaml with open(jaeger_config, 'r') as stream: config_dict = yaml.load(stream) config = Config( config=config_dict, service_name=args.interface_name, validate=True, ) # this call also sets opentracing.tracer tracer = config.initialize_tracer() if args.api_type == "REST": def rest_prediction_server(): app = seldon_microservice.get_rest_microservice( user_object, debug=DEBUG) if args.tracing: from flask_opentracing import FlaskTracer tracing = FlaskTracer(tracer,True, app) app.run(host='0.0.0.0', port=port) logger.info("REST microservice running on port %i",port) server1_func = rest_prediction_server elif args.api_type == "GRPC": def grpc_prediction_server(): if args.tracing: from grpc_opentracing import open_tracing_server_interceptor logger.info("Adding tracer") interceptor = open_tracing_server_interceptor(tracer) else: interceptor = None server = seldon_microservice.get_grpc_server( user_object, debug=DEBUG, annotations=annotations, trace_interceptor=interceptor) server.add_insecure_port("0.0.0.0:{}".format(port)) server.start() logger.info("GRPC microservice Running on port %i",port) while True: time.sleep(1000) server1_func = grpc_prediction_server elif args.api_type == "FBS": def fbs_prediction_server(): seldon_microservice.run_flatbuffers_server(user_object, port) logger.info("FBS microservice Running on port %i",port) server1_func = fbs_prediction_server else: server1_func = None if hasattr(user_object, 'custom_service') and callable(getattr(user_object, 'custom_service')): server2_func = user_object.custom_service else: server2_func = None logger.info('Starting servers') startServers(server1_func, server2_func)
__name__ = "fastapi_opentracing" from opentracing.scope_managers.contextvars import ContextVarsScopeManager from opentracing.propagation import Format from jaeger_client import Config import os # deployment.yaml will set the following env for each service project_name = os.getenv("PROJECT_NAME", "PROJECT_NAME") namespace = os.getenv("NAMESPACE", "NAMESPACE") config = Config( config={ "sampler": {"type": "const", "param": 1}, "logging": False, "reporter_queue_size": 2000, "propagation": "b3", # Compatible with istio "generate_128bit_trace_id": True, # Compatible with istio }, service_name=f"{project_name}.{namespace}", scope_manager=ContextVarsScopeManager(), validate=True, ) tracer = config.initialize_tracer() async def get_opentracing_span_headers(): scope = tracer.scope_manager.active carrier = {} if scope is not None: span = scope.span tracer.inject(
request.headers) logging.info(data) return data app = Flask(__name__) # Create configuration object with enabled logging and sampling of all requests. config = Config( config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'propagation': 'b3', 'local_agent': { 'reporting_host': JAEGER_HOST, 'reporting_port': JAEGER_PORT } }, # Service name can be arbitrary string describing this particular web service. service_name="alicek106-flask-test", ) jaeger_tracer = config.initialize_tracer() tracing = FlaskTracing(jaeger_tracer) @app.route('/') @tracing.trace() def flask_root_path():
import requests import time from opentracing_instrumentation.client_hooks import install_all_patches from jaeger_client import Config config = Config(config={'sampler':{'type': 'const','param': 1},'logging': True},service_name="jaeger_opentracing_example2") tracer = config.initialize_tracer() install_all_patches() url = 'http://localhost:4999/log2' r = requests.get(url) time.sleep(2) tracer.close()
from jaeger_client import Config from opentracing.propagation import Format app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' metrics = PrometheusMetrics(app) metrics.info('workout_gateway', 'Workout Gateway', version='1.0.3') config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='workout-gateway', validate=True, ) tracer = config.initialize_tracer() class WorkoutKind(Enum): RUNNING = "running" JUMPING = "jumping" SWIMMING = "swimming" RUN_CONTROLLER_URL = "please provide url via env vars"
def test_default_local_agent_reporting_port(self): c = Config({}, service_name='x') assert c.local_agent_reporting_port == 6831
def test_disable_metrics(self): config = Config({'metrics': False}, service_name='x') assert isinstance(config._metrics_factory, MetricsFactory)
JAEGER_HOST = getenv('JAEGER_HOST', 'localhost') if __name__ == '__main__': app = Flask(__name__) log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) # Create configuration object with enabled logging and sampling of all requests. config = Config( config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'local_agent': # Also, provide a hostname of Jaeger instance to send traces to. { 'reporting_host': JAEGER_HOST } }, # Service name can be arbitrary string describing this particular web service. service_name="jaeger_opentracing_example") jaeger_tracer = config.initialize_tracer() tracer = FlaskTracer(jaeger_tracer) @app.route('/log') @tracer.trace() # Indicate that /log endpoint should be traced def log(): parent_span = tracer.get_span(request) # Extract the span information for request object.
""" opentracing and functions """ import logging from random import randint from time import sleep from jaeger_client import Config logging.getLogger('').handlers = [] logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s') config = Config( config={ 'sampler': { 'type': 'const', 'param': '1', }, 'logging': True, 'metrics': True, }, service_name='pythonFunc', ) tracer = config.initialize_tracer() def function1(): """function1""" span = tracer.start_span('function1') try: logging.info("function1") span.set_baggage_item("token", "token")
from random import randint from time import sleep from flask import request from flask import Flask from jaeger_client import Config from flask_opentracing import FlaskTracing app = Flask(__name__) config = Config(config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'reporter_batch_size': 1, }, service_name="service") jaeger_tracer = config.initialize_tracer() tracing = FlaskTracing(jaeger_tracer, True, app) counter_value = 1 def get_counter(): return str(counter_value) def increase_counter(): global counter_value int(counter_value)
import time from jaeger_client import Config if __name__ == "__main__": log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', # 全部 'param': 1, # 1开启全部采样 0 关闭全部采样 }, 'local_agent': { 'reporting_host': '127.0.0.1', 'reporting_port': '6831', }, 'logging': True, }, service_name='your-app-name', validate=True, ) # this call also sets opentracing.tracer tracer = config.initialize_tracer() # get_span = tracer.start_span('get') # requests.get("https://www.imooc.com") # get_span.finish() with tracer.start_span("get") as get_span: requests.get("https://www.imooc.com")
def test_for_unexpected_config_entries(self): with self.assertRaises(Exception): _ = Config({"unexpected": "value"}, validate=True)
import pickle from opentracing.propagation import Format import json from dls.dls_api import app, register_resource, register_host, register_span, initialize_group, run_group, configure_tracer, initialize_host from jaeger_client import Config hostname = os.environ["WORKER_HOST"] configure_tracer(Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='dls', validate=True )) def connect_to_database(resources, host): resources.conn = psycopg2.connect("dbname='forum' user='******' host='" + host + "' password='******'") print(resources.conn) def connect_to_redis(resources, host): resources.r = redis.Redis(host=host, port=6379, db=0)
def test_enabled(self): c = Config({'enabled': True}, service_name='x') assert c.enabled c = Config({'enabled': False}, service_name='x') assert not c.enabled
import requests import time from opentracing_instrumentation.client_hooks import install_all_patches from jaeger_client import Config JAEGER_HOST = "localhost" WEBSERVER_HOST = "localhost" 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() install_all_patches() url = "http://{}:5000/log".format(WEBSERVER_HOST) requests.get(url) url = f'http://{WEBSERVER_HOST}:5000/ep1' requests.get(url) url = f'http://{WEBSERVER_HOST}:5000/ep2' 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_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))
JAEGER_HOST = os.environ['JAEGER_REPORTING_HOST'] JAEGER_PORT = os.environ['JAEGER_REPORTING_PORT'] JAEGER_SVC_NAME = os.environ['JAEGER_SVC_NAME'] AUTH_ENDPOINT = os.environ['AUTH_ENDPOINT'] app = Flask(__name__) app.secret_key = "super secret key" config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': JAEGER_HOST, 'reporting_port': JAEGER_PORT, }, 'logging': True, }, service_name=JAEGER_SVC_NAME, ) opentracing_tracer = config.initialize_tracer() tracer = FlaskTracer(opentracing_tracer) def inject_as_headers(tracer, span): text_carrier = {} tracer.inject(span.context, opentracing.Format.TEXT_MAP, text_carrier) return text_carrier def validate_email(email):
def test_bad_sampler(self): c = Config({'sampler': {'type': 'bad-sampler'}}, service_name='x') with self.assertRaises(ValueError): c.sampler.is_sampled(0)
from flask import Flask, request JAEGER_HOST = 'jaeger-agent.default.svc.cluster.local' JAEGER_PORT = '6831' if __name__ == '__main__': app = Flask(__name__) log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) config = Config(config={ 'sampler': { 'type': 'const', 'param': True }, 'logging': True, 'local_agent': { 'reporting_host': JAEGER_HOST, 'reporting_post': JAEGER_PORT } }, service_name="opentracing_server_app") jaeger_tracer = config.initialize_tracer() tracing = FlaskTracing(jaeger_tracer) @app.route('/trace') @tracing.trace() def trace(): with jaeger_tracer.start_active_span( 'trace method (Flask app)') as scope: scope.span.log_kv({ 'event':
from rust_python_jaeger_reporter import Reporter if __name__ == "__main__": import logging log_level = logging.DEBUG logging.getLogger("").handlers = [] logging.basicConfig(format="%(asctime)s %(message)s", level=log_level) reporter = Reporter() config = Config( config={ # usually read from some yaml config "sampler": { "type": "const", "param": 1, }, # 'logging': True, }, service_name="your-app-name", ) tracer = config.create_tracer(reporter, config.sampler) # tracer = config.initialize_tracer() opentracing.set_global_tracer(tracer) i = 0 # for _ in range(100000): # tr = tracker.SummaryTracker() # tr = tracker.SummaryTracker()
# Set up the tracer cfg = { 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, 'local_agent': { 'reporting_host': jaeger_agent_host, }, } if istio > 0: cfg['propagation'] = 'b3' config = Config( config=cfg, service_name='router', validate=True, ) # this call also sets opentracing.tracer tracer = config.initialize_tracer() httpd = http.server.HTTPServer(('0.0.0.0', listen_port), Router) httpd.ready = True httpd.tracer = tracer # Simple handler function to show that we we're handling the SIGTERM def do_shutdown(signum, frame): global httpd log = { 'router': { 'message': 'Graceful shutdown.' } }
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. By default this function is partially memoized so that the tracer returned on first invocation will always be returned in subsequent calls, no matter their arguments. It can be overridden if `allow_multiple` is a provided as a named argument with True value, with the resulting tracer being cached. """ global _tracer allow_multiple = kwargs.pop('allow_multiple', False) if not allow_multiple and _tracer is not None: return _tracer 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_ENDPOINT_URL', _get_env_var( 'SIGNALFX_INGEST_URL', # Backwards compatibility 'http://localhost:9080/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 logger = logging.getLogger('signalfx-tracing') config['logging'] = True config['logger'] = logger if _get_env_var('SIGNALFX_TRACING_DEBUG', False): logger.setLevel(logging.DEBUG) config['root_span_tags'] = { SFX_TRACING_LIBRARY: 'python-tracing', SFX_TRACING_VERSION: __version__ } config['max_tag_value_length'] = int( _get_env_var( 'SIGNALFX_RECORDED_VALUE_MAX_LENGTH', default_max_tag_value_length, )) jaeger_config = Config(config, *args, **kwargs) tracer = jaeger_config.new_tracer() atexit.register(tracer.close) if set_global: import opentracing opentracing.tracer = tracer _tracer = tracer return tracer
def test_reporter_queue_size_valid(self): config = Config({'reporter_queue_size': 100}, service_name='x', validate=True) assert config.reporter_queue_size == 100