2323)
2424
2525import logging
26- from typing import Optional , List , Union , NoReturn , Tuple
26+ from typing import Optional , List , Union , NoReturn , Tuple , Dict
2727
2828import google .auth
2929from google .api .distribution_pb2 import ( # pylint: disable=no-name-in-module
3939 MonitoredResource ,
4040)
4141
42- from google .cloud .monitoring_v3 .services .metric_service .transports .grpc import (
43- MetricServiceGrpcTransport ,
44- )
45-
4642# pylint: disable=no-name-in-module
4743from google .protobuf .timestamp_pb2 import Timestamp
4844from google .cloud .spanner_v1 .gapic_version import __version__
6056 Sum ,
6157 )
6258 from opentelemetry .sdk .resources import Resource
63-
64- HAS_OPENTELEMETRY_INSTALLED = True
65- except ImportError : # pragma: NO COVER
66- HAS_OPENTELEMETRY_INSTALLED = False
67-
68- try :
59+ from google .cloud .monitoring_v3 .services .metric_service .transports .grpc import (
60+ MetricServiceGrpcTransport ,
61+ )
6962 from google .cloud .monitoring_v3 import (
7063 CreateTimeSeriesRequest ,
7164 MetricServiceClient ,
7568 TypedValue ,
7669 )
7770
78- HAS_GOOGLE_CLOUD_MONITORING_INSTALLED = True
79- except ImportError :
80- HAS_GOOGLE_CLOUD_MONITORING_INSTALLED = False
81-
82- HAS_DEPENDENCIES_INSTALLED = (
83- HAS_OPENTELEMETRY_INSTALLED and HAS_GOOGLE_CLOUD_MONITORING_INSTALLED
84- )
71+ HAS_OPENTELEMETRY_INSTALLED = True
72+ except ImportError : # pragma: NO COVER
73+ HAS_OPENTELEMETRY_INSTALLED = False
74+ MetricExporter = object
8575
8676logger = logging .getLogger (__name__ )
8777MAX_BATCH_WRITE = 200
@@ -120,7 +110,7 @@ class CloudMonitoringMetricsExporter(MetricExporter):
120110 def __init__ (
121111 self ,
122112 project_id : Optional [str ] = None ,
123- client : Optional [MetricServiceClient ] = None ,
113+ client : Optional [" MetricServiceClient" ] = None ,
124114 ):
125115 """Initialize a custom exporter to send metrics for the Spanner Service Metrics."""
126116 # Default preferred_temporality is all CUMULATIVE so need to customize
@@ -144,7 +134,7 @@ def __init__(
144134 self .project_id = project_id
145135 self .project_name = self .client .common_project_path (self .project_id )
146136
147- def _batch_write (self , series : List [TimeSeries ], timeout_millis : float ) -> None :
137+ def _batch_write (self , series : List [" TimeSeries" ], timeout_millis : float ) -> None :
148138 """Cloud Monitoring allows writing up to 200 time series at once.
149139
150140 :param series: ProtoBuf TimeSeries
@@ -166,8 +156,8 @@ def _batch_write(self, series: List[TimeSeries], timeout_millis: float) -> None:
166156
167157 @staticmethod
168158 def _resource_to_monitored_resource_pb (
169- resource : Resource , labels : any
170- ) -> MonitoredResource :
159+ resource : " Resource" , labels : Dict [ str , str ]
160+ ) -> " MonitoredResource" :
171161 """
172162 Convert the resource to a Google Cloud Monitoring monitored resource.
173163
@@ -182,7 +172,7 @@ def _resource_to_monitored_resource_pb(
182172 return monitored_resource
183173
184174 @staticmethod
185- def _to_metric_kind (metric : Metric ) -> MetricDescriptor .MetricKind :
175+ def _to_metric_kind (metric : " Metric" ) -> MetricDescriptor .MetricKind :
186176 """
187177 Convert the metric to a Google Cloud Monitoring metric kind.
188178
@@ -210,7 +200,7 @@ def _to_metric_kind(metric: Metric) -> MetricDescriptor.MetricKind:
210200
211201 @staticmethod
212202 def _extract_metric_labels (
213- data_point : Union [NumberDataPoint , HistogramDataPoint ]
203+ data_point : Union [" NumberDataPoint" , " HistogramDataPoint" ]
214204 ) -> Tuple [dict , dict ]:
215205 """
216206 Extract the metric labels from the data point.
@@ -233,8 +223,8 @@ def _extract_metric_labels(
233223 @staticmethod
234224 def _to_point (
235225 kind : "MetricDescriptor.MetricKind.V" ,
236- data_point : Union [NumberDataPoint , HistogramDataPoint ],
237- ) -> Point :
226+ data_point : Union [" NumberDataPoint" , " HistogramDataPoint" ],
227+ ) -> " Point" :
238228 # Create a Google Cloud Monitoring data point value based on the OpenTelemetry metric data point type
239229 ## For histograms, we need to calculate the mean and bucket counts
240230 if isinstance (data_point , HistogramDataPoint ):
@@ -281,7 +271,7 @@ def _data_point_to_timeseries_pb(
281271 metric ,
282272 monitored_resource ,
283273 labels ,
284- ) -> TimeSeries :
274+ ) -> " TimeSeries" :
285275 """
286276 Convert the data point to a Google Cloud Monitoring time series.
287277
@@ -308,8 +298,8 @@ def _data_point_to_timeseries_pb(
308298
309299 @staticmethod
310300 def _resource_metrics_to_timeseries_pb (
311- metrics_data : MetricsData ,
312- ) -> List [TimeSeries ]:
301+ metrics_data : " MetricsData" ,
302+ ) -> List [" TimeSeries" ]:
313303 """
314304 Convert the metrics data to a list of Google Cloud Monitoring time series.
315305
@@ -346,18 +336,18 @@ def _resource_metrics_to_timeseries_pb(
346336
347337 def export (
348338 self ,
349- metrics_data : MetricsData ,
339+ metrics_data : " MetricsData" ,
350340 timeout_millis : float = 10_000 ,
351341 ** kwargs ,
352- ) -> MetricExportResult :
342+ ) -> " MetricExportResult" :
353343 """
354344 Export the metrics data to Google Cloud Monitoring.
355345
356346 :param metrics_data: OpenTelemetry metrics data
357347 :param timeout_millis: timeout in milliseconds
358348 :return: MetricExportResult
359349 """
360- if not HAS_DEPENDENCIES_INSTALLED :
350+ if not HAS_OPENTELEMETRY_INSTALLED :
361351 logger .warning ("Metric exporter called without dependencies installed." )
362352 return False
363353
0 commit comments