Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Serverless site functions #26671

Merged
merged 75 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
f679a2a
Started adding remote config
benjackwhite Nov 21, 2024
c36bba5
Added generator and tests
benjackwhite Nov 22, 2024
cc61230
Fixes
benjackwhite Nov 22, 2024
bcd6577
Update query snapshots
github-actions[bot] Nov 22, 2024
9ae9f75
Update query snapshots
github-actions[bot] Nov 22, 2024
46d8eba
Fixes
benjackwhite Nov 28, 2024
901c40f
Merge branch 'feat/serverless-decide' of github.com:PostHog/posthog i…
benjackwhite Nov 28, 2024
d8e679f
Merge branch 'master' into feat/serverless-decide
benjackwhite Nov 28, 2024
c3308f8
Fixes
benjackwhite Nov 28, 2024
ce490ad
Fixes
benjackwhite Nov 28, 2024
13ff981
Fix up
benjackwhite Nov 28, 2024
12d6ef5
Added array configs to config page
benjackwhite Nov 28, 2024
ca61624
Update UI snapshots for `chromium` (1)
github-actions[bot] Nov 28, 2024
9131733
fix
benjackwhite Nov 29, 2024
1de9759
Fixes
benjackwhite Nov 29, 2024
b65bf07
fix
benjackwhite Nov 29, 2024
942f9d7
Fix up to use a celery task for updates
benjackwhite Nov 29, 2024
3c65713
Fix
benjackwhite Nov 29, 2024
406e016
Added syncing to S3
benjackwhite Nov 29, 2024
babacf3
Fix tests
benjackwhite Dec 2, 2024
8e64e35
Fixes
benjackwhite Dec 2, 2024
3c552cc
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 2, 2024
f21a315
Fixes
benjackwhite Dec 2, 2024
17fe62b
Fix
benjackwhite Dec 2, 2024
3fecfdc
Fix
benjackwhite Dec 2, 2024
016c85a
Fixes
benjackwhite Dec 2, 2024
48efbda
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 2, 2024
6f7e5cb
Fixes
benjackwhite Dec 2, 2024
90e877f
Fix
benjackwhite Dec 2, 2024
8be131c
Fixes
benjackwhite Dec 2, 2024
10f598f
Remove S3 stuff
benjackwhite Dec 2, 2024
d27534e
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 2, 2024
d5892d7
Fixes
benjackwhite Dec 2, 2024
015cd12
Fix array js loading
benjackwhite Dec 2, 2024
d46bc65
Update query snapshots
github-actions[bot] Dec 2, 2024
ebd444a
Update query snapshots
github-actions[bot] Dec 2, 2024
01c9fad
Update query snapshots
github-actions[bot] Dec 2, 2024
86b84bd
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 3, 2024
d913c84
Fixed up tests
benjackwhite Dec 3, 2024
0f0e0f2
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 3, 2024
30ac3ea
Update query snapshots
github-actions[bot] Dec 3, 2024
bf32a66
fix: Match config to old format (#26600)
benjackwhite Dec 4, 2024
27c69cb
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 4, 2024
749f55d
fix
benjackwhite Dec 4, 2024
a5344c3
Fix
benjackwhite Dec 4, 2024
aafc1cc
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 4, 2024
1ccaf2b
Update query snapshots
github-actions[bot] Dec 4, 2024
6beebaf
Update query snapshots
github-actions[bot] Dec 4, 2024
fa4b039
Fix
benjackwhite Dec 4, 2024
771648e
Merge branch 'feat/serverless-decide' of github.com:PostHog/posthog i…
benjackwhite Dec 4, 2024
3ae8b46
Added array gzipping
benjackwhite Dec 5, 2024
f0dee04
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 5, 2024
f10c6ce
Fixed up formatting
benjackwhite Dec 5, 2024
89b1205
Fixes
benjackwhite Dec 5, 2024
50a1189
Merge branch 'master' into feat/site-functions-serverless
benjackwhite Dec 5, 2024
076e641
Fixes
benjackwhite Dec 5, 2024
df082af
Revert site apps loading
benjackwhite Dec 5, 2024
d4860f9
Fixes
benjackwhite Dec 5, 2024
72e170f
Fix tests
benjackwhite Dec 5, 2024
12e5e46
Fixes
benjackwhite Dec 5, 2024
f27a493
Fixes
benjackwhite Dec 5, 2024
b782c25
Fixes
benjackwhite Dec 5, 2024
b59c449
Fixes
benjackwhite Dec 5, 2024
73606bf
Update query snapshots
github-actions[bot] Dec 5, 2024
4656c1a
Merge branch 'master' into feat/site-functions-serverless
benjackwhite Dec 6, 2024
8c92a51
Fixes
benjackwhite Dec 6, 2024
82ef525
Fix
benjackwhite Dec 6, 2024
4617db2
Fix comments
benjackwhite Dec 6, 2024
485f069
Update query snapshots
github-actions[bot] Dec 6, 2024
79bf932
Fixes
benjackwhite Dec 6, 2024
7a3dff5
Fixes
benjackwhite Dec 6, 2024
8172fec
Merge branch 'feat/site-functions-serverless' of github.com:PostHog/p…
benjackwhite Dec 6, 2024
26c2bcf
Fixes
benjackwhite Dec 6, 2024
81d3f3a
fix
benjackwhite Dec 6, 2024
fdcf059
Update query snapshots
github-actions[bot] Dec 6, 2024
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
4 changes: 1 addition & 3 deletions posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from posthog.models.feature_flag.flag_analytics import increment_request_count
from posthog.models.filters.mixins.utils import process_bool
from posthog.models.utils import execute_with_timeout
from posthog.plugins.site import get_decide_site_apps, get_decide_site_functions
from posthog.plugins.site import get_decide_site_apps
from posthog.utils import (
get_ip_address,
label_for_team_id_to_track,
Expand Down Expand Up @@ -297,8 +297,6 @@ def get_decide(request: HttpRequest):
try:
with execute_with_timeout(200, DATABASE_FOR_FLAG_MATCHING):
site_apps = get_decide_site_apps(team, using_database=DATABASE_FOR_FLAG_MATCHING)
with execute_with_timeout(200, DATABASE_FOR_FLAG_MATCHING):
site_apps += get_decide_site_functions(team, using_database=DATABASE_FOR_FLAG_MATCHING)
except Exception:
pass

Expand Down
19 changes: 9 additions & 10 deletions posthog/api/hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,18 @@ def validate(self, attrs):

if "hog" in attrs:
if attrs["type"] in TYPES_WITH_JAVASCRIPT_SOURCE:
# Upon creation, this code will be run before the model has an "id".
# If that's the case, the code just makes sure transpilation doesn't throw. We'll re-transpile after creation.
id = str(instance.id) if instance else "__"
try:
# Validate transpilation using the model instance
attrs["transpiled"] = get_transpiled_function(
id, attrs["hog"], attrs["filters"], attrs["inputs"], team
HogFunction(
team=team,
hog=attrs["hog"],
filters=attrs["filters"],
inputs=attrs["inputs"],
)
)
except TranspilerError:
raise serializers.ValidationError({"hog": f"Error in TypeScript code"})
raise serializers.ValidationError({"hog": "Error in TypeScript code"})
attrs["bytecode"] = None
else:
attrs["bytecode"] = compile_hog(attrs["hog"])
Expand Down Expand Up @@ -231,11 +234,7 @@ def create(self, validated_data: dict, *args, **kwargs) -> HogFunction:
request = self.context["request"]
validated_data["created_by"] = request.user
hog_function = super().create(validated_data=validated_data)
if validated_data.get("type") in TYPES_WITH_JAVASCRIPT_SOURCE:
# Re-run the transpilation now that we have an ID
hog_function.transpiled = get_transpiled_function(
str(hog_function.id), hog_function.hog, hog_function.filters, hog_function.inputs, hog_function.team
)

return hog_function

def update(self, instance: HogFunction, validated_data: dict, *args, **kwargs) -> HogFunction:
Expand Down
32 changes: 0 additions & 32 deletions posthog/api/site_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from posthog.exceptions import generate_exception_response
from posthog.logging.timing import timed
from posthog.plugins.site import get_transpiled_site_source, get_site_config_from_schema
from posthog.models.hog_functions.hog_function import HogFunction


@csrf_exempt
Expand Down Expand Up @@ -36,34 +35,3 @@ def get_site_app(request: HttpRequest, id: int, token: str, hash: str) -> HttpRe
type="server_error",
status_code=status.HTTP_404_NOT_FOUND,
)


@csrf_exempt
@timed("posthog_cloud_site_app_endpoint")
def get_site_function(request: HttpRequest, id: str, hash: str) -> HttpResponse:
try:
# TODO: Should we add a token as well? Is the UUID enough?
function = (
HogFunction.objects.filter(
id=id, enabled=True, type__in=("site_destination", "site_app"), transpiled__isnull=False
)
.values_list("transpiled")
.first()
)
if not function:
raise Exception("No function found")

response = HttpResponse(content=function[0], content_type="application/javascript")
response["Cache-Control"] = "public, max-age=31536000" # Cache for 1 year
statsd.incr(f"posthog_cloud_raw_endpoint_success", tags={"endpoint": "site_function"})
return response
except Exception as e:
capture_exception(e, {"data": {"id": id}})
statsd.incr("posthog_cloud_raw_endpoint_failure", tags={"endpoint": "site_function"})
return generate_exception_response(
"site_function",
"Unable to serve site function source code.",
code="missing_site_function_source",
type="server_error",
status_code=status.HTTP_404_NOT_FOUND,
)
201 changes: 179 additions & 22 deletions posthog/api/test/__snapshots__/test_decide.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,97 @@
'''
# ---
# name: TestDecide.test_web_app_queries.10
'''
SELECT "posthog_remoteconfig"."id",
"posthog_remoteconfig"."team_id",
"posthog_remoteconfig"."config",
"posthog_remoteconfig"."updated_at",
"posthog_remoteconfig"."synced_at"
FROM "posthog_remoteconfig"
WHERE "posthog_remoteconfig"."team_id" = 99999
LIMIT 21
'''
# ---
# name: TestDecide.test_web_app_queries.11
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
"posthog_team"."organization_id",
"posthog_team"."project_id",
"posthog_team"."api_token",
"posthog_team"."app_urls",
"posthog_team"."name",
"posthog_team"."slack_incoming_webhook",
"posthog_team"."created_at",
"posthog_team"."updated_at",
"posthog_team"."anonymize_ips",
"posthog_team"."completed_snippet_onboarding",
"posthog_team"."has_completed_onboarding_for",
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."person_processing_opt_out",
"posthog_team"."session_recording_opt_in",
"posthog_team"."session_recording_sample_rate",
"posthog_team"."session_recording_minimum_duration_milliseconds",
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_recording_url_trigger_config",
"posthog_team"."session_recording_url_blocklist_config",
"posthog_team"."session_recording_event_trigger_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."capture_dead_clicks",
"posthog_team"."surveys_opt_in",
"posthog_team"."heatmaps_opt_in",
"posthog_team"."session_recording_version",
"posthog_team"."signup_token",
"posthog_team"."is_demo",
"posthog_team"."access_control",
"posthog_team"."week_start_day",
"posthog_team"."inject_web_apps",
"posthog_team"."test_account_filters",
"posthog_team"."test_account_filters_default_checked",
"posthog_team"."path_cleaning_filters",
"posthog_team"."timezone",
"posthog_team"."data_attributes",
"posthog_team"."person_display_name_properties",
"posthog_team"."live_events_columns",
"posthog_team"."recording_domains",
"posthog_team"."primary_dashboard_id",
"posthog_team"."extra_settings",
"posthog_team"."modifiers",
"posthog_team"."correlation_config",
"posthog_team"."session_recording_retention_period_days",
"posthog_team"."plugins_opt_in",
"posthog_team"."opt_out_capture",
"posthog_team"."event_names",
"posthog_team"."event_names_with_usage",
"posthog_team"."event_properties",
"posthog_team"."event_properties_with_usage",
"posthog_team"."event_properties_numerical",
"posthog_team"."external_data_workspace_id",
"posthog_team"."external_data_workspace_last_synced_at"
FROM "posthog_team"
WHERE "posthog_team"."id" = 99999
LIMIT 21
'''
# ---
# name: TestDecide.test_web_app_queries.12
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_featureflag"
WHERE ("posthog_featureflag"."active"
AND NOT "posthog_featureflag"."deleted"
AND "posthog_featureflag"."team_id" = 99999)
'''
# ---
# name: TestDecide.test_web_app_queries.13
'''
SELECT "posthog_pluginconfig"."id",
"posthog_pluginconfig"."web_token",
Expand All @@ -2260,17 +2351,36 @@
AND "posthog_pluginconfig"."team_id" = 99999)
'''
# ---
# name: TestDecide.test_web_app_queries.11
# name: TestDecide.test_web_app_queries.14
'''
SELECT "posthog_hogfunction"."id",
"posthog_hogfunction"."updated_at",
"posthog_hogfunction"."type"
FROM "posthog_hogfunction"
WHERE ("posthog_hogfunction"."enabled"
AND "posthog_hogfunction"."team_id" = 99999
AND "posthog_hogfunction"."transpiled" IS NOT NULL
AND "posthog_hogfunction"."type" IN ('site_destination',
'site_app'))
SELECT "posthog_pluginconfig"."id",
"posthog_pluginconfig"."web_token",
"posthog_pluginsourcefile"."updated_at",
"posthog_plugin"."updated_at",
"posthog_pluginconfig"."updated_at"
FROM "posthog_pluginconfig"
INNER JOIN "posthog_plugin" ON ("posthog_pluginconfig"."plugin_id" = "posthog_plugin"."id")
INNER JOIN "posthog_pluginsourcefile" ON ("posthog_plugin"."id" = "posthog_pluginsourcefile"."plugin_id")
WHERE ("posthog_pluginconfig"."enabled"
AND "posthog_pluginsourcefile"."filename" = 'site.ts'
AND "posthog_pluginsourcefile"."status" = 'TRANSPILED'
AND "posthog_pluginconfig"."team_id" = 99999)
'''
# ---
# name: TestDecide.test_web_app_queries.15
'''
SELECT "posthog_pluginconfig"."id",
"posthog_pluginconfig"."web_token",
"posthog_pluginsourcefile"."updated_at",
"posthog_plugin"."updated_at",
"posthog_pluginconfig"."updated_at"
FROM "posthog_pluginconfig"
INNER JOIN "posthog_plugin" ON ("posthog_pluginconfig"."plugin_id" = "posthog_plugin"."id")
INNER JOIN "posthog_pluginsourcefile" ON ("posthog_plugin"."id" = "posthog_pluginsourcefile"."plugin_id")
WHERE ("posthog_pluginconfig"."enabled"
AND "posthog_pluginsourcefile"."filename" = 'site.ts'
AND "posthog_pluginsourcefile"."status" = 'TRANSPILED'
AND "posthog_pluginconfig"."team_id" = 99999)
'''
# ---
# name: TestDecide.test_web_app_queries.2
Expand Down Expand Up @@ -2552,17 +2662,64 @@
# ---
# name: TestDecide.test_web_app_queries.9
'''
SELECT "posthog_pluginconfig"."id",
"posthog_pluginconfig"."web_token",
"posthog_pluginsourcefile"."updated_at",
"posthog_plugin"."updated_at",
"posthog_pluginconfig"."updated_at"
FROM "posthog_pluginconfig"
INNER JOIN "posthog_plugin" ON ("posthog_pluginconfig"."plugin_id" = "posthog_plugin"."id")
INNER JOIN "posthog_pluginsourcefile" ON ("posthog_plugin"."id" = "posthog_pluginsourcefile"."plugin_id")
WHERE ("posthog_pluginconfig"."enabled"
AND "posthog_pluginsourcefile"."filename" = 'site.ts'
AND "posthog_pluginsourcefile"."status" = 'TRANSPILED'
AND "posthog_pluginconfig"."team_id" = 99999)
SELECT "posthog_team"."id",
"posthog_team"."uuid",
"posthog_team"."organization_id",
"posthog_team"."project_id",
"posthog_team"."api_token",
"posthog_team"."app_urls",
"posthog_team"."name",
"posthog_team"."slack_incoming_webhook",
"posthog_team"."created_at",
"posthog_team"."updated_at",
"posthog_team"."anonymize_ips",
"posthog_team"."completed_snippet_onboarding",
"posthog_team"."has_completed_onboarding_for",
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."person_processing_opt_out",
"posthog_team"."session_recording_opt_in",
"posthog_team"."session_recording_sample_rate",
"posthog_team"."session_recording_minimum_duration_milliseconds",
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_recording_url_trigger_config",
"posthog_team"."session_recording_url_blocklist_config",
"posthog_team"."session_recording_event_trigger_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."capture_dead_clicks",
"posthog_team"."surveys_opt_in",
"posthog_team"."heatmaps_opt_in",
"posthog_team"."session_recording_version",
"posthog_team"."signup_token",
"posthog_team"."is_demo",
"posthog_team"."access_control",
"posthog_team"."week_start_day",
"posthog_team"."inject_web_apps",
"posthog_team"."test_account_filters",
"posthog_team"."test_account_filters_default_checked",
"posthog_team"."path_cleaning_filters",
"posthog_team"."timezone",
"posthog_team"."data_attributes",
"posthog_team"."person_display_name_properties",
"posthog_team"."live_events_columns",
"posthog_team"."recording_domains",
"posthog_team"."primary_dashboard_id",
"posthog_team"."extra_settings",
"posthog_team"."modifiers",
"posthog_team"."correlation_config",
"posthog_team"."session_recording_retention_period_days",
"posthog_team"."external_data_workspace_id",
"posthog_team"."external_data_workspace_last_synced_at"
FROM "posthog_team"
WHERE "posthog_team"."id" = 99999
LIMIT 21
'''
# ---
Loading
Loading