# OpenLineage

> Emit OpenLineage lineage events from Airflow to an OpenLineage backend such as Marquez.

Airflow can emit [OpenLineage](https://openlineage.io/) lineage events describing the datasets your DAGs read and write. This is implemented by the [Airflow OpenLineage provider](https://airflow.apache.org/docs/apache-airflow-providers-openlineage/stable/), which must be installed in the product image.

> **Note:** The `apache-airflow-providers-openlineage` provider package must be present in the Airflow image. The Stackable images include it; if you build a custom image, make sure the provider is installed.

## Enabling OpenLineage

Set `spec.clusterConfig.lineage` and provide a `connection` to your OpenLineage backend. The connection can be defined inline:

```yaml
apiVersion: airflow.stackable.tech/v1alpha2
kind: AirflowCluster
metadata:
  name: airflow
spec:
  clusterConfig:
    lineage:
      connection:
        inline:
          http:              (1)
            host: marquez    (2)
            port: 5000       (3)
      namespace: production  (4)
```

(1) The transport used to publish events. `http` is currently the only supported transport.
(2) Host of the OpenLineage backend, without protocol or port.
(3) Port the backend listens on.
(4) OpenLineage namespace lineage is reported under. Defaults to `default`.

The operator configures the transport via the OpenLineage client environment variables (`OPENLINEAGE_TRANSPORT_*`) and sets `AIRFLOW_OPENLINEAGE_NAMESPACE`. Lineage events are emitted to `\http(s)://<host>:<port><path>`, where `path` defaults to `/api/v1/lineage`.

## Referencing an OpenLineageConnection

To share a backend definition across multiple stacklets, create an `OpenLineageConnection` resource and reference it by name. The referenced resource must live in the same namespace as the `AirflowCluster`.

```yaml
apiVersion: lineage.stackable.tech/v1alpha1
kind: OpenLineageConnection
metadata:
  name: marquez
spec:
  http:
    host: marquez
    port: 5000
```

```yaml
apiVersion: airflow.stackable.tech/v1alpha2
kind: AirflowCluster
metadata:
  name: airflow
spec:
  clusterConfig:
    lineage:
      connection:
        reference: marquez  (1)
```

(1) Name of the `OpenLineageConnection` resource.

## TLS

Configure TLS on the connection to reach a backend over HTTPS:

```yaml
lineage:
  connection:
    inline:
      http:
        host: marquez
        port: 5000
        tls:
          verification:
            server:
              caCert:
                secretClass: openlineage-tls  (1)
```

(1) A [SecretClass](https://docs.stackable.tech/home/stable/secret-operator/secretclass/index.md) providing the CA certificate used to verify the backend.

When a SecretClass CA is configured, the CA certificate is mounted into the Airflow pods and the OpenLineage client is pointed at it via `REQUESTS_CA_BUNDLE`.

> **Warning:** `REQUESTS_CA_BUNDLE` is set on the whole Airflow container, not just for the OpenLineage client. The Python `requests` library reads it for every call that verifies certificates, and it _replaces_ the default certifi CA bundle instead of extending it. This might break DAGs and Airflow itself. If this is a problem, build a CA bundle that contains both the certifi CAs and the OpenLineage backend CA.

## Authentication

If the backend requires a bearer token, set `credentialsSecretName` on the connection to the name of a Secret containing the token. The Secret must contain the token under the fixed key `apiKey` and must live in the same namespace as the cluster:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: openlineage-credentials
stringData:
  apiKey: "<your-token>"  (1)
```

(1) The bearer token, under the fixed key `apiKey`.

```yaml
lineage:
  connection:
    inline:
      http:
        host: marquez
        port: 5000
        credentialsSecretName: openlineage-credentials  (1)
```

(1) Name of the Secret holding the API key.

The token is injected into the Airflow pods from the Secret via a `secretKeyRef` environment variable. It never appears in the CRD, a ConfigMap, or a plaintext environment value.

## Endpoint path

By default events are sent to `/api/v1/lineage` on the backend. Set `path` on the transport to target a different endpoint:

```yaml
lineage:
  connection:
    inline:
      http:
        host: marquez
        port: 5000
        path: /custom/lineage  (1)
```

(1) URL path of the endpoint events are sent to. Defaults to `/api/v1/lineage`.

## Overriding injected configuration

The OpenLineage environment variables are applied to every role (and the Kubernetes-executor pod template) _before_ user `envOverrides`, so you can override any of them. For example, to disable emission for a single role group without removing the `lineage` block:

```yaml
webservers:
  roleGroups:
    default:
      envOverrides:
        AIRFLOW__OPENLINEAGE__DISABLED: "true"
```

See [Configuration & Environment Overrides](https://docs.stackable.tech/home/stable/airflow/usage-guide/overrides/index.md) for more on overrides.
