Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Openlineage Prefect

The openlineage-prefect integration collects, adapts, and emits Prefect flow and task metadata in the OpenLineage specification for data lineage.

Features

The integration collects and emits lineage metadata of Prefect tasks and flows, enabling the analysis of both over time in OpenLineage-compatible consumers. A custom facet collects deployment information, and other OpenLineage facets are leveraged to collect version information, input and output datasets, parent run information, and nominal start times of runs.

Note: by default, the integration tracks runs of idempotent tasks and flows. If tracking unique runs is preferred, environment variables are provided to override the default behavior.

  • To collect unique task runs, set the TASK_NAME_TYPE env variable to "full". Setting it to "base" reverts to the default idempotent names.
  • To collect unique flow runs, set the FLOW_NAME_TYPE env variable to "flow-run". Setting it to "flow" reverts to the default idempotent names.
export TASK_NAME_TYPE="full"
export FLOW_NAME_TYPE="flow-run"

OpenLineage Datasets enable the aggregation of a data source's producers and consumers across OpenLineage-compatible systems and platforms. This allows for the tracking of external upstream and downstream dependencies of Prefect data sources together with their producing and consuming tasks in Prefect flows. For more details, see "Leveraging Datasets" below.

All transport options offered by openlineage-python are available, including intelligent routing of emitted events to Datadog and GCP, async http, http, and composite.

Basic Configuration

At a minimum, define a namespace, Prefect API URL, and transport consisting of a type, URL, and endpoint. All three can be supplied using environment variables.

For example:

export OPENLINEAGE_NAMESPACE="prefect_test" &&
export OPENLINEAGE__TRANSPORT__TYPE="http" &&
export OPENLINEAGE__TRANSPORT__URL="http://lineageconsumer.com:5000" &&
export OPENLINEAGE__TRANSPORT__ENDPOINT="/api/v1/lineage" &&
export PREFECT_API_URL="http://prefecthost.com:4200/api"

For more details of OpenLineage transport options and how to configure them, consult the OpenLineage Python Client Documentation.

Leveraging Datasets

The integration looks for OpenLineage Datasets in Prefect Artifacts. To attach an input or output Dataset to a job run, use create_table_artifact from the Prefect Artifact library to create an Artifact in a task definition context. Provide a database URI and table name via an Artifact's table and description, respectively, as shown below. Distinguish the type of Dataset by appending _output or _input to ol-dataset in the description.

For example:

ol_table = [{"database_uri":"duckdb:///customers_db", "table":"customers"}]

create_table_artifact(
    key="upstream-insert",
    table=ol_table,
    description="ol-dataset_output"
)

Execution

Import the PrefectOpenLineageListener class and execute the collect_and_process_runs method asynchronously in its own process.

For example:

import asyncio
from openlineage_prefect.prefect_adapter.listener import PrefectOpenLineageListener

async def main():
    await PrefectOpenLineageListener().collect_and_process_runs()

if __name__ == "__main__":
    asyncio.run(main())

Quickstart Guide

This guide explains how to spin up a Marquez instance for OpenLineage visualization and then configure a Prefect flow to send OpenLineage events to the Marquez API.

Required: an active local Prefect instance. This guide assumes the server and API are using port 4200.

  1. Spin up Marquez:

    mkdir prefect-test
    cd prefect-test
    git clone git@github.com:ilum-cloud/marquez.git
    cd marquez
    ./docker/up.sh --db-port 2345
    ..
    
  2. Install local environment requirements:

    python3 -m venv .venv
    source .venv/bin/activate
    pip install prefect openlineage-prefect openlineage-python duckdb
    
  3. Configure the integration:

    export OPENLINEAGE_NAMESPACE="prefect_test" &&
    export OPENLINEAGE__TRANSPORT__TYPE="http" &&
    export OPENLINEAGE__TRANSPORT__URL="http://localhost:5000" &&
    export OPENLINEAGE__TRANSPORT__ENDPOINT="/api/v1/lineage" &&
    export PREFECT_API_URL="http://localhost:4200/api"
    
  4. Copy and paste the below code into new file quickstart.py:

    import random
    from prefect import flow, task
    import duckdb
    from prefect.artifacts import create_table_artifact
    
    ORG_CUSTOMERS_TABLE = [{"database_uri":"duckdb:///company_customers_db", "table":"org_customers"}]
    FIRM_CUSTOMERS_TABLE = [{"database_uri":"duckdb:///company_customers_db", "table":"firm_customers"}]
    
    @task
    def load_customers():
        """Flaky task"""
        choices = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
        choice = random.choice(choices)
        if choice == 1:
            with duckdb.connect("company_customers_db.duckdb") as connector:
                customers = connector.execute("SELECT * FROM org_customers").fetchall()
        else:
            with duckdb.connect("company_customers_db.duckdb") as connector:
                customers = connector.execute("SELECT * FROM org_customer").fetchall() # fails
    
        create_table_artifact(
            key="get-org-customers",
            table=ORG_CUSTOMERS_TABLE,
            description="ol-dataset_input"
        )
    
        with duckdb.connect("company_customers_db.duckdb") as connector:
            connector.execute(
                "CREATE TABLE IF NOT EXISTS firm_customers (name VARCHAR, address VARCHAR)"
            )
            connector.executemany(
                "INSERT INTO firm_customers VALUES (?, ?)", customers
            )
    
        create_table_artifact(
            key="load-firm-customers",
            table=FIRM_CUSTOMERS_TABLE,
            description="ol-dataset_output"
        )
    
    @task
    def extract_customers():
        with duckdb.connect("company_customers_db.duckdb") as connector:
            connector.execute("DROP TABLE IF EXISTS org_customers")
            connector.execute(
                "CREATE TABLE IF NOT EXISTS org_customers (name VARCHAR, address VARCHAR)"
            )
            connector.execute(
                "INSERT INTO org_customers VALUES (?, ?)", ["Marvin Martian", "1 Mars Pl"]
            )
    
        create_table_artifact(
            key="org-customers",
            table=ORG_CUSTOMERS_TABLE,
            description="ol-dataset_output"
        )    
    
    @flow(name="Update Customers")
    def update_customers():
        extract_customers()
        load_customers()
    
    if __name__ == "__main__":
        update_customers()
    
  5. Copy and paste the below code into new file listener.py:

    import asyncio
    from openlineage_prefect.prefect_adapter.listener import PrefectOpenLineageListener
    
    async def main():
        await PrefectOpenLineageListener().collect_and_process_runs()
    
    if __name__ == "__main__":
        asyncio.run(main())
    

    Run the file in its own process:

    python listener.py
    
  6. Deploy the Update Customers flow in your local Prefect instance:

    prefect deploy -n prefect-test 
    
  7. Trigger a run of prefect-test in the Prefect UI.

  8. Verify event tracking in Marquez:

    • Watch the flow completion logs pop up in your Prefect UI at https://localhost:4200.
    • Switch tabs to the Marquez UI on https://localhost:3000 to see the lineage metadata graphs dynamically drawn from your code run.

Metadata

Release files for openlineage-prefect 0.0.1b21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openlineage-prefect 0.0.1b21
File Size Uploaded
openlineage_prefect-0.0.1b21.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openlineage-prefect 0.0.1b21
File Interpreter ABI Platform
openlineage_prefect-0.0.1b21-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / openlineage_prefect-0.0.1b21.tar.gz

Download URL openlineage_prefect-0.0.1b21.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b2433a119b43ced8b8e8b594c4b2f4b5d20a4eb6c90b8e4ed5830b640b2ce086
BLAKE2b-256 checksum
How to use checksums
2ff8b8b87c6d7313087a2ecd428282890ec0460de2950f28bc2e91b38899d3ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / openlineage_prefect-0.0.1b21-py3-none-any.whl

Download URL openlineage_prefect-0.0.1b21-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
646cc1536ea6920eb9a2eb2907a38550abf53a652dc78b1873e08ad3347444e3
BLAKE2b-256 checksum
How to use checksums
0d8474aa8da8c65ac1757a6ae01d1bae658f811d88952b40f2c5cc95b273bd60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page