Skip to content

Commit 0139735

Browse files
committed
feat: FilestoreNodeStorage, all credits to #76250
1 parent 92e31b8 commit 0139735

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .backend import FilestoreNodeStorage # NOQA
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import datetime
2+
import logging
3+
from io import BytesIO
4+
5+
from django.conf import settings
6+
7+
from sentry import options as options_store
8+
from sentry.models.file import get_storage
9+
from sentry.nodestore.base import NodeStorage
10+
11+
logger = logging.getLogger("sentry.nodestore")
12+
13+
class FilestoreNodeStorage(NodeStorage):
14+
"""
15+
A backend that persist nodes to configured File Store.
16+
Intended for "s3" or "gcs", which performnace may not be ideal.
17+
config.yml Configuration reference:
18+
>>> filestore.backend: "s3"
19+
... filestore.options:
20+
... access_key: "xxx"
21+
... secret_key: "xxx"
22+
... endpoint_url: "https://s3.us-east-1.amazonaws.com"
23+
... bucket_name: "sentry"
24+
... location: "/sentry"
25+
"""
26+
27+
def __init__(self, prefix_path=None):
28+
self.prefix_path: str = "nodestore/"
29+
backend = options_store.get("filestore.backend")
30+
if backend not in ["s3", "gcs"]:
31+
logger.warning("FilestoreNodeStorage was intended for s3 or gcs, currently using: %s", backend)
32+
if prefix_path:
33+
self.prefix_path = prefix_path
34+
35+
def _get_bytes(self, id: str):
36+
storage = get_storage()
37+
path = self.node_path(id)
38+
return storage.open(path).read()
39+
40+
def _set_bytes(self, id: str, data: bytes, ttl=0):
41+
storage = get_storage()
42+
path = self.node_path(id)
43+
storage.save(path, BytesIO(data))
44+
45+
def delete(self, id):
46+
storage = get_storage()
47+
path = self.node_path(id)
48+
storage.delete(path)
49+
50+
def cleanup(self, cutoff: datetime.datetime):
51+
"""
52+
This driver does not have managed TTLs. To enable TTLs you will need to enable it on your
53+
bucket.
54+
"""
55+
raise NotImplementedError
56+
57+
def bootstrap(self):
58+
# Nothing for this backend to do during bootstrap
59+
pass
60+
61+
def node_path(self, id: str):
62+
return f"{self.prefix_path}{id}.json"

0 commit comments

Comments
 (0)