Adapt an existing source
Mage builds data integrations from Singer taps, so if there’s a Singer source you want to use that isn’t supported yet, you can adapt it yourself!
Getting started
Mage sources live in the /mage_integrations/mage_integrations
folder. To add a new source, you’ll need to create a new folder in the /sources
subdirectory.
The folder name should be the name of the tap. For existing sources, that starts with pulling in the GitHub repo for the tap. For example, for the GitHub tap, we would:
The result:
Configuring the source
We now have a folder with our tap and a bunch of miscellaneous files. Our first step is to clear out everything we don’t need. We can delete all files/folders other than tap_your_source
and tests
, but hold onto anything that looks like a config.json
file or a sample config.
Next, we need to create a configuration file and an init file:
We can now populate our config.json
file with the sample from the tap. In the GitHub example, this is config.sample.json
. It looks like this:
These are the parameters we’ll need to configure our tap. After cleanup, our tree looks like:
Now, we need to override some methods in the class to make it work with Mage. This is where things get fun! Copy and paste the following template into your_tap/__init__.py
:
Replace YourSource with the name of your source, e.g. Github
for Github, and tap_your_tap
with the name of your tap, e.g. tap_github
.
Overwrite tap methods
Our goal is to overwrite the discover
and sync
methods to make them work with Mage. Discover is likely in tap_your_tap/discover.py
and sync is likely in tap_your_tap/sync.py
. It requires a bit of nuance, but our job is to look through the existing Singer tap, understand how the sync
and discover
methods were implemented, then execute them in Mage __init__.py
under the corresponding method. This is necessarily different for every tap, since each project is entirely independent.
Don’t forget to use absolute imports for these files. HubSpot is another good example of a completed tap.
Add test connection method
We need a test_connection
method to enable testing the tap in the Mage UI. This is a simple method that instantiates the tap and closes it. Just replace YourSource
with the source class, e.g. GitHub
and provide the configuration arguments to the tap. Here's a good example from the sFTP tap.
Overwrite write_schema
method
Finally, we need to overwrite the write_schema
method in the tap_your_tap/sync.py
file. Add the following import:
from mage_integrations.sources.messages import write_schema
Now, switch out the default function (likely singer.write_schema
) for the mage function using the default arguments. Here’s an example:
Add logging
To add verbose logging to Mage, add a logger argument each tap file, tap_github/sync.py
, tap_github/discover.py
:
Then replace usage of LOGGER
with mage_logger
.
This will enable Mage to better log Singer taps! 🥳
Add source to the Mage UI
Add the new source to the SOURCES
list constant in this file
This will make your new source visible in the Mage UI.
Test your source
Sources should be tested both in the console and via the UI. Follow the directions here to configure a lightweight test of your sources taps via a bash terminal and the Mage UI in the dev docker instance. This entails:
- Setting up a sample source and connection locally.
- Running the tap directly, via Python.
- Writing to an intermediate file to inspect the output.
- Performing an end-to-end sync.
Populate README.md
Finally, populate a README file in the source folder, e.g. /sources/github/README.md
. This should include:
Was this page helpful?