| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9535e5e commit bb5095d
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,17 +24,12 @@ | |||
| 24 | 24 | _metadata_with_span_context, | |
| 25 | 25 | ) | |
| 26 | 26 | ||
| 27 | - try: | ||
| 28 | - from opentelemetry import trace | ||
| 29 | - from opentelemetry.trace.status import Status, StatusCode | ||
| 30 | - from opentelemetry.semconv.attributes.otel_attributes import ( | ||
| 31 | - OTEL_SCOPE_NAME, | ||
| 32 | - OTEL_SCOPE_VERSION, | ||
| 33 | - ) | ||
| 34 | - | ||
| 35 | - HAS_OPENTELEMETRY_INSTALLED = True | ||
| 36 | - except ImportError: | ||
| 37 | - HAS_OPENTELEMETRY_INSTALLED = False | ||
| 27 | + from opentelemetry import trace | ||
| 28 | + from opentelemetry.trace.status import Status, StatusCode | ||
| 29 | + from opentelemetry.semconv.attributes.otel_attributes import ( | ||
| 30 | + OTEL_SCOPE_NAME, | ||
| 31 | + OTEL_SCOPE_VERSION, | ||
| 32 | + ) | ||
| 38 | 33 | ||
| 39 | 34 | from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture | |
| 40 | 35 | ||
@@ -70,11 +65,6 @@ def trace_call( | |||
| 70 | 65 | if session: | |
| 71 | 66 | session._last_use_time = datetime.now() | |
| 72 | 67 | ||
| 73 | - if not (HAS_OPENTELEMETRY_INSTALLED and name): | ||
| 74 | - # Empty context manager. Users will have to check if the generated value is None or a span | ||
| 75 | - yield None | ||
| 76 | - return | ||
| 77 | - | ||
| 78 | 68 | tracer_provider = None | |
| 79 | 69 | ||
| 80 | 70 | # By default enable_extended_tracing=True because in a bid to minimize | |
@@ -155,11 +145,8 @@ def trace_call( | |||
| 155 | 145 | ||
| 156 | 146 | ||
| 157 | 147 | def get_current_span(): | |
| 158 | - if not HAS_OPENTELEMETRY_INSTALLED: | ||
| 159 | - return None | ||
| 160 | 148 | return trace.get_current_span() | |
| 161 | 149 | ||
| 162 | 150 | ||
| 163 | 151 | def add_span_event(span, event_name, event_attributes=None): | |
| 164 | - if span: | ||
| 165 | - span.add_event(event_name, event_attributes) | ||
| 152 | + span.add_event(event_name, event_attributes) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,7 +43,7 @@ def with_request_id( | |||
| 43 | 43 | all_metadata = (other_metadata or []).copy() | |
| 44 | 44 | all_metadata.append((REQ_ID_HEADER_KEY, req_id)) | |
| 45 | 45 | ||
| 46 | - if span is not None: | ||
| 46 | + if span: | ||
| 47 | 47 | span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id) | |
| 48 | 48 | ||
| 49 | 49 | return all_metadata | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -251,11 +251,9 @@ def exists(self): | |||
| 251 | 251 | span, | |
| 252 | 252 | ), | |
| 253 | 253 | ) | |
| 254 | - if span: | ||
| 255 | - span.set_attribute("session_found", True) | ||
| 254 | + span.set_attribute("session_found", True) | ||
| 256 | 255 | except NotFound: | |
| 257 | - if span: | ||
| 258 | - span.set_attribute("session_found", False) | ||
| 256 | + span.set_attribute("session_found", False) | ||
| 259 | 257 | return False | |
| 260 | 258 | ||
| 261 | 259 | return True | |
@@ -317,18 +315,21 @@ def ping(self): | |||
| 317 | 315 | """ | |
| 318 | 316 | if self._session_id is None: | |
| 319 | 317 | raise ValueError("Session ID not set by back-end") | |
| 318 | + | ||
| 320 | 319 | database = self._database | |
| 321 | 320 | api = database.spanner_api | |
| 322 | - request = ExecuteSqlRequest(session=self.name, sql="SELECT 1") | ||
| 323 | - api.execute_sql( | ||
| 324 | - request=request, | ||
| 325 | - metadata=database.metadata_with_request_id( | ||
| 326 | - database._next_nth_request, | ||
| 327 | - 1, | ||
| 328 | - _metadata_with_prefix(database.name), | ||
| 329 | - ), | ||
| 330 | - ) | ||
| 331 | - self._last_use_time = datetime.now() | ||
| 321 | + | ||
| 322 | + with trace_call("CloudSpanner.Session.ping", self) as span: | ||
| 323 | + request = ExecuteSqlRequest(session=self.name, sql="SELECT 1") | ||
| 324 | + api.execute_sql( | ||
| 325 | + request=request, | ||
| 326 | + metadata=database.metadata_with_request_id( | ||
| 327 | + database._next_nth_request, | ||
| 328 | + 1, | ||
| 329 | + _metadata_with_prefix(database.name), | ||
| 330 | + span, | ||
| 331 | + ), | ||
| 332 | + ) | ||
| 332 | 333 | ||
| 333 | 334 | def snapshot(self, **kw): | |
| 334 | 335 | """Create a snapshot to perform a set of reads with shared staleness. | |
@@ -566,20 +567,18 @@ def run_in_transaction(self, func, *args, **kw): | |||
| 566 | 567 | ||
| 567 | 568 | except Aborted as exc: | |
| 568 | 569 | previous_transaction_id = txn._transaction_id | |
| 569 | - if span: | ||
| 570 | - delay_seconds = _get_retry_delay( | ||
| 571 | - exc.errors[0], | ||
| 572 | - attempts, | ||
| 573 | - default_retry_delay=default_retry_delay, | ||
| 574 | - ) | ||
| 575 | - attributes = dict(delay_seconds=delay_seconds, cause=str(exc)) | ||
| 576 | - attributes.update(span_attributes) | ||
| 577 | - add_span_event( | ||
| 578 | - span, | ||
| 579 | - "Transaction was aborted in user operation, retrying", | ||
| 580 | - attributes, | ||
| 581 | - ) | ||
| 582 | - | ||
| 570 | + delay_seconds = _get_retry_delay( | ||
| 571 | + exc.errors[0], | ||
| 572 | + attempts, | ||
| 573 | + default_retry_delay=default_retry_delay, | ||
| 574 | + ) | ||
| 575 | + attributes = dict(delay_seconds=delay_seconds, cause=str(exc)) | ||
| 576 | + attributes.update(span_attributes) | ||
| 577 | + add_span_event( | ||
| 578 | + span, | ||
| 579 | + "Transaction was aborted in user operation, retrying", | ||
| 580 | + attributes, | ||
| 581 | + ) | ||
| 583 | 582 | _delay_until_retry( | |
| 584 | 583 | exc, deadline, attempts, default_retry_delay=default_retry_delay | |
| 585 | 584 | ) | |
@@ -611,20 +610,18 @@ def run_in_transaction(self, func, *args, **kw): | |||
| 611 | 610 | ||
| 612 | 611 | except Aborted as exc: | |
| 613 | 612 | previous_transaction_id = txn._transaction_id | |
| 614 | - if span: | ||
| 615 | - delay_seconds = _get_retry_delay( | ||
| 616 | - exc.errors[0], | ||
| 617 | - attempts, | ||
| 618 | - default_retry_delay=default_retry_delay, | ||
| 619 | - ) | ||
| 620 | - attributes = dict(delay_seconds=delay_seconds) | ||
| 621 | - attributes.update(span_attributes) | ||
| 622 | - add_span_event( | ||
| 623 | - span, | ||
| 624 | - "Transaction was aborted during commit, retrying", | ||
| 625 | - attributes, | ||
| 626 | - ) | ||
| 627 | - | ||
| 613 | + delay_seconds = _get_retry_delay( | ||
| 614 | + exc.errors[0], | ||
| 615 | + attempts, | ||
| 616 | + default_retry_delay=default_retry_delay, | ||
| 617 | + ) | ||
| 618 | + attributes = dict(delay_seconds=delay_seconds) | ||
| 619 | + attributes.update(span_attributes) | ||
| 620 | + add_span_event( | ||
| 621 | + span, | ||
| 622 | + "Transaction was aborted during commit, retrying", | ||
| 623 | + attributes, | ||
| 624 | + ) | ||
| 628 | 625 | _delay_until_retry( | |
| 629 | 626 | exc, deadline, attempts, default_retry_delay=default_retry_delay | |
| 630 | 627 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,18 +44,15 @@ | |||
| 44 | 44 | "proto-plus >= 1.22.2, <2.0.0; python_version>='3.11'", | |
| 45 | 45 | "protobuf>=3.20.2,<7.0.0,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", | |
| 46 | 46 | "grpc-interceptor >= 0.15.4", | |
| 47 | + # Make OpenTelemetry a core dependency | ||
| 48 | + "opentelemetry-api >= 1.22.0", | ||
| 49 | + "opentelemetry-sdk >= 1.22.0", | ||
| 50 | + "opentelemetry-semantic-conventions >= 0.43b0", | ||
| 51 | + "opentelemetry-resourcedetector-gcp >= 1.8.0a0", | ||
| 52 | + "google-cloud-monitoring >= 2.16.0", | ||
| 53 | + "mmh3 >= 4.1.0 ", | ||
| 47 | 54 | ] | |
| 48 | - extras = { | ||
| 49 | - "tracing": [ | ||
| 50 | - "opentelemetry-api >= 1.22.0", | ||
| 51 | - "opentelemetry-sdk >= 1.22.0", | ||
| 52 | - "opentelemetry-semantic-conventions >= 0.43b0", | ||
| 53 | - "opentelemetry-resourcedetector-gcp >= 1.8.0a0", | ||
| 54 | - "google-cloud-monitoring >= 2.16.0", | ||
| 55 | - "mmh3 >= 4.1.0 ", | ||
| 56 | - ], | ||
| 57 | - "libcst": "libcst >= 0.2.5", | ||
| 58 | - } | ||
| 55 | + extras = {"libcst": "libcst >= 0.2.5"} | ||
| 59 | 56 | ||
| 60 | 57 | url = "https://github.com/googleapis/python-spanner" | |
| 61 | 58 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments