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

feat: add library context identifier #33

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion examples/flask_example/flaskapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def track_revenue(user_id):
@app.route("/flush")
def flush_event():
amp_client.flush()
return f"<p>All events flushed</p>"
return "<p>All events flushed</p>"


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion src/amplitude/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Config:
storage_provider (amplitude.storage.StorageProvider, optional): Default to InMemoryStorageProvider.
Provide storage instance for events buffer.
plan (amplitude.event.Plan, optional): Tracking plan information. Default to None.
library_context (str, optional): Library context information. Default to None.

Properties:
options: A dictionary contains minimum id length information. None if min_id_length not set.
Expand All @@ -55,7 +56,8 @@ def __init__(self, api_key: str = None,
use_batch: bool = False,
server_url: Optional[str] = None,
storage_provider: StorageProvider = InMemoryStorageProvider(),
plan: Plan = None):
plan: Plan = None,
library_context: str = None):
"""The constructor of Config class"""
self.api_key: str = api_key
self._flush_queue_size: int = flush_queue_size
Expand All @@ -71,6 +73,7 @@ def __init__(self, api_key: str = None,
self.storage_provider: StorageProvider = storage_provider
self.opt_out: bool = False
self.plan: Plan = plan
self.library_context: str = library_context
liuyang1520 marked this conversation as resolved.
Show resolved Hide resolved

def get_storage(self) -> Storage:
"""Use configured StorageProvider to create a Storage instance then return.
Expand Down
2 changes: 2 additions & 0 deletions src/amplitude/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def get_plan_body(self):
"session_id": ["session_id", int],
"insert_id": ["insert_id", str],
"library": ["library", str],
"library_context": ["library_context", str],
"plan": ["plan", Plan],
"group_properties": ["group_properties", dict],
"partner_id": ["partner_id", str],
Expand Down Expand Up @@ -239,6 +240,7 @@ def __init__(self, user_id: Optional[str] = None,
self.session_id: Optional[int] = None
self.insert_id: Optional[str] = None
self.library: Optional[str] = None
self.library_context: Optional[str] = None
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved
self.plan: Optional[Plan] = None
self.partner_id: Optional[str] = None
self.version_name: Optional[str] = None
Expand Down
14 changes: 9 additions & 5 deletions src/amplitude/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,17 @@ class ContextPlugin(Plugin):
Also set event default timestamp and insert_id if not set elsewhere.

Methods:
apply_context_data(event): Add SDK name and version to event.library.
apply_context_data(event):
- Add SDK name and version to event.library.
execute(event): Set event default timestamp and insert_id if not set elsewhere.
Add SDK name and version to event.library.
- Add SDK name and version to event.library.
- Add library context information to event.library_context.
"""

def __init__(self):
"""The constructor of ContextPlugin class"""
super().__init__(constants.PluginType.BEFORE)
self.context_string = f"{constants.SDK_LIBRARY}/{constants.SDK_VERSION}"
self.library = f"{constants.SDK_LIBRARY}/{constants.SDK_VERSION}"
self.configuration = None

def setup(self, client):
Expand All @@ -198,10 +200,10 @@ def apply_context_data(self, event: BaseEvent):
Args:
event (BaseEvent): The event to be processed.
"""
event.library = self.context_string
event.library = self.library

def execute(self, event: BaseEvent) -> BaseEvent:
"""Set event default timestamp and insert_id if not set elsewhere. Add SDK name and version to event.library.
"""Set event default timestamp and insert_id if not set elsewhere. Apply context data to event.

Args:
event (BaseEvent): The event to be processed.
Expand All @@ -212,6 +214,8 @@ def execute(self, event: BaseEvent) -> BaseEvent:
event["insert_id"] = str(uuid.uuid4())
if self.configuration.plan and (not event.plan):
event["plan"] = self.configuration.plan
if self.configuration.library_context and (not event.library_context):
event["library_context"] = self.configuration.library_context
self.apply_context_data(event)
return event

Expand Down
6 changes: 6 additions & 0 deletions src/test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ def test_plugin_initialize_amplitude_client_context_plugin_creation_success(self
client.shutdown()

def test_plugin_context_plugin_execute_event_success(self):
test_library_context = "test_library_context"
context_plugin = ContextPlugin()
context_plugin.setup(Amplitude("test_api_key"))
context_plugin.configuration.plan = Plan(source="test_source")
context_plugin.configuration.library_context = test_library_context
event = BaseEvent("test_event", user_id="test_user")
self.assertIsNone(event.time)
self.assertIsNone(event.insert_id)
self.assertIsNone(event.library)
self.assertIsNone(event.library_context)
self.assertIsNone(event.plan)
context_plugin.execute(event)
self.assertTrue(isinstance(event.time, int))
self.assertTrue(isinstance(event.insert_id, str))
self.assertTrue(isinstance(event.library, str))
self.assertEqual(event.library_context, test_library_context)
self.assertTrue(isinstance(event.plan, Plan))

def test_plugin_event_plugin_process_event_success(self):
Expand All @@ -61,12 +65,14 @@ def test_plugin_destination_plugin_add_remove_plugin_success(self):
self.assertTrue(isinstance(event.time, int))
self.assertTrue(isinstance(event.insert_id, str))
self.assertTrue(isinstance(event.library, str))
self.assertIsNone(event.library_context)
destination_plugin.remove(context_plugin)
event = BaseEvent("test_event", user_id="test_user")
destination_plugin.execute(event)
self.assertIsNone(event.time)
self.assertIsNone(event.insert_id)
self.assertIsNone(event.library)
self.assertIsNone(event.library_context)


if __name__ == '__main__':
Expand Down