Skip to content

Commit 49e1b09

Browse files
Add rum (#258)
* add rum config and compose file * Update README.md to document compose * Updated some stuff, added env vars back * Update README.md * Update README.md * make git hash be version in rum * Update README.md * Fixing linting * more fixing lint
1 parent 73f9866 commit 49e1b09

7 files changed

Lines changed: 99 additions & 15 deletions

File tree

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PROFILES_SERVER_NAME=localhost:8080
2+
3+
DEBUG=true
4+
5+
PROFILES_OIDC_CLIENT_ID=profiles-dev
6+
PROFILES_OIDC_CLIENT_SECRET=
7+
PROFILES_OIDC_LOGOUT_REDIRECT_URI=http://localhost:8080/logout
8+
9+
LDAP_BIND_DN=uid=yourusername,cn=users,cn=accounts,dc=csh,dc=rit,dc=edu
10+
LDAP_BIND_PASS=
11+
12+
DATADOG_ENV=local

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,32 @@ to
6161
SERVER_NAME = os.environ.get('PROFILES_SERVER_NAME', 'localhost:8080')
6262
```
6363

64-
Reach out to an RTP to get OIDC credentials that will allow you to develop locally behind OIDC auth.
64+
#### OIDC
65+
66+
Reach out to an RTP to get OIDC credentials that will allow you to develop locally behind OIDC auth. You will need `PROFILES_OIDC_CLIENT_ID` and `PROFILES_OIDC_CLIENT_SECRET`
67+
68+
#### LDAP
6569

6670
```LDAP_BIND_DN``` is your CSH DN. It is in the following format:
6771

6872
```uid={CSH User Name},cn=users,cn=accounts,dc=csh,dc=rit,dc=edu```
6973

74+
```LDAP_BIND_PASS``` is your CSH password.
75+
76+
#### DATADOG
7077

71-
```LDAP_BIND_PASS``` is your CSH password.
78+
```DATADOG_ENV``` is the env where you are developing, should stay local for local development
79+
80+
```DATADOG_APP_VERSION``` is the version of your app, when unset defaults to git commit hash
7281

7382
If you did everything right, you should be able to run ```python app.py``` and develop locally.
7483

84+
Running with Docker
85+
--------------------
86+
87+
Alternatively, you can run profiles using docker or podman compose. You can configure the environment in same way as above, using a `config.py` file, or you can copy the `.env.example` file to `.env` and configure it there.
88+
89+
The docker-compose file is also set up for automatic redeploying with watch if you run with `podman compose up --watch --build`
7590

7691
Code Standards
7792
------------

config.env.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import random
33
import string
44
from os import environ as env
5+
import subprocess
56

67
# Sentry DSN
78
SENTRY_DSN = env.get("PROFILES_SENTRY_DSN", "")
89

910
# Flask config
10-
DEBUG = False
11+
DEBUG = os.environ.get("DEBUG", "false").lower() == "true"
1112
IP = os.environ.get('PROFILES_IP', 'localhost')
1213
PORT = os.environ.get('PROFILES_PORT', 8080)
1314
SERVER_NAME = os.environ.get('PROFILES_SERVER_NAME', 'profiles.csh.rit.edu')
@@ -25,3 +26,10 @@
2526

2627
LDAP_BIND_DN = env.get("LDAP_BIND_DN", default="cn=profiles,ou=Apps,dc=csh,dc=rit,dc=edu")
2728
LDAP_BIND_PASS = env.get("LDAP_BIND_PW", default=None)
29+
30+
GIT_HASH = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip()
31+
32+
DATADOG_RUM_CONFIG = {
33+
'DATADOG_ENV': os.environ.get('DATADOG_ENV', 'local'),
34+
'DATADOG_APP_VERSION': os.environ.get('DATADOG_APP_VERSION', GIT_HASH),
35+
}

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "3"
2+
services:
3+
profiles:
4+
build: .
5+
container_name: profiles
6+
ports:
7+
- "127.0.0.1:8080:8080"
8+
env_file:
9+
- path: .env
10+
required: false
11+
develop:
12+
watch:
13+
- action: sync+restart
14+
path: ./profiles
15+
target: /opt/profiles/profiles
16+
ignore:
17+
- __pycache__

profiles/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,15 @@ def home(info=None):
9191
@before_request
9292
def user(uid=None, info=None):
9393
try:
94-
return render_template("profile.html", info=info, member_info=get_member_info(uid))
94+
return render_template(
95+
"profile.html", info=info,
96+
member_info=get_member_info(uid), **app.config['DATADOG_RUM_CONFIG']
97+
)
9598
except BadQueryError as bqe:
9699
# ldap_get_member() returns a BadQueryError if getting the user's information fails.
97100
# Flask already treats a stray BadQueryError as a 404, but actually handling it prevents the traceback
98101
# from getting dumped into the log.
99-
return render_template("404.html", message=bqe), 404
102+
return render_template("404.html", message=bqe, *app.config['DATADOG_RUM_CONFIG']), 404
100103

101104

102105
@app.route("/results", methods=["POST"])
@@ -117,7 +120,8 @@ def search(searched=None, info=None):
117120
if len(members) == 1:
118121
return redirect("/user/" + members[0].uid, 302)
119122
return render_template(
120-
"listing.html", info=info, title="Search Results: " + searched, members=members
123+
"listing.html", info=info, title="Search Results: " + searched,
124+
members=members, *app.config['DATADOG_RUM_CONFIG']
121125
)
122126

123127

@@ -129,14 +133,16 @@ def group(_group=None, info=None):
129133

130134
if _group == "eboard":
131135
return render_template(
132-
"listing.html", info=info, title=group_desc, members=ldap_get_eboard()
136+
"listing.html", info=info, title=group_desc,
137+
members=ldap_get_eboard(), *app.config['DATADOG_RUM_CONFIG']
133138
)
134139

135140
return render_template(
136141
"listing.html",
137142
info=info,
138143
title=group_desc,
139144
members=_ldap_get_group_members(_group),
145+
*app.config['DATADOG_RUM_CONFIG']
140146
)
141147

142148

@@ -145,7 +151,8 @@ def group(_group=None, info=None):
145151
@before_request
146152
def year(_year=None, info=None):
147153
return render_template(
148-
"listing.html", info=info, title="Year: " + _year, members=ldap_get_year(_year)
154+
"listing.html", info=info, title="Year: " + _year,
155+
members=ldap_get_year(_year), *app.config['DATADOG_RUM_CONFIG']
149156
)
150157

151158

@@ -182,7 +189,7 @@ def image(uid):
182189
try:
183190
return get_image(uid)
184191
except BadQueryError as bqe:
185-
return render_template("404.html", message=bqe), 404
192+
return render_template("404.html", message=bqe, *app.config['DATADOG_RUM_CONFIG']), 404
186193

187194

188195
@app.route("/clearcache")
@@ -215,9 +222,9 @@ def clear_cache(info=None):
215222
@app.errorhandler(500)
216223
def handle_internal_error(e):
217224
if isinstance(e, NotFound):
218-
return render_template("404.html", message=str(e)), 404
225+
return render_template("404.html", message=str(e), *app.config['DATADOG_RUM_CONFIG']), 404
219226
if isinstance(e.original_exception, BadQueryError):
220-
return render_template("404.html", message=e.original_exception), 404
227+
return render_template("404.html", message=e.original_exception, *app.config['DATADOG_RUM_CONFIG']), 404
221228
raise e.original_exception
222229

223230

profiles/templates/include/head.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,31 @@
2828

2929
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js"></script>
3030

31+
<script>
32+
(function(h,o,u,n,d) {
33+
h = h[d] = h[d] || { q: [], onReady: function (c) { h.q.push(c) } }
34+
d = o.createElement(u); d.async = 1; d.src = n; d.crossOrigin=''
35+
n = o.getElementsByTagName(u)[0]; n.parentNode.insertBefore(d, n)
36+
})(window, document, 'script', 'https://www.datadoghq-browser-agent.com/us1/v6/datadog-rum.js', 'DD_RUM')
37+
38+
window.DD_RUM.onReady(function () {
39+
window.DD_RUM.init({
40+
applicationId: '65be0cac-59ab-4ad7-8c23-5a3e577893fe',
41+
clientToken: 'pub2f0696fe063adeb0dc204547f4584f55',
42+
site: 'datadoghq.com',
43+
service: 'profiles',
44+
env: '{{ DATADOG_ENV }}',
45+
version: '{{ DATADOG_APP_VERSION }}',
46+
sessionSampleRate: 100, // capture 100% of sessions
47+
sessionReplaySampleRate: 100, // capture 100% of sessions with replay
48+
trackResources: true, // Enable Resource tracking
49+
trackUserInteractions: true, // Enable Action tracking
50+
trackLongTasks: true, // Enable Long Tasks tracking
51+
52+
allowedTracingUrls: [ 'http://localhost:8080', 'https://profiles.csh.rit.edu', 'https://profiles-dev.csh.rit.edu'],
53+
defaultPrivacyLevel: 'mask-user-input',
54+
});
55+
});
56+
</script>
57+
3158
</head>

profiles/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Credit to Liam Middlebrook and Ram Zallan
22
# https://github.com/liam-middlebrook/gallery
3-
import subprocess
43
import base64
54
import datetime
65

@@ -12,6 +11,7 @@
1211
import ldap
1312

1413
from profiles import _ldap
14+
import profiles
1515
from profiles.ldap import (ldap_get_calendar,
1616
ldap_get_member,
1717
ldap_get_pronouns,
@@ -25,13 +25,11 @@
2525
def before_request(func):
2626
@wraps(func)
2727
def wrapped_function(*args, **kwargs):
28-
git_revision = subprocess.check_output(
29-
['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip()
3028
uuid = str(session["userinfo"].get("sub", ""))
3129
uid = str(session["userinfo"].get("preferred_username", ""))
3230
user_obj = _ldap.get_member(uid, uid=True)
3331
info = {
34-
"git_revision": git_revision,
32+
"git_revision": profiles.app.config['GIT_HASH'],
3533
"uuid": uuid,
3634
"uid": uid,
3735
"user_obj": user_obj,

0 commit comments

Comments
 (0)