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

Integrating sql commenter into otel_django_instrumentation #896

Merged

Conversation

Thiyagu55
Copy link
Contributor

@Thiyagu55 Thiyagu55 commented Feb 3, 2022

Description

This PR describes one possible way to integrate [sqlcommenter-django](https://open-telemetry.github.io/opentelemetry-sqlcommenter/) library into opentelemetry-django

One of the ways in which we can implement this is adding the SQLCommenter middleware into django instrumentation when a flag enable_sqlcommenter=True is added. This is similar to how other middlewares are added in opentelemetry-django

When this flag is enabled the SQLCommenter middleware is appended to the django’s existing middlewares. See reference

So for each request, SQLCommenter middleware appends additional infos to the postgresql query logs whenever any db call is made from the controller

We can customize the info to be added at the logs using variables defined in settings.py

It is enabled by adding DjangoInstrumentor().instrument(tracer_provider=provider, is_sql_commentor_enabled=True) in django’s settings.py

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Unit test cases has been added for sqlcommenter integration

  • test_sqlcommenter.py

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added

@Thiyagu55 Thiyagu55 requested a review from a team February 3, 2022 08:47
@owais
Copy link
Contributor

owais commented Feb 3, 2022

Q: Any reason to not add this in psycopg2/mysql/dbapi instrumentations so all frameworks/libraries can benefit instead of only Django? SQL client instrumentations look like a better place to me to host this feature.

@Thiyagu55 Thiyagu55 marked this pull request as draft February 3, 2022 09:48
@sjs994
Copy link

sjs994 commented Feb 7, 2022

Q: Any reason to not add this in psycopg2/mysql/dbapi instrumentations so all frameworks/libraries can benefit instead of only Django? SQL client instrumentations look like a better place to me to host this feature.

Thanks @owais for taking a look.
We have existing code for psycopg2 and SQLAlchemy, which we can import into opentelemetry-python-contrib. For mysql/dbapi instrumentations, we have to take a look at how to implement.

It would be great to understand your views on which approach is preferable ?

  • Adding all instrumentations at once OR
  • Incrementally adding sqlcommenter for one instrumentation at a time

cc: @lonewolf3739, @aabmass

@srikanthccv
Copy link
Member

I would vote for incrementally addition.

We have existing code for psycopg2 and SQLAlchemy, which we can import into opentelemetry-python-contrib

We can't directly import code as is from commenter because there will be conflicting wrappers (ex: before_execute of sqlalchemy). I think it's better we start by updating the corresponding instrumentation with just w3c trace context Information and eventually other info such as request routes etc. Many instrumentations like sqlite3, pymysql, psycopg2, mysql rely on dbapi so you wouldn't need to update each of them individually.

@owais
Copy link
Contributor

owais commented Feb 7, 2022

Incrementally adding sqlcommenter for one instrumentation at a time

Incremental would definitely be better, easier to review and faster merge.

@owais
Copy link
Contributor

owais commented Feb 7, 2022

We can't directly import code as is from commenter because there will be conflicting wrappers (ex: before_execute of sqlalchemy). I think it's better we start by updating the corresponding instrumentation with just w3c trace context Information and eventually other info such as request routes etc. Many instrumentations like sqlite3, pymysql, psycopg2, mysql rely on dbapi so you wouldn't need to update each of them individually.

👍 👍 👍

@sjs994
Copy link

sjs994 commented Feb 7, 2022

We can't directly import code as is from commenter because there will be conflicting wrappers (ex: before_execute of sqlalchemy). I think it's better we start by updating the corresponding instrumentation with just w3c trace context Information and eventually other info such as request routes etc. Many instrumentations like sqlite3, pymysql, psycopg2, mysql rely on dbapi so you wouldn't need to update each of them individually.

Agree, for sqlAlchemy, we have to update the existing instrumentation controlled by a flag. But as many of the instrumentations rely on dbapi, we should look at implementing there instead.

Thanks, then we can start with commenting the trace_context as the first AI. And then will brainstorm on how to populate other metrics.

So, we have to tag along with the existing opentelementry-django middleware and update it to comment queries with trace-context hidden behind an experiment flag instead of adding a new middleware ?

@srikanthccv
Copy link
Member

So, we have to tag along with the existing opentelementry-django middleware and update it to comment queries with trace-context hidden behind an experiment flag instead of adding a new middleware ?

We already have instrumentations for the popular DB backends (Sqlite, Postgres, MariaDB etc..) supported by Django. The query middleware might double comment and doesn't enrich comment significantly. As suggested by Owais let's start with sql client instrumentations. Let's make a simplest plan that covers the 90% real world use cases and then improve on top of it, what do you think?

@sjs994
Copy link

sjs994 commented Feb 8, 2022

@lonewolf3739 I agree to that. I believe we already have the tracecontext information in otel sql client libraries. So, it should be just appending these tracecontext values into sql comment.

@Thiyagu55 Thiyagu55 marked this pull request as ready for review June 15, 2022 09:41
@Thiyagu55 Thiyagu55 changed the title [WIP] : Integrating sql commenter into otel_django_instrumentation Integrating sql commenter into otel_django_instrumentation Jun 15, 2022
Copy link
Member

@srikanthccv srikanthccv left a comment

Choose a reason for hiding this comment

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

LGTM, some minor comments around word choices.

@lzchen
Copy link
Contributor

lzchen commented Jun 28, 2022

@Thiyagu55
Would you like to be an owner for this specific component? See this for more details.

@Thiyagu55
Copy link
Contributor Author

Thiyagu55 commented Jun 29, 2022

@Thiyagu55
Would you like to be an owner for this specific component? See this for more details.

Yes I can cc: @sjs994

@ocelotl ocelotl merged commit ac84e99 into open-telemetry:main Jun 29, 2022
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.

6 participants