Skip to content

Commit

Permalink
Add changelog entry and supports_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr committed Feb 6, 2023
1 parent cecafe1 commit 6e84d30
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Added

- Add metrics instrumentation for sqlalchemy
([#1645](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1645))
- `opentelemetry-instrumentation-redis` Add `sanitize_query` config option to allow query sanitization. ([#1572](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1572))
- `opentelemetry-instrumentation-celery` Record exceptions as events on the span.
([#1573](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1573))
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
| [opentelemetry-instrumentation-remoulade](./opentelemetry-instrumentation-remoulade) | remoulade >= 0.50 | No
| [opentelemetry-instrumentation-requests](./opentelemetry-instrumentation-requests) | requests ~= 2.0 | Yes
| [opentelemetry-instrumentation-sklearn](./opentelemetry-instrumentation-sklearn) | scikit-learn ~= 0.24.0 | No
| [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy | No
| [opentelemetry-instrumentation-sqlalchemy](./opentelemetry-instrumentation-sqlalchemy) | sqlalchemy | Yes
| [opentelemetry-instrumentation-sqlite3](./opentelemetry-instrumentation-sqlite3) | sqlite3 | No
| [opentelemetry-instrumentation-starlette](./opentelemetry-instrumentation-starlette) | starlette ~= 0.13.0 | Yes
| [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _instrument(self, **kwargs):
meter = get_meter(__name__, __version__, meter_provider)

connections_usage = meter.create_up_down_counter(
name=MetricInstruments.DB_CLIENT_CONNECTIONS_USAGE,
name="db.client.connections.usage",
unit="connections",
description="The number of connections that are currently in state described by the state attribute.",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
_instrumenting_module_name = "opentelemetry.instrumentation.sqlalchemy"

_instruments = ("sqlalchemy",)

_supports_metrics = True
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from sqlalchemy import create_engine
import sqlalchemy
from sqlalchemy.pool import QueuePool

from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
Expand All @@ -27,6 +27,12 @@


class TestSqlalchemyMetricsInstrumentation(TestBase):
def setUp(self):
super().setUp()
SQLAlchemyInstrumentor().instrument(
tracer_provider=self.tracer_provider,
)

def tearDown(self):
super().tearDown()
SQLAlchemyInstrumentor().uninstrument()
Expand Down Expand Up @@ -138,17 +144,13 @@ def assert_metrics_used_idle_as_expected(self, pool_name, idle, used):

def test_metrics_one_connection(self):
pool_name = "pool_test_name"
self.engine = create_engine(
self.engine = sqlalchemy.create_engine(
"sqlite:///:memory:",
pool_size=5,
poolclass=QueuePool,
pool_logging_name=pool_name,
)

SQLAlchemyInstrumentor().instrument(
engine=self.engine,
tracer_provider=self.tracer_provider,
)
metrics = self.get_sorted_metrics()
self.assertEqual(len(metrics), 0)

Expand All @@ -164,16 +166,12 @@ def test_metrics_one_connection(self):

def test_metrics_without_pool_name(self):
pool_name = ""
self.engine = create_engine(
self.engine = sqlalchemy.create_engine(
"sqlite:///:memory:",
pool_size=5,
poolclass=QueuePool,
)

SQLAlchemyInstrumentor().instrument(
engine=self.engine,
tracer_provider=self.tracer_provider,
)
metrics = self.get_sorted_metrics()
self.assertEqual(len(metrics), 0)

Expand All @@ -189,17 +187,13 @@ def test_metrics_without_pool_name(self):

def test_metrics_two_connections(self):
pool_name = "pool_test_name"
self.engine = create_engine(
self.engine = sqlalchemy.create_engine(
"sqlite:///:memory:",
pool_size=5,
poolclass=QueuePool,
pool_logging_name=pool_name,
)

SQLAlchemyInstrumentor().instrument(
engine=self.engine,
tracer_provider=self.tracer_provider,
)
metrics = self.get_sorted_metrics()
self.assertEqual(len(metrics), 0)

Expand All @@ -219,17 +213,13 @@ def test_metrics_two_connections(self):

def test_metrics_connections(self):
pool_name = "pool_test_name"
self.engine = create_engine(
self.engine = sqlalchemy.create_engine(
"sqlite:///:memory:",
pool_size=5,
poolclass=QueuePool,
pool_logging_name=pool_name,
)

SQLAlchemyInstrumentor().instrument(
engine=self.engine,
tracer_provider=self.tracer_provider,
)
metrics = self.get_sorted_metrics()
self.assertEqual(len(metrics), 0)

Expand All @@ -254,3 +244,15 @@ def test_metrics_connections(self):
self.assert_metrics_used_idle_as_expected(
pool_name=pool_name, idle=2, used=0
)

def test_metric_uninstrument(self):
SQLAlchemyInstrumentor().uninstrument()
self.engine = sqlalchemy.create_engine(
"sqlite:///:memory:",
poolclass=QueuePool,
)

self.engine.connect()

metrics = self.get_sorted_metrics()
self.assertEqual(len(metrics), 0)

0 comments on commit 6e84d30

Please sign in to comment.