Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add opt-in exit spans (part 1) #522

Merged
merged 1 commit into from
May 3, 2024
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
10 changes: 5 additions & 5 deletions instana/instrumentation/cassandra_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
import wrapt
from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off

try:
import cassandra
Expand Down Expand Up @@ -51,9 +51,9 @@ def cb_request_error(results, span, fn):


def request_init_with_instana(fn):
active_tracer = get_active_tracer()
tracer, parent_span, _ = get_tracer_tuple()

if active_tracer is None:
if tracing_is_off():
return

ctags = {}
Expand All @@ -65,8 +65,8 @@ def request_init_with_instana(fn):
ctags["cassandra.keyspace"] = fn.session.keyspace
ctags["cassandra.cluster"] = fn.session.cluster.metadata.cluster_name

with active_tracer.start_active_span("cassandra", child_of=active_tracer.active_span,
tags=ctags, finish_on_close=False) as scope:
with tracer.start_active_span("cassandra", child_of=parent_span,
tags=ctags, finish_on_close=False) as scope:
fn.add_callback(cb_request_finish, scope.span, fn)
fn.add_errback(cb_request_error, scope.span, fn)

Expand Down
14 changes: 7 additions & 7 deletions instana/instrumentation/couchbase_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import wrapt

from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off

try:
import couchbase
Expand Down Expand Up @@ -53,13 +53,13 @@ def capture_kvs(scope, instance, query_arg, op):

def make_wrapper(op):
def wrapper(wrapped, instance, args, kwargs):
active_tracer = get_active_tracer()
tracer, parent_span, _ = get_tracer_tuple()

# If we're not tracing, just return
if active_tracer is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

