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

dbapi: Add db.collection.name, use connection kwargs for connection attributes #2869

Closed

Conversation

keithZmudzinski
Copy link

@keithZmudzinski keithZmudzinski commented Sep 12, 2024

Description

This pull request adds 3 features to the opentelemetry-instrumentation-dbapi library.

  1. Includes db.collection.name in the span name if possible. This is in line with the semantic conventions for database span names: https://opentelemetry.io/docs/specs/semconv/database/database-spans/#name
  2. Adds the db.collection.name attribute to the span if possible.
  3. Uses more sources for the db.user, net.peer.name, and net.peer.port span attributes.
    • I found that when using opentelemetry-instrumentation-dbapi to instrument MySQLdb, there wasn't a way to get the host from the instantiated Connection class. However, the host is present in the kwargs used to create the connection. If we parse the kwargs for these attributes, we are more likely to get them.

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

I've used these changes in a local project using the console exporter and New Relic across a variety of sql connections. Confirmed the expected attributes were populated.
Added unit tests to verify the changes.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated (NA)

Copy link

linux-foundation-easycla bot commented Sep 12, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@keithZmudzinski keithZmudzinski requested a review from a team September 12, 2024 22:51
@keithZmudzinski
Copy link
Author

I'm working on getting the CLA signed, but for what it's worth I've passed dbapi tests and linting locally

@keithZmudzinski
Copy link
Author

Making progress on the CLA, I've got a person who said they'd be the CLA manager

@keithZmudzinski
Copy link
Author

We've got a CLA manager, just awaiting approval now

@keithZmudzinski keithZmudzinski requested a review from a team as a code owner September 24, 2024 20:31
@keithZmudzinski
Copy link
Author

Sweet, I just got approved. Good to review now I believe

@keithZmudzinski
Copy link
Author

@emdneto Do you know who I could ping to get a review on this?

Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Since the semantic conventions for Database Calls are Experimental, I understand we shouldn't change the version of the database conventions that we emit until it's marked as stable. This implies not adding new attributes that only exist in the new versions of the semantic conventions.

However, I'm ok with adding the DB name to the span name because in v1.11.0 it is allowed.

You may want to consider addressing the linting issues and setting up pre-commit in your environment.

@keithZmudzinski
Copy link
Author

@emdneto Linting and test failures addressed:

I ran the dbapi tests for py10,11,12, all passed:
image
image
image

I've run isort and black and the dbapi files:
image

@keithZmudzinski
Copy link
Author

I'm having some trouble running the docker-tests on my machine. Before I dive into it @emdneto , I just wanted to make sure that the failing test isn't flaky as far as you know?

@@ -84,23 +84,23 @@ def test_execute(self):
stmt = "CREATE TABLE IF NOT EXISTS test (id INT)"
with self._tracer.start_as_current_span("rootSpan"):
self._cursor.execute(stmt)
self.validate_spans("CREATE")
self.validate_spans("CREATE test")
Copy link
Member

@emdneto emdneto Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having some trouble running the docker-tests on my machine. Before I dive into it @emdneto , I just wanted to make sure that the failing test isn't flaky as far as you know?

@keithZmudzinski, they are legit tests that need to be fixed if we want this PR to be merged.

span.set_attribute(
SpanAttributes.DB_SYSTEM, self._db_api_integration.database_system
)
span.set_attribute(
SpanAttributes.DB_NAME, self._db_api_integration.database
)
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
if collection_name:
span.set_attribute(DB_COLLECTION_NAME, collection_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But as I said in the comments before, I wouldn't introduce new semantic convention attributes in this PR.

Comment on lines +296 to +298
# First set from kwargs
if kwargs and value in kwargs:
self.connection_props[key] = kwargs.get(value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting in this PR!

  1. Uses more sources for the db.user, net.peer.name, and net.peer.port span attributes.

Could some tests be added to show expectations for this?

Comment on lines +411 to +413
return " ".join(
name for name in (operation_name, collection_name) if name
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these changes also implement these parts of the spec?

If there is no (low-cardinality) db.operation.name available, database span names SHOULD be {target}.

If neither {db.operation.name} nor {target} are available, span name SHOULD be {db.system}.

I think Yes for the first point, would be good to see some test cases for that as well.

@tammy-baylis-swi
Copy link
Contributor

Hi @keithZmudzinski, we talked about Otel database semantic conventions in today's Python SIG meeting. The changes in PR are appreciated and will be needed at some point. However, because the database semconv is not yet stable (currently it's release candidate), we will wait before making the proposed updates for the span name and attributes changes. This is a topic we will be bringing up with the Otel TC for guidance.

@keithZmudzinski
Copy link
Author

@tammy-baylis-swi Thanks for the update, and for the feedback, I really appreciate it.

Should I let this PR stay put, or close it out and open a new one when the database conventions are marked as STABLE?

@tammy-baylis-swi
Copy link
Contributor

@keithZmudzinski I'd say it's easiest to Close this one and start a fresh PR later, since this monorepo tends to get a lot of merge conflicts introduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants