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

Make the media /upload tracing less ambiguous #15888

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15888.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tracing to media `/upload` code paths.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12 changes: 7 additions & 5 deletions synapse/media/media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from synapse.api.errors import NotFoundError
from synapse.logging.context import defer_to_thread, make_deferred_yieldable
from synapse.logging.opentracing import trace
from synapse.logging.opentracing import trace, trace_with_opname
from synapse.util import Clock
from synapse.util.file_consumer import BackgroundFileConsumer

Expand Down Expand Up @@ -77,7 +77,7 @@ def __init__(
self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
self.clock = hs.get_clock()

@trace
@trace_with_opname("MediaStorage.store_file")
async def store_file(self, source: IO, file_info: FileInfo) -> str:
"""Write `source` to the on disk media store, and also any other
configured storage providers
Expand All @@ -91,18 +91,19 @@ async def store_file(self, source: IO, file_info: FileInfo) -> str:
"""

with self.store_into_file(file_info) as (f, fname, finish_cb):
# Write to the main repository
# Write to the main media repository
await self.write_to_file(source, f)
# Write to the other storage providers
await finish_cb()

return fname

@trace
@trace_with_opname("MediaStorage.write_to_file")
async def write_to_file(self, source: IO, output: IO) -> None:
"""Asynchronously write the `source` to `output`."""
await defer_to_thread(self.reactor, _write_file_synchronously, source, output)

@trace
@trace_with_opname("MediaStorage.store_into_file")
@contextlib.contextmanager
def store_into_file(
self, file_info: FileInfo
Expand Down Expand Up @@ -142,6 +143,7 @@ def store_into_file(
try:
with open(fname, "wb") as f:

@trace_with_opname("MediaStorage.store_into_file.finish")
async def finish() -> None:
# Ensure that all writes have been flushed and close the
# file.
Expand Down
25 changes: 13 additions & 12 deletions synapse/media/storage_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from synapse.config._base import Config
from synapse.logging.context import defer_to_thread, run_in_background
from synapse.logging.opentracing import trace
from synapse.logging.opentracing import start_active_span, trace_with_opname
from synapse.util.async_helpers import maybe_awaitable

from ._base import FileInfo, Responder
Expand Down Expand Up @@ -87,7 +87,7 @@ def __init__(
def __str__(self) -> str:
return "StorageProviderWrapper[%s]" % (self.backend,)

@trace
@trace_with_opname("StorageProviderWrapper.store_file")
async def store_file(self, path: str, file_info: FileInfo) -> None:
if not file_info.server_name and not self.store_local:
return None
Expand Down Expand Up @@ -116,7 +116,7 @@ async def store() -> None:

run_in_background(store)

@trace
@trace_with_opname("StorageProviderWrapper.fetch")
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:
if file_info.url_cache:
# Files in the URL preview cache definitely aren't stored here,
Expand Down Expand Up @@ -144,7 +144,7 @@ def __init__(self, hs: "HomeServer", config: str):
def __str__(self) -> str:
return "FileStorageProviderBackend[%s]" % (self.base_directory,)

@trace
@trace_with_opname("FileStorageProviderBackend.store_file")
async def store_file(self, path: str, file_info: FileInfo) -> None:
"""See StorageProvider.store_file"""

Expand All @@ -156,14 +156,15 @@ async def store_file(self, path: str, file_info: FileInfo) -> None:

# mypy needs help inferring the type of the second parameter, which is generic
shutil_copyfile: Callable[[str, str], str] = shutil.copyfile
await defer_to_thread(
self.hs.get_reactor(),
shutil_copyfile,
primary_fname,
backup_fname,
)

@trace
with start_active_span("shutil_copyfile"):
await defer_to_thread(
self.hs.get_reactor(),
shutil_copyfile,
primary_fname,
backup_fname,
)

@trace_with_opname("FileStorageProviderBackend.fetch")
async def fetch(self, path: str, file_info: FileInfo) -> Optional[Responder]:
"""See StorageProvider.fetch"""

Expand Down