Skip to content

Commit fdebe4f

Browse files
authored
fix: enable usage cache by default (#5356)
1 parent 9974494 commit fdebe4f

7 files changed

Lines changed: 30 additions & 5 deletions

File tree

api/app/settings/common.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,14 @@
340340
INFLUXDB_ORG = env.str("INFLUXDB_ORG", default="")
341341

342342
USE_POSTGRES_FOR_ANALYTICS = env.bool("USE_POSTGRES_FOR_ANALYTICS", default=False)
343-
USE_CACHE_FOR_USAGE_DATA = env.bool("USE_CACHE_FOR_USAGE_DATA", default=False)
344-
PG_API_USAGE_CACHE_SECONDS = env.int("PG_API_USAGE_CACHE_SECONDS", default=60)
343+
USE_CACHE_FOR_USAGE_DATA = env.bool("USE_CACHE_FOR_USAGE_DATA", default=True)
344+
345+
API_USAGE_CACHE_SECONDS = env.int("API_USAGE_CACHE_SECONDS", default=0)
346+
347+
if not API_USAGE_CACHE_SECONDS:
348+
# Fallback to the old variable name, which is deprecated
349+
# and will be removed in the future.
350+
API_USAGE_CACHE_SECONDS = env.int("PG_API_USAGE_CACHE_SECONDS", default=60)
345351

346352
FEATURE_EVALUATION_CACHE_SECONDS = env.int(
347353
"FEATURE_EVALUATION_CACHE_SECONDS", default=60

api/app/settings/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
}
1515

1616
AWS_SSE_LOGS_BUCKET_NAME = "test_bucket"
17+
USE_CACHE_FOR_USAGE_DATA = False
1718

1819
RETRY_WEBHOOKS = True
1920

api/app_analytics/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def track_request(
4646
self._cache[key] += 1
4747
if (
4848
timezone.now() - self._last_flushed_at
49-
).seconds > settings.PG_API_USAGE_CACHE_SECONDS:
49+
).seconds > settings.API_USAGE_CACHE_SECONDS:
5050
self._flush()
5151

5252

api/tests/unit/app_analytics/test_unit_app_analytics_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_api_usage_cache(
1212
settings: SettingsWrapper,
1313
) -> None:
1414
# Given
15-
settings.PG_API_USAGE_CACHE_SECONDS = 60
15+
settings.API_USAGE_CACHE_SECONDS = 60
1616

1717
cache = APIUsageCache()
1818
now = timezone.now()
@@ -40,7 +40,7 @@ def test_api_usage_cache(
4040
assert not mocked_track_request_task.called
4141

4242
# Now, let's move the time forward
43-
frozen_time.tick(settings.PG_API_USAGE_CACHE_SECONDS + 1) # type: ignore[arg-type]
43+
frozen_time.tick(settings.API_USAGE_CACHE_SECONDS + 1) # type: ignore[arg-type]
4444

4545
# let's track another request(to trigger flush)
4646
cache.track_request(

docs/docs/deployment/hosting/locally-api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ the below variables will be ignored.
224224
- `ENABLE_API_USAGE_TRACKING`: Enable tracking of all API requests in Postgres / Influx. Default is True. Setting to
225225
False will mean that the Usage tab in the Organisation Settings will not show any data. Useful when using Postgres for
226226
analytics in high traffic environments to limit the size of database.
227+
- `USE_CACHE_FOR_USAGE_DATA`: If enabled, this will use in-process caching to track usage data. Defaults to true.
228+
- `API_USAGE_CACHE_SECONDS`: Controls how frequently the usage cache is flushed. Defaults to 60 seconds
227229
- `PROMETHEUS_ENABLED`: Enables the Prometheus `/metrics` endpoint. Default is False.
228230
- `PROMETHEUS_HISTOGRAM_BUCKETS`: Allows to specify your bucket sizes for Prometheus histograms, e.g., `"0.5,0.6,1.0,Inf"`. Defaults to Python Prometheus client default histogram sizes.
229231

infrastructure/aws/production/ecs-task-definition-web.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@
159159
"name": "FEATURE_EVALUATION_CACHE_SECONDS",
160160
"value": "300"
161161
},
162+
{
163+
"name": "USE_CACHE_FOR_USAGE_DATA",
164+
"value": "True"
165+
},
166+
{
167+
"name": "API_USAGE_CACHE_SECONDS",
168+
"value": "300"
169+
},
162170
{
163171
"name": "EDGE_RELEASE_DATETIME",
164172
"value": "2022-06-07T10:00:00Z"

infrastructure/aws/staging/ecs-task-definition-web.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@
171171
"name": "USE_POSTGRES_FOR_ANALYTICS",
172172
"value": "False"
173173
},
174+
{
175+
"name": "USE_CACHE_FOR_USAGE_DATA",
176+
"value": "True"
177+
},
178+
{
179+
"name": "API_USAGE_CACHE_SECONDS",
180+
"value": "300"
181+
},
174182
{
175183
"name": "INFLUXDB_ORG",
176184
"value": "ben.rometsch@bullet-train.io"

0 commit comments

Comments
 (0)