forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-2333] Add Segment Hook and TrackEventOperator
Add support for Segment with an accompanying hook and an operator for sending track events Closes apache#3335 from jzucker2/add-segment-support
- Loading branch information
Showing
9 changed files
with
302 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
""" | ||
This module contains a Segment Hook | ||
which allows you to connect to your Segment account, | ||
retrieve data from it or write to that file. | ||
NOTE: this hook also relies on the Segment analytics package: | ||
https://github.com/segmentio/analytics-python | ||
""" | ||
import analytics | ||
from airflow.hooks.base_hook import BaseHook | ||
from airflow.exceptions import AirflowException | ||
|
||
from airflow.utils.log.logging_mixin import LoggingMixin | ||
|
||
|
||
class SegmentHook(BaseHook, LoggingMixin): | ||
def __init__( | ||
self, | ||
segment_conn_id='segment_default', | ||
segment_debug_mode=False, | ||
*args, | ||
**kwargs | ||
): | ||
""" | ||
Create new connection to Segment | ||
and allows you to pull data out of Segment or write to it. | ||
You can then use that file with other | ||
Airflow operators to move the data around or interact with segment. | ||
:param segment_conn_id: the name of the connection that has the parameters | ||
we need to connect to Segment. | ||
The connection should be type `json` and include a | ||
write_key security token in the `Extras` field. | ||
:type segment_conn_id: str | ||
:param segment_debug_mode: Determines whether Segment should run in debug mode. | ||
Defaults to False | ||
:type segment_debug_mode: boolean | ||
.. note:: | ||
You must include a JSON structure in the `Extras` field. | ||
We need a user's security token to connect to Segment. | ||
So we define it in the `Extras` field as: | ||
`{"write_key":"YOUR_SECURITY_TOKEN"}` | ||
""" | ||
self.segment_conn_id = segment_conn_id | ||
self.segment_debug_mode = segment_debug_mode | ||
self._args = args | ||
self._kwargs = kwargs | ||
|
||
# get the connection parameters | ||
self.connection = self.get_connection(self.segment_conn_id) | ||
self.extras = self.connection.extra_dejson | ||
self.write_key = self.extras.get('write_key') | ||
if self.write_key is None: | ||
raise AirflowException('No Segment write key provided') | ||
|
||
def get_conn(self): | ||
self.log.info('Setting write key for Segment analytics connection') | ||
analytics.debug = self.segment_debug_mode | ||
if self.segment_debug_mode: | ||
self.log.info('Setting Segment analytics connection to debug mode') | ||
analytics.on_error = self.on_error | ||
analytics.write_key = self.write_key | ||
return analytics | ||
|
||
def on_error(self, error, items): | ||
""" | ||
Handles error callbacks when using Segment with segment_debug_mode set to True | ||
""" | ||
self.log.error('Encountered Segment error: {segment_error} with ' | ||
'items: {with_items}'.format(segment_error=error, | ||
with_items=items)) | ||
raise AirflowException('Segment error: {}'.format(error)) |
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,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from airflow.contrib.hooks.segment_hook import SegmentHook | ||
from airflow.models import BaseOperator | ||
from airflow.utils.decorators import apply_defaults | ||
|
||
|
||
class SegmentTrackEventOperator(BaseOperator): | ||
""" | ||
Send Track Event to Segment for a specified user_id and event | ||
:param user_id: The ID for this user in your database | ||
:type user_id: string | ||
:param event: The name of the event you're tracking | ||
:type event: string | ||
:param properties: A dictionary of properties for the event. | ||
:type properties: dict | ||
:param segment_conn_id: The connection ID to use when connecting to Segment. | ||
:type segment_conn_id: string | ||
:param segment_debug_mode: Determines whether Segment should run in debug mode. | ||
Defaults to False | ||
:type segment_debug_mode: boolean | ||
""" | ||
template_fields = ('user_id', 'event', 'properties') | ||
ui_color = '#ffd700' | ||
|
||
@apply_defaults | ||
def __init__(self, | ||
user_id, | ||
event, | ||
properties=None, | ||
segment_conn_id='segment_default', | ||
segment_debug_mode=False, | ||
*args, | ||
**kwargs): | ||
super(SegmentTrackEventOperator, self).__init__(*args, **kwargs) | ||
self.user_id = user_id | ||
self.event = event | ||
properties = properties or {} | ||
self.properties = properties | ||
self.segment_debug_mode = segment_debug_mode | ||
self.segment_conn_id = segment_conn_id | ||
|
||
def execute(self, context): | ||
hook = SegmentHook(segment_conn_id=self.segment_conn_id, | ||
segment_debug_mode=self.segment_debug_mode) | ||
|
||
self.log.info( | ||
'Sending track event ({0}) for user id: {1} with properties: {2}'. | ||
format(self.event, self.user_id, self.properties)) | ||
|
||
hook.track(self.user_id, self.event, self.properties) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
import mock | ||
import unittest | ||
|
||
from airflow import configuration, AirflowException | ||
|
||
from airflow.contrib.hooks.segment_hook import SegmentHook | ||
|
||
TEST_CONN_ID = 'test_segment' | ||
WRITE_KEY = 'foo' | ||
|
||
|
||
class TestSegmentHook(unittest.TestCase): | ||
|
||
def setUp(self): | ||
super(TestSegmentHook, self).setUp() | ||
configuration.load_test_config() | ||
|
||
self.conn = conn = mock.MagicMock() | ||
conn.write_key = WRITE_KEY | ||
self.expected_write_key = WRITE_KEY | ||
self.conn.extra_dejson = {'write_key': self.expected_write_key} | ||
|
||
class UnitTestSegmentHook(SegmentHook): | ||
|
||
def get_conn(self): | ||
return conn | ||
|
||
def get_connection(self, connection_id): | ||
return conn | ||
|
||
self.test_hook = UnitTestSegmentHook(segment_conn_id=TEST_CONN_ID) | ||
|
||
def test_get_conn(self): | ||
expected_connection = self.test_hook.get_conn() | ||
self.assertEqual(expected_connection, self.conn) | ||
self.assertIsNotNone(expected_connection.write_key) | ||
self.assertEqual(expected_connection.write_key, self.expected_write_key) | ||
|
||
def test_on_error(self): | ||
with self.assertRaises(AirflowException): | ||
self.test_hook.on_error('error', ['items']) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
64 changes: 64 additions & 0 deletions
64
tests/contrib/operators/test_segment_track_event_operator.py
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,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
import mock | ||
import unittest | ||
|
||
from airflow import configuration, AirflowException | ||
|
||
from airflow.contrib.hooks.segment_hook import SegmentHook | ||
|
||
TEST_CONN_ID = 'test_segment' | ||
WRITE_KEY = 'foo' | ||
|
||
|
||
class TestSegmentHook(unittest.TestCase): | ||
|
||
def setUp(self): | ||
super(TestSegmentHook, self).setUp() | ||
configuration.load_test_config() | ||
|
||
self.conn = conn = mock.MagicMock() | ||
conn.write_key = WRITE_KEY | ||
self.expected_write_key = WRITE_KEY | ||
self.conn.extra_dejson = {'write_key': self.expected_write_key} | ||
|
||
class UnitTestSegmentHook(SegmentHook): | ||
|
||
def get_conn(self): | ||
return conn | ||
|
||
def get_connection(self, connection_id): | ||
return conn | ||
|
||
self.test_hook = UnitTestSegmentHook(segment_conn_id=TEST_CONN_ID) | ||
|
||
def test_get_conn(self): | ||
expected_connection = self.test_hook.get_conn() | ||
self.assertEqual(expected_connection, self.conn) | ||
self.assertIsNotNone(expected_connection.write_key) | ||
self.assertEqual(expected_connection.write_key, self.expected_write_key) | ||
|
||
def test_on_error(self): | ||
with self.assertRaises(AirflowException): | ||
self.test_hook.on_error('error', ['items']) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.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