Skip to content

Commit b90cbc3

Browse files
committed
linting + fix prometheus server test
1 parent fd52b26 commit b90cbc3

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

tests/test_opentelemetry_distro_configurator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def _start_http_server(*a: Any, **kw: Any) -> None:
111111
catched_values.append(kw)
112112

113113
setattr(prometheus_client, "start_http_server", _start_http_server)
114+
setattr(meter_provider, "_start_prometheus_server_without_instrumented_service", True)
114115

115116
meter_provider.get_meter("tomodachi")
116117
assert getattr(meter_provider, "_prometheus_server_started", None) is True
@@ -123,3 +124,4 @@ def _start_http_server(*a: Any, **kw: Any) -> None:
123124
for k, v in environ.items():
124125
os.environ[k] = v
125126
setattr(prometheus_client, "start_http_server", start_http_server)
127+
setattr(meter_provider, "_start_prometheus_server_without_instrumented_service", False)

tomodachi/opentelemetry/distro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
from opentelemetry.instrumentation.distro import BaseDistro # type: ignore
1818
from opentelemetry.instrumentation.environment_variables import OTEL_PYTHON_CONFIGURATOR
1919
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore
20-
from opentelemetry.metrics import Instrument, set_meter_provider
20+
from opentelemetry.metrics import Instrument
2121
from opentelemetry.metrics import _internal as metrics_internal
22+
from opentelemetry.metrics import set_meter_provider
2223
from opentelemetry.sdk._configuration import (
2324
_BaseConfigurator,
2425
_get_exporter_names,

tomodachi/opentelemetry/prometheus.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ def _receive_metrics(
346346
class TomodachiPrometheusMeterProvider(MeterProvider):
347347
def __init__(self, prefix: Optional[str] = None, registry: CollectorRegistry = REGISTRY_) -> None:
348348
self._prometheus_server_started = False
349+
self._start_prometheus_server_without_instrumented_service = False
349350
self._prometheus_registry = registry
350351
self._non_letters_digits_underscore_re = compile(r"[^\w]", UNICODE | IGNORECASE)
351352

@@ -443,7 +444,7 @@ def _start_prometheus_http_server(self) -> None:
443444
try:
444445
prometheus_client.start_http_server(port=port, addr=addr, registry=self._prometheus_registry)
445446
except OSError as e:
446-
error_message = re.sub(".*: ", "", e.strerror)
447+
error_message = re.sub(".*: ", "", e.strerror) if e.strerror else str(e)
447448
logging.get_logger("tomodachi.opentelemetry").warning(
448449
"unable to bind prometheus http server [http] to http://{}:{}/ in otel metric provider".format(
449450
"localhost" if addr in ("0.0.0.0", "127.0.0.1") else addr, port
@@ -478,7 +479,7 @@ def get_meter(
478479
) -> Meter:
479480
from .instrumentation import TomodachiInstrumentor
480481

481-
if TomodachiInstrumentor._instrumented_services:
482+
if TomodachiInstrumentor._instrumented_services or self._start_prometheus_server_without_instrumented_service:
482483
self._start_prometheus_http_server()
483484

484485
return super().get_meter(name, version=version, schema_url=schema_url)

tomodachi/transport/amqp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ async def connect(cls, obj: Any, context: Dict) -> Any:
497497
logger.warning("Unable to connect [amqp] to {}:{} ({})".format(host, port, error_message))
498498
raise AmqpConnectionException(str(e), log_level=context.get("log_level")) from e
499499
except OSError as e:
500-
error_message = e.strerror
500+
error_message = e.strerror if e.strerror else str(e)
501501
logger.warning("Unable to connect [amqp] to {}:{} ({})".format(host, port, error_message))
502502
raise AmqpConnectionException(str(e), log_level=context.get("log_level")) from e
503503

tomodachi/transport/aws_sns_sqs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,9 @@ async def send_message(
607607
await cls.create_client("sqs", service.context)
608608

609609
async with connector("tomodachi.sqs", service_name="sqs") as client:
610-
botocore_error_type = cast(
611-
Type[botocore.exceptions.ClientError], client.exceptions.QueueDoesNotExist
610+
botocore_error_type = cast( # type: ignore[redundant-cast]
611+
Type[botocore.exceptions.ClientError],
612+
client.exceptions.QueueDoesNotExist,
612613
)
613614
except Exception as e:
614615
logging.getLogger("exception").exception(
@@ -2175,8 +2176,7 @@ async def create_queue(
21752176
if fifo is not queue_fifo:
21762177
queue_types = {False: "Standard", True: "FIFO"}
21772178
error_message = (
2178-
f"AWS SQS queue configured as {queue_types[queue_fifo]}, "
2179-
f"but the handler expected {queue_types[fifo]}."
2179+
f"AWS SQS queue configured as {queue_types[queue_fifo]}, but the handler expected {queue_types[fifo]}."
21802180
)
21812181
logger.warning("Queue [sqs] type mismatch ({})".format(error_message))
21822182
raise AWSSNSSQSException(error_message, log_level=context.get("log_level"))

tomodachi/transport/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ async def middleware(request: web.Request, handler: Callable) -> Union[web.Respo
14401440
server = await server_task
14411441
except OSError as e:
14421442
context["_http_accept_new_requests"] = False
1443-
error_message = re.sub(".*: ", "", e.strerror)
1443+
error_message = re.sub(".*: ", "", e.strerror) if e.strerror else str(e)
14441444
logger.warning(
14451445
"unable to bind service [http] to http://{}:{}/".format(
14461446
"localhost" if host in ("0.0.0.0", "127.0.0.1") else host, port

0 commit comments

Comments
 (0)