Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Catch BrokenPipeError from metrics server, and log as a warning (#14072)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson authored Oct 7, 2022
1 parent d6ae14e commit 1fa2e58
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/14072.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't create noisy Sentry events when a requester drops connection to the metrics server mid-request.
18 changes: 12 additions & 6 deletions synapse/metrics/_legacy_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
vendoring of the code will emit both the old versions that Synapse dashboards
expect, and the newer "best practice" version of the up-to-date official client.
"""

import logging
import math
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
Expand All @@ -34,6 +34,7 @@
from twisted.web.resource import Resource
from twisted.web.server import Request

logger = logging.getLogger(__name__)
CONTENT_TYPE_LATEST = "text/plain; version=0.0.4; charset=utf-8"


Expand Down Expand Up @@ -219,11 +220,16 @@ def do_GET(self) -> None:
except Exception:
self.send_error(500, "error generating metric output")
raise
self.send_response(200)
self.send_header("Content-Type", CONTENT_TYPE_LATEST)
self.send_header("Content-Length", str(len(output)))
self.end_headers()
self.wfile.write(output)
try:
self.send_response(200)
self.send_header("Content-Type", CONTENT_TYPE_LATEST)
self.send_header("Content-Length", str(len(output)))
self.end_headers()
self.wfile.write(output)
except BrokenPipeError as e:
logger.warning(
"BrokenPipeError when serving metrics (%s). Did Prometheus restart?", e
)

def log_message(self, format: str, *args: Any) -> None:
"""Log nothing."""
Expand Down

0 comments on commit 1fa2e58

Please sign in to comment.