Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions instana/instrumentation/aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ async def stan_request_end(session, trace_config_ctx, params):
scope.span.set_tag("http.%s" % custom_header, params.response.headers[custom_header])

if 500 <= params.response.status <= 599:
scope.span.set_tag("http.error", params.response.reason)
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec + 1)
scope.span.mark_as_errored({"http.error": params.response.reason})

scope.close()
except Exception:
Expand Down
5 changes: 1 addition & 4 deletions instana/instrumentation/aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ async def stan_middleware(request, handler):
if response is not None:
# Mark 500 responses as errored
if 500 <= response.status <= 511:
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
if ec == 0:
scope.span.set_tag("ec", ec + 1)
scope.span.mark_as_errored()

scope.span.set_tag("http.status_code", response.status)
async_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
Expand Down
11 changes: 3 additions & 8 deletions instana/instrumentation/asynqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def publish_with_instana(wrapped, instance, argv, kwargs):

rv = wrapped(*argv, **kwargs)
except Exception as e:
scope.span.log_kv({'message': e})
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec+1)
scope.span.mark_as_errored({'message': e})
raise
else:
return rv
Expand Down Expand Up @@ -90,11 +87,9 @@ def callback_with_instana(*argv, **kwargs):

original_callback(*argv, **kwargs)
except Exception as e:
scope.span.log_kv({'message': e})
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec+1)
scope.span.mark_as_errored({'message': e})
raise

return callback_with_instana

cb = argv[0]
Expand Down
6 changes: 1 addition & 5 deletions instana/instrumentation/cassandra_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ def cb_request_finish(results, span, fn):

def cb_request_error(results, span, fn):
collect_response(span, fn)

span.set_tag("error", True)
ec = span.tags.get('ec', 0)
span.set_tag("ec", ec + 1)
span.set_tag("cassandra.error", results.message)
span.mark_as_errored({"cassandra.error": results.message})
span.finish()

def request_init_with_instana(fn):
Expand Down
11 changes: 2 additions & 9 deletions instana/instrumentation/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ def process_response(self, request, response):
try:
if request.iscope is not None:
if 500 <= response.status_code <= 511:
request.iscope.span.set_tag("error", True)
ec = request.iscope.span.tags.get('ec', 0)
if ec == 0:
request.iscope.span.set_tag("ec", ec+1)
request.iscope.span.assure_errored()

request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
tracer.inject(request.iscope.span.context, ot.Format.HTTP_HEADERS, response)
Expand All @@ -72,11 +69,7 @@ def process_response(self, request, response):

def process_exception(self, request, exception):
if request.iscope is not None:
request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, 500)
request.iscope.span.set_tag('http.error', str(exception))
request.iscope.span.set_tag("error", True)
ec = request.iscope.span.tags.get('ec', 0)
request.iscope.span.set_tag("ec", ec+1)
request.iscope.span.log_exception(exception)


def load_middleware_wrapper(wrapped, instance, args, kwargs):
Expand Down
5 changes: 1 addition & 4 deletions instana/instrumentation/flask/vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ def after_request_with_instana(response):
span = scope.span

if 500 <= response.status_code <= 511:
span.set_tag("error", True)
ec = span.tags.get('ec', 0)
if ec == 0:
span.set_tag("ec", ec+1)
span.mark_as_errored()

span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
Expand Down
5 changes: 1 addition & 4 deletions instana/instrumentation/flask/with_blinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ def request_finished_with_instana(sender, response, **extra):
span = scope.span

if 500 <= response.status_code <= 511:
span.set_tag("error", True)
ec = span.tags.get('ec', 0)
if ec == 0:
span.set_tag("ec", ec+1)
span.mark_as_errored()

span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
Expand Down
4 changes: 1 addition & 3 deletions instana/instrumentation/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def log_with_instana(wrapped, instance, argv, kwargs):
scope.span.log_kv({ 'parameters': parameters })
# extra tags for an error
if argv[0] >= logging.ERROR:
scope.span.set_tag('error', True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag('ec', ec + 1)
scope.span.mark_as_errored()
except Exception as e:
logger.debug('Exception: %s', e, exc_info=True)
finally:
Expand Down
20 changes: 9 additions & 11 deletions instana/instrumentation/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,25 @@ def receive_after_cursor_execute(**kw):
context = kw['context']

if context is not None and hasattr(context, '_stan_scope'):
this_scope = context._stan_scope
if this_scope is not None:
this_scope.close()
scope = context._stan_scope
if scope is not None:
scope.close()

@event.listens_for(Engine, 'dbapi_error', named=True)
def receive_dbapi_error(**kw):
context = kw['context']

if context is not None and hasattr(context, '_stan_scope'):
this_scope = context._stan_scope
if this_scope is not None:
this_scope.span.set_tag("error", True)
ec = this_scope.span.tags.get('ec', 0)
this_scope.span.set_tag("ec", ec+1)
scope = context._stan_scope
if scope is not None:
scope.span.mark_as_errored()

if 'exception' in kw:
e = kw['exception']
this_scope.span.set_tag('sqlalchemy.err', str(e))
scope.span.set_tag('sqlalchemy.err', str(e))
else:
this_scope.span.set_tag('sqlalchemy.err', "No dbapi error specified.")
this_scope.close()
scope.span.set_tag('sqlalchemy.err', "No dbapi error specified.")
scope.close()


logger.debug("Instrumenting sqlalchemy")
Expand Down
5 changes: 1 addition & 4 deletions instana/instrumentation/tornado/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ def on_finish_with_instana(wrapped, instance, argv, kwargs):

# Mark 500 responses as errored
if 500 <= status_code <= 511:
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
if ec == 0:
scope.span.set_tag("ec", ec + 1)
scope.span.mark_as_errored()

scope.span.set_tag("http.status_code", status_code)
scope.close()
Expand Down
9 changes: 2 additions & 7 deletions instana/instrumentation/urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def collect_response(scope, response):
scope.span.set_tag("http.%s" % custom_header, response.headers[custom_header])

if 500 <= response.status <= 599:
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec + 1)
scope.span.mark_as_errored()
except Exception:
logger.debug("collect_response", exc_info=True)

