Skip to main content

Utilities for dbt metrics.

Project description

dbt-metric-utils

This tool allows you to query metrics from your dbt semantic model directly from a dbt model through the dbt_metric_utils_materialize macro. One way to look at it is that it revives the metric.calculate() macro from dbt <=v1.5. By having access to this macro, the dbt semantic layer becomes more useful for dbt-core users. You still don't have all the goodness of dbt-cloud semantic layer but it does allow you to get started with connecting your users and BI tools to aggregation tables/views that are directly querying your metrics.

[!TIP] Check out some examples queries here

[!TIP] Browse the dbt docs pages for the example project here

Installation instructions

This project is a Python package that wraps around dbt in the most transparant way I could find. Try it out through the following steps:

  1. Install dbt-metric-utils from Pypi in your project (e.g. pip install dbt-metric-utils)
  2. Run dbt-metric-utils init or dbtmu init. This will install the macro into your project and will make sure that any dbt CLI calls are intercepted and processed in the correct way (check below for explanation)
  3. Introduce a dbt model that calls the dbt_metric_utils_materialize macro.
  4. Continue using dbt as you're used to.

How it works

Any dbt command that doesn't require dbt to compile your project is simply passed directly to dbt (Mode A in the diagram). A dbt invocation that does require compilation (e.g. compile, run, test , etc) is intercepted by the package.

After intercepting we run through the following sequence of steps

  1. Call dbt parse . This will build a partially filled manifest.json from which we can extract all the models, their dependencies, and the raw SQL queries.
  2. Extract all models that contain a dbt_metric_utils_materialize invocation.
  3. Run mf query --explain commands for all the dbt_metric_utils_materialize invocations.
  4. Inject the generated queries by Metricflow as dbt variables in the actual dbt command. If the user ran dbt run , we actually trigger dbt run --vars {<macro_invocation_signature>: <query>}

The passed variables will be a mapping from dbt_metric_utils_materialize invocation signature (e.g. metric=['m1'],dimensions='[dim1']... ) to the generated metric query. The dbt_metric_utils_materialize macro will find that variable at compile time and return it as the macro result.

Along this sequence of steps, we also ensure that the dependency graph in manifest.json is updated correctly. Dbt itself only detects dependencies based on ref and source , not on macros that are external to it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dbt_metric_utils-0.0.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file dbt_metric_utils-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dbt_metric_utils-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 786b8356dca702b2fa9ddb46ffbfe52cf5ea07a5dab241a5cbe7f8c865737ed6
MD5 db2e4294097995c781a9590c0967471c
BLAKE2b-256 2b7f373769dca1ff180a0b9e390ae14192bdf2c229f6e1d01039c185077d3d57

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page