|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +try: |
| 18 | + from opentelemetry.metrics import Counter, Histogram, get_meter_provider |
| 19 | + |
| 20 | + HAS_OPENTELEMETRY_INSTALLED = True |
| 21 | +except ImportError: # pragma: NO COVER |
| 22 | + HAS_OPENTELEMETRY_INSTALLED = False |
| 23 | +from typing import List |
| 24 | +import re |
| 25 | +from google.cloud.spanner_v1.metrics.constants import ( |
| 26 | + BUILT_IN_METRICS_METER_NAME, |
| 27 | + SPANNER_SERVICE_NAME, |
| 28 | + METRIC_NAME_GFE_LATENCY, |
| 29 | + METRIC_NAME_GFE_MISSING_HEADER_COUNT, |
| 30 | +) |
| 31 | +from google.cloud.spanner_v1 import __version__ |
| 32 | + |
| 33 | + |
| 34 | +class MetricsGfeTracer: |
| 35 | + _instrument_gfe_latency: Histogram |
| 36 | + _instrument_gfe_missing_header_count: Counter |
| 37 | + enabled = False |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + self._create_gfe_metric_instruments() |
| 41 | + |
| 42 | + def record_gfe_metrics(self, metadata: List): |
| 43 | + if not self.enabled: |
| 44 | + return |
| 45 | + |
| 46 | + gfe_headers = [ |
| 47 | + header.value |
| 48 | + for header in metadata |
| 49 | + if header.key == "server-timing" and header.value.startswith("gfe") |
| 50 | + ] |
| 51 | + |
| 52 | + if len(gfe_headers) == 0: |
| 53 | + self.record_gfe_missing_header_count() |
| 54 | + return |
| 55 | + |
| 56 | + for gfe_header in gfe_headers: |
| 57 | + match = re.search(r"dur=(\d+)", gfe_header) |
| 58 | + if match: |
| 59 | + duration = int(match.group(1)) |
| 60 | + self.record_gfe_latency(duration) |
| 61 | + |
| 62 | + def record_gfe_latency(self, latency: int) -> None: |
| 63 | + print("real called") |
| 64 | + if not self.enabled: |
| 65 | + return |
| 66 | + self._instrument_gfe_latency.record(amount=latency) |
| 67 | + |
| 68 | + def record_gfe_missing_header_count(self) -> None: |
| 69 | + if not self.enabled: |
| 70 | + return |
| 71 | + self._instrument_gfe_missing_header_count.add(amount=1) |
| 72 | + |
| 73 | + def _create_gfe_metric_instruments(self): |
| 74 | + meter_provider = get_meter_provider() |
| 75 | + meter = meter_provider.get_meter( |
| 76 | + name=BUILT_IN_METRICS_METER_NAME, version=__version__ |
| 77 | + ) |
| 78 | + self._instrument_gfe_latency = meter.create_histogram( |
| 79 | + name=f"{SPANNER_SERVICE_NAME}/{METRIC_NAME_GFE_LATENCY}", |
| 80 | + unit="ms", |
| 81 | + description="GFE Latency.", |
| 82 | + ) |
| 83 | + self._instrument_gfe_missing_header_count = meter.create_counter( |
| 84 | + name=f"{SPANNER_SERVICE_NAME}/{METRIC_NAME_GFE_MISSING_HEADER_COUNT}", |
| 85 | + unit="1", |
| 86 | + description="GFE missing header count.", |
| 87 | + ) |
0 commit comments