фикс гистограмм с крупными интервалами

This commit is contained in:
kaktuz20000
2026-11-06 09:04:37 +03:00
parent 6196d4ee8e
commit cc87ce7e90
4 changed files with 54 additions and 5 deletions
+2
View File
@@ -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.
+26 -1
View File
@@ -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(
+11 -4
View File
@@ -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:
+15
View File
@@ -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")