-
Notifications
You must be signed in to change notification settings - Fork 1
/
cornelius.py
88 lines (59 loc) · 2.18 KB
/
cornelius.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import base64
import os
import json
import logging
import requests
import flask
import raven
from raven.contrib.flask import Sentry
app = flask.Flask(__name__)
app.config['SENTRY_DSN'] = os.environ.get('SENTRY_DSN', '')
client = raven.Client(os.environ.get('LOVE_DSN', ''))
sentry = Sentry(app)
def track(event, properties):
if not event:
return
token = os.environ.get('MIXPANEL_TOKEN', None)
dev_token = os.environ.get('MIXPANEL_DEV_TOKEN', None)
if token is None or dev_token is None:
return
if properties.get('version', '0.0.0') == '0.0.0':
properties['token'] = dev_token
else:
properties['token'] = token
headers = flask.request.headers
if 'X-Forwarded-For' in headers:
ip = headers['X-Forwarded-For'].split(",").pop().strip()
if 'distinct_id' not in properties:
properties['distinct_id'] = ip
properties['ip'] = ip
params = {
'event': event,
'properties': properties,
}
data = base64.b64encode(json.dumps(params))
url = "http://api.mixpanel.com/track/?data=" + data
return requests.get(url)
@app.route("/metrics", methods=["POST"])
def metrics():
payload = flask.request.json
if payload is None or not isinstance(payload, dict):
return flask.jsonify(message="Could not decode JSON object"), 400
if 'metrics' not in payload:
return flask.jsonify(message="Key 'metrics' is required in JSON payload"), 400
for metric in payload['metrics']:
track(metric.get('event'), metric.get('properties', {}))
return flask.jsonify(success=True), 201
@app.route("/errors", methods=["POST"])
def errors():
payload = flask.request.json
if payload is None or not isinstance(payload, dict):
return flask.jsonify(message="Could not decode JSON object"), 400
if 'message' not in payload:
return flask.jsonify(message="Key 'message' is required in JSON payload"), 400
message = payload['message']
tags = payload.get('tags', {})
request_id = client.captureMessage(message, tags=tags)
return flask.jsonify(success=True, request_id=request_id), 201
if __name__ == "__main__":
app.run(debug=True)