示例#1
0
    def setup_tracer(
        cls,
        service_name: str = "kerasltiprovider",
        tracer_host: str = "localhost",
        tracer_port: int = 6831,
    ) -> None:
        cls.service_name = service_name
        cls.tracer_host = tracer_host
        cls.tracer_port = tracer_port

        cls.config = dict(sampler=dict(type="const", param=1), logging=True)

        if None not in (cls.tracer_host, cls.tracer_port):
            cls.config.update(
                dict(
                    local_agent=dict(
                        reporting_host=cls.tracer_host, reporting_port=cls.tracer_port
                    )
                )
            )

        jaeger = jaeger_client.Config(
            config=cls.config,
            service_name=cls.service_name,
            metrics_factory=jaeger_metrics.PrometheusMetricsFactory(
                namespace=cls.service_name
            ),
            validate=True,
        )

        cls._tracer = jaeger.initialize_tracer()
示例#2
0
def get_tracer() -> jaeger_client.Config: # pragma: no cover
    """Function used to retrieve Jaeger Tracer.
    The Jaeger Host and Port can be specified in
    the environment variables, else the default
    connection to localhost at UDP port 6831
    will be used"""

    service_name = os.environ.get('SERVICE_NAME')
    
    logger.info('Getting Tracer for service %s', service_name)

    jaeger_config = {
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'logging': True
    } 
    
    # get jaeger host and port from environment variables
    jaeger_host, jaeger_port = os.environ.get('JAEGER_HOST', 'localhost'), os.environ.get('JAEGER_PORT', 6831)

    logger.debug('Creating Jaeger Tracer for %s:%s', jaeger_host, jaeger_port)

    jaeger_config['local_agent'] = {'reporting_host': jaeger_host, 'reporting_port': int(jaeger_port)}

    # create jaeger client config object and return tracer
    _config = jaeger_client.Config(config=jaeger_config, service_name=service_name, validate=True)

    return _config.initialize_tracer()
示例#3
0
def initialize_tracing():
    if args.jaeger:
        print("Enabling Jaeger traces")
        global opentracing
        import opentracing
        import jaeger_client

        from urllib.parse import urlparse
        agent_addr = urlparse(args.jaeger)
        if not agent_addr.netloc:
            raise Exception("invalid jaeger agent address: {}".format(
                args.jaeger))
        if not agent_addr.hostname:
            raise Exception(
                "missing hostname in jaeger agent address: {}".format(
                    args.jaeger))
        if not agent_addr.port:
            raise Exception("missing port in jaeger agent address: {}".format(
                args.jaeger))
        tracer_config = {
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'local_agent': {
                'reporting_host': agent_addr.hostname,
                'reporting_port': agent_addr.port
            },
            'logging': True
        }
        config = jaeger_client.Config(config=tracer_config,
                                      service_name='NVCClient',
                                      validate=True)
        global tracer
        tracer = config.initialize_tracer()
def get_tracer_config(service_name):
    if config.TRACING_CONFIG_FILENAME is not None:
        tracing_config = load_yaml_config(config.TRACING_CONFIG_FILENAME)
    else:
        tracing_config = DEFAULT_TRACER_CONFIG
    return jaeger_client.Config(config=tracing_config,
                                service_name=service_name)
示例#5
0
    def __init__(self, connection_str, project=None, service=None, host=None,
                 conf=cfg.CONF, **kwargs):
        """Jaeger driver for OSProfiler."""

        super(Jaeger, self).__init__(connection_str, project=project,
                                     service=service, host=host,
                                     conf=conf, **kwargs)
        try:
            import jaeger_client
            self.jaeger_client = jaeger_client
        except ImportError:
            raise exc.CommandError(
                "To use OSProfiler with Uber Jaeger tracer, "
                "you have to install `jaeger-client` manually. "
                "Install with pip:\n `pip install jaeger-client`."
            )

        parsed_url = parser.urlparse(connection_str)
        cfg = {
            "local_agent": {
                "reporting_host": parsed_url.hostname,
                "reporting_port": parsed_url.port,
            }
        }

        # Initialize tracer for each profiler
        service_name = "{}-{}".format(project, service)
        config = jaeger_client.Config(cfg, service_name=service_name)
        self.tracer = config.initialize_tracer()

        self.spans = collections.deque()
示例#6
0
def init_tracer(service):
    """Initialize an OpenTracing tracer.

    Args:
        service ([string]): the name of the service

    Returns:
        [tracer]: an OpenTracing tracer
    """

    agent = environ.get("TRACING_AGENT_ADDR", settings.DEFAULT_TRACING_ADDR)
    LOGGER.info("Initialize Jaeger client for service: %s %s", service, agent)
    tracer_conf = jaeger_client.Config(
        config={
            "sampler": {
                "type": "const",
                "param": 1
            },
            "local_agent": {
                "reporting_host": agent,
                "reporting_port": config.DEFAULT_REPORTING_PORT,
            },
            "logging": True,
            "reporter_batch_size": 1,
        },
        service_name=service,
    )

    # this call also sets opentracing.tracer
    return tracer_conf.initialize_tracer()
示例#7
0
def get_tracer() -> jaeger_client.Config: # pragma: no cover
    """Function used to retrieve Jaeger Tracer.
    The Jaeger Host and Port can be specified in
    the environment variables, else the default
    connection to localhost at UDP port 6831
    will be used"""

    LOGGER.info('getting jaeger tracer for service %s', config.JAEGER_CONFIG.service_name)

    jaeger_config = {
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'logging': True
    } 
    
    # enable prometheus metrics
    if config.ENABLE_JAEGER_WITH_PROMETHEUS:
        config['metrics_factory'] = prometheus.PrometheusMetricsFactory(service_name_label=config.JAEGER_CONFIG.service_name)
    
    LOGGER.debug('Creating Jaeger Tracer for %s:%s', config.JAEGER_CONFIG.jaeger_host, config.JAEGER_CONFIG.jaeger_port)

    jaeger_config['local_agent'] = {
        'reporting_host': config.JAEGER_CONFIG.jaeger_host, 
        'reporting_port': config.JAEGER_CONFIG.jaeger_port
    }

    # create jaeger client config object and return tracer
    _config = jaeger_client.Config(config=jaeger_config, service_name=config.JAEGER_CONFIG.service_name, validate=True)

    return _config.initialize_tracer()
示例#8
0
def init_tracer(config: dict):
    config = jaeger_client.Config(
        config=config,
        validate=True,
        scope_manager=contextvars.ContextVarsScopeManager(),
        service_name=config["service_name"],
    )
    global_tracer = config.new_tracer()
    opentracing.set_global_tracer(global_tracer)
    atexit.register(close_tracer)
示例#9
0
def init_tracer(service_name):
    global _tracer
    if _tracer is not None:
        return _tracer

    config = jaeger_client.Config(config={},
                                  service_name=service_name,
                                  validate=True)
    config.initialize_tracer()
    _tracer = opentracing.global_tracer()
    # A nasty hack to ensure enough time for the tracing data to be flushed
    atexit.register(_fini_tracer)
    return _tracer
示例#10
0
def create_tracer(service_name='worker'):
    jaeger_config = jaeger_client.Config(config={
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'local_agent': {
            'reporting_host': config.TRACING_HOST,
            'reporting_port': config.TRACING_PORT,
        }
    },
                                         service_name=service_name)

    return jaeger_config.new_tracer()
示例#11
0
def init_jaeger(app):
    config = jaeger_client.Config(config={
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'logging': True,
        'local_agent': {
            'reporting_host': settings.JAEGER_HOST
        }
    },
                                  service_name='books')
    jaeger_tracer = config.initialize_tracer()
    flask_opentracing.FlaskTracing(jaeger_tracer, app=app)
    install_all_patches()
示例#12
0
def initialize_tracer(service_name='anonlink'):
    jaeger_config = jaeger_client.Config(config={
        'sampler': {
            'type': 'const',
            'param': 1
        },
        'local_agent': {
            'reporting_host': config.TRACING_HOST,
            'reporting_port': config.TRACING_PORT,
        }
    },
                                         service_name=service_name)

    # Note this call also sets opentracing.tracer
    return jaeger_config.initialize_tracer()
示例#13
0
def initialize_tracer():
    """
    initialize_tracer
    """
    config = jaeger.Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name='swapi',
    )
    return config.initialize_tracer()
示例#14
0
def initialize_tracer():
    log_level = logging.DEBUG
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)
    config = jaeger_client.Config(
        config={ # usually read from some yaml config
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name='flask-tracing-app',
        validate=True,
    )
    # return config.initialize_tracer(io_loop=ioloop.IOLoop.current())
    return config.initialize_tracer()
示例#15
0
    def __init__(self,
                 service_name=None,
                 logger=None,
                 reporting_host=None,
                 reporting_port=None):
        # type: (Optional[str], Optional[ContextAdapter], Optional[str], Optional[int]) -> None

        if not tracing_client or os.getenv(TRACING_DISABLE_ENVVAR):
            return

        self.logger = logger or Logging.get_logger()

        if not service_name:
            service_name = os.getenv(TRACING_SERVICE_NAME_ENVVAR,
                                     default=DEFAULT_TRACING_SERVICE_NAME)

        if not reporting_host:
            reporting_host = os.getenv(TRACING_REPORTING_HOST_ENVVAR,
                                       default=DEFAULT_TRACING_REPORTING_HOST)

        if not reporting_port:
            # pylint: disable=bad-option-value,invalid-envvar-default
            reporting_port = int(
                os.getenv(TRACING_REPORTING_PORT_ENVVAR,
                          default=DEFAULT_TRACING_REPORTING_PORT))

        config = tracing_client.Config(
            config={
                # Using `const` sampler - the same sampling decission for all spans,
                # and that decision is "record" (because `param == 1`).
                'sampler': {
                    'type': 'const',
                    'param': 1
                },
                'local_agent': {
                    'reporting_host': reporting_host,
                    'reporting_port': reporting_port
                },
                'logging': True
            },
            service_name=service_name,
            validate=True)

        Tracer.TRACER = config.initialize_tracer()
示例#16
0
def init_tracer():
    config = {
        'sampler': {
            'type': 'const',
            'param': 1,
        },
        'logging': True,
    }

    if FLAGS.consul:
        jaeger_host, jaeger_port = consul_resolve(
            FLAGS.jaeger_service).split(':')
        logging.info(f'Sending Jaeger tracing to {jaeger_host}:{jaeger_port}')
        config['local_agent'] = {
            'reporting_host': jaeger_host,
            'reporting_port': jaeger_port,
        }
    config_obj = jaeger_client.Config(config, service_name='steward.app')
    return config_obj.initialize_tracer()
示例#17
0
def initialize_tracer():
    install_all_patches()

    config = jaeger_client.Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1
            },
            'logging': True,
            'local_agent': {
                'reporting_host': JAEGER_REPORTING_HOST,
            }
        },
        service_name='tracebacks',
        validate=True,
        metrics_factory=PrometheusMetricsFactory(namespace='tracebacks'),
    )

    return config.initialize_tracer()  # also sets opentracing.tracer
