-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Redis Example App | ||
|
||
This example shows how to set up the Python tracker with a Redis database and a Redis worker to forward events to a Snowplow pipeline. | ||
|
||
#### Installation | ||
- Install the Python tracker from the root folder of the project. | ||
|
||
`python setup.py install` | ||
|
||
- Install redis for your machine. More information can be found [here](https://redis.io/docs/getting-started/installation/) | ||
|
||
`brew install redis` | ||
|
||
- Run `redis-server` to check your redis installation, to stop the server enter `ctrl+c`. | ||
|
||
#### Usage | ||
Navigate to the example folder. | ||
|
||
`cd examples/redis_example` | ||
|
||
This example has two programmes, `redis_app.py` tracks events and sends them to a redis database, `redis_worker.py` then forwards these events onto a Snowplow pipeline. | ||
|
||
To send events to your pipeline, run `redis-server`, followed by the `redis_worker.py {{your_collector_endpoint}}` and finally `redis_app.py`. You should see 3 events in your pipleine. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from snowplow_tracker import Tracker | ||
from snowplow_tracker.typing import PayloadDict | ||
import json | ||
import redis | ||
import logging | ||
|
||
# logging | ||
logging.basicConfig() | ||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class RedisEmitter(object): | ||
""" | ||
Sends Snowplow events to a Redis database | ||
""" | ||
|
||
def __init__(self, rdb=None, key: str = "redis_key") -> None: | ||
""" | ||
:param rdb: Optional custom Redis database | ||
:type rdb: redis | None | ||
:param key: The Redis key for the list of events | ||
:type key: string | ||
""" | ||
|
||
if rdb is None: | ||
rdb = redis.StrictRedis() | ||
|
||
self.rdb = rdb | ||
self.key = key | ||
|
||
def input(self, payload: PayloadDict) -> None: | ||
""" | ||
:param payload: The event properties | ||
:type payload: dict(string:*) | ||
""" | ||
logger.info("Pushing event to Redis queue...") | ||
self.rdb.rpush(self.key, json.dumps(payload)) | ||
logger.info("Finished sending event to Redis.") | ||
|
||
def flush(self) -> None: | ||
logger.warning("The RedisEmitter class does not need to be flushed") | ||
return | ||
|
||
def sync_flush(self) -> None: | ||
self.flush() | ||
|
||
|
||
def main(): | ||
emitter = RedisEmitter() | ||
|
||
t = Tracker(emitter) | ||
|
||
t.track_page_view("https://www.snowplow.io", "Homepage") | ||
t.track_page_ping("https://www.snowplow.io", "Homepage") | ||
t.track_link_click("https://www.snowplow.io") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import sys | ||
from snowplow_tracker import Emitter | ||
from typing import Any | ||
from snowplow_tracker.typing import PayloadDict | ||
import json | ||
import redis | ||
import signal | ||
import gevent | ||
from gevent.pool import Pool | ||
|
||
|
||
def get_url_from_args(): | ||
if len(sys.argv) != 2: | ||
raise ValueError("Collector Endpoint is required") | ||
return sys.argv[1] | ||
|
||
|
||
class RedisWorker: | ||
def __init__(self, emitter: Emitter, key) -> None: | ||
self.pool = Pool(5) | ||
self.emitter = emitter | ||
self.rdb = redis.StrictRedis() | ||
self.key = key | ||
|
||
signal.signal(signal.SIGTERM, self.request_shutdown) | ||
signal.signal(signal.SIGINT, self.request_shutdown) | ||
signal.signal(signal.SIGQUIT, self.request_shutdown) | ||
|
||
def send(self, payload: PayloadDict) -> None: | ||
""" | ||
Send an event to an emitter | ||
""" | ||
self.emitter.input(payload) | ||
|
||
def pop_payload(self) -> None: | ||
""" | ||
Get a single event from Redis and send it | ||
If the Redis queue is empty, sleep to avoid making continual requests | ||
""" | ||
payload = self.rdb.lpop(self.key) | ||
if payload: | ||
self.pool.spawn(self.send, json.loads(payload.decode("utf-8"))) | ||
else: | ||
gevent.sleep(5) | ||
|
||
def run(self) -> None: | ||
""" | ||
Run indefinitely | ||
""" | ||
self._shutdown = False | ||
while not self._shutdown: | ||
self.pop_payload() | ||
self.pool.join(timeout=20) | ||
|
||
def request_shutdown(self, *args: Any) -> None: | ||
""" | ||
Halt the worker | ||
""" | ||
self._shutdown = True | ||
|
||
|
||
def main(): | ||
collector_url = get_url_from_args() | ||
|
||
# Configure Emitter | ||
emitter = Emitter(collector_url, batch_size=1) | ||
|
||
# Setup worker | ||
worker = RedisWorker(emitter=emitter, key="redis_key") | ||
worker.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
redis~=4.5 | ||
gevent~=22.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Snowplow API Example App | ||
|
||
This example shows how to set up the Python tracker with the Snowplow API to send events to a Snowplow pipeline. | ||
|
||
#### Installation | ||
- Install the Python tracker from the root folder of the project. | ||
|
||
`python setup.py install` | ||
|
||
#### Usage | ||
Navigate to the example folder. | ||
|
||
`cd examples/snowplow_api_example` | ||
|
||
To send events to your pipeline, run `snowplow_app.py {{your_collector_endpoint}}`. You should see 6 events in your pipleine. | ||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Example App | ||
|
||
This example shows how to set up the Python tracker with the tracker API to send events to a Snowplow pipeline. | ||
|
||
#### Installation | ||
- Install the Python tracker from the root folder of the project. | ||
|
||
`python setup.py install` | ||
|
||
#### Usage | ||
Navigate to the example folder. | ||
|
||
`cd examples/tracker_api_example` | ||
|
||
To send events to your pipeline, run `app.py {{your_collector_endpoint}}`. You should see 5 events in your pipleine. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters