Skip to content

Commit 42b25e0

Browse files
committed
Add exporter/opentelemetry-exporter-otlp-proto-grpc to the pyright include list. Fix all lint errors in that package.
1 parent e46db88 commit 42b25e0

File tree

8 files changed

+128
-76
lines changed

8 files changed

+128
-76
lines changed

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313

1414
from os import environ
15-
from typing import Dict, Optional, Sequence, Tuple, Union
15+
from typing import Dict, Literal, Optional, Sequence, Tuple, Union
1616
from typing import Sequence as TypingSequence
1717

1818
from grpc import ChannelCredentials, Compression
@@ -29,7 +29,6 @@
2929
LogsServiceStub,
3030
)
3131
from opentelemetry.sdk._logs import LogData
32-
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
3332
from opentelemetry.sdk._logs.export import LogExporter, LogExportResult
3433
from opentelemetry.sdk.environment_variables import (
3534
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
@@ -45,11 +44,13 @@
4544

4645
class OTLPLogExporter(
4746
LogExporter,
48-
OTLPExporterMixin[SDKLogRecord, ExportLogsServiceRequest, LogExportResult],
47+
OTLPExporterMixin[
48+
Sequence[LogData],
49+
ExportLogsServiceRequest,
50+
LogExportResult,
51+
LogsServiceStub,
52+
],
4953
):
50-
_result = LogExportResult
51-
_stub = LogsServiceStub
52-
5354
def __init__(
5455
self,
5556
endpoint: Optional[str] = None,
@@ -61,10 +62,9 @@ def __init__(
6162
timeout: Optional[int] = None,
6263
compression: Optional[Compression] = None,
6364
):
64-
if insecure is None:
65-
insecure = environ.get(OTEL_EXPORTER_OTLP_LOGS_INSECURE)
66-
if insecure is not None:
67-
insecure = insecure.lower() == "true"
65+
insecure_logs = environ.get(OTEL_EXPORTER_OTLP_LOGS_INSECURE)
66+
if insecure is None and insecure_logs is not None:
67+
insecure = insecure_logs.lower() == "true"
6868

6969
if (
7070
not insecure
@@ -99,6 +99,8 @@ def __init__(
9999
"headers": headers,
100100
"timeout": timeout or environ_timeout,
101101
"compression": compression,
102+
"stub": LogsServiceStub,
103+
"result": LogExportResult,
102104
}
103105
)
104106

@@ -107,8 +109,11 @@ def _translate_data(
107109
) -> ExportLogsServiceRequest:
108110
return encode_logs(data)
109111

110-
def export(self, batch: Sequence[LogData]) -> LogExportResult:
111-
return self._export(batch)
112+
def export( # pyright: ignore [reportIncompatibleMethodOverride]
113+
self,
114+
batch: Sequence[LogData],
115+
) -> Literal[LogExportResult.SUCCESS, LogExportResult.FAILURE]:
116+
return OTLPExporterMixin._export(self, batch)
112117

113118
def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
114119
OTLPExporterMixin.shutdown(self, timeout_millis=timeout_millis)

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
Dict,
2727
Generic,
2828
List,
29+
Literal,
30+
NewType,
2931
Optional,
3032
Tuple,
33+
Type,
3134
TypeVar,
3235
Union,
3336
)
@@ -53,12 +56,32 @@
5356
from opentelemetry.exporter.otlp.proto.grpc import (
5457
_OTLP_GRPC_HEADERS,
5558
)
59+
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import (
60+
ExportLogsServiceRequest,
61+
)
62+
from opentelemetry.proto.collector.logs.v1.logs_service_pb2_grpc import (
63+
LogsServiceStub,
64+
)
65+
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import (
66+
ExportMetricsServiceRequest,
67+
)
68+
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2_grpc import (
69+
MetricsServiceStub,
70+
)
71+
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
72+
ExportTraceServiceRequest,
73+
)
74+
from opentelemetry.proto.collector.trace.v1.trace_service_pb2_grpc import (
75+
TraceServiceStub,
76+
)
5677
from opentelemetry.proto.common.v1.common_pb2 import ( # noqa: F401
5778
AnyValue,
5879
ArrayValue,
5980
KeyValue,
6081
)
6182
from opentelemetry.proto.resource.v1.resource_pb2 import Resource # noqa: F401
83+
from opentelemetry.sdk._logs import LogData
84+
from opentelemetry.sdk._logs.export import LogExportResult
6285
from opentelemetry.sdk.environment_variables import (
6386
OTEL_EXPORTER_OTLP_CERTIFICATE,
6487
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE,
@@ -69,17 +92,36 @@
6992
OTEL_EXPORTER_OTLP_INSECURE,
7093
OTEL_EXPORTER_OTLP_TIMEOUT,
7194
)
72-
from opentelemetry.sdk.metrics.export import MetricsData
95+
from opentelemetry.sdk.metrics.export import MetricExportResult, MetricsData
7396
from opentelemetry.sdk.resources import Resource as SDKResource
7497
from opentelemetry.sdk.trace import ReadableSpan
98+
from opentelemetry.sdk.trace.export import SpanExportResult
7599
from opentelemetry.util.re import parse_env_headers
76100

77101
logger = getLogger(__name__)
78-
SDKDataT = TypeVar("SDKDataT")
102+
SDKDataT = TypeVar(
103+
"SDKDataT",
104+
TypingSequence[LogData],
105+
MetricsData,
106+
TypingSequence[ReadableSpan],
107+
)
79108
ResourceDataT = TypeVar("ResourceDataT")
80109
TypingResourceT = TypeVar("TypingResourceT")
81-
ExportServiceRequestT = TypeVar("ExportServiceRequestT")
82-
ExportResultT = TypeVar("ExportResultT")
110+
ExportServiceRequestT = TypeVar(
111+
"ExportServiceRequestT",
112+
ExportTraceServiceRequest,
113+
ExportMetricsServiceRequest,
114+
ExportLogsServiceRequest,
115+
)
116+
ExportResultT = TypeVar(
117+
"ExportResultT",
118+
LogExportResult,
119+
MetricExportResult,
120+
SpanExportResult,
121+
)
122+
ExportStubT = TypeVar(
123+
"ExportStubT", TraceServiceStub, MetricsServiceStub, LogsServiceStub
124+
)
83125

84126
_ENVIRON_TO_COMPRESSION = {
85127
None: None,
@@ -102,7 +144,10 @@ def environ_to_compression(environ_key: str) -> Optional[Compression]:
102144
if environ_key in environ
103145
else None
104146
)
105-
if environ_value not in _ENVIRON_TO_COMPRESSION:
147+
if (
148+
environ_value not in _ENVIRON_TO_COMPRESSION
149+
and environ_value is not None
150+
):
106151
raise InvalidCompressionValueException(environ_key, environ_value)
107152
return _ENVIRON_TO_COMPRESSION[environ_value]
108153

