|
10 | 10 |
|
11 | 11 | import opentelemetry.instrumentation.cassandra |
12 | 12 | from opentelemetry import trace as trace_api |
| 13 | +from opentelemetry.instrumentation._semconv import ( |
| 14 | + _OpenTelemetrySemanticConventionStability, |
| 15 | +) |
13 | 16 | from opentelemetry.instrumentation.cassandra import CassandraInstrumentor |
14 | 17 | from opentelemetry.instrumentation.cassandra.package import ( |
15 | 18 | _instruments_cassandra_driver, |
16 | 19 | _instruments_scylla_driver, |
17 | 20 | ) |
18 | 21 | from opentelemetry.sdk import resources |
| 22 | +from opentelemetry.semconv._incubating.attributes.db_attributes import ( |
| 23 | + DB_NAME, |
| 24 | + DB_STATEMENT, |
| 25 | + DB_SYSTEM, |
| 26 | +) |
| 27 | +from opentelemetry.semconv._incubating.attributes.net_attributes import ( |
| 28 | + NET_PEER_NAME, |
| 29 | +) |
| 30 | +from opentelemetry.semconv.attributes.db_attributes import ( |
| 31 | + DB_NAMESPACE, |
| 32 | + DB_QUERY_TEXT, |
| 33 | + DB_SYSTEM_NAME, |
| 34 | +) |
| 35 | +from opentelemetry.semconv.attributes.server_attributes import ( |
| 36 | + SERVER_ADDRESS, |
| 37 | +) |
19 | 38 | from opentelemetry.test.test_base import TestBase |
20 | 39 | from opentelemetry.trace import SpanKind |
21 | 40 |
|
@@ -241,3 +260,123 @@ def _distribution(name): |
241 | 260 | package_to_instrument, |
242 | 261 | (_instruments_cassandra_driver, _instruments_scylla_driver), |
243 | 262 | ) |
| 263 | + |
| 264 | + |
| 265 | +class TestCassandraSemconvStability(TestBase): |
| 266 | + def tearDown(self): |
| 267 | + super().tearDown() |
| 268 | + with self.disable_logging(): |
| 269 | + CassandraInstrumentor().uninstrument() |
| 270 | + _OpenTelemetrySemanticConventionStability._initialized = False |
| 271 | + |
| 272 | + @property |
| 273 | + def _mocked_session(self): |
| 274 | + return cassandra.cluster.Session(cluster=mock.Mock(), hosts=[]) |
| 275 | + |
| 276 | + @mock.patch("cassandra.cluster.Cluster.connect") |
| 277 | + @mock.patch("cassandra.cluster.Session.__init__") |
| 278 | + @mock.patch("cassandra.cluster.Session._create_response_future") |
| 279 | + def test_default_semconv( |
| 280 | + self, mock_create_response_future, mock_session_init, mock_connect |
| 281 | + ): |
| 282 | + mock_create_response_future.return_value = mock.Mock() |
| 283 | + mock_session_init.return_value = None |
| 284 | + mock_connect.return_value = self._mocked_session |
| 285 | + |
| 286 | + CassandraInstrumentor().instrument(include_db_statement=True) |
| 287 | + connect_and_execute_query() |
| 288 | + |
| 289 | + spans = self.memory_exporter.get_finished_spans() |
| 290 | + self.assertEqual(len(spans), 1) |
| 291 | + span = spans[0] |
| 292 | + |
| 293 | + self.assertEqual(span.attributes[DB_NAME], "test") |
| 294 | + self.assertEqual(span.attributes[DB_SYSTEM], "cassandra") |
| 295 | + self.assertEqual(span.attributes[DB_STATEMENT], "SELECT * FROM test") |
| 296 | + self.assertIn(NET_PEER_NAME, span.attributes) |
| 297 | + self.assertNotIn(DB_NAMESPACE, span.attributes) |
| 298 | + self.assertNotIn(DB_SYSTEM_NAME, span.attributes) |
| 299 | + self.assertNotIn(DB_QUERY_TEXT, span.attributes) |
| 300 | + self.assertNotIn(SERVER_ADDRESS, span.attributes) |
| 301 | + self.assertEqual( |
| 302 | + span.instrumentation_scope.schema_url, |
| 303 | + "https://opentelemetry.io/schemas/1.11.0", |
| 304 | + ) |
| 305 | + |
| 306 | + @mock.patch("cassandra.cluster.Cluster.connect") |
| 307 | + @mock.patch("cassandra.cluster.Session.__init__") |
| 308 | + @mock.patch("cassandra.cluster.Session._create_response_future") |
| 309 | + def test_new_semconv( |
| 310 | + self, mock_create_response_future, mock_session_init, mock_connect |
| 311 | + ): |
| 312 | + mock_create_response_future.return_value = mock.Mock() |
| 313 | + mock_session_init.return_value = None |
| 314 | + mock_connect.return_value = self._mocked_session |
| 315 | + |
| 316 | + with mock.patch.dict( |
| 317 | + "os.environ", |
| 318 | + {"OTEL_SEMCONV_STABILITY_OPT_IN": "database"}, |
| 319 | + ): |
| 320 | + _OpenTelemetrySemanticConventionStability._initialized = False |
| 321 | + CassandraInstrumentor().instrument(include_db_statement=True) |
| 322 | + connect_and_execute_query() |
| 323 | + |
| 324 | + spans = self.memory_exporter.get_finished_spans() |
| 325 | + self.assertEqual(len(spans), 1) |
| 326 | + span = spans[0] |
| 327 | + |
| 328 | + self.assertEqual(span.attributes[DB_NAMESPACE], "test") |
| 329 | + self.assertEqual(span.attributes[DB_SYSTEM_NAME], "cassandra") |
| 330 | + self.assertEqual( |
| 331 | + span.attributes[DB_QUERY_TEXT], "SELECT * FROM test" |
| 332 | + ) |
| 333 | + self.assertIn(SERVER_ADDRESS, span.attributes) |
| 334 | + self.assertNotIn(DB_NAME, span.attributes) |
| 335 | + self.assertNotIn(DB_SYSTEM, span.attributes) |
| 336 | + self.assertNotIn(DB_STATEMENT, span.attributes) |
| 337 | + self.assertNotIn(NET_PEER_NAME, span.attributes) |
| 338 | + self.assertEqual( |
| 339 | + span.instrumentation_scope.schema_url, |
| 340 | + "https://opentelemetry.io/schemas/1.25.0", |
| 341 | + ) |
| 342 | + _OpenTelemetrySemanticConventionStability._initialized = False |
| 343 | + |
| 344 | + @mock.patch("cassandra.cluster.Cluster.connect") |
| 345 | + @mock.patch("cassandra.cluster.Session.__init__") |
| 346 | + @mock.patch("cassandra.cluster.Session._create_response_future") |
| 347 | + def test_dup_semconv( |
| 348 | + self, mock_create_response_future, mock_session_init, mock_connect |
| 349 | + ): |
| 350 | + mock_create_response_future.return_value = mock.Mock() |
| 351 | + mock_session_init.return_value = None |
| 352 | + mock_connect.return_value = self._mocked_session |
| 353 | + |
| 354 | + with mock.patch.dict( |
| 355 | + "os.environ", |
| 356 | + {"OTEL_SEMCONV_STABILITY_OPT_IN": "database/dup"}, |
| 357 | + ): |
| 358 | + _OpenTelemetrySemanticConventionStability._initialized = False |
| 359 | + CassandraInstrumentor().instrument(include_db_statement=True) |
| 360 | + connect_and_execute_query() |
| 361 | + |
| 362 | + spans = self.memory_exporter.get_finished_spans() |
| 363 | + self.assertEqual(len(spans), 1) |
| 364 | + span = spans[0] |
| 365 | + |
| 366 | + self.assertEqual(span.attributes[DB_NAME], "test") |
| 367 | + self.assertEqual(span.attributes[DB_SYSTEM], "cassandra") |
| 368 | + self.assertEqual( |
| 369 | + span.attributes[DB_STATEMENT], "SELECT * FROM test" |
| 370 | + ) |
| 371 | + self.assertIn(NET_PEER_NAME, span.attributes) |
| 372 | + self.assertEqual(span.attributes[DB_NAMESPACE], "test") |
| 373 | + self.assertEqual(span.attributes[DB_SYSTEM_NAME], "cassandra") |
| 374 | + self.assertEqual( |
| 375 | + span.attributes[DB_QUERY_TEXT], "SELECT * FROM test" |
| 376 | + ) |
| 377 | + self.assertIn(SERVER_ADDRESS, span.attributes) |
| 378 | + self.assertEqual( |
| 379 | + span.instrumentation_scope.schema_url, |
| 380 | + "https://opentelemetry.io/schemas/1.25.0", |
| 381 | + ) |
| 382 | + _OpenTelemetrySemanticConventionStability._initialized = False |
0 commit comments