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

2116 improve resilience of posthog integration #2148

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ RUN curl -sSL https://install.python-poetry.org \
POETRY_VERSION=${POETRY_VERSION} \
POETRY_HOME=${POETRY_HOME} \
python3 -q
# Add project
COPY . /app
WORKDIR /app
RUN python3 -m venv $VIRTUAL_ENV
RUN poetry install

# Add project
COPY . /app

USER root
RUN apt-get clean && apt-get purge

Expand Down
16 changes: 12 additions & 4 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@
"description": "The name of the review app",
"required": false
},
"HUBSPOT_HOME_PAGE_FORM_GUID": {
"description": "Hubspot ID for the home page contact form",
"required": false
},
"HOST_IP": {
"description": "This server's host IP",
"required": false
},
"HUBSPOT_HOME_PAGE_FORM_GUID": {
"description": "Hubspot ID for the home page contact form",
"required": false
},
"HUBSPOT_MAX_CONCURRENT_TASKS": {
"description": "Max number of concurrent Hubspot tasks to run",
"required": false
Expand Down Expand Up @@ -539,6 +539,14 @@
"description": "API token to communicate with PostHog",
"required": false
},
"POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS": {
"description": "Timeout(MS) for PostHog feature flag requests.",
"required": false
},
"POSTHOG_MAX_RETRIES": {
"description": "Numbers of time requests to PostHog should be retried after failing.",
"required": false
},
"RECAPTCHA_SECRET_KEY": {
"description": "The ReCaptcha secret key",
"required": false
Expand Down
1 change: 1 addition & 0 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.utils.functional import cached_property
from django.utils.text import slugify
from mitol.common.utils.datetime import now_in_utc
from mitol.posthog import features
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel, InlinePanel, PageChooserPanel
from wagtail.blocks import PageChooserBlock, StreamBlock
Expand Down
27 changes: 16 additions & 11 deletions cms/models_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for Wagtail models"""

import json
from datetime import timedelta
from urllib.parse import quote_plus
Expand Down Expand Up @@ -37,9 +38,9 @@
ProgramFactory,
program_with_empty_requirements,
)
from courses.models import Course, CourseRun, limit_to_certificate_pages
from courses.models import Course, CourseRun
from ecommerce.constants import DISCOUNT_TYPE_FIXED_PRICE
from ecommerce.factories import DiscountFactory, ProductFactory
from ecommerce.factories import ProductFactory
from flexiblepricing.api import determine_courseware_flexible_price_discount
from flexiblepricing.constants import FlexiblePriceStatus
from flexiblepricing.factories import FlexiblePriceFactory, FlexiblePriceTierFactory
Expand Down Expand Up @@ -157,19 +158,23 @@ def test_course_page_context(
"run": run,
"course_runs": relevant_runs,
"is_enrolled": exp_is_enrolled,
"sign_in_url": f"/signin/?next={quote_plus(course_page.get_url())}"
if exp_sign_in_url
else None,
"sign_in_url": (
f"/signin/?next={quote_plus(course_page.get_url())}"
if exp_sign_in_url
else None
),
"start_date": getattr(run, "start_date", None),
"can_access_edx_course": is_authenticated and has_relevant_run,
"finaid_price": finaid_price,
"product": product,
"instructors": []
if not has_instructor
else [
member.linked_instructor_page
for member in course_page.linked_instructors.order_by("order").all()
],
"instructors": (
[]
if not has_instructor
else [
member.linked_instructor_page
for member in course_page.linked_instructors.order_by("order").all()
]
),
"new_design": features.is_enabled(
"mitxonline-new-product-page",
False,
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"popper.js": "1.16.1",
"postcss": "8.4.31",
"postcss-loader": "6.2.1",
"posthog-js": "^1.75.4",
"posthog-js": "^1.117.0",
"prettier-eslint": "9.0.2",
"prettier-eslint-cli": "5.0.1",
"prop-types": "15.8.1",
Expand Down
3 changes: 2 additions & 1 deletion frontend/public/src/flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ declare type Settings = {
digital_credentials: boolean,
digital_credentials_supported_runs: Array<string>,
posthog_api_token: ?string,
posthog_api_host: ?string
posthog_api_host: ?string,
posthog_feature_flag_request_timeout_ms: ?number,
}
declare var SETTINGS: Settings

Expand Down
16 changes: 9 additions & 7 deletions frontend/public/src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ if (SETTINGS.posthog_api_host && SETTINGS.posthog_api_token) {
posthog.debug()
}
posthog.init(SETTINGS.posthog_api_token, {
api_host: SETTINGS.posthog_api_host,
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
cross_subdomain_cookie: false,
persistence: "localStorage+cookie",
loaded: function(posthog) {
api_host: SETTINGS.posthog_api_host,
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
cross_subdomain_cookie: false,
persistence: "localStorage+cookie",
feature_flag_request_timeout_ms:
SETTINGS.posthog_feature_flag_request_timeout_ms,
loaded: function(posthog) {
posthog.setPersonPropertiesForFlags({
environment: environment
})
Expand Down
5 changes: 2 additions & 3 deletions main/features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""MITxOnline feature flags"""

import os
import uuid
from functools import wraps

from django.conf import settings
Expand All @@ -10,7 +10,6 @@
ENABLE_NEW_DESIGN = "mitxonline-new-product-page"
ENABLE_NEW_HOME_PAGE_FEATURED = "mitxonline-new-featured-carousel"
ENABLE_NEW_HOME_PAGE_HERO = "mitxonline-new-featured-hero"
ENABLE_NEW_HEADER = "mitxonline-new-header"
ENABLE_NEW_HOME_PAGE_VIDEO = "mitxonline-new-home-page-video-component"
ENABLE_NEW_HOME_PAGE_CONTACT_FORM = "mitxonline-new-home-page-contact-form"
ENABLE_NEW_FOOTER = "mitxonline-new-footer"
Expand All @@ -32,7 +31,7 @@ def is_enabled(name, default=None, unique_id=settings.HOSTNAME):
bool: True if the feature flag is enabled
"""

if "IN_TEST_SUITE" not in os.environ:
if os.getenv("POSTHOG_ENABLED", "False").lower() in ("true", "1", "t"):
import posthog
else:
posthog = None
Expand Down
25 changes: 23 additions & 2 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"mitol.hubspot_api",
"mitol.mail.apps.MailApp",
"mitol.authentication.apps.TransitionalAuthenticationApp",
"mitol.payment_gateway.apps.PaymentGatewayApp"
"mitol.payment_gateway.apps.PaymentGatewayApp",
# "mitol.oauth_toolkit_extensions.apps.OAuthToolkitExtensionsApp",
)
# Only include the seed data app if this isn't running in prod
Expand Down Expand Up @@ -880,6 +880,10 @@
"LOCATION": CELERY_BROKER_URL,
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
},
"durable": {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "durable_cache",
},
}

AUTHENTICATION_BACKENDS = (
Expand Down Expand Up @@ -1148,9 +1152,26 @@
default="",
description="API host for PostHog",
)
if "IN_TEST_SUITE" not in os.environ:

POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS = get_int(
name="POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS",
default=3000,
description="Timeout(MS) for PostHog feature flag requests.",
)

POSTHOG_MAX_RETRIES = get_int(
name="POSTHOG_MAX_RETRIES",
default=3,
description="Numbers of time requests to PostHog should be retried after failing.",
)

if os.getenv("POSTHOG_ENABLED", "False").lower() in ("true", "1", "t"):
posthog.api_key = POSTHOG_API_TOKEN
posthog.host = POSTHOG_API_HOST
posthog.feature_flags_request_timeout_seconds = (
POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS / 1000
)
posthog.max_retries = POSTHOG_MAX_RETRIES

# HomePage Hubspot Form Settings
HUBSPOT_HOME_PAGE_FORM_GUID = get_string(
Expand Down
2 changes: 2 additions & 0 deletions main/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""mitx_online utilities"""

import json
from datetime import datetime
from decimal import Decimal
Expand Down Expand Up @@ -51,6 +52,7 @@ def get_js_settings(request: HttpRequest):
"features": {},
"posthog_api_token": settings.POSTHOG_API_TOKEN,
"posthog_api_host": settings.POSTHOG_API_HOST,
"posthog_feature_flag_request_timeout_ms": settings.POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS,
}


