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

[DevOps] Elastic APM #580

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM python:3.7.7-slim-buster
ENV PARAMETERS=./defaults/webapp.cfg
ENV ELASTIC_APM_ENABLED false
ENV ELASTIC_APM_SERVICE_NAME chime_local
ENV ELASTIC_APM_SERVER_URL http://apm-server:8200
WORKDIR /app
COPY README.md .
COPY setup.cfg .
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"streamlit",
"gspread",
"oauth2client",
"python-i18n"
"python-i18n",
"elastic-apm"
],
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
23 changes: 23 additions & 0 deletions src/APM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Elastic APM

This application has been instrumented with Elastic APM.

In order to [configure](https://www.elastic.co/guide/en/apm/agent/python/current/configuration.html) this application environment variables should be
used in the configMap of the `app.yaml` file.


Custom instrumentation begins in the main method in `st_app.py` with the call

```
client = Client()
client.begin_transaction('main_page')
```
and ends with `client.end_transaction('main_page')`

The sidebar menu and charts are instrumented with [custom instrumentations](https://www.elastic.co/guide/en/apm/agent/python/current/api.html).


The `display_sidebar` has its internal functions instrumented
with the `with` notation.

The `charts` and `display_header` are instrumented with the `annotation` method.
17 changes: 3 additions & 14 deletions src/penn_chime/model/sir.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ def get_argmin_doubling_time(self, p: Parameters, dts):
intrinsic_growth_rate = get_growth_rate(i_dt)
self.beta = get_beta(intrinsic_growth_rate, self.gamma, self.susceptible, 0.0)
self.beta_t = get_beta(intrinsic_growth_rate, self.gamma, self.susceptible, p.relative_contact_rate)

raw = self.run_projection(p, self.gen_policy(p))

# Skip values the would put the fit past peak
peak_admits_day = raw["admits_hospitalized"].argmax()
if peak_admits_day < 0:
Expand Down Expand Up @@ -228,7 +228,7 @@ def gen_policy(self, p: Parameters) -> Sequence[Tuple[float, int]]:
(self.beta, pre_mitigation_days),
(self.beta_t, post_mitigation_days),
]

def run_projection(self, p: Parameters, policy: Sequence[Tuple[float, int]]):
raw = sim_sir(
self.susceptible,
Expand All @@ -245,20 +245,17 @@ def run_projection(self, p: Parameters, policy: Sequence[Tuple[float, int]]):

return raw


def get_loss(current_hospitalized, predicted) -> float:
"""Squared error: predicted vs. actual current hospitalized."""
return (current_hospitalized - predicted) ** 2.0


def get_argmin_ds(census, current_hospitalized: float) -> float:
# By design, this forbids choosing a day after the peak
# If that's a problem, see #381
peak_day = census.argmax()
losses = (census[:peak_day] - current_hospitalized) ** 2.0
return losses.argmin()


def get_beta(
intrinsic_growth_rate: float,
gamma: float,
Expand All @@ -271,14 +268,12 @@ def get_beta(
* (1.0 - relative_contact_rate)
)


def get_growth_rate(doubling_time: Optional[float]) -> float:
"""Calculates average daily growth rate from doubling time."""
if doubling_time is None or doubling_time == 0.0:
return 0.0
return (2.0 ** (1.0 / doubling_time) - 1.0)


def sir(
s: float, i: float, r: float, beta: float, gamma: float, n: float
) -> Tuple[float, float, float]:
Expand All @@ -289,7 +284,6 @@ def sir(
scale = n / (s_n + i_n + r_n)
return s_n * scale, i_n * scale, r_n * scale


def sim_sir(
s: float, i: float, r: float, gamma: float, i_day: int, policies: Sequence[Tuple[float, int]]
):
Expand Down Expand Up @@ -334,7 +328,6 @@ def sim_sir(
"ever_infected": i_a + r_a
}


def build_sim_sir_w_date_df(
raw_df: pd.DataFrame,
current_date: datetime,
Expand All @@ -350,7 +343,6 @@ def build_sim_sir_w_date_df(
}
})


def build_floor_df(df, keys, prefix):
"""Build floor sim sir w date."""
return pd.DataFrame({
Expand All @@ -362,7 +354,6 @@ def build_floor_df(df, keys, prefix):
}
})


def calculate_dispositions(
raw: Dict,
rates: Dict[str, float],
Expand All @@ -373,7 +364,6 @@ def calculate_dispositions(
raw["ever_" + key] = raw["ever_infected"] * rate * market_share
raw[key] = raw["ever_infected"] * rate * market_share


def calculate_admits(raw: Dict, rates):
"""Build admits dataframe from dispositions."""
for key in rates.keys():
Expand All @@ -384,7 +374,6 @@ def calculate_admits(raw: Dict, rates):
raw["admits_"+key] = admit
raw[key] = admit


def calculate_census(
raw: Dict,
lengths_of_stay: Dict[str, int],
Expand Down
8 changes: 5 additions & 3 deletions src/penn_chime/view/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import pandas as pd
import i18n
import numpy as np
import elasticapm

from ..constants import DATE_FORMAT


@elasticapm.capture_span()
def build_admits_chart(
*, alt, admits_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
) -> Chart:
Expand Down Expand Up @@ -51,7 +52,7 @@ def build_admits_chart(
.interactive()
)


@elasticapm.capture_span()
def build_census_chart(
*, alt, census_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
) -> Chart:
Expand Down Expand Up @@ -95,7 +96,7 @@ def build_census_chart(
.interactive()
)


@elasticapm.capture_span()
def build_sim_sir_w_date_chart(
*, alt, sim_sir_w_date_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
) -> Chart:
Expand Down Expand Up @@ -139,6 +140,7 @@ def build_sim_sir_w_date_chart(
.interactive()
)

@elasticapm.capture_span()
def build_table(
*, df: pd.DataFrame, labels: Dict[str, str], modulo: int = 1
) -> pd.DataFrame:
Expand Down
10 changes: 9 additions & 1 deletion src/penn_chime/view/st_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import streamlit as st # type: ignore
import i18n # type: ignore

from elasticapm import Client

i18n.set('filename_format', '{locale}.{format}')
i18n.set('locale', 'en')
i18n.set('fallback', 'en')
Expand All @@ -28,6 +30,10 @@


def main():
#client = Client(server_url='http://apm-server:8200',service_name='chime_local')
#client = Client(server_url='${ELASTIC_APM_SERVER_URL}',service_name='${ELASTIC_APM_SERVICE_NAME}')
client = Client()
client.begin_transaction('main_page')
# This is somewhat dangerous:
# Hide the main menu with "Rerun", "run on Save", "clear cache", and "record a screencast"
# This should not be hidden in prod, but removed
Expand All @@ -37,8 +43,9 @@ def main():
d = Parameters.create(os.environ, [])
p = display_sidebar(st, d)
m = Sir(p)

display_header(st, m, p)


st.subheader(i18n.t("app-new-admissions-title"))
st.markdown(i18n.t("app-new-admissions-text"))
Expand Down Expand Up @@ -73,3 +80,4 @@ def main():
df=m.sim_sir_w_date_df,
)
display_footer(st)
client.end_transaction('main_page')
Loading