add level 3 and 4
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
rule_files:
|
||||
- /workspace/infra/prometheus/alerts.yaml
|
||||
evaluation_interval: 5s
|
||||
tests:
|
||||
- interval: 5s
|
||||
input_series:
|
||||
- series: 'messenger_http_requests_total{method="POST",route="/api/v1/chats/{chat_id}/messages",status_class="5xx"}'
|
||||
values: '0+50x30'
|
||||
- series: 'messenger_http_requests_total{method="POST",route="/api/v1/chats/{chat_id}/messages",status_class="2xx"}'
|
||||
values: '0+450x30'
|
||||
- series: 'messenger_queue_age_seconds{service_name="messenger-outbox"}'
|
||||
values: '60x30'
|
||||
alert_rule_test:
|
||||
- eval_time: 2m
|
||||
alertname: MessengerHighErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels: {severity: warning}
|
||||
exp_annotations:
|
||||
summary: 'API 5xx ratio exceeds 5%'
|
||||
runbook: 'Inspect API latency, traces and dependency spans; check DB readiness.'
|
||||
- eval_time: 2m
|
||||
alertname: MessengerQueueLag
|
||||
exp_alerts:
|
||||
- exp_labels: {severity: warning}
|
||||
exp_annotations:
|
||||
summary: 'Oldest queued job exceeds 30 seconds'
|
||||
runbook: 'Check worker health, busy/capacity, broker and consumer traces.'
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Submit synthetic attachments without waiting for worker; no credentials printed."""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
|
||||
client = Client()
|
||||
actor, _, _, chat = client.bootstrap()
|
||||
for _ in range(3):
|
||||
attachment, _ = client.attachment(actor, chat)
|
||||
print('accepted attachment:', attachment['id'])
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Offline checks for trace propagation and telemetry redaction."""
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from opentelemetry import trace, metrics
|
||||
from opentelemetry.sdk.metrics import MeterProvider
|
||||
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
|
||||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
|
||||
from src import telemetry as tel
|
||||
from src import broker
|
||||
|
||||
class TelemetryTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.exporter = InMemorySpanExporter()
|
||||
provider = TracerProvider()
|
||||
provider.add_span_processor(SimpleSpanProcessor(cls.exporter))
|
||||
trace.set_tracer_provider(provider)
|
||||
cls.reader = InMemoryMetricReader()
|
||||
metrics.set_meter_provider(MeterProvider(metric_readers=[cls.reader]))
|
||||
|
||||
def test_upload_outbox_publish_consume_context(self):
|
||||
self.exporter.clear()
|
||||
recorded = {}
|
||||
def publish(payload, headers):
|
||||
recorded.update(headers)
|
||||
with tel.tracer.start_as_current_span('upload') as upload:
|
||||
saved = tel.carrier()
|
||||
self.assertIn('traceparent', saved)
|
||||
with patch.object(broker, '_publish', side_effect=publish):
|
||||
asyncio.run(broker.publish({'type': 'attachment.process.v1', 'attempt': 2}, json.loads(json.dumps(saved))))
|
||||
with tel.tracer.start_as_current_span('consume', context=tel.extract(recorded), kind=tel.SpanKind.CONSUMER):
|
||||
pass
|
||||
spans = {s.name: s for s in self.exporter.get_finished_spans()}
|
||||
self.assertEqual(spans['attachment.publish'].parent.span_id, spans['upload'].context.span_id)
|
||||
self.assertEqual(spans['attachment.publish'].attributes['messaging.retry.attempt'], 2)
|
||||
self.assertEqual(spans['consume'].parent.span_id, spans['attachment.publish'].context.span_id)
|
||||
self.assertEqual(spans['consume'].context.trace_id, spans['upload'].context.trace_id)
|
||||
self.assertNotIn('type', recorded)
|
||||
|
||||
def test_log_formatter_only_explicit_fields(self):
|
||||
rec = logging.LogRecord('test', logging.INFO, __file__, 1, 'http.request', (), None)
|
||||
rec.authorization = 'secret'; rec.body = 'private message'; rec.route = '/api/v1/chats/{chat_id}'
|
||||
output = tel.JsonFormatter().format(rec)
|
||||
self.assertNotIn('secret', output)
|
||||
self.assertNotIn('private message', output)
|
||||
self.assertEqual(json.loads(output)['route'], '/api/v1/chats/{chat_id}')
|
||||
|
||||
def test_metrics_keep_route_template(self):
|
||||
attrs = {'method': 'POST', 'route': '/api/v1/chats/{chat_id}/attachments', 'status_class': '2xx'}
|
||||
tel.http_requests.add(1, attrs)
|
||||
tel.http_duration.record(0.125, attrs)
|
||||
result = self.reader.get_metrics_data()
|
||||
data = [item for resource in result.resource_metrics for scope in resource.scope_metrics for item in scope.metrics]
|
||||
requests = next(item for item in data if item.name == 'messenger.http.requests')
|
||||
point = next(point for point in requests.data.data_points if point.attributes == attrs)
|
||||
self.assertEqual(point.value, 1)
|
||||
self.assertEqual(set(point.attributes), {'method', 'route', 'status_class'})
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Synthetic upload with a known W3C trace ID; wait through a worker restart."""
|
||||
import secrets
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
|
||||
client = Client()
|
||||
actor, _, _, chat = client.bootstrap()
|
||||
trace_id = secrets.token_hex(16)
|
||||
parent_id = secrets.token_hex(8)
|
||||
content = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes()
|
||||
attachment = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png', 202,
|
||||
actor=actor, raw=content, headers={'Content-Type':'image/png',
|
||||
'traceparent':f'00-{trace_id}-{parent_id}-01'}, schema='Attachment')['json']
|
||||
print('trace_id:', trace_id, 'attachment_id:', attachment['id'], flush=True)
|
||||
started = time.monotonic()
|
||||
result = client.wait_ready(actor, attachment, wait_seconds=150)
|
||||
print('status:', result['status'], 'elapsed_seconds:', round(time.monotonic()-started, 3), flush=True)
|
||||
Reference in New Issue
Block a user