with active_tracer.start_active_span("couchbase", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("couchbase", child_of=parent_span) as scope:
capture_kvs(scope, instance, None, op)
try:
return wrapped(*args, **kwargs)
Expand All @@ -70,13 +70,13 @@ def wrapper(wrapped, instance, args, kwargs):
return wrapper

def query_with_instana(wrapped, instance, args, kwargs):
active_tracer = get_active_tracer()
tracer, parent_span, _ = get_tracer_tuple()

# If we're not tracing, just return
if active_tracer is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

with active_tracer.start_active_span("couchbase", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("couchbase", child_of=parent_span) as scope:
capture_kvs(scope, instance, args[0], 'n1ql_query')
try:
return wrapped(*args, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions instana/instrumentation/google/cloud/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ....log import logger
from ....singletons import tracer
from ....util.traceutils import get_tracer_tuple, tracing_is_off

try:
from google.cloud import pubsub_v1
Expand Down Expand Up @@ -36,13 +37,12 @@ def publish_with_instana(wrapped, instance, args, kwargs):
"""References:
- PublisherClient.publish(topic_path, messages, metadata)
"""
# check if active
parent_span = tracer.active_span

# return early if we're not tracing
if parent_span is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

tracer, parent_span, _ = get_tracer_tuple()

with tracer.start_active_span('gcps-producer', child_of=parent_span) as scope:
# trace continuity, inject to the span context
headers = dict()
Expand Down
30 changes: 13 additions & 17 deletions instana/instrumentation/google/cloud/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import re

from ....log import logger
from ....singletons import tracer
from .collectors import _storage_api
from ....util.traceutils import get_tracer_tuple, tracing_is_off

try:
from google.cloud import storage
Expand Down Expand Up @@ -50,15 +50,11 @@ def _collect_tags(api_request):

def execute_with_instana(wrapped, instance, args, kwargs):
# batch requests are traced with finish_batch_with_instana()
if isinstance(instance, storage.Batch):
return wrapped(*args, **kwargs)

parent_span = tracer.active_span

# return early if we're not tracing
if parent_span is None:
# also return early if we're not tracing
if isinstance(instance, storage.Batch) or tracing_is_off():
return wrapped(*args, **kwargs)

tracer, parent_span, _ = get_tracer_tuple()
tags = _collect_tags(kwargs)

# don't trace if the call is not instrumented
Expand All @@ -79,12 +75,12 @@ def execute_with_instana(wrapped, instance, args, kwargs):
return kv

def download_with_instana(wrapped, instance, args, kwargs):
parent_span = tracer.active_span

# return early if we're not tracing
if parent_span is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

tracer, parent_span, _ = get_tracer_tuple()

with tracer.start_active_span('gcs', child_of=parent_span) as scope:
scope.span.set_tag('gcs.op', 'objects.get')
scope.span.set_tag('gcs.bucket', instance.bucket.name)
Expand All @@ -110,12 +106,12 @@ def download_with_instana(wrapped, instance, args, kwargs):
return kv

def upload_with_instana(wrapped, instance, args, kwargs):
parent_span = tracer.active_span

# return early if we're not tracing
if parent_span is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

tracer, parent_span, _ = get_tracer_tuple()

with tracer.start_active_span('gcs', child_of=parent_span) as scope:
scope.span.set_tag('gcs.op', 'objects.insert')
scope.span.set_tag('gcs.bucket', instance.bucket.name)
Expand All @@ -130,12 +126,12 @@ def upload_with_instana(wrapped, instance, args, kwargs):
return kv

def finish_batch_with_instana(wrapped, instance, args, kwargs):
parent_span = tracer.active_span

# return early if we're not tracing
if parent_span is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

tracer, parent_span, _ = get_tracer_tuple()

with tracer.start_active_span('gcs', child_of=parent_span) as scope:
scope.span.set_tag('gcs.op', 'batch')
scope.span.set_tag('gcs.projectId', instance._client.project)
Expand Down
20 changes: 10 additions & 10 deletions instana/instrumentation/pep0249.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import wrapt

from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off
from ..util.sql import sql_sanitizer


Expand Down Expand Up @@ -40,13 +40,13 @@ def __enter__(self):
return self

def execute(self, sql, params=None):
active_tracer = get_active_tracer()
tracer, parent_span, operation_name = get_tracer_tuple()

# If not tracing or we're being called from sqlalchemy, just pass through
if (active_tracer is None) or (active_tracer.active_span.operation_name == "sqlalchemy"):
if (tracing_is_off() or (operation_name == "sqlalchemy")):
return self.__wrapped__.execute(sql, params)

with active_tracer.start_active_span(self._module_name, child_of=active_tracer.active_span) as scope:
with tracer.start_active_span(self._module_name, child_of=parent_span) as scope:
try:
self._collect_kvs(scope.span, sql)

Expand All @@ -59,13 +59,13 @@ def execute(self, sql, params=None):
return result

def executemany(self, sql, seq_of_parameters):
active_tracer = get_active_tracer()
tracer, parent_span, operation_name = get_tracer_tuple()

# If not tracing or we're being called from sqlalchemy, just pass through
if (active_tracer is None) or (active_tracer.active_span.operation_name == "sqlalchemy"):
if (tracing_is_off() or (operation_name == "sqlalchemy")):
return self.__wrapped__.executemany(sql, seq_of_parameters)

with active_tracer.start_active_span(self._module_name, child_of=active_tracer.active_span) as scope:
with tracer.start_active_span(self._module_name, child_of=parent_span) as scope:
try:
self._collect_kvs(scope.span, sql)

Expand All @@ -78,13 +78,13 @@ def executemany(self, sql, seq_of_parameters):
return result

def callproc(self, proc_name, params):
active_tracer = get_active_tracer()
tracer, parent_span, operation_name = get_tracer_tuple()

# If not tracing or we're being called from sqlalchemy, just pass through
if (active_tracer is None) or (active_tracer.active_span.operation_name == "sqlalchemy"):
if (tracing_is_off() or (operation_name == "sqlalchemy")):
return self.__wrapped__.execute(proc_name, params)

with active_tracer.start_active_span(self._module_name, child_of=active_tracer.active_span) as scope:
with tracer.start_active_span(self._module_name, child_of=parent_span) as scope:
try:
self._collect_kvs(scope.span, proc_name)

Expand Down
8 changes: 4 additions & 4 deletions instana/instrumentation/pika.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ..log import logger
from ..singletons import tracer
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off

try:
import pika
Expand Down Expand Up @@ -40,14 +40,14 @@ def basic_publish_with_instana(wrapped, instance, args, kwargs):
def _bind_args(exchange, routing_key, body, properties=None, *args, **kwargs):
return (exchange, routing_key, body, properties, args, kwargs)

active_tracer = get_active_tracer()
tracer, parent_span, _ = get_tracer_tuple()

if active_tracer is None:
if tracing_is_off():
return wrapped(*args, **kwargs)

(exchange, routing_key, body, properties, args, kwargs) = (_bind_args(*args, **kwargs))

with tracer.start_active_span("rabbitmq", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("rabbitmq", child_of=parent_span) as scope:
try:
_extract_publisher_tags(scope.span,
conn=instance.connection,
Expand Down
8 changes: 4 additions & 4 deletions instana/instrumentation/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off

try:
import pymongo
Expand All @@ -16,12 +16,12 @@ def __init__(self):
self.__active_commands = {}

def started(self, event):
active_tracer = get_active_tracer()
tracer, parent_span, _ = get_tracer_tuple()
# return early if we're not tracing
if active_tracer is None:
if tracing_is_off():
return

with active_tracer.start_active_span("mongo", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("mongo", child_of=parent_span) as scope:
self._collect_connection_tags(scope.span, event)
self._collect_command_tags(scope.span, event)

Expand Down
14 changes: 7 additions & 7 deletions instana/instrumentation/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import wrapt

from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off


try:
Expand Down Expand Up @@ -36,13 +36,13 @@ def collect_tags(span, instance, args, kwargs):


def execute_command_with_instana(wrapped, instance, args, kwargs):
active_tracer = get_active_tracer()
tracer, parent_span, operation_name = get_tracer_tuple()

# If we're not tracing, just return
if active_tracer is None or active_tracer.active_span.operation_name in EXCLUDED_PARENT_SPANS:
if (tracing_is_off() or (operation_name in EXCLUDED_PARENT_SPANS)):
return wrapped(*args, **kwargs)

with active_tracer.start_active_span("redis", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("redis", child_of=parent_span) as scope:
try:
collect_tags(scope.span, instance, args, kwargs)
if (len(args) > 0):
Expand All @@ -57,13 +57,13 @@ def execute_command_with_instana(wrapped, instance, args, kwargs):


def execute_with_instana(wrapped, instance, args, kwargs):
active_tracer = get_active_tracer()
tracer, parent_span, operation_name = get_tracer_tuple()

# If we're not tracing, just return
if active_tracer is None or active_tracer.active_span.operation_name in EXCLUDED_PARENT_SPANS:
if (tracing_is_off() or (operation_name in EXCLUDED_PARENT_SPANS)):
return wrapped(*args, **kwargs)

with active_tracer.start_active_span("redis", child_of=active_tracer.active_span) as scope:
with tracer.start_active_span("redis", child_of=parent_span) as scope:
try:
collect_tags(scope.span, instance, args, kwargs)
scope.span.set_tag("command", 'PIPELINE')
Expand Down
11 changes: 5 additions & 6 deletions instana/instrumentation/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from operator import attrgetter

from ..log import logger
from ..util.traceutils import get_active_tracer
from ..util.traceutils import get_tracer_tuple, tracing_is_off

try:
import sqlalchemy
Expand All @@ -19,13 +19,12 @@
@event.listens_for(Engine, 'before_cursor_execute', named=True)
def receive_before_cursor_execute(**kw):
try:
active_tracer = get_active_tracer()

# If we're not tracing, just return
if active_tracer is None:
if tracing_is_off():
return

scope = active_tracer.start_active_span("sqlalchemy", child_of=active_tracer.active_span)
tracer, parent_span, _ = get_tracer_tuple()
scope = tracer.start_active_span("sqlalchemy", child_of=parent_span)
context = kw['context']
if context:
context._stan_scope = scope
Expand Down Expand Up @@ -72,7 +71,7 @@ def _set_error_tags(context, exception_string, scope_string):
@event.listens_for(Engine, error_event, named=True)
def receive_handle_db_error(**kw):

if get_active_tracer() is None:
if tracing_is_off():
return

# support older db error event
Expand Down
Loading
Loading