示例#18
0
    def __init__(
        self,
        config: Dict[str, Any],
        custom_reactor: SygnalReactor,
        tracer: Tracer = opentracing.tracer,
    ):
        """
        Object that holds state for the entirety of a Sygnal instance.
        Args:
            config: Configuration for this Sygnal
            custom_reactor: a Twisted Reactor to use.
            tracer (optional): an OpenTracing tracer. The default is the no-op tracer.
        """
        self.config = config
        self.reactor = custom_reactor
        self.pushkins: Dict[str, Pushkin] = {}
        self.tracer = tracer

        logging_dict_config = config["log"]["setup"]
        logging.config.dictConfig(logging_dict_config)

        logger.debug("Started logging")

        observer = twisted_log.PythonLoggingObserver()
        observer.start()

        proxy_url = config.get("proxy")
        if proxy_url is not None:
            logger.info("Using proxy configuration from Sygnal configuration file")
        else:
            proxy_url = os.getenv("HTTPS_PROXY")
            if proxy_url:
                logger.info(
                    "Using proxy configuration from HTTPS_PROXY environment variable."
                )
                config["proxy"] = proxy_url

        sentrycfg = config["metrics"]["sentry"]
        if sentrycfg["enabled"] is True:
            import sentry_sdk

            logger.info("Initialising Sentry")
            sentry_sdk.init(sentrycfg["dsn"])

        if config.get("db") is not None:
            logger.warning(
                "Config includes the legacy 'db' option and will be ignored"
                " as Sygnal no longer uses a database, this field can be removed"
            )

        if config.get("database") is not None:
            logger.warning(
                "Config includes the legacy 'database' option and will be ignored"
                " as Sygnal no longer uses a database, this field can be removed"
            )

        promcfg = config["metrics"]["prometheus"]
        if promcfg["enabled"] is True:
            prom_addr = promcfg["address"]
            prom_port = int(promcfg["port"])
            logger.info(
                "Starting Prometheus Server on %s port %d", prom_addr, prom_port
            )

            prometheus_client.start_http_server(port=prom_port, addr=prom_addr or "")

        tracecfg = config["metrics"]["opentracing"]
        if tracecfg["enabled"] is True:
            if tracecfg["implementation"] == "jaeger":
                try:
                    import jaeger_client

                    jaeger_cfg = jaeger_client.Config(
                        config=tracecfg["jaeger"],
                        service_name=tracecfg["service_name"],
                        scope_manager=AsyncioScopeManager(),
                    )

                    jaeger_tracer = jaeger_cfg.initialize_tracer()
                    assert jaeger_tracer is not None
                    self.tracer = jaeger_tracer

                    logger.info("Enabled OpenTracing support with Jaeger")
                except ModuleNotFoundError:
                    logger.critical(
                        "You have asked for OpenTracing with Jaeger but do not have"
                        " the Python package 'jaeger_client' installed."
                    )
                    raise
            else:
                raise RuntimeError(
                    "Unknown OpenTracing implementation: %s.", tracecfg["impl"]
                )
示例#19
0
def main():
    # log everything to stderr because compose containers for some reason aren't logging stdout
    logging.basicConfig(level=logging.DEBUG,
                        filename='/proc/self/fd/2',
                        filemode='w')

    peers = None if "NBDD_PEERS" not in os.environ else os.environ[
        "NBDD_PEERS"].split(",")
    hostname = os.environ.get("NBDD_HOSTNAME")
    # contains all blocks for all devices as a contiguous list of bytes
    blocks = []
    # a list of all devices so we know the starting offset of a given device in `blocks`
    # (all devices are fixed size)
    volumes = []

    tracer = jaeger_client.Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name='nbd',
    ).initialize_tracer()

    if peers:
        LocalState.f = open('/tmp/blocks', 'r+b')
        write_cache = LoglessCache()
        LocalState.write_sharer = WriteSharer(peers, write_cache)
        _thread.start_new_thread(LocalState.write_sharer.listen_for_asks, ())
        LocalState.lock = threading.Lock()
        LocalState.hostname = hostname
        LocalState.write_count = 0
        blocks = ReplFile()
        volumes = ReplList()
        health_counter = ReplCounter()
        HealthHandler.counter = health_counter
        self_address = "{}:2001".format(hostname)
        peer_addresses = ["{}:2001".format(peer) for peer in peers]
        syncObj = SyncObj(self_address,
                          peer_addresses,
                          consumers=[blocks, volumes, health_counter])

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('0.0.0.0', 2000))
    sock.setblocking(True)
    sock.listen(1)

    httpd = HTTPServer(('0.0.0.0', 8080), HealthHandler)
    _thread.start_new_thread(httpd.serve_forever, ())

    # Prototype will listen to one client at a time
    # -- can be made concurrent without much extra work
    logging.info("NBD Server '{}' Starting with peers {}...".format(
        hostname, peers))
    while True:
        cxn, client = sock.accept()
        logging.info("Connection accepted from client {}".format(client))
        _thread.start_new_thread(handle_cxn, (cxn, blocks, volumes, tracer))
        logging.info(
            "Connection closed by client {} -- listening for next client".
            format(client))
示例#20
0
TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

TRACER = jaeger_client.Config(
    config={
        'sampler': {
            'type': 'const',
            'param': 1,
        },
        'local_agent': {
            'reporting_host': "127.0.0.1",
            'reporting_port': 5775,
        },
        'logging': True,
    },
    service_name='delivery',
).initialize_tracer()

OPENTRACING_TRACE_ALL = False
OPENTRACING_TRACER = django_opentracing.DjangoTracing(TRACER)
示例#21
0
def main():
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    loop = asyncio.get_event_loop()
    logging.basicConfig(level=os.getenv('LOG_LEVEL', logging.INFO))
    logging.getLogger("asyncio").setLevel(logging.ERROR)
    logging.getLogger("aiohttp").setLevel(logging.ERROR)
    logging.getLogger("jaeger_tracing").setLevel(logging.ERROR)
    logger = logging.getLogger(__name__)
    ctx.logger = logger
    h = '0.0.0.0'

    # configuration
    config = os.getenv("CONFIG")
    if not config:
        raise EnvironmentError("no config file specified")
    if not os.path.exists(config):
        raise EnvironmentError("config file does not exist")
    logger.info("Reading configuration from {config}".format(**locals()))
    try:
        with open(config, "r") as cfg:
            config = ujson.load(cfg)
    except ValueError:
        raise EnvironmentError("could not parse config file")
    jsonschema.validate(config, schema)

    # tracer
    jport = os.getenv('JAEGER_AGENT_PORT')
    if jport:
        config = jaeger_client.Config(config={
            'sampler': {
                'type': 'const',
                'param': 1
            },
            'logging': True,
        },
                                      service_name='pyskull',
                                      validate=True)
        ctx.tracer = config.initialize_tracer()
        logger.info("jaeger started against {}:{}".format(
            os.getenv('JAEGER_AGENT_HOST'), jport))

    # http api server
    runner = None
    api_port = os.environ['HTTP_PORT']
    if api_port:
        from pyskull.http import routes, middlewares
        from aiohttp import web
        app = web.Application(middlewares=middlewares)
        app.add_routes(routes)
        runner = web.AppRunner(app)
        loop.run_until_complete(runner.setup())
        site = web.TCPSite(runner, h, int(api_port))
        loop.run_until_complete(site.start())
        logger.info("HTTP API started on {h}:{api_port}".format(**locals()))

    # grpc server
    gapi = None
    gport = os.getenv('GRPC_PORT')
    if gport:
        from pyskull import grpc as g
        from grpclib.server import Server
        gapi = Server([g.Greeter()], loop=loop)
        loop.run_until_complete(gapi.start('0.0.0.0', 50051))
        logger.info("gRPC started on {h}:{gport}".format(**locals()))

    # metrics server
    mserver = None
    metrics_port = os.environ['METRICS_PORT']
    if metrics_port:
        mserver = MetricsServer()
        loop.run_until_complete(mserver.start(h, metrics_port))
        logger.info("metrics started on {h}:{metrics_port}".format(**locals()))

    def killer(**kwargs):
        raise KeyboardInterrupt

    loop.add_signal_handler(signal.SIGINT,
                            lambda: asyncio.ensure_future(killer()))
    loop.add_signal_handler(signal.SIGTERM,
                            lambda: asyncio.ensure_future(killer()))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        logger.info('shutting down')
        cleanup = []
        if runner:
            cleanup += [asyncio.ensure_future(runner.cleanup())]

        if gapi:
            gapi.close()
            cleanup += [asyncio.ensure_future(gapi.wait_closed())]

        if mserver:
            cleanup += [asyncio.ensure_future(mserver.stop())]

        loop.run_until_complete(asyncio.gather(*cleanup))
        loop.close()

        # allow for tracer to flush if enabled
        if ctx.tracer:
            time.sleep(2)
            ctx.tracer.close()
示例#22
0
    def setup(self):
        logging.info("setting up")
        self.command = self.getinfo['searchinfo']["command"]
        search_id = self.getinfo['searchinfo']["sid"]
        self.dispatch_dir = self.getinfo['searchinfo']["dispatch_dir"]
        logging.info("command name: %s" % self.command)
        is_searchpeer = "searchpeers" in __file__

        command_options = options_parser.parse_options(
            self.getinfo['searchinfo']["args"],
            [
                "model",
                "method",
                "algorithm",
                "environment",
                "type",
                "max_buffer_size",
                "is_preop",
                "prevent_preop",
                "fields",
                "opentracing_endpoint",
                "opentracing_user",
                "opentracing_password",
                "trace_context",
                "search_id",
                "trace_level",
            ],
            ignore_unknown=True,
        )
        #raise Exception("command_options: %s" % command_options)

        if "search_id" in command_options:
            search_id = command_options["search_id"]

        if "trace_level" in command_options:
            trace_level = command_options["trace_level"]
        else:
            trace_level = ""

        if search_id.startswith("searchparsetmp") or search_id.endswith(
                "_tmp"):
            is_temp_search = True
        else:
            is_temp_search = False
        logging.info("is_temp_search: %s" % is_temp_search)
        if len(search_id) > 20:
            search_id = search_id[-20:]

        app_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))
        if "fields" not in command_options:
            raise Exception("missing 'fields' parameter")
        self.fields = command_options["fields"].split(",")
        if "is_preop" in command_options:
            is_preop = is_truthy(command_options["is_preop"])
        else:
            is_preop = False
        splunk = ConfBasedService(app_path)

        logging.info("is_preop: %s" % is_preop)
        if "prevent_preop" in command_options:
            prevent_preop = is_truthy(command_options["prevent_preop"])
        else:
            prevent_preop = False
        logging.info("prevent_preop: %s" % prevent_preop)

        if "model" in command_options:
            self.model = model.get(splunk, command_options["model"])
            self.deployment = self.model.deployment
        else:
            if not "algorithm" in command_options:
                raise Exception("missing 'algorithm' parameter")
            algorithm_name = command_options["algorithm"]
            if not "environment" in command_options:
                raise Exception("missing 'environment' parameter")
            environment_name = command_options["environment"]
            self.deployment = deployment.get(splunk, algorithm_name,
                                             environment_name)
            if not self.deployment:
                raise Exception(
                    "Algorithm \"%s\" is not deployed to environment \"%s\"" %
                    (algorithm_name, environment_name))
            self.model = None

        if not "method" in command_options:
            default_method_name = self.deployment.algorithm.default_method
            if default_method_name:
                method_name = default_method_name
            else:
                raise Exception("missing 'method' parameter. E.g. method=fit")
        else:
            method_name = command_options["method"]
        method = self.deployment.algorithm.get_method(method_name)
        if not method:
            raise Exception("method '%s' does not exist" % method_name)

        if "max_buffer_size" in command_options:
            max_buffer_size = command_options["max_buffer_size"]
        else:
            max_buffer_size = method.max_buffer_size
        if not max_buffer_size or max_buffer_size == "auto":
            # keep initial class value
            pass
        elif self.max_buffer_size == "all":
            self.max_buffer_size = 0
        else:
            self.max_buffer_size = int(self.max_buffer_size)
        self.reset_buffer()

        if not "type" in command_options:
            command_type = method.command_type
        else:
            command_type = command_options["type"]

        opentracing_endpoint = self.deployment.environment.opentracing_endpoint
        opentracing_user = self.deployment.environment.opentracing_user
        opentracing_password = self.deployment.environment.opentracing_password
        if "opentracing_endpoint" in command_options:
            opentracing_endpoint = command_options["opentracing_endpoint"]
        if "opentracing_user" in command_options:
            opentracing_user = command_options["opentracing_user"]
        if "opentracing_password" in command_options:
            opentracing_password = command_options["opentracing_password"]
        if "trace_context" in command_options:
            self.trace_context_string = command_options["trace_context"]

        if opentracing_endpoint and opentracing_user and opentracing_password:
            trace_config = jaeger_client.Config(
                config={
                    'sampler': {
                        'type': 'const',
                        'param': 1,
                    },
                    'logging': False,
                    'jaeger_endpoint': opentracing_endpoint,
                    'jaeger_user': opentracing_user,
                    'jaeger_password': opentracing_password,
                    "propagation": "b3",
                },
                service_name="indexer" if is_searchpeer else "search_head",
                validate=True,
            )
            self.tracer = trace_config.initialize_tracer()
        else:
            self.tracer = opentracing.tracer

        if self.trace_context_string:
            trace_context_bytes = self.trace_context_string.encode()
            trace_context_bytes = base64.b64decode(trace_context_bytes)
            trace_context_dict = json.loads(trace_context_bytes)
            trace_context = opentracing.tracer.extract(
                format=opentracing.propagation.Format.TEXT_MAP,
                carrier=trace_context_dict)
        else:
            trace_context = None
        self.trace_scope = self.tracer.start_span(
            "compute-command",
            child_of=trace_context,
            # ignore_active_span
            tags={
                'dltk-method': method_name,
                'dltk-search_id': search_id,
                'dltk-algorithm': self.deployment.algorithm.name,
                'dltk-runtime': self.deployment.algorithm.runtime.name,
                'dltk-environment': self.deployment.environment.name,
                'dltk-preop': is_preop,
                'dltk-command_type': command_type,
            })
        if not self.trace_context_string:
            trace_context_dict = {}
            opentracing.tracer.inject(
                span_context=self.trace_scope,
                format=opentracing.propagation.Format.TEXT_MAP,
                carrier=trace_context_dict)
            trace_context_json = json.dumps(trace_context_dict)
            self.trace_context_string = base64.b64encode(
                trace_context_json.encode()).decode()
        if not trace_context:
            trace_context_dict = {}
            opentracing.tracer.inject(
                span_context=self.trace_scope,
                format=opentracing.propagation.Format.HTTP_HEADERS,
                carrier=trace_context_dict)
            if "X-B3-TraceId" in trace_context_dict:
                self.trace_id = trace_context_dict["X-B3-TraceId"]

        if self.deployment.is_disabled:
            raise Exception("Algorithm '%s' disabled for environment '%s'" % (
                self.deployment.algorithm_name,
                self.deployment.environment_name,
            ))
        if not self.deployment.is_deployed:
            raise Exception(
                "Algorithm '%s' not yet successfully deployed to environment '%s'. (status: %s message: %s)"
                % (
                    self.deployment.algorithm_name,
                    self.deployment.environment_name,
                    self.deployment.status,
                    self.deployment.status_message,
                ))
        self.context = ExecutionContext(
            is_preop=is_preop,
            is_searchpeer=is_searchpeer,
            search_id=search_id,
            model=self.model,
            root_trace_context_string=self.trace_context_string,
            method=method,
            message_logger=self.messages_logger,
            fields=self.fields,
            params=command_options)

        if not is_temp_search:
            with self.tracer.start_active_span(
                    'initialize',
                    child_of=self.trace_scope,
            ):
                self.execution = self.deployment.create_execution(self.context)
                try:
                    self.execution.logger.warning(
                        "starting execution setup (search=\"%s\")" %
                        self.getinfo['searchinfo']["search"])
                    self.execution.setup()
                    self.execution.logger.warning("execution setup stopped")
                except Exception as e:
                    self.execution.logger.warning(traceback.format_exc())
                    self.die(
                        "Unexpected error starting deployment execution: %s" %
                        (', '.join(traceback.format_exception_only(type(e),
                                                                   e))))

        # https://community.splunk.com/t5/Splunk-Search/Metadata-fields-of-custom-search-command/m-p/300360
        # mltk fit: EVENTS
        # mltk apply: streaming or stateful
        info = {
            "type": command_type,
            "required_fields": self.fields,
            # generating
            # maxinputs
        }
        # streaming_preop
        support_preop = method.support_preop and not prevent_preop
        if not is_preop and command_type == "reporting" and support_preop:
            info.update({
                "streaming_preop":
                "%s is_preop=1 type=streaming %s %s %s %s %s %s %s %s" % (
                    self.command,
                    "search_id=\"%s\"" % search_id,
                    "method=\"%s\"" % method_name,
                    ("model=\"%s\"" % command_options["model"])
                    if "model" in command_options else "",
                    ("algorithm=\"%s\"" % command_options["algorithm"])
                    if "algorithm" in command_options else "",
                    ("environment=\"%s\"" % command_options["environment"])
                    if "environment" in command_options else "",
                    ("max_buffer_size=%s" % command_options["max_buffer_size"])
                    if "max_buffer_size" in command_options else "",
                    ("fields=\"%s\"" % command_options["fields"])
                    if "fields" in command_options else "",
                    "trace_context=\"%s\"" % self.trace_context_string,
                ),
            })

        return info
示例#23
0
def main():
    log_level = logging.INFO
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)

    config = jaeger_client.Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': False,
            'reporter_batch_size': 1,
        },
        service_name=APPNAME,
        validate=True,
    )

    tracer = config.initialize_tracer()

    con = pymongo.MongoClient(MONGODB_URL)
    db = con['test']
    kittens = db['kittens']

    # Generate some seed data
    kittens.delete_many({})
    for i in range(NUM_KITTENS):
        kittens.insert_one({"name": "Purry", "id": i})

    # XXX: Make this a cmdline option
    test_big_documents = False

    if test_big_documents:
        # Insert one that is close to MongoDb limit
        kittens.insert_one({"name": "Spot", "s": 'Meow' * 1024 * 1023 * 4})
    else:
        kittens.insert_one({"name": "Spot", "s": 'Meow'})

    def span_as_text(span):
        """Format the span as "uber-trace-id" text map that Mongoproxy understands"""
        text_map = {}
        tracer.inject(span_context=span,
                      format=Format.TEXT_MAP,
                      carrier=text_map)
        return ''.join(["%s:%s" % (k, v) for k, v in text_map.items()])

    kitten_labels = {
        'app': 'mongoproxy-i9n-test',
        'collection': 'kittens',
        'db': 'test',
    }

    with tracer.start_span('Trace those cats') as root_span:

        # Fetch one document
        with MetricsDelta('FindOne', 'mongoproxy_documents_returned_total',
                          '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                for c in kittens.find({
                        "name": "Spot"
                }).comment(span_as_text(span)):
                    pass
            md.assert_metric_value({'op': 'find', **kitten_labels}, 1)

        # Simple "find" operation that returns nothing
        with MetricsDelta('FindNone', 'mongoproxy_documents_returned_total',
                          '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                for c in kittens.find({
                        "name": "Spotty"
                }).comment(span_as_text(span)):
                    pass
            md.assert_metric_value({'op': 'find', **kitten_labels}, 0)

        # "find" with a following "getMore"
        with MetricsDelta('FindAndGetMore',
                          'mongoproxy_documents_returned_total', '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                count = 0
                for c in kittens.find({
                        "name": "Purry"
                }).limit(1000).comment(span_as_text(span)):
                    count += 1

            # expect to have a single 101 row find followed by a getMore for the remainder
            md.assert_metric_value({'op': 'find', **kitten_labels}, 101)
            md.assert_metric_value({
                'op': 'getMore',
                **kitten_labels
            }, NUM_KITTENS - 101)

        # Update one document
        with MetricsDelta('UpdateOne', 'mongoproxy_documents_changed_total',
                          '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                kittens.update_one(
                    {
                        "name": "Purry",
                        "id": 0,
                        "$comment": span_as_text(span)
                    }, {"$set": {
                        "name": "Furry"
                    }})
            md.assert_metric_value({'op': 'update', **kitten_labels}, 1)

        # Update one document - should be a NOP
        with MetricsDelta('UpdateNone', 'mongoproxy_documents_changed_total',
                          '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                kittens.update_one(
                    {
                        "name": "Purry",
                        "id": 0,
                        "$comment": span_as_text(span)
                    }, {"$set": {
                        "name": "Furry"
                    }})
            md.assert_metric_value({'op': 'update', **kitten_labels}, 0)

        # Delete one document
        with MetricsDelta('DeleteOne', 'mongoproxy_documents_changed_total',
                          '_sum') as md:
            with tracer.start_span(md.name, root_span) as span:
                kittens.delete_one({
                    "name": "Furry",
                    "$comment": span_as_text(span)
                })
            md.assert_metric_value({'op': 'delete', **kitten_labels}, 1)

    tracer.close()
示例#24
0
    def __init__(self, config, custom_reactor, tracer=opentracing.tracer):
        """
        Object that holds state for the entirety of a Sygnal instance.
        Args:
            config (dict): Configuration for this Sygnal
            custom_reactor: a Twisted Reactor to use.
            tracer (optional): an OpenTracing tracer. The default is the no-op tracer.
        """
        self.config = config
        self.reactor = custom_reactor
        self.pushkins = {}
        self.tracer = tracer

        logging_dict_config = config["log"]["setup"]
        logging.config.dictConfig(logging_dict_config)

        logger.debug("Started logging")

        observer = twisted_log.PythonLoggingObserver()
        observer.start()

        # Old format db config
        if config.get("db") is not None:
            logger.warning(
                "Config includes the legacy 'db' option, please migrate"
                " to 'database' instead")
            config["database"] = {
                "name": "sqlite3",
                "args": {
                    "dbfile": config["db"]["dbfile"]
                },
            }
        elif config.get("database") is None:
            config["database"] = {
                "name": "sqlite3",
                "args": {
                    "dbfile": "sygnal.db"
                },
            }

        sentrycfg = config["metrics"]["sentry"]
        if sentrycfg["enabled"] is True:
            import sentry_sdk

            logger.info("Initialising Sentry")
            sentry_sdk.init(sentrycfg["dsn"])

        promcfg = config["metrics"]["prometheus"]
        if promcfg["enabled"] is True:
            prom_addr = promcfg["address"]
            prom_port = int(promcfg["port"])
            logger.info("Starting Prometheus Server on %s port %d", prom_addr,
                        prom_port)

            prometheus_client.start_http_server(port=prom_port,
                                                addr=prom_addr or "")

        tracecfg = config["metrics"]["opentracing"]
        if tracecfg["enabled"] is True:
            if tracecfg["implementation"] == "jaeger":
                try:
                    import jaeger_client

                    jaeger_cfg = jaeger_client.Config(
                        config=tracecfg["jaeger"],
                        service_name=tracecfg["service_name"],
                        scope_manager=AsyncioScopeManager(),
                    )

                    self.tracer = jaeger_cfg.initialize_tracer()

                    logger.info("Enabled OpenTracing support with Jaeger")
                except ModuleNotFoundError:
                    logger.critical(
                        "You have asked for OpenTracing with Jaeger but do not have"
                        " the Python package 'jaeger_client' installed.")
                    raise
            else:
                logger.error("Unknown OpenTracing implementation: %s.",
                             tracecfg["impl"])
                sys.exit(1)

        db_name = config["database"]["name"]

        if db_name == "psycopg2":
            logger.info("Using postgresql database")
            self.database_engine = "postgresql"
            self.database = ConnectionPool(
                "psycopg2",
                cp_reactor=self.reactor,
                **config["database"].get("args"),
            )
        elif db_name == "sqlite3":
            logger.info("Using sqlite database")
            self.database_engine = "sqlite"
            self.database = ConnectionPool(
                "sqlite3",
                config["database"]["args"]["dbfile"],
                cp_reactor=self.reactor,
                cp_min=1,
                cp_max=1,
                check_same_thread=False,
            )
        else:
            raise Exception("Unsupported database 'name'")
示例#25
0
import os
import jaeger_client

__all__ = ["tracer"]

jaeger_config = jaeger_client.Config(
    config={
        "sampler": {
            "type": "const",
            "param": 1
        },
        "logging": True,
        # Increase max length of logs so we can save vega lite specs
        "max_tag_value_length": 1000 * 100,
        # Send each trace one by one, dont wait for next
        # Need this b/c eventloop stops once cell is done executing
        # in jupyter
        "reporter_batch_size": 1,
    },
    service_name=os.environ.get("JAEGER_SERVICE_NAME", "kernel"),
    validate=True,
)

tracer = jaeger_config.initialize_tracer()
def create_tracer(service_name, config, use_asyncio=False):
    create_tracer.logger = init_logging(service_name)
    create_tracer.logger.info("Creating OpenTracing Tracer")

    # Store default tracer in case creating concrete implementation fails.
    tracer = opentracing.tracer
    if config.get("implementation") == "Jaeger":
        try:
            # import deferred until Jaeger is selected in config.
            import jaeger_client
            import tornado.ioloop
            """
            If Implementation = Jaeger get the Jaeger config from the config
            dict if available, if not present create a sane default config.
            """
            jaeger_config = config.get("config")
            if not jaeger_config:
                jaeger_config = {
                    "sampler": {
                        "type": "const",
                        "param": 1
                    },
                    "logging": False
                }

            jaeger = jaeger_client.Config(
                service_name=config.get("service_name", service_name),
                config=jaeger_config,
            )
            """
            The init_logging(log_name="tornado") is important, though a bit
            obtuse. Without it all subsequent log messages generated will
            be duplicated. The issue is that "under the covers" Jaeger uses
            the tornado https://www.tornadoweb.org async networking library.
            Tornado's IOLoop creates a log handler if necessary when it
            is started, because if there were no handler configured
            you'd never see any of its event loop exception messages.
            The default handler is created for the root logger and ends up
            resulting in duplicate messages for other logs. By explicitly
            adding a handler for the tornado logger, as the following line
            does, the logging should be correctly handled. See:
            https://stackoverflow.com/questions/30373620/why-does-ioloop-in-tornado-seem-to-add-root-logger-handler
            """
            init_logging(log_name="tornado")
            """
            If we are using asyncio we want the tracer to use the main asyncio
            event loop rather than create a new ThreadLoop (which is the default
            behaviour unless a tornado IOLoop is passed. In recent versions of
            Tornado that delegates to the asyncio event loop so getting the
            current tornado IOLoop and passing that to initialize_tracer will
            cause the tracer to use the main event loop.
            """
            if use_asyncio:
                jaeger.initialize_tracer(
                    io_loop=tornado.ioloop.IOLoop.current())
            else:
                jaeger.initialize_tracer()

            create_tracer.logger.info("Jaeger Tracer initialised")
        except Exception as e:
            create_tracer.logger.warning(
                "Failed to initialise Jaeger Tracer : {}".format(e))
            opentracing.tracer = tracer

    patch_boto3()
示例#27
0
# Initialize a simple and basic Jaeger Tracing integration
# for open-tracing if enabled.
#
# Refer to our guide on https://docs.saleor.io/docs/next/guides/opentracing-jaeger/.
#
# If running locally, set:
#   JAEGER_AGENT_HOST=localhost
if "JAEGER_AGENT_HOST" in os.environ:
    jaeger_client.Config(
        config={
            "sampler": {"type": "const", "param": 1},
            "local_agent": {
                "reporting_port": os.environ.get(
                    "JAEGER_AGENT_PORT", jaeger_client.config.DEFAULT_REPORTING_PORT
                ),
                "reporting_host": os.environ.get("JAEGER_AGENT_HOST"),
            },
            "logging": get_bool_from_env("JAEGER_LOGGING", False),
        },
        service_name="saleor",
        validate=True,
    ).initialize_tracer()


# Some cloud providers (Heroku) export REDIS_URL variable instead of CACHE_URL
REDIS_URL = os.environ.get("REDIS_URL")
if REDIS_URL:
    CACHE_URL = os.environ.setdefault("CACHE_URL", REDIS_URL)
CACHES = {"default": django_cache_url.config()}

# Default False because storefront and dashboard don't support expiration of token
示例#28
0
import jaeger_client

import uvicorn
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse

__version__ = "1.0.1"

jaeger_config = jaeger_client.Config(
    config={
        "sampler": {
            "type": "const",
            "param": 1
        },
        "logging": True
    },
    service_name="browser",
    validate=True,
)

tracer = jaeger_config.initialize_tracer()

app = Starlette(debug=True)
app.add_middleware(CORSMiddleware,
                   allow_origins=["*"],
                   allow_methods=["*"],
                   allow_headers=["*"])

SPANS: typing.Dict[str, opentracing.Span] = {}