-
Notifications
You must be signed in to change notification settings - Fork 111
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
cassandra-driver Instrumentation #1280
base: main
Are you sure you want to change the base?
Changes from all commits
29e2bda
7cb0fed
0c13c6c
6d87d1d
5621527
839b5e8
bd2841e
5a2470b
01c77f7
f70ddf4
747e4e1
369e2c3
b4b519a
ea6f81e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,119 @@ | ||||
# Copyright 2010 New Relic, Inc. | ||||
# | ||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
# | ||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||
# | ||||
# Unless required by applicable law or agreed to in writing, software | ||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
|
||||
from newrelic.api.database_trace import DatabaseTrace, register_database_client | ||||
from newrelic.api.function_trace import wrap_function_trace | ||||
from newrelic.api.time_trace import current_trace | ||||
from newrelic.common.object_wrapper import wrap_function_wrapper | ||||
from newrelic.common.signature import bind_args | ||||
|
||||
DBAPI2_MODULE = None | ||||
DEFAULT = object() | ||||
|
||||
|
||||
def wrap_Session_execute_async(wrapped, instance, args, kwargs): | ||||
# Most of this wrapper is lifted from DBAPI2 wrappers, which can't be used | ||||
# directly since Cassandra doesn't actually conform to DBAPI2. | ||||
|
||||
trace = current_trace() | ||||
if not trace or trace.terminal_node(): | ||||
# Exit early there's no transaction, or if we're under an existing DatabaseTrace | ||||
return wrapped(*args, **kwargs) | ||||
|
||||
bound_args = bind_args(wrapped, args, kwargs) | ||||
|
||||
sql_parameters = bound_args.get("parameters", None) | ||||
|
||||
sql = bound_args.get("query", None) | ||||
if not isinstance(sql, str): | ||||
statement = getattr(sql, "prepared_statement", sql) # Unbind BoundStatement | ||||
sql = statement.query_string # Unpack query from SimpleStatement and PreparedStatement | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use a getattr here in case this attribute doesn't exist? |
||||
|
||||
database_name = getattr(instance, "keyspace", None) | ||||
|
||||
# hosts = instance.cluster.metadata.all_hosts() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
# breakpoint() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
host = None | ||||
port = None | ||||
try: | ||||
contact_points = instance.cluster.contact_points | ||||
if len(contact_points) == 1: | ||||
contact_point = next(iter(contact_points)) | ||||
if isinstance(contact_point, str): | ||||
host = contact_point | ||||
port = instance.cluster.port | ||||
elif isinstance(contact_point, tuple): | ||||
host, port = contact_point | ||||
else: # Handle cassandra.connection.Endpoint types | ||||
host = contact_point.address | ||||
port = contact_point.port | ||||
except Exception: | ||||
pass | ||||
|
||||
if sql_parameters is not DEFAULT: | ||||
with DatabaseTrace( | ||||
sql=sql, | ||||
sql_parameters=sql_parameters, | ||||
execute_params=(args, kwargs), | ||||
host=host, | ||||
port_path_or_id=port, | ||||
database_name=database_name, | ||||
dbapi2_module=DBAPI2_MODULE, | ||||
source=wrapped, | ||||
): | ||||
return wrapped(*args, **kwargs) | ||||
else: | ||||
with DatabaseTrace( | ||||
sql=sql, | ||||
execute_params=(args, kwargs), | ||||
host=host, | ||||
port_path_or_id=port, | ||||
database_name=database_name, | ||||
dbapi2_module=DBAPI2_MODULE, | ||||
source=wrapped, | ||||
): | ||||
return wrapped(*args, **kwargs) | ||||
|
||||
return wrapped(*args, **kwargs) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this reachable? |
||||
|
||||
|
||||
def instrument_cassandra(module): | ||||
# Cassandra isn't DBAPI2 compliant, but we need the DatabaseTrace to function properly. We can set parameters | ||||
# for CQL parsing and the product name here, and leave the explain plan functionality unused. | ||||
global DBAPI2_MODULE | ||||
DBAPI2_MODULE = module | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little unorthodox but seems like it will work fine. |
||||
|
||||
register_database_client( | ||||
module, | ||||
database_product="Cassandra", | ||||
quoting_style="single+double", | ||||
explain_query=None, | ||||
explain_stmts=(), | ||||
instance_info=None, # Already handled in wrappers | ||||
) | ||||
|
||||
|
||||
def instrument_cassandra_cluster(module): | ||||
if hasattr(module, "Session"): | ||||
# Cluster connect instrumentation, normally supplied by DBAPI2ConnectionFactory | ||||
wrap_function_trace( | ||||
module, "Cluster.connect", terminal=True, rollup=["Datastore/all", "Datastore/Cassandra/all"] | ||||
) | ||||
|
||||
# Currently Session.execute() is a wrapper for calling Session.execute_async() and immediately waiting for | ||||
# the result. We can therefore just instrument execute_async() and achieve full sync/async coverage. | ||||
# If this changes in the future we'll need an additional wrapper, but care should be taken not to double wrap. | ||||
wrap_function_wrapper(module, "Session.execute_async", wrap_Session_execute_async) | ||||
wrap_function_wrapper(module, "Session.execute", wrap_Session_execute_async) # TODO check this | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove this wrapper then given the above comment about only needing to wrap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So glad we made this-I use it all the time now!