@@ -135,7 +180,7 @@ def _load_credentials(
135180
certificate_file: Optional[str],
136181
client_key_file: Optional[str],
137182
client_certificate_file: Optional[str],
138-
) -> Optional[ChannelCredentials]:
183+
) -> ChannelCredentials:
139184
root_certificates = (
140185
_read_file(certificate_file) if certificate_file else None
141186
)
@@ -174,7 +219,7 @@ def _get_credentials(
174219

175220
# pylint: disable=no-member
176221
class OTLPExporterMixin(
177-
ABC, Generic[SDKDataT, ExportServiceRequestT, ExportResultT]
222+
ABC, Generic[SDKDataT, ExportServiceRequestT, ExportResultT, ExportStubT]
178223
):
179224
"""OTLP span exporter
180225
@@ -189,6 +234,8 @@ class OTLPExporterMixin(
189234

190235
def __init__(
191236
self,
237+
stub: ExportStubT,
238+
result: ExportResultT,
192239
endpoint: Optional[str] = None,
193240
insecure: Optional[bool] = None,
194241
credentials: Optional[ChannelCredentials] = None,
@@ -199,7 +246,8 @@ def __init__(
199246
compression: Optional[Compression] = None,
200247
):
201248
super().__init__()
202-
249+
self._result = result
250+
self._stub = stub
203251
self._endpoint = endpoint or environ.get(
204252
OTEL_EXPORTER_OTLP_ENDPOINT, "http://localhost:4317"
205253
)
@@ -208,15 +256,12 @@ def __init__(
208256

209257
if parsed_url.scheme == "https":
210258
insecure = False
259+
insecure_exporter = environ.get(OTEL_EXPORTER_OTLP_INSECURE)
211260
if insecure is None:
212-
insecure = environ.get(OTEL_EXPORTER_OTLP_INSECURE)
213-
if insecure is not None:
214-
insecure = insecure.lower() == "true"
261+
if insecure_exporter is not None:
262+
insecure = insecure_exporter.lower() == "true"
215263
else:
216-
if parsed_url.scheme == "http":
217-
insecure = True
218-
else:
219-
insecure = False
264+
insecure = parsed_url.scheme == "http"
220265

221266
if parsed_url.netloc:
222267
self._endpoint = parsed_url.netloc
@@ -257,25 +302,27 @@ def __init__(
257302
self._channel = secure_channel(
258303
self._endpoint, credentials, compression=compression
259304
)
260-
self._client = self._stub(self._channel)
305+
self._client = self._stub(self._channel) # pyright: ignore [reportCallIssue]
261306

262307
self._export_lock = threading.Lock()
263308
self._shutdown = False
264309

265310
@abstractmethod
266311
def _translate_data(
267-
self, data: TypingSequence[SDKDataT]
312+
self,
313+
data: SDKDataT,
268314
) -> ExportServiceRequestT:
269315
pass
270316

271317
def _export(
272-
self, data: Union[TypingSequence[ReadableSpan], MetricsData]
318+
self,
319+
data: SDKDataT,
273320
) -> ExportResultT:
274321
# After the call to shutdown, subsequent calls to Export are
275322
# not allowed and should return a Failure result.
276323
if self._shutdown:
277324
logger.warning("Exporter already shutdown, ignoring batch")
278-
return self._result.FAILURE
325+
return self._result.FAILURE # pyright: ignore [reportReturnType]
279326

280327
# FIXME remove this check if the export type for traces
281328
# gets updated to a class that represents the proto
@@ -292,7 +339,7 @@ def _export(
292339
# value will remain constant.
293340
for delay in _create_exp_backoff_generator(max_value=max_value):
294341
if delay == max_value or self._shutdown:
295-
return self._result.FAILURE
342+
return self._result.FAILURE # pyright: ignore [reportReturnType]
296343

297344
with self._export_lock:
298345
try:
@@ -302,10 +349,11 @@ def _export(
302349
timeout=self._timeout,
303350
)
304351

305-
return self._result.SUCCESS
352+
return self._result.SUCCESS # pyright: ignore [reportReturnType]
306353

307354
except RpcError as error:
308-
if error.code() in [
355+
code = error.code() # pyright: ignore [reportAttributeAccessIssue]
356+
if code in [
309357
StatusCode.CANCELLED,
310358
StatusCode.DEADLINE_EXCEEDED,
311359
StatusCode.RESOURCE_EXHAUSTED,
@@ -314,7 +362,7 @@ def _export(
314362
StatusCode.UNAVAILABLE,
315363
StatusCode.DATA_LOSS,
316364
]:
317-
retry_info_bin = dict(error.trailing_metadata()).get(
365+
retry_info_bin = dict(error.trailing_metadata()).get( # pyright: ignore [reportAttributeAccessIssue]
318366
"google.rpc.retryinfo-bin"
319367
)
320368
if retry_info_bin is not None:
@@ -330,7 +378,7 @@ def _export(
330378
"Transient error %s encountered while exporting "
331379
"%s to %s, retrying in %ss."
332380
),
333-
error.code(),
381+
code,
334382
self._exporting,
335383
self._endpoint,
336384
delay,
@@ -342,16 +390,16 @@ def _export(
342390
"Failed to export %s to %s, error code: %s",
343391
self._exporting,
344392
self._endpoint,
345-
error.code(),
346-
exc_info=error.code() == StatusCode.UNKNOWN,
393+
code,
394+
exc_info=code == StatusCode.UNKNOWN,
347395
)
348396

349-
if error.code() == StatusCode.OK:
350-
return self._result.SUCCESS
397+
if code == StatusCode.OK:
398+
return self._result.SUCCESS # pyright: ignore [reportReturnType]
351399

352-
return self._result.FAILURE
400+
return self._result.FAILURE # pyright: ignore [reportReturnType]
353401

354-
return self._result.FAILURE
402+
return self._result.FAILURE # pyright: ignore [reportReturnType]
355403

356404
def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
357405
if self._shutdown:

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@
7777

7878
class OTLPMetricExporter(
7979
MetricExporter,
80-
OTLPExporterMixin[Metric, ExportMetricsServiceRequest, MetricExportResult],
80+
OTLPExporterMixin[
81+
MetricsData,
82+
ExportMetricsServiceRequest,
83+
MetricExportResult,
84+
MetricsServiceStub,
85+
],
8186
OTLPMetricExporterMixin,
8287
):
8388
"""OTLP metric exporter
@@ -89,9 +94,6 @@ class OTLPMetricExporter(
8994
If it is set and the number of data points exceeds the max, the request will be split.
9095
"""
9196

92-
_result = MetricExportResult
93-
_stub = MetricsServiceStub
94-
9597
def __init__(
9698
self,
9799
endpoint: str | None = None,
@@ -106,10 +108,9 @@ def __init__(
106108
preferred_aggregation: dict[type, Aggregation] | None = None,
107109
max_export_batch_size: int | None = None,
108110
):
109-
if insecure is None:
110-
insecure = environ.get(OTEL_EXPORTER_OTLP_METRICS_INSECURE)
111-
if insecure is not None:
112-
insecure = insecure.lower() == "true"
111+
insecure_metrics = environ.get(OTEL_EXPORTER_OTLP_METRICS_INSECURE)
112+
if insecure is None and insecure_metrics is not None:
113+
insecure = insecure_metrics.lower() == "true"
113114

114115
if (
115116
not insecure
@@ -139,6 +140,8 @@ def __init__(
139140

140141
OTLPExporterMixin.__init__(
141142
self,
143+
stub=MetricsServiceStub,
144+
result=MetricExportResult,
142145
endpoint=endpoint
143146
or environ.get(OTEL_EXPORTER_OTLP_METRICS_ENDPOINT),
144147
insecure=insecure,
@@ -150,7 +153,7 @@ def __init__(
150153

151154
self._max_export_batch_size: int | None = max_export_batch_size
152155

153-
def _translate_data(
156+
def _translate_data( # pyright: ignore [reportIncompatibleMethodOverride]
154157
self, data: MetricsData
155158
) -> ExportMetricsServiceRequest:
156159
return encode_metrics(data)
@@ -179,6 +182,7 @@ def _split_metrics_data(
179182
self,
180183
metrics_data: MetricsData,
181184
) -> Iterable[MetricsData]:
185+
assert self._max_export_batch_size is not None
182186
batch_size: int = 0
183187
split_resource_metrics: List[ResourceMetrics] = []
184188

0 commit comments

Comments
 (0)