Expand Down Expand Up @@ -88,10 +86,7 @@ def urlopen_with_instana(wrapped, instance, args, kwargs):

return response
except Exception as e:
scope.span.log_kv({'message': e})
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec+1)
scope.span.mark_as_errored({'message': e})
raise

logger.debug("Instrumenting urllib3")
Expand Down
4 changes: 1 addition & 3 deletions instana/instrumentation/webapp2_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def new_start_response(status, headers, exc_info=None):

sc = status.split(' ')[0]
if 500 <= int(sc) <= 511:
scope.span.set_tag("error", True)
ec = scope.span.tags.get('ec', 0)
scope.span.set_tag("ec", ec+1)
scope.span.mark_as_errored()

scope.span.set_tag(tags.HTTP_STATUS_CODE, sc)
scope.close()
Expand Down
4 changes: 2 additions & 2 deletions instana/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class StandardRecorder(object):
THREAD_NAME = "Instana Span Reporting"

REGISTERED_SPANS = ("aiohttp-client", "aiohttp-server", "aws.lambda.entry", "cassandra", "couchbase",
"django", "log","memcache", "mongo", "mysql", "postgres", "pymongo", "rabbitmq", "redis", "render",
"rpc-client", "rpc-server", "sqlalchemy", "soap", "tornado-client", "tornado-server",
"django", "log", "memcache", "mongo", "mysql", "postgres", "pymongo", "rabbitmq", "redis",
"render", "rpc-client", "rpc-server", "sqlalchemy", "soap", "tornado-client", "tornado-server",
"urllib3", "wsgi")

# Recorder thread for collection/reporting of spans
Expand Down
50 changes: 41 additions & 9 deletions instana/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,59 @@ class InstanaSpan(BasicSpan):
def finish(self, finish_time=None):
super(InstanaSpan, self).finish(finish_time)

def mark_as_errored(self, tags = None):
"""
Mark this span as errored.

@param tags: optional tags to add to the span
"""
try:
ec = self.tags.get('ec', 0)
self.set_tag('ec', ec + 1)

if tags is not None and type(tags) is dict:
for key in tags:
self.set_tag(key, tags[key])
except Exception:
logger.debug('span.mark_as_errored', exc_info=True)

def assure_errored(self):
"""
Make sure that this span is marked as errored.
@return: None
"""
try:
ec = self.tags.get('ec', None)
if ec is None or ec == 0:
self.set_tag('ec', 1)
except Exception:
logger.debug('span.assure_errored', exc_info=True)

def log_exception(self, e):
"""
Log an exception onto this span. This will log pertinent info from the exception and
assure that this span is marked as errored.

@param e: the exception to log
"""
try:
message = ""
self.mark_as_errored()

self.set_tag("error", True)
ec = self.tags.get('ec', 0)
self.set_tag("ec", ec+1)

if hasattr(e, '__str__'):
if hasattr(e, '__str__') and len(str(e)) > 0:
message = str(e)
elif hasattr(e, 'message') and e.message is not None:
message = e.message
else:
message = repr(e)

if self.operation_name in ['rpc-server', 'rpc-client']:
self.set_tag('rpc.error', message)
elif self.operation_name == "mysql":
self.set_tag('mysql.error', message)
elif self.operation_name == "postgres":
self.set_tag('pg.error', message)
elif self.operation_name == "soap":
elif self.operation_name in RegisteredSpan.HTTP_SPANS:
self.set_tag('http.error', message)
else:
self.log_kv({'message': message})
Expand All @@ -39,7 +72,7 @@ def log_exception(self, e):

def collect_logs(self):
"""
Collect up log data and feed it to the Instana brain.
Collect up log data and feed it to the Instana brain.

:param span: The span to search for logs in
:return: Logs ready for consumption by the Instana brain.
Expand Down Expand Up @@ -74,8 +107,7 @@ def __init__(self, span, source, **kwargs):
self.ts = int(round(span.start_time * 1000))
self.d = int(round(span.duration * 1000))
self.f = source
self.ec = span.tags.pop("ec", None)
self.error = span.tags.pop("error", None)
self.ec = span.tags.pop('ec', None)

if span.stack:
self.stack = span.stack
Expand Down
4 changes: 1 addition & 3 deletions instana/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def new_start_response(status, headers, exc_info=None):

sc = status.split(' ')[0]
if 500 <= int(sc) <= 511:
self.scope.span.set_tag("error", True)
ec = self.scope.span.tags.get('ec', 0)
self.scope.span.set_tag("ec", ec+1)
self.scope.span.mark_as_errored()

self.scope.span.set_tag(tags.HTTP_STATUS_CODE, sc)
self.scope.close()
Expand Down
Loading