Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test(self, request, context):
"""Handle a test request by calling other test services"""
logger.debug("grpc service received: %s", request)
if not request.service_hops:
response = pb2.TestResponse(
id=request.id,
status=[pb2.CommonResponseStatus(
status=pb2.SUCCESS,
)],
)
else:
status = ([pb2.CommonResponseStatus(status=pb2.SUCCESS)] +
list(service.call_next(request).status))
response = pb2.TestResponse(id=request.id, status=status)
tracer = execution_context.get_opencensus_tracer()
tracer.add_attribute_to_current_span("reqId", request.id)
return response
request = pb2.TestRequest.FromString(flask.request.get_data())
logger.debug("Flask service received: %s", request)
if not request.service_hops:
response = pb2.TestResponse(
id=request.id,
status=[pb2.CommonResponseStatus(
status=pb2.SUCCESS,
)],
)
else:
status = ([pb2.CommonResponseStatus(status=pb2.SUCCESS)] +
list(service.call_next(request).status))
response = pb2.TestResponse(id=request.id, status=status)
tracer = execution_context.get_opencensus_tracer()
tracer.add_attribute_to_current_span("reqId", request.id)
return response.SerializeToString()
def tearDown(self):
execution_context.clear()
def call(self, func, args=(), kwds={}, **kwargs):
wrapped_func = wrap_task_func(func)
_tracer = execution_context.get_opencensus_tracer()
propagator = binary_format.BinaryFormatPropagator()
wrapped_kwargs = {}
wrapped_kwargs["span_context_binary"] = propagator.to_header(
_tracer.span_context
)
wrapped_kwargs["kwds"] = kwds
wrapped_kwargs["sampler"] = _tracer.sampler
wrapped_kwargs["exporter"] = _tracer.exporter
wrapped_kwargs["propagator"] = _tracer.propagator
return apply_async_func(
self, wrapped_func, args=args, kwds=wrapped_kwargs, **kwargs
)
def call(query, *args, **kwargs):
_tracer = execution_context.get_opencensus_tracer()
_span = _tracer.start_span()
_span.name = 'mysql.query'
_span.span_kind = span_module.SpanKind.CLIENT
_tracer.add_attribute_to_current_span('mysql.query', query)
_tracer.add_attribute_to_current_span(
'mysql.cursor.method.name',
query_func.__name__)
result = query_func(query, *args, **kwargs)
_tracer.end_span()
return result
return call
span.name = '[{}]{}'.format(
flask.request.method,
flask.request.url)
tracer.add_attribute_to_current_span(
HTTP_HOST, flask.request.host
)
tracer.add_attribute_to_current_span(
HTTP_METHOD, flask.request.method
)
tracer.add_attribute_to_current_span(
HTTP_PATH, flask.request.path
)
tracer.add_attribute_to_current_span(
HTTP_URL, str(flask.request.url)
)
execution_context.set_opencensus_attr(
'blacklist_hostnames',
self.blacklist_hostnames
)
except Exception: # pragma: NO COVER
log.error('Failed to trace request', exc_info=True)
def call(url, *args, **kwargs):
# Check if request was sent from an exporter. If so, do not wrap.
if execution_context.is_exporter():
return requests_func(url, *args, **kwargs)
blacklist_hostnames = execution_context.get_opencensus_attr(
'blacklist_hostnames')
parsed_url = urlparse(url)
if parsed_url.port is None:
dest_url = parsed_url.hostname
else:
dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port)
if utils.disable_tracing_hostname(dest_url, blacklist_hostnames):
return requests_func(url, *args, **kwargs)
path = parsed_url.path if parsed_url.path else '/'
_tracer = execution_context.get_opencensus_tracer()
_span = _tracer.start_span()
_span.name = '{}'.format(path)
def get_current_span(cls):
# type: () -> Span
"""
Get the current span from the execution context. Return None otherwise.
"""
return execution_context.get_current_span()
def tracer(self):
return self._tracer or execution_context.get_opencensus_tracer()
def _after_cursor_execute(conn, cursor, statement, parameters,
context, executemany):
"""Intercept low-level cursor execute() events after execution.
If executemany is True, this is an executemany call, else an execute call.
See: http://docs.sqlalchemy.org/en/latest/core/events.html#sqlalchemy.
events.ConnectionEvents.after_cursor_execute
"""
_tracer = execution_context.get_opencensus_tracer()
_tracer.end_span()