Expand Down
2 changes: 2 additions & 0 deletions main/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utils tests"""

from datetime import datetime

import pytest
Expand Down Expand Up @@ -50,6 +51,7 @@ def test_get_js_settings(settings, rf):
"features": {},
"posthog_api_token": settings.POSTHOG_API_TOKEN,
"posthog_api_host": settings.POSTHOG_API_HOST,
"posthog_feature_flag_request_timeout_ms": settings.POSTHOG_FEATURE_FLAG_REQUEST_TIMEOUT_MS,
}


Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ ulid-py = "^1.1.0"
uwsgi = "^2.0.19"
wagtail = "^5.0"
hypothesis = "4.23.9"
posthog = "^3.0.1"
uwsgitop = "^0.11"
deepdiff = "^6.6.1"
wagtail-metadata = "^5.0.0"
mitol-django-posthog = {path = "mitol-django-posthog-2024.04.16.tar.gz"}


[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ env =
SENTRY_DSN=
RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=
IN_TEST_SUITE=True
POSTHOG_ENABLED=False
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15246,7 +15246,7 @@ __metadata:
popper.js: 1.16.1
postcss: 8.4.31
postcss-loader: 6.2.1
posthog-js: ^1.75.4
posthog-js: ^1.117.0
prettier-eslint: 9.0.2
prettier-eslint-cli: 5.0.1
prop-types: 15.8.1
Expand Down Expand Up @@ -17521,13 +17521,13 @@ __metadata:
languageName: node
linkType: hard

"posthog-js@npm:^1.75.4":
version: 1.104.4
resolution: "posthog-js@npm:1.104.4"
"posthog-js@npm:^1.117.0":
version: 1.117.0
resolution: "posthog-js@npm:1.117.0"
dependencies:
fflate: ^0.4.8
preact: ^10.19.3
checksum: d8b9cc7ea69bfef124060c3d11777b761dabcc636a003f1fcd8c52a9da7eb7c2116dff6fbc52bcf80bf09130dad64e9e320b7ee14ecd01f70b268691b3d88ca1
checksum: 4d6c6d91561cbe53772f138fbf155cf3ef641f29569124927e10c6db4e1461358723efb2a65b4dc9148877f38bd3d2d220740eeb18cc4fba6bdee7dd6fe4bdee
languageName: node
linkType: hard

Expand Down
Loading