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

improve elastic apm setup #2344

Merged
merged 3 commits into from
May 17, 2022
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/nose-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
python-version: [3.6, 3.8]
python-version: [3.8]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:

strategy:
matrix:
python-version: [3.6, 3.8]
python-version: [3.8]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:

strategy:
matrix:
python-version: [3.6]
python-version: [3.8]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:

strategy:
matrix:
python-version: [3.6, 3.8]
python-version: [3.8]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion content_api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from superdesk.default_settings import env, urlparse

from superdesk.default_settings import ( # noqa
DEBUG,
SECRET_KEY,
MONGO_DBNAME,
MONGO_URI,
Expand All @@ -35,7 +36,8 @@
PAGINATION_LIMIT,
APM_SERVER_URL,
APM_SECRET_TOKEN,
DEBUG,
APM_SERVICE_NAME,
CLIENT_URL,
)

CONTENTAPI_INSTALLED_APPS = [
Expand Down
2 changes: 2 additions & 0 deletions prod_api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
PAGINATION_LIMIT,
APM_SERVER_URL,
APM_SECRET_TOKEN,
APM_SERVICE_NAME,
CLIENT_URL,
)

SECRET_KEY = env("PRODAPI_SECRET_KEY", "")
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exclude-dir = prod_api/tests/

[mypy]
mypy_path = ./stubs
python_version = 3.6
python_version = 3.8
warn_unused_configs = True
allow_untyped_globals = True
exclude = env
Expand Down
8 changes: 8 additions & 0 deletions superdesk/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,11 @@ def local_to_utc_hour(hour):
#:
APM_SERVER_URL = env("APM_SERVER_URL")
APM_SECRET_TOKEN = env("APM_SECRET_TOKEN")

#: Elastic APM Service name
#:
#: APM service name
#:
#: .. versionadded:: 2.4.1
#:
APM_SERVICE_NAME = env("APM_SERVICE_NAME")
23 changes: 17 additions & 6 deletions superdesk/factory/elastic_apm.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import sys
import re
import flask

from typing import Literal
from elasticapm.contrib.flask import ElasticAPM


def setup_apm(app, name="Superdesk"):
in_celery = sys.argv and "worker" in sys.argv
def setup_apm(app: flask.Flask, service="Core API") -> None:
if getattr(app, "apm", None) is None and app.config.get("APM_SERVER_URL") and app.config.get("APM_SECRET_TOKEN"):
app.config["ELASTIC_APM"] = {
"DEBUG": app.debug,
"ENVIRONMENT": get_environment(app),
"SERVER_URL": app.config["APM_SERVER_URL"],
"SECRET_TOKEN": app.config["APM_SECRET_TOKEN"],
"TRANSACTIONS_IGNORE_PATTERNS": ["^OPTIONS "],
"SERVICE_NAME": "{name} {service}".format(
name=name,
service="Celery" if in_celery else "Web",
"SERVICE_NAME": "{app} - {service}".format(
app=app.config.get("APM_SERVICE_NAME") or app.config.get("APPLICATION_NAME"), service=service
),
}

app.apm = ElasticAPM(app)


def get_environment(app: flask.Flask) -> Literal["testing", "staging", "production"]:
if app.config.get("CLIENT_URL"):
if "localhost" in app.config["CLIENT_URL"] or app.debug:
return "testing"
if re.search(r"-(dev|demo|test|staging)", app.config["CLIENT_URL"]):
return "staging"
return "production"