From cc87ce7e90ae4f16a30b0771186106697c537a23 Mon Sep 17 00:00:00 2001 From: kaktuz20000 <149298757+kaktuz20000@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:04:37 +0300 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=B3=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=20=D1=81=20=D0=BA?= =?UTF-8?q?=D1=80=D1=83=D0=BF=D0=BD=D1=8B=D0=BC=D0=B8=20=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B0=D0=BB=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/otel/README.md | 2 ++ src/telemetry.py | 27 ++++++++++++++++++++++++++- src/worker.py | 15 +++++++++++---- student-tests/test_worker.py | 15 +++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/infra/otel/README.md b/infra/otel/README.md index 386510d..58bf90b 100644 --- a/infra/otel/README.md +++ b/infra/otel/README.md @@ -7,3 +7,5 @@ The Compose stack pins Collector Contrib 0.163.2, Prometheus 2.55.1, Loki 3.3.2, Loki, Tempo, and Prometheus retain local data in named volumes. Grafana datasources, the dashboard, and Prometheus alert rules are provisioned from this directory. Prometheus evaluates the 7-day rolling API availability SLI and local alerts; use the alert annotations and `REPORT.md` runbooks to investigate. API, publisher, and worker share a W3C trace context through the database outbox and the broker payload. Application logs contain only structured metadata, trace/span IDs, request IDs, route templates, and sanitized event names. Message bodies, credentials, filenames, and signed URLs are not exported. + +HTTP request duration, worker job duration, and attachment time-to-ready histograms use explicit second-based buckets. HTTP buckets are dense from 1 ms through 100 ms to resolve normal API latency; job buckets extend to 60 s and end-to-end readiness buckets to 600 s. This keeps Prometheus percentile interpolation useful across all three time scales. diff --git a/src/telemetry.py b/src/telemetry.py index 9fb6572..9d1a244 100644 --- a/src/telemetry.py +++ b/src/telemetry.py @@ -18,6 +18,7 @@ from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler from opentelemetry.sdk._logs.export import BatchLogRecordProcessor from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader +from opentelemetry.sdk.metrics.view import ExplicitBucketHistogramAggregation, View from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor @@ -36,6 +37,21 @@ _runtime = { "cpu": 0.0, "memory": 0, } +HISTOGRAM_BUCKETS = { + "messenger.http.duration": ( + 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.0075, 0.008, + 0.0085, 0.009, 0.0095, 0.01, 0.0125, 0.015, 0.02, 0.025, + 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, + ), + "messenger.jobs.duration": ( + 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, + 0.5, 1, 2.5, 5, 10, 30, 60, + ), + "messenger.attachments.time_to_ready": ( + 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, + 5, 10, 30, 60, 120, 300, 600, + ), +} _provider = None _meter = None _service_name = "messenger" @@ -126,7 +142,16 @@ def initialize(service_name: str) -> None: export_interval_millis=10000, export_timeout_millis=5000, ) - metrics.set_meter_provider(MeterProvider(resource=resource, metric_readers=[reader])) + views = [ + View( + instrument_name=name, + aggregation=ExplicitBucketHistogramAggregation(boundaries=buckets), + ) + for name, buckets in HISTOGRAM_BUCKETS.items() + ] + metrics.set_meter_provider( + MeterProvider(resource=resource, metric_readers=[reader], views=views) + ) logger_provider = LoggerProvider(resource=resource) logger_provider.add_log_record_processor( BatchLogRecordProcessor( diff --git a/src/worker.py b/src/worker.py index 0828d6c..8a0b2b3 100644 --- a/src/worker.py +++ b/src/worker.py @@ -83,6 +83,15 @@ def _encode(image: Image.Image, image_format: str, **options) -> bytes: return output.getvalue() +def _resize_within(image: Image.Image, bounds: tuple[int, int]) -> Image.Image: + width, height = image.size + scale = min(1.0, bounds[0] / width, bounds[1] / height) + size = (max(1, int(width * scale)), max(1, int(height * scale))) + if size == image.size: + return image.copy() + return image.resize(size, Image.Resampling.LANCZOS) + + def build_variants(attachment_id: str, content: bytes, pipeline_version: int = 1): quality = PIPELINE_QUALITY.get(pipeline_version) if quality is None: @@ -90,9 +99,8 @@ def build_variants(attachment_id: str, content: bytes, pipeline_version: int = 1 image = _rgb_image(content) try: variants = [] - thumbnail = image.copy() + thumbnail = _resize_within(image, (256, 256)) try: - thumbnail.thumbnail((256, 256), Image.Resampling.LANCZOS) thumbnail_bytes = _encode(thumbnail, "JPEG", quality=quality["jpeg"], optimize=True) variants.append(("thumbnail", "image/jpeg", thumbnail.width, thumbnail.height, thumbnail_bytes)) finally: @@ -105,9 +113,8 @@ def build_variants(attachment_id: str, content: bytes, pipeline_version: int = 1 finally: crop.close() - optimized = image.copy() + optimized = _resize_within(image, (1280, 1280)) try: - optimized.thumbnail((1280, 1280), Image.Resampling.LANCZOS) optimized_bytes = _encode(optimized, "WEBP", quality=quality["webp"], method=6) variants.append(("optimized", "image/webp", optimized.width, optimized.height, optimized_bytes)) finally: diff --git a/student-tests/test_worker.py b/student-tests/test_worker.py index 8e922fa..6e7b00d 100644 --- a/student-tests/test_worker.py +++ b/student-tests/test_worker.py @@ -38,6 +38,21 @@ def main(): assert by_name["thumbnail"]["sha256"] != { item["name"]: item for item in newer }["thumbnail"]["sha256"] + + odd_ratio = io.BytesIO() + Image.new("RGB", (319, 200), "blue").save(odd_ratio, format="PNG") + odd_variants = { + item["name"]: item for item in build_variants(attachment_id, odd_ratio.getvalue()) + } + assert (odd_variants["thumbnail"]["width"], odd_variants["thumbnail"]["height"]) == (256, 160) + + small_image = io.BytesIO() + Image.new("RGB", (100, 50), "red").save(small_image, format="PNG") + small_variants = { + item["name"]: item for item in build_variants(attachment_id, small_image.getvalue()) + } + assert (small_variants["thumbnail"]["width"], small_variants["thumbnail"]["height"]) == (100, 50) + assert (small_variants["optimized"]["width"], small_variants["optimized"]["height"]) == (100, 50) print("PASS: versioned thumbnail, crop, and optimized variants match dimensions and checksums")