Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@wrapt.patch_function_wrapper('grpc._channel', '_StreamUnaryMultiCallable.with_call')
def stream_unary_with_call_with_instana(wrapped, instance, argv, kwargs):
parent_span = tracer.active_span
# If we're not tracing, just return
if parent_span is None:
return wrapped(*argv, **kwargs)
with tracer.start_active_span("rpc-client", child_of=parent_span) as scope:
try:
if not "metadata" in kwargs:
kwargs["metadata"] = []
kwargs["metadata"] = tracer.inject(scope.span.context, opentracing.Format.BINARY, kwargs['metadata'])
collect_tags(scope.span, instance, argv, kwargs)
scope.span.set_tag('rpc.call_type', 'stream')
@wrapt.patch_function_wrapper('grpc._channel', '_UnaryStreamMultiCallable.__call__')
def unary_stream_call_with_instana(wrapped, instance, argv, kwargs):
parent_span = tracer.active_span
# If we're not tracing, just return
if parent_span is None:
return wrapped(*argv, **kwargs)
with tracer.start_active_span("rpc-client", child_of=parent_span) as scope:
try:
if not "metadata" in kwargs:
kwargs["metadata"] = []
kwargs["metadata"] = tracer.inject(scope.span.context, opentracing.Format.BINARY, kwargs['metadata'])
collect_tags(scope.span, instance, argv, kwargs)
scope.span.set_tag('rpc.call_type', 'stream')
@wrapt.patch_function_wrapper('jupyterhub.spawner', 'LocalProcessSpawner.get_env')
def _wrapper_get_env(wrapped, instance, args, kwargs):
env = wrapped(*args, **kwargs)
target = env.get('JUPYTERHUB_API_URL')
old = 'http://%s:%s' % (c.JupyterHub.hub_connect_ip, c.JupyterHub.hub_port)
new = 'http://127.0.0.1:%s' % c.JupyterHub.hub_port
if target and target.startswith(old):
target = target.replace(old, new)
env['JUPYTERHUB_API_URL'] = target
return env
@wrapt.patch_function_wrapper('grpc._server', '_call_behavior')
def call_behavior_with_instana(wrapped, instance, argv, kwargs):
# Prep any incoming context headers
metadata = argv[0].invocation_metadata
metadata_dict = {}
for c in metadata:
metadata_dict[c.key] = c.value
ctx = tracer.extract(opentracing.Format.BINARY, metadata_dict)
with tracer.start_active_span("rpc-server", child_of=ctx) as scope:
try:
collect_tags(scope.span, instance, argv, kwargs)
rv = wrapped(*argv, **kwargs)
except Exception as e:
scope.span.log_exception(e)
raise
@wrapt.patch_function_wrapper('tornado.httpclient', 'AsyncHTTPClient.fetch')
def fetch_with_instana(wrapped, instance, argv, kwargs):
try:
parent_span = tornado_tracer.active_span
# If we're not tracing, just return
if (parent_span is None) or (parent_span.operation_name == "tornado-client"):
return wrapped(*argv, **kwargs)
request = argv[0]
# To modify request headers, we have to preemptively create an HTTPRequest object if a
# URL string was passed.
if not isinstance(request, tornado.httpclient.HTTPRequest):
request = tornado.httpclient.HTTPRequest(url=request, **kwargs)
new_kwargs = {}
@wrapt.patch_function_wrapper('jupyterhub.proxy', 'ConfigurableHTTPProxy.add_route')
def _wrapper_add_route(wrapped, instance, args, kwargs):
def _extract_args(routespec, target, data, *_args, **_kwargs):
return (routespec, target, data, _args, _kwargs)
routespec, target, data, _args, _kwargs = _extract_args(*args, **kwargs)
old = 'http://%s:%s' % (c.JupyterHub.hub_connect_ip, c.JupyterHub.hub_port)
new = 'http://127.0.0.1:%s' % c.JupyterHub.hub_port
if target.startswith(old):
target = target.replace(old, new)
return wrapped(routespec, target, data, *_args, **_kwargs)
@wrapt.patch_function_wrapper('jupyterhub.spawner', 'LocalProcessSpawner.get_env')
def _wrapper_get_env(wrapped, instance, args, kwargs):
env = wrapped(*args, **kwargs)
target = env.get('JUPYTERHUB_API_URL')
old = 'http://%s:%s' % (c.JupyterHub.hub_connect_ip, c.JupyterHub.hub_port)
new = 'http://127.0.0.1:%s' % c.JupyterHub.hub_port
if target and target.startswith(old):
target = target.replace(old, new)
env['JUPYTERHUB_API_URL'] = target
return env
@wrapt.patch_function_wrapper('asynqp.queue','Queue.consume')
def consume_with_instana(wrapped, instance, argv, kwargs):
def callback_generator(original_callback):
def callback_with_instana(*argv, **kwargs):
ctx = None
msg = argv[0]
if msg.headers is not None:
ctx = async_tracer.extract(opentracing.Format.TEXT_MAP, dict(msg.headers))
with async_tracer.start_active_span("rabbitmq", child_of=ctx) as scope:
host, port = msg.sender.protocol.transport._sock.getsockname()
try:
scope.span.set_tag("exchange", msg.exchange_name)
scope.span.set_tag("sort", "consume")
scope.span.set_tag("address", host + ":" + str(port) )
scope.span.set_tag("key", msg.routing_key)
@wrapt.patch_function_wrapper('asynqp.exchange','Exchange.publish')
def publish_with_instana(wrapped, instance, argv, kwargs):
parent_span = async_tracer.active_span
# If we're not tracing, just return
if parent_span is None:
return wrapped(*argv, **kwargs)
with async_tracer.start_active_span("rabbitmq", child_of=parent_span) as scope:
host, port = instance.sender.protocol.transport._sock.getsockname()
msg = argv[0]
if msg.headers is None:
msg.headers = {}
async_tracer.inject(scope.span.context, opentracing.Format.TEXT_MAP, msg.headers)
try:
@wrapt.patch_function_wrapper('grpc._channel', '_UnaryUnaryMultiCallable.with_call')
def unary_unary_with_call_with_instana(wrapped, instance, argv, kwargs):
parent_span = tracer.active_span
# If we're not tracing, just return
if parent_span is None:
return wrapped(*argv, **kwargs)
with tracer.start_active_span("rpc-client", child_of=parent_span) as scope:
try:
if "metadata" not in kwargs:
kwargs["metadata"] = []
kwargs["metadata"] = tracer.inject(scope.span.context, opentracing.Format.BINARY, kwargs['metadata'])
collect_tags(scope.span, instance, argv, kwargs)
scope.span.set_tag('rpc.call_type', 'unary')