├── .circleci └── config.yml ├── .github ├── ISSUE_TEMPLATE │ ├── CODEOWNERS │ ├── bug_report.md │ └── feature_request.md └── pull_request_template.md ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analyses └── periscope │ ├── README.md │ ├── dashboard.png │ ├── deployments.sql │ ├── models.sql │ ├── models_out_of_sla.sql │ └── models_summary.sql ├── dbt_project.yml ├── integration_tests ├── .gitignore ├── Makefile ├── README.md ├── ci │ └── sample.profiles.yml ├── dbt_project.yml ├── macros │ ├── create_old_audit_table.sql │ └── drop_audit_schema.sql ├── models │ └── my_model.sql └── packages.yml ├── lookml ├── dbt_audit.dashboard.lookml ├── dbt_audit.model.lkml ├── dbt_audit_log.view.lkml ├── dbt_deployments.view.lkml └── dbt_model_deployments.view.lkml ├── macros ├── audit.sql └── bigquery.sql ├── models ├── bigquery │ ├── stg_dbt_deployments.sql │ └── stg_dbt_model_deployments.sql ├── default │ ├── stg_dbt_deployments.sql │ └── stg_dbt_model_deployments.sql └── stg_dbt_audit_log.sql └── packages.yml /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | 2 | version: 2.1 3 | 4 | jobs: 5 | build: 6 | docker: 7 | - image: cimg/python:3.9.9 8 | - image: circleci/postgres:9.6.5-alpine-ram 9 | 10 | steps: 11 | - checkout 12 | 13 | - run: 14 | run: setup_creds 15 | command: | 16 | echo $BIGQUERY_SERVICE_ACCOUNT_JSON > ${HOME}/bigquery-service-key.json 17 | 18 | - restore_cache: 19 | key: deps1-{{ .Branch }} 20 | 21 | - run: 22 | name: "Setup dbt" 23 | command: | 24 | python3 -m venv dbt_venv 25 | . dbt_venv/bin/activate 26 | 27 | pip install --upgrade pip setuptools 28 | pip install --pre dbt-core dbt-postgres dbt-redshift dbt-snowflake dbt-bigquery 29 | 30 | mkdir -p ~/.dbt 31 | cp integration_tests/ci/sample.profiles.yml ~/.dbt/profiles.yml 32 | 33 | - run: 34 | name: "Run Tests - Postgres" 35 | environment: 36 | POSTGRES_TEST_HOST: localhost 37 | POSTGRES_TEST_USER: root 38 | POSTGRES_TEST_PASS: '' 39 | POSTGRES_TEST_PORT: 5432 40 | POSTGRES_TEST_DBNAME: circle_test 41 | command: | 42 | . dbt_venv/bin/activate 43 | cd integration_tests 44 | dbt --warn-error deps --target postgres 45 | dbt --warn-error run-operation drop_audit_schema --target postgres 46 | dbt --warn-error run --target postgres --full-refresh 47 | dbt --warn-error run --target postgres 48 | 49 | dbt --warn-error run-operation drop_audit_schema --target postgres 50 | dbt --warn-error run-operation create_legacy_audit_table --target postgres 51 | dbt --warn-error run --target postgres --full-refresh 52 | dbt --warn-error run --target postgres 53 | 54 | - run: 55 | name: "Run Tests - Redshift" 56 | command: | 57 | . dbt_venv/bin/activate 58 | echo `pwd` 59 | cd integration_tests 60 | dbt --warn-error deps --target redshift 61 | dbt --warn-error run-operation drop_audit_schema --target redshift 62 | dbt --warn-error run --target redshift --full-refresh 63 | dbt --warn-error run --target redshift 64 | 65 | dbt --warn-error run-operation drop_audit_schema --target redshift 66 | dbt --warn-error run-operation create_legacy_audit_table --target redshift 67 | dbt --warn-error run --target redshift --full-refresh 68 | dbt --warn-error run --target redshift 69 | 70 | - run: 71 | name: "Run Tests - Snowflake" 72 | command: | 73 | . dbt_venv/bin/activate 74 | echo `pwd` 75 | cd integration_tests 76 | # dbt --warn-error deps --target snowflake 77 | # dbt --warn-error run-operation drop_audit_schema --target snowflake 78 | # dbt --warn-error run --target snowflake --full-refresh 79 | # dbt --warn-error run --target snowflake 80 | 81 | dbt --warn-error run-operation drop_audit_schema --target snowflake || true 82 | dbt --warn-error run-operation create_legacy_audit_table --target snowflake 83 | dbt --warn-error run --target snowflake --full-refresh 84 | dbt --warn-error run --target snowflake 85 | 86 | - run: 87 | name: "Run Tests - BigQuery" 88 | environment: 89 | BIGQUERY_SERVICE_KEY_PATH: "/home/circleci/bigquery-service-key.json" 90 | 91 | command: | 92 | . dbt_venv/bin/activate 93 | echo `pwd` 94 | cd integration_tests 95 | dbt --warn-error deps --target bigquery 96 | dbt --warn-error run-operation drop_audit_schema --target bigquery 97 | dbt --warn-error run --target bigquery --full-refresh 98 | dbt --warn-error run --target bigquery 99 | 100 | dbt --warn-error run-operation drop_audit_schema --target bigquery 101 | dbt --warn-error run-operation create_legacy_audit_table --target bigquery 102 | dbt --warn-error run --target bigquery --full-refresh 103 | dbt --warn-error run --target bigquery 104 | 105 | - save_cache: 106 | key: deps1-{{ .Branch }} 107 | paths: 108 | - "dbt_venv" 109 | 110 | workflows: 111 | version: 2 112 | test-all: 113 | jobs: 114 | - build: 115 | context: 116 | - profile-redshift 117 | - profile-snowflake 118 | #- profile-bigquery 119 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @clrcrl -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Report a bug or an issue you've found with this package 4 | title: '' 5 | labels: bug, triage 6 | assignees: '' 7 | 8 | --- 9 | 10 | ### Describe the bug 11 | 15 | 16 | ### Steps To Reproduce 17 | 22 | 23 | ### Expected behavior 24 | 27 | 28 | ### Actual behavior 29 | 33 | 34 | ### System information 35 | **How did you add this package to your project:** 36 | ``` 37 | 38 | ``` 39 | 40 | **Which database are you using dbt with?** 41 | - [ ] Postgres 42 | - [ ] Redshift 43 | - [ ] BigQuery 44 | - [ ] Snowflake 45 | - [ ] Other (specify: ____________) 46 | 47 | 48 | **The output of `dbt --version`:** 49 | ``` 50 | 51 | ``` 52 | 53 | 54 | ### Additional context 55 | 58 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this package 4 | title: '' 5 | labels: enhancement, triage 6 | assignees: '' 7 | 8 | --- 9 | 10 | ### Describe the feature 11 | 14 | 15 | ### Describe alternatives you've considered 16 | 20 | 21 | ### Additional context 22 | 26 | 27 | ### Who will this benefit? 28 | 32 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Description & motivation 2 | 5 | 6 | ## Checklist 7 | - [ ] I have verified that these changes work locally 8 | - [ ] I have updated the README.md (if applicable) 9 | - [ ] I have added tests & descriptions to my models (and macros if applicable) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | target/ 3 | dbt_modules/ 4 | logs/ 5 | 6 | # pycharm 7 | .idea/ 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # dbt-event-logging v0.6.0 2 | This release supports any version (minor and patch) of v1, which means far less need for compatibility releases in the future. 3 | 4 | ## Under the hood 5 | - Change `require-dbt-version` to `[">=1.0.0", "<2.0.0"]` 6 | - Bump dbt-utils dependency 7 | - Replace `source-paths` and `data-paths` with `model-paths` and `seed-paths` respectively 8 | - Rename `data` and `analysis` directories to `seeds` and `analyses` respectively 9 | - Replace `dbt_modules` with `dbt_packages` in `clean-targets` 10 | 11 | # dbt-event-logging v0.5.1 12 | 🚨 This is a compatibility release in preparation for `dbt-core` v1.0.0 (🎉). Projects using this version with `dbt-core` v1.0.x can expect to see a deprecation warning. This will be resolved in the next minor release. 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ***Archival Notice*** 2 | This repository has been archived. 3 | 4 | As a result all of its historical issues and PRs have been closed. 5 | 6 | Please *do not clone* this repo without understanding the risk in doing so: 7 | - It may have unaddressed security vulnerabilities 8 | - It may have unaddressed bugs 9 | 10 |
11 | Click for historical readme 12 | 13 | ## dbt Event Logging 14 | 15 | > ⛔🏚️ This package is obsolete and no longer developed; use [dbt_artifacts](https://hub.getdbt.com/brooklyn-data/dbt_artifacts/latest/) for a higher performance and richer view into your project. 16 | 17 | > :warning: **ADDING THIS PACKAGE TO YOUR DBT PROJECT CAN SIGNIFICANTLY SLOW 18 | > DOWN YOUR DBT RUNS**. This is due to the number of insert statements executed by 19 | > this package, especially as a post-hook. Please consider if this package is 20 | > appropriate for your use case before using it. 21 | 22 | Requires dbt >= 0.18.0 23 | 24 | This package provides out-of-the-box functionality to log events for all dbt 25 | invocations, including run start, run end, model start, and model end. It 26 | outputs all data and models to schema `[target.schema]_meta`. There are three 27 | convenience models to make it easier to parse the event log data. 28 | 29 | ### Setup 30 | 31 | 1. Include this package in your `packages.yml` -- check [here](https://hub.getdbt.com/dbt-labs/logging/latest/) 32 | for installation instructions. 33 | 2. Include the following in your `dbt_project.yml` directly within your 34 | `models:` block (making sure to handle indenting appropriately): 35 | 36 | ```YAML 37 | # dbt_project.yml 38 | ... 39 | 40 | models: 41 | ... 42 | pre-hook: "{{ logging.log_model_start_event() }}" 43 | post-hook: "{{ logging.log_model_end_event() }}" 44 | ``` 45 | 46 | That's it! You'll now have a stream of events for all dbt invocations in your 47 | warehouse. 48 | 49 | #### Customising audit schema 50 | 51 | It's possible to customise the audit schema for any project by adding a macro named: `get_audit_schema` into your DBT project. 52 | 53 | For example to always log into a specific schema, say `analytics_meta`, regardless of DBT schema, you can include the following in your project: 54 | 55 | ```sql 56 | -- your_dbt_project/macros/get_audit_schema.sql 57 | {% macro get_audit_schema() %} 58 | 59 | {{ return('analytics_meta') }} 60 | 61 | {% endmacro %} 62 | ``` 63 | 64 | #### Customising audit database 65 | 66 | It's possible to customise the audit database for any project by adding a macro named: `get_audit_database` into your DBT project. 67 | 68 | For example to always log into a specific database, say `META`, regardless of DBT database, you can include the following in your project: 69 | 70 | ```sql 71 | -- your_dbt_project/macros/get_audit_database.sql 72 | {% macro get_audit_database() %} 73 | 74 | {{ return('META') }} 75 | 76 | {% endmacro %} 77 | ``` 78 | 79 | ### Adapter support 80 | 81 | This package is currently compatible with dbt's BigQuery<sup>1</sup>, Snowflake, Redshift, and 82 | Postgres integrations. 83 | 84 | <sup>1</sup> BigQuery support may only work when 1 thread is set in your `profiles.yml` file. Anything larger may result in "quota exceeded" errors. 85 | 86 | ### Migration guide 87 | 88 | #### v0.1.17 -> v0.2.0 89 | 90 | New columns were added in v0.2.0: 91 | 92 | - **event_user as user** - `varchar(512)`the user who ran the model 93 | - **event_target as target** - `varchar(512)` the target used when running DBT 94 | - **event_is_full_refresh as is_full_refresh** - `boolean` whether the DBT run was a full refresh 95 | 96 | These will be added to your existing audit table automatically in the `on-run-start` DBT hook, and added to the staging tables deployed by this table when they are ran. The existing `event_schema` column will also be propagated into to `stg_dbt_model_deployments` as `schema`. 97 | 98 | ### Contributing 99 | 100 | Additional contributions to this repo are very welcome! Check out [this](https://discourse.getdbt.com/t/contributing-to-an-external-dbt-package/657) post on the best workflow for contributing to a package. All PRs should only include functionality that is contained within all Segment deployments; no implementation-specific details should be included. 101 | 102 | -------------------------------------------------------------------------------- /analyses/periscope/README.md: -------------------------------------------------------------------------------- 1 | # Periscope Data DBT Dashboard 2 | 3 | ![DBT Dashboard Screenshot](dashboard.png "DBT Dashboard") 4 | 5 | 1. Run `dbt compile` 6 | 2. Create a dashboard in Periscope Data, name it e.g. DBT Dashboard 7 | 3. Enable filters for Aggregation and Date Range 8 | 4. Create a number overlay chart named Models out of SLA 9 | 5. Copy and paste the SQL from 10 | `target/compiled/logging/analysis/periscope/models_out_of_sla.sql` 11 | 6. Adjust SLA in the SQL query if desired (default is 24 hours) 12 | 7. Repeat for: 13 | - Table named Models Summary - [Daterange] 14 | - Line chart with two axes named [Aggregation] Deployments - [Daterange] 15 | - Area chart named [Aggregation] Models - [Daterange] 16 | -------------------------------------------------------------------------------- /analyses/periscope/dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbt-labs/dbt-event-logging/7b289a58cfe5a0219d0a291f5439aa7c639076f2/analyses/periscope/dashboard.png -------------------------------------------------------------------------------- /analyses/periscope/deployments.sql: -------------------------------------------------------------------------------- 1 | select 2 | [deployment_started_at:aggregation] as period 3 | , sum(datediff('minute', deployment_started_at, deployment_completed_at)) as total_runtime_m 4 | , avg(datediff('minute', deployment_started_at, deployment_completed_at)) as avg_runtime_m 5 | , sum(models_deployed) as models_deployed 6 | from 7 | {{ ref('stg_dbt_deployments') }} 8 | where 9 | [deployment_started_at=daterange] 10 | group by 11 | 1 12 | order by 13 | 1 desc 14 | -------------------------------------------------------------------------------- /analyses/periscope/models.sql: -------------------------------------------------------------------------------- 1 | select 2 | [deployment_started_at:aggregation] as period 3 | , model 4 | , count(1) as deployment_count 5 | from 6 | {{ ref('stg_dbt_model_deployments') }} 7 | where 8 | [deployment_started_at=daterange] 9 | group by 10 | 1 11 | , 2 12 | order by 13 | 1 desc 14 | , 2 15 | -------------------------------------------------------------------------------- /analyses/periscope/models_out_of_sla.sql: -------------------------------------------------------------------------------- 1 | with 2 | models_out_of_sla as ( 3 | select 4 | model 5 | , max(deployment_started_at) as last_deployment_ts 6 | , datediff('hour', max(deployment_started_at), {{ dbt.current_timestamp_backcompat() }}) as hours_since_refreshed 7 | from 8 | {{ ref('stg_dbt_model_deployments') }} 9 | group by 10 | 1 11 | having 12 | max(deployment_started_at) < dateadd('hour', -24, getdate()) 13 | ) 14 | select 15 | count(1) 16 | from 17 | models_out_of_sla 18 | -------------------------------------------------------------------------------- /analyses/periscope/models_summary.sql: -------------------------------------------------------------------------------- 1 | select 2 | model 3 | , cast(min(deployment_started_at) as date) as first 4 | , max(deployment_started_at) as last 5 | from 6 | {{ ref('stg_dbt_model_deployments') }} 7 | where 8 | [deployment_started_at=daterange] 9 | group by 10 | 1 11 | order by 12 | 3 asc 13 | -------------------------------------------------------------------------------- /dbt_project.yml: -------------------------------------------------------------------------------- 1 | name: 'logging' 2 | version: '0.5.0' 3 | config-version: 2 4 | 5 | require-dbt-version: [">=1.3.0", "<2.0.0"] 6 | 7 | model-paths: ["models"] 8 | analysis-paths: ["analyses"] 9 | test-paths: ["tests"] 10 | seed-paths: ["seeds"] 11 | macro-paths: ["macros"] 12 | 13 | target-path: "target" 14 | clean-targets: 15 | - "target" 16 | - "dbt_packages" 17 | 18 | 19 | on-run-start: 20 | - "{{ logging.create_audit_schema() }}" 21 | - "{{ logging.create_audit_log_table() }}" 22 | - "{{ logging.log_run_start_event() }}" 23 | 24 | 25 | on-run-end: 26 | - "{{ logging.log_run_end_event() }}" 27 | 28 | 29 | models: 30 | logging: 31 | +schema: meta 32 | bigquery: 33 | +enabled: '{{ target.type == "bigquery" | as_bool }}' 34 | default: 35 | +enabled: '{{ target.type != "bigquery" | as_bool }}' 36 | -------------------------------------------------------------------------------- /integration_tests/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | target/ 3 | dbt_modules/ 4 | logs/ 5 | -------------------------------------------------------------------------------- /integration_tests/Makefile: -------------------------------------------------------------------------------- 1 | 2 | test-postgres: 3 | dbt run-operation drop_audit_schema --target postgres 4 | dbt run --target postgres --full-refresh 5 | dbt run --target postgres 6 | 7 | dbt run-operation drop_audit_schema --target postgres 8 | dbt run-operation create_legacy_audit_table --target postgres 9 | dbt run --target postgres --full-refresh 10 | dbt run --target postgres 11 | 12 | test-redshift: 13 | dbt run-operation drop_audit_schema --target redshift 14 | dbt run --target redshift --full-refresh 15 | dbt run --target redshift 16 | 17 | dbt run-operation drop_audit_schema --target redshift 18 | dbt run-operation create_legacy_audit_table --target redshift 19 | dbt run --target redshift --full-refresh 20 | dbt run --target redshift 21 | 22 | 23 | test-snowflake: 24 | dbt run-operation drop_audit_schema --target snowflake 25 | dbt run --target snowflake --full-refresh 26 | dbt run --target snowflake 27 | 28 | 29 | dbt run-operation drop_audit_schema --target snowflake 30 | dbt run-operation create_legacy_audit_table --target snowflake 31 | dbt run --target snowflake --full-refresh 32 | dbt run --target snowflake 33 | 34 | 35 | test-bigquery: 36 | dbt run-operation drop_audit_schema --target bigquery 37 | dbt run --target bigquery --full-refresh 38 | dbt run --target bigquery 39 | 40 | 41 | dbt run-operation drop_audit_schema --target bigquery 42 | dbt run-operation create_legacy_audit_table --target bigquery 43 | dbt run --target bigquery --full-refresh 44 | dbt run --target bigquery 45 | 46 | test-all: test-postgres test-redshift test-snowflake test-bigquery 47 | echo "Completed successfully" 48 | 49 | test-cloud: test-redshift test-snowflake 50 | echo "Completed successfully" 51 | -------------------------------------------------------------------------------- /integration_tests/README.md: -------------------------------------------------------------------------------- 1 | ### dbt integration test suite for event-logging 2 | 3 | These simply test that the package doesn't throw an error. No quality assurance 4 | is done on the results. 5 | -------------------------------------------------------------------------------- /integration_tests/ci/sample.profiles.yml: -------------------------------------------------------------------------------- 1 | 2 | # HEY! This file is used in the dbt-event-logging integrations tests with CircleCI. 3 | # You should __NEVER__ check credentials into version control. Thanks for reading :) 4 | 5 | config: 6 | send_anonymous_usage_stats: False 7 | use_colors: True 8 | 9 | integration_tests: 10 | target: postgres 11 | outputs: 12 | postgres: 13 | type: postgres 14 | host: "{{ env_var('POSTGRES_TEST_HOST') }}" 15 | user: "{{ env_var('POSTGRES_TEST_USER') }}" 16 | pass: "{{ env_var('POSTGRES_TEST_PASS') }}" 17 | port: "{{ env_var('POSTGRES_TEST_PORT') | as_number }}" 18 | dbname: "{{ env_var('POSTGRES_TEST_DBNAME') }}" 19 | schema: event_logging_integration_tests_postgres 20 | threads: 1 21 | 22 | redshift: 23 | type: redshift 24 | host: "{{ env_var('REDSHIFT_TEST_HOST') }}" 25 | user: "{{ env_var('REDSHIFT_TEST_USER') }}" 26 | pass: "{{ env_var('REDSHIFT_TEST_PASS') }}" 27 | dbname: "{{ env_var('REDSHIFT_TEST_DBNAME') }}" 28 | port: "{{ env_var('REDSHIFT_TEST_PORT') | as_number }}" 29 | schema: event_logging_integration_tests_redshift 30 | threads: 1 31 | 32 | snowflake: 33 | type: snowflake 34 | account: "{{ env_var('SNOWFLAKE_TEST_ACCOUNT') }}" 35 | user: "{{ env_var('SNOWFLAKE_TEST_USER') }}" 36 | password: "{{ env_var('SNOWFLAKE_TEST_PASSWORD') }}" 37 | role: "{{ env_var('SNOWFLAKE_TEST_ROLE') }}" 38 | database: "{{ env_var('SNOWFLAKE_TEST_DATABASE') }}" 39 | warehouse: "{{ env_var('SNOWFLAKE_TEST_WAREHOUSE') }}" 40 | schema: event_logging_integration_tests_snowflake 41 | threads: 1 42 | 43 | bigquery: 44 | type: bigquery 45 | method: service-account 46 | keyfile: "{{ env_var('BIGQUERY_SERVICE_KEY_PATH') }}" 47 | project: "{{ env_var('BIGQUERY_TEST_DATABASE') }}" 48 | schema: event_logging_integration_tests_bigquery 49 | threads: 1 50 | -------------------------------------------------------------------------------- /integration_tests/dbt_project.yml: -------------------------------------------------------------------------------- 1 | 2 | name: 'event_logging_integration_tests' 3 | version: '0.3.0' 4 | config-version: 2 5 | 6 | profile: 'integration_tests' 7 | 8 | 9 | models: 10 | pre-hook: "{{ logging.log_model_start_event() }}" 11 | post-hook: "{{ logging.log_model_end_event() }}" 12 | -------------------------------------------------------------------------------- /integration_tests/macros/create_old_audit_table.sql: -------------------------------------------------------------------------------- 1 | 2 | {# create_legacy_audit_table creates the audit table with the columns defined in version 0.1.7 #} 3 | {% macro create_legacy_audit_table() %} 4 | 5 | {{ logging.create_audit_schema() }} 6 | 7 | create table if not exists {{ logging.get_audit_relation() }} 8 | ( 9 | event_name {{ dbt.type_string() }}, 10 | event_timestamp {{ dbt.type_timestamp() }}, 11 | event_schema {{ dbt.type_string() }}, 12 | event_model {{ dbt.type_string() }}, 13 | invocation_id {{ dbt.type_string() }} 14 | ) 15 | {% do dbt_utils.log_info("Created legacy audit table") %} 16 | {% endmacro %} 17 | -------------------------------------------------------------------------------- /integration_tests/macros/drop_audit_schema.sql: -------------------------------------------------------------------------------- 1 | {% macro drop_audit_schema() %} 2 | {% set audit_schema=logging.get_audit_schema() %} 3 | 4 | {% if adapter.check_schema_exists(target.database, audit_schema) %} 5 | {% set audit_schema_relation = api.Relation.create(database=target.database, schema=audit_schema).without_identifier() %} 6 | {% do drop_schema(audit_schema_relation) %} 7 | {% if adapter.type() != 'bigquery' %} 8 | {% do run_query("commit;") %} 9 | {% endif %} 10 | {{ dbt_utils.log_info("Audit schema dropped")}} 11 | 12 | {% else %} 13 | {{ dbt_utils.log_info("Audit schema does not exist so was not dropped") }} 14 | {% endif %} 15 | 16 | {% endmacro %} 17 | -------------------------------------------------------------------------------- /integration_tests/models/my_model.sql: -------------------------------------------------------------------------------- 1 | select 1 as my_col 2 | -------------------------------------------------------------------------------- /integration_tests/packages.yml: -------------------------------------------------------------------------------- 1 | 2 | packages: 3 | - local: ../ 4 | -------------------------------------------------------------------------------- /lookml/dbt_audit.dashboard.lookml: -------------------------------------------------------------------------------- 1 | - dashboard: dbt_audit 2 | title: dbt Audit 3 | layout: newspaper 4 | elements: 5 | - title: dbt Deployments by Hour 6 | name: dbt Deployments by Hour 7 | model: dbt 8 | explore: dbt_deployments 9 | type: looker_line 10 | fields: 11 | - dbt_deployments.deployment_completed_hour 12 | - dbt_deployments.count 13 | fill_fields: 14 | - dbt_deployments.deployment_completed_hour 15 | sorts: 16 | - dbt_deployments.deployment_completed_hour desc 17 | limit: 500 18 | query_timezone: America/New_York 19 | stacking: '' 20 | show_value_labels: false 21 | label_density: 25 22 | legend_position: center 23 | x_axis_gridlines: false 24 | y_axis_gridlines: true 25 | show_view_names: true 26 | limit_displayed_rows: false 27 | y_axis_combined: true 28 | show_y_axis_labels: true 29 | show_y_axis_ticks: true 30 | y_axis_tick_density: default 31 | y_axis_tick_density_custom: 5 32 | show_x_axis_label: true 33 | show_x_axis_ticks: true 34 | x_axis_scale: auto 35 | y_axis_scale_mode: linear 36 | x_axis_reversed: false 37 | y_axis_reversed: false 38 | show_null_points: true 39 | point_style: none 40 | interpolation: linear 41 | ordering: none 42 | show_null_labels: false 43 | show_totals_labels: false 44 | show_silhouette: false 45 | totals_color: "#808080" 46 | series_types: {} 47 | row: 0 48 | col: 12 49 | width: 12 50 | height: 7 51 | - title: dbt Model Average Deployment Time 52 | name: dbt Model Average Deployment Time 53 | model: dbt 54 | explore: dbt_deployments 55 | type: table 56 | fields: 57 | - dbt_model_deployments.average_duration_in_m 58 | - dbt_model_deployments.model 59 | filters: 60 | dbt_model_deployments.average_duration_in_m: NOT NULL 61 | dbt_model_deployments.deployment_completed_date: 7 days 62 | sorts: 63 | - dbt_model_deployments.average_duration_in_m desc 64 | limit: 500 65 | query_timezone: America/New_York 66 | show_view_names: false 67 | show_row_numbers: true 68 | truncate_column_names: false 69 | hide_totals: false 70 | hide_row_totals: false 71 | table_theme: white 72 | limit_displayed_rows: false 73 | enable_conditional_formatting: false 74 | conditional_formatting_include_totals: false 75 | conditional_formatting_include_nulls: false 76 | stacking: '' 77 | show_value_labels: false 78 | label_density: 25 79 | legend_position: center 80 | x_axis_gridlines: false 81 | y_axis_gridlines: true 82 | y_axis_combined: true 83 | show_y_axis_labels: true 84 | show_y_axis_ticks: true 85 | y_axis_tick_density: default 86 | y_axis_tick_density_custom: 5 87 | show_x_axis_label: true 88 | show_x_axis_ticks: true 89 | x_axis_scale: auto 90 | y_axis_scale_mode: linear 91 | x_axis_reversed: false 92 | y_axis_reversed: false 93 | ordering: none 94 | show_null_labels: false 95 | show_totals_labels: false 96 | show_silhouette: false 97 | totals_color: "#808080" 98 | show_null_points: true 99 | point_style: none 100 | interpolation: linear 101 | series_types: {} 102 | row: 16 103 | col: 13 104 | width: 11 105 | height: 21 106 | - title: dbt Longest Running Models Trends 107 | name: dbt Longest Running Models Trends 108 | model: dbt 109 | explore: dbt_deployments 110 | type: looker_line 111 | fields: 112 | - dbt_model_deployments.average_duration_in_m 113 | - dbt_model_deployments.model 114 | - dbt_model_deployments.deployment_completed_hour 115 | pivots: 116 | - dbt_model_deployments.model 117 | filters: 118 | dbt_model_deployments.deployment_completed_date: 7 days 119 | dbt_model_deployments.duration_in_m: ">1" 120 | dbt_model_deployments.average_duration_in_m: NOT NULL 121 | sorts: 122 | - dbt_model_deployments.model 0 123 | - dbt_model_deployments.deployment_completed_hour desc 124 | limit: 500 125 | query_timezone: America/New_York 126 | stacking: '' 127 | show_value_labels: false 128 | label_density: 25 129 | legend_position: center 130 | x_axis_gridlines: false 131 | y_axis_gridlines: true 132 | show_view_names: false 133 | limit_displayed_rows: false 134 | y_axis_combined: true 135 | show_y_axis_labels: true 136 | show_y_axis_ticks: true 137 | y_axis_tick_density: default 138 | y_axis_tick_density_custom: 5 139 | show_x_axis_label: true 140 | show_x_axis_ticks: true 141 | x_axis_scale: auto 142 | y_axis_scale_mode: linear 143 | x_axis_reversed: false 144 | y_axis_reversed: false 145 | show_null_points: false 146 | point_style: none 147 | interpolation: linear 148 | ordering: none 149 | show_null_labels: false 150 | show_totals_labels: false 151 | show_silhouette: false 152 | totals_color: "#808080" 153 | series_types: {} 154 | hide_legend: false 155 | row: 7 156 | col: 0 157 | width: 24 158 | height: 9 159 | - title: dbt Deployment Duration (Full Run) 160 | name: dbt Deployment Duration (Full Run) 161 | model: dbt 162 | explore: dbt_deployments 163 | type: looker_line 164 | fields: 165 | - dbt_deployments.deployment_completed_hour 166 | - dbt_deployments.average_duration_in_m 167 | filters: 168 | dbt_deployments.deployment_completed_hour: 7 days 169 | dbt_deployments.models_deployed: ">=100" 170 | sorts: 171 | - dbt_deployments.deployment_completed_hour 172 | limit: 500 173 | query_timezone: America/New_York 174 | stacking: '' 175 | show_value_labels: false 176 | label_density: 25 177 | legend_position: center 178 | x_axis_gridlines: false 179 | y_axis_gridlines: true 180 | show_view_names: false 181 | limit_displayed_rows: false 182 | y_axis_combined: true 183 | show_y_axis_labels: true 184 | show_y_axis_ticks: true 185 | y_axis_tick_density: default 186 | y_axis_tick_density_custom: 5 187 | show_x_axis_label: true 188 | show_x_axis_ticks: true 189 | x_axis_scale: auto 190 | y_axis_scale_mode: linear 191 | x_axis_reversed: false 192 | y_axis_reversed: false 193 | show_null_points: true 194 | point_style: none 195 | interpolation: linear 196 | ordering: none 197 | show_null_labels: false 198 | show_totals_labels: false 199 | show_silhouette: false 200 | totals_color: "#808080" 201 | series_types: {} 202 | row: 0 203 | col: 0 204 | width: 12 205 | height: 7 206 | - title: dbt Most Recent Model Deployments 207 | name: dbt Most Recent Model Deployments 208 | model: dbt 209 | explore: dbt_deployments 210 | type: table 211 | fields: 212 | - dbt_model_deployments.model 213 | - dbt_model_deployments.most_recent_deployment_completed 214 | filters: 215 | dbt_model_deployments.model: "-NULL" 216 | dbt_model_deployments.deployment_completed_date: 7 days 217 | sorts: 218 | - most_recent_runtime_hours_ago desc 219 | limit: 500 220 | dynamic_fields: 221 | - table_calculation: most_recent_runtime_hours_ago 222 | label: Most Recent Runtime Hours Ago 223 | expression: diff_minutes(${dbt_model_deployments.most_recent_deployment_completed}, 224 | now()) / 60 225 | value_format: 226 | value_format_name: decimal_1 227 | _kind_hint: measure 228 | _type_hint: number 229 | query_timezone: America/New_York 230 | show_view_names: false 231 | show_row_numbers: true 232 | truncate_column_names: false 233 | hide_totals: false 234 | hide_row_totals: false 235 | table_theme: white 236 | limit_displayed_rows: false 237 | enable_conditional_formatting: false 238 | conditional_formatting_include_totals: false 239 | conditional_formatting_include_nulls: false 240 | stacking: '' 241 | show_value_labels: false 242 | label_density: 25 243 | legend_position: center 244 | x_axis_gridlines: false 245 | y_axis_gridlines: true 246 | y_axis_combined: true 247 | show_y_axis_labels: true 248 | show_y_axis_ticks: true 249 | y_axis_tick_density: default 250 | y_axis_tick_density_custom: 5 251 | show_x_axis_label: true 252 | show_x_axis_ticks: true 253 | x_axis_scale: auto 254 | y_axis_scale_mode: linear 255 | x_axis_reversed: false 256 | y_axis_reversed: false 257 | ordering: none 258 | show_null_labels: false 259 | show_totals_labels: false 260 | show_silhouette: false 261 | totals_color: "#808080" 262 | series_types: {} 263 | row: 16 264 | col: 0 265 | width: 13 266 | height: 21 267 | -------------------------------------------------------------------------------- /lookml/dbt_audit.model.lkml: -------------------------------------------------------------------------------- 1 | connection: "" 2 | 3 | include: "*.view.lkml" # include all views in this project 4 | include: "*.dashboard.lookml" # include all dashboards in this project 5 | 6 | explore: dbt_deployments { 7 | 8 | label: "dbt Deployments" 9 | 10 | join: dbt_model_deployments { 11 | sql_on: ${dbt_deployments.invocation_id} = ${dbt_model_deployments.invocation_id} ;; 12 | relationship: one_to_many 13 | type: left_outer 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lookml/dbt_audit_log.view.lkml: -------------------------------------------------------------------------------- 1 | view: dbt_audit_log { 2 | sql_table_name: ANALYTICS_META.STG_DBT_AUDIT_LOG ;; 3 | label: "dbt Audit Log" 4 | 5 | dimension: event_id { 6 | type: string 7 | sql: ${TABLE}.EVENT_ID ;; 8 | primary_key: yes 9 | hidden: yes 10 | } 11 | 12 | dimension: event_model { 13 | type: string 14 | sql: ${TABLE}.EVENT_MODEL ;; 15 | } 16 | 17 | dimension: event_name { 18 | type: string 19 | sql: ${TABLE}.EVENT_NAME ;; 20 | } 21 | 22 | dimension: event_schema { 23 | type: string 24 | sql: ${TABLE}.EVENT_SCHEMA ;; 25 | } 26 | 27 | dimension: event_user { 28 | type: string 29 | sql: ${TABLE}.EVENT_USER ;; 30 | } 31 | 32 | dimension: event_target { 33 | type: string 34 | sql: ${TABLE}.EVENT_TARGET ;; 35 | } 36 | 37 | dimension: event_is_full_refresh { 38 | type: boolean 39 | sql: ${TABLE}.EVENT_IS_FULL_REFRESH ;; 40 | } 41 | 42 | dimension_group: event_timestamp { 43 | type: time 44 | timeframes: [ 45 | month, 46 | week, 47 | date, 48 | hour, 49 | minute 50 | ] 51 | datatype: datetime 52 | convert_tz: yes 53 | sql: ${TABLE}.EVENT_TIMESTAMP ;; 54 | } 55 | 56 | dimension: invocation_id { 57 | type: string 58 | sql: ${TABLE}.INVOCATION_ID ;; 59 | } 60 | 61 | measure: count { 62 | type: count 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /lookml/dbt_deployments.view.lkml: -------------------------------------------------------------------------------- 1 | view: dbt_deployments { 2 | sql_table_name: ANALYTICS_META.STG_DBT_DEPLOYMENTS ;; 3 | label: "dbt Deployments" 4 | 5 | dimension: invocation_id { 6 | type: string 7 | sql: ${TABLE}.INVOCATION_ID ;; 8 | primary_key: yes 9 | hidden: yes 10 | } 11 | 12 | dimension_group: deployment_started { 13 | type: time 14 | timeframes: [ 15 | month, 16 | week, 17 | date, 18 | hour, 19 | minute, 20 | time, 21 | raw 22 | ] 23 | datatype: datetime 24 | convert_tz: yes 25 | sql: ${TABLE}.DEPLOYMENT_STARTED_AT ;; 26 | } 27 | 28 | dimension_group: deployment_completed { 29 | type: time 30 | timeframes: [ 31 | month, 32 | week, 33 | date, 34 | hour, 35 | minute, 36 | time, 37 | raw 38 | ] 39 | datatype: datetime 40 | convert_tz: yes 41 | sql: ${TABLE}.DEPLOYMENT_COMPLETED_AT ;; 42 | } 43 | 44 | dimension: models_deployed { 45 | type: number 46 | sql: ${TABLE}.models_deployed ;; 47 | } 48 | 49 | dimension: user { 50 | type: string 51 | sql: ${TABLE}.USER ;; 52 | } 53 | 54 | dimension: target { 55 | type: string 56 | sql: ${TABLE}.TARGET ;; 57 | } 58 | 59 | dimension: is_full_refresh { 60 | type: boolean 61 | sql: ${TABLE}.IS_FULL_REFRESH ;; 62 | } 63 | 64 | dimension: duration { 65 | type: duration 66 | dimension_group: dimension_group_name { 67 | sql_start: ${deployment_started_raw} ;; 68 | sql_end: ${deployment_completed_raw} ;; 69 | intervals: [second, minute] 70 | } 71 | 72 | measure: count { 73 | type: count 74 | } 75 | 76 | measure: average_duration_in_s { 77 | label: "Average Duration (seconds)" 78 | type: average 79 | sql: ${duration_in_s} ;; 80 | } 81 | 82 | measure: average_duration_in_m { 83 | label: "Average Duration (minutes)" 84 | type: average 85 | sql: ${duration_in_m} ;; 86 | value_format_name: decimal_2 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /lookml/dbt_model_deployments.view.lkml: -------------------------------------------------------------------------------- 1 | view: dbt_model_deployments { 2 | sql_table_name: ANALYTICS_META.STG_DBT_MODEL_DEPLOYMENTS ;; 3 | label: "dbt Model Deployments" 4 | 5 | dimension: model_deployment_id { 6 | type: string 7 | sql: ${TABLE}.MODEL_DEPLOYMENT_ID ;; 8 | primary_key: yes 9 | hidden: yes 10 | } 11 | 12 | dimension_group: deployment_started { 13 | type: time 14 | timeframes: [ 15 | month, 16 | week, 17 | date, 18 | hour, 19 | minute, 20 | time, 21 | raw 22 | ] 23 | datatype: datetime 24 | convert_tz: yes 25 | sql: ${TABLE}.DEPLOYMENT_STARTED_AT ;; 26 | } 27 | 28 | dimension_group: deployment_completed { 29 | type: time 30 | timeframes: [ 31 | month, 32 | week, 33 | date, 34 | hour, 35 | minute, 36 | time, 37 | raw 38 | ] 39 | datatype: datetime 40 | convert_tz: yes 41 | sql: ${TABLE}.DEPLOYMENT_COMPLETED_AT ;; 42 | } 43 | 44 | dimension: invocation_id { 45 | type: string 46 | sql: ${TABLE}.INVOCATION_ID ;; 47 | hidden: yes 48 | } 49 | 50 | dimension: model { 51 | type: string 52 | sql: ${TABLE}.MODEL ;; 53 | } 54 | 55 | dimension: schema { 56 | type: string 57 | sql: ${TABLE}.SCHEMA ;; 58 | } 59 | 60 | dimension: user { 61 | type: string 62 | sql: ${TABLE}.USER ;; 63 | } 64 | 65 | dimension: target { 66 | type: string 67 | sql: ${TABLE}.TARGET ;; 68 | } 69 | 70 | dimension: is_full_refresh { 71 | type: boolean 72 | sql: ${TABLE}.IS_FULL_REFRESH ;; 73 | } 74 | 75 | dimension: duration { 76 | type: duration 77 | dimension_group: dimension_group_name { 78 | sql_start: ${deployment_started_raw} ;; 79 | sql_end: SQL deployment_completed_raw ;; 80 | intervals: [second, minute] 81 | } 82 | 83 | measure: count { 84 | type: count 85 | } 86 | 87 | measure: average_duration_in_s { 88 | label: "Average Duration (seconds)" 89 | type: average 90 | sql: ${duration_in_s} ;; 91 | } 92 | 93 | measure: average_duration_in_m { 94 | label: "Average Duration (minutes)" 95 | type: average 96 | sql: ${duration_in_m} ;; 97 | value_format_name: decimal_2 98 | } 99 | 100 | measure: most_recent_deployment_completed { 101 | type: date_time 102 | sql: max(${deployment_completed_raw}) ;; 103 | convert_tz: no 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /macros/audit.sql: -------------------------------------------------------------------------------- 1 | {% macro get_audit_schema() %} 2 | 3 | {# if the get_audit_schema macro exists in the base project use that #} 4 | {% if context.get(project_name, {}).get('get_audit_schema') %} 5 | {{ return(context[project_name].get_audit_schema()) }} 6 | {% else %} 7 | {{ return(target.schema~'_meta') }} 8 | {% endif %} 9 | 10 | {% endmacro %} 11 | 12 | {% macro get_audit_database() %} 13 | 14 | {# if the get_audit_database macro exists in the base project use that #} 15 | {% if context.get(project_name, {}).get('get_audit_database') %} 16 | {{ return(context[project_name].get_audit_database()) }} 17 | {% else %} 18 | {{ return(target.database) }} 19 | {% endif %} 20 | 21 | {% endmacro %} 22 | 23 | {% macro get_audit_relation() %} 24 | 25 | {%- set audit_schema=logging.get_audit_schema() -%} 26 | {%- set audit_database=logging.get_audit_database() -%} 27 | 28 | {%- set audit_table = 29 | api.Relation.create( 30 | database=audit_database, 31 | schema=audit_schema, 32 | identifier='dbt_audit_log', 33 | type='table' 34 | ) -%} 35 | 36 | {{ return(audit_table) }} 37 | 38 | {% endmacro %} 39 | 40 | 41 | {% macro log_audit_event(event_name, schema, relation, user, target_name, is_full_refresh) -%} 42 | 43 | {{ return(adapter.dispatch('log_audit_event', 'logging')(event_name, schema, relation, user, target_name, is_full_refresh)) }} 44 | 45 | {% endmacro %} 46 | 47 | {% macro default__log_audit_event(event_name, schema, relation, user, target_name, is_full_refresh) %} 48 | 49 | insert into {{ logging.get_audit_relation() }} ( 50 | event_name, 51 | event_timestamp, 52 | event_schema, 53 | event_model, 54 | event_user, 55 | event_target, 56 | event_is_full_refresh, 57 | invocation_id 58 | ) 59 | 60 | values ( 61 | '{{ event_name }}', 62 | {{ dbt.current_timestamp_in_utc_backcompat() }}, 63 | {% if schema != None %}'{{ schema }}'{% else %}null::varchar(512){% endif %}, 64 | {% if relation != None %}'{{ relation }}'{% else %}null::varchar(512){% endif %}, 65 | {% if user != None %}'{{ user }}'{% else %}null::varchar(512){% endif %}, 66 | {% if target_name != None %}'{{ target_name }}'{% else %}null::varchar(512){% endif %}, 67 | {% if is_full_refresh %}TRUE{% else %}FALSE{% endif %}, 68 | '{{ invocation_id }}' 69 | ); 70 | 71 | commit; 72 | 73 | {% endmacro %} 74 | 75 | 76 | {% macro create_audit_schema() %} 77 | {%- set schema_name = logging.get_audit_schema() -%} 78 | {%- set database_name = logging.get_audit_database() -%} 79 | {%- set schema_exists = adapter.check_schema_exists(database=database_name, schema=schema_name) -%} 80 | {% if schema_exists == 0 %} 81 | {% do create_schema(api.Relation.create( 82 | database=database_name, 83 | schema=schema_name) 84 | ) %} 85 | {% endif %} 86 | {% endmacro %} 87 | 88 | 89 | {% macro create_audit_log_table() -%} 90 | 91 | {{ return(adapter.dispatch('create_audit_log_table', 'logging')()) }} 92 | 93 | {% endmacro %} 94 | 95 | 96 | {% macro default__create_audit_log_table() -%} 97 | 98 | {% set required_columns = [ 99 | ["event_name", dbt.type_string()], 100 | ["event_timestamp", dbt.type_timestamp()], 101 | ["event_schema", dbt.type_string()], 102 | ["event_model", dbt.type_string()], 103 | ["event_user", dbt.type_string()], 104 | ["event_target", dbt.type_string()], 105 | ["event_is_full_refresh", "boolean"], 106 | ["invocation_id", dbt.type_string()], 107 | ] -%} 108 | 109 | {% set audit_table = logging.get_audit_relation() -%} 110 | 111 | {% set audit_table_exists = adapter.get_relation(audit_table.database, audit_table.schema, audit_table.name) -%} 112 | 113 | 114 | {% if audit_table_exists -%} 115 | 116 | {%- set columns_to_create = [] -%} 117 | 118 | {# map to lower to cater for snowflake returning column names as upper case #} 119 | {%- set existing_columns = adapter.get_columns_in_relation(audit_table)|map(attribute='column')|map('lower')|list -%} 120 | 121 | {%- for required_column in required_columns -%} 122 | {%- if required_column[0] not in existing_columns -%} 123 | {%- do columns_to_create.append(required_column) -%} 124 | 125 | {%- endif -%} 126 | {%- endfor -%} 127 | 128 | 129 | {%- for column in columns_to_create -%} 130 | alter table {{ audit_table }} 131 | add column {{ column[0] }} {{ column[1] }} 132 | default null; 133 | {% endfor -%} 134 | 135 | {%- if columns_to_create|length > 0 %} 136 | commit; 137 | {% endif -%} 138 | 139 | {%- else -%} 140 | create table if not exists {{ audit_table }} 141 | ( 142 | {% for column in required_columns %} 143 | {{ column[0] }} {{ column[1] }}{% if not loop.last %},{% endif %} 144 | {% endfor %} 145 | ) 146 | {%- endif -%} 147 | 148 | {%- endmacro %} 149 | 150 | 151 | {% macro log_run_start_event() %} 152 | {{ logging.log_audit_event('run started', user=target.user, target_name=target.name, is_full_refresh=flags.FULL_REFRESH) }} 153 | {% endmacro %} 154 | 155 | 156 | {% macro log_run_end_event() %} 157 | {{ logging.log_audit_event('run completed', user=target.user, target_name=target.name, is_full_refresh=flags.FULL_REFRESH) }} 158 | {% endmacro %} 159 | 160 | 161 | {% macro log_model_start_event() %} 162 | {{ logging.log_audit_event( 163 | 'model deployment started', schema=this.schema, relation=this.name, user=target.user, target_name=target.name, is_full_refresh=flags.FULL_REFRESH 164 | ) }} 165 | {% endmacro %} 166 | 167 | 168 | {% macro log_model_end_event() %} 169 | {{ logging.log_audit_event( 170 | 'model deployment completed', schema=this.schema, relation=this.name, user=target.user, target_name=target.name, is_full_refresh=flags.FULL_REFRESH 171 | ) }} 172 | {% endmacro %} 173 | 174 | 175 | {% macro log_custom_event(event_name) %} 176 | {{ logging.log_audit_event( 177 | event_name, schema=this.schema, relation=this.name, user=target.user, target_name=target.name, is_full_refresh=flags.FULL_REFRESH 178 | ) }} 179 | {% endmacro %} 180 | -------------------------------------------------------------------------------- /macros/bigquery.sql: -------------------------------------------------------------------------------- 1 | {% macro bigquery__log_audit_event(event_name, schema, relation, user, target_name, is_full_refresh) %} 2 | 3 | insert into {{ logging.get_audit_relation() }} ( 4 | event_name, 5 | event_timestamp, 6 | event_schema, 7 | event_model, 8 | event_target, 9 | event_is_full_refresh, 10 | invocation_id 11 | ) 12 | 13 | values ( 14 | '{{ event_name }}', 15 | {{ dbt.current_timestamp_in_utc_backcompat() }}, 16 | {% if schema != None %}'{{ schema }}'{% else %}null{% endif %}, 17 | {% if relation != None %}'{{ relation }}'{% else %}null{% endif %}, 18 | {% if target_name != None %}'{{ target_name }}'{% else %}null{% endif %}, 19 | {% if is_full_refresh %}TRUE{% else %}FALSE{% endif %}, 20 | '{{ invocation_id }}' 21 | ); 22 | 23 | {% endmacro %} 24 | 25 | 26 | {% macro bigquery__create_audit_log_table() -%} 27 | 28 | {% set required_columns = [ 29 | ["event_name", dbt.type_string()], 30 | ["event_timestamp", dbt.type_timestamp()], 31 | ["event_schema", dbt.type_string()], 32 | ["event_model", dbt.type_string()], 33 | ["event_target", dbt.type_string()], 34 | ["event_is_full_refresh", "BOOLEAN"], 35 | ["invocation_id", dbt.type_string()], 36 | ] -%} 37 | 38 | {% set audit_table = logging.get_audit_relation() -%} 39 | 40 | {% set audit_table_exists = adapter.get_relation(audit_table.database, audit_table.schema, audit_table.name) -%} 41 | 42 | 43 | {% if audit_table_exists -%} 44 | 45 | {%- set columns_to_create = [] -%} 46 | 47 | {# map to lower to cater for snowflake returning column names as upper case #} 48 | {%- set existing_columns = adapter.get_columns_in_relation(audit_table)|map(attribute='column')|map('lower')|list -%} 49 | 50 | {%- for required_column in required_columns -%} 51 | {%- if required_column[0] not in existing_columns -%} 52 | {%- do columns_to_create.append(required_column) -%} 53 | 54 | {%- endif -%} 55 | {%- endfor -%} 56 | 57 | 58 | {%- for column in columns_to_create -%} 59 | alter table {{ audit_table }} 60 | add column {{ column[0] }} {{ column[1] }} 61 | default null; 62 | {% endfor -%} 63 | 64 | {%- else -%} 65 | create table if not exists {{ audit_table }} 66 | ( 67 | {% for column in required_columns %} 68 | {{ column[0] }} {{ column[1] }}{% if not loop.last %},{% endif %} 69 | {% endfor %} 70 | ) 71 | {%- endif -%} 72 | 73 | {%- endmacro %} -------------------------------------------------------------------------------- /models/bigquery/stg_dbt_deployments.sql: -------------------------------------------------------------------------------- 1 | with events as ( 2 | 3 | select * from {{ref('stg_dbt_audit_log')}} 4 | 5 | ), 6 | 7 | aggregated as ( 8 | 9 | select 10 | 11 | invocation_id, 12 | event_target as target, 13 | event_is_full_refresh as is_full_refresh, 14 | 15 | min(case 16 | when event_name = 'run started' then event_timestamp 17 | end) as deployment_started_at, 18 | 19 | min(case 20 | when event_name = 'run completed' then event_timestamp 21 | end) as deployment_completed_at, 22 | 23 | count(distinct case 24 | when event_name like '%model%' then event_model 25 | end) as models_deployed 26 | 27 | from events 28 | 29 | {{ dbt_utils.group_by(n=3) }} 30 | 31 | ) 32 | 33 | select * from aggregated 34 | -------------------------------------------------------------------------------- /models/bigquery/stg_dbt_model_deployments.sql: -------------------------------------------------------------------------------- 1 | with events as ( 2 | 3 | select * from {{ ref('stg_dbt_audit_log') }} 4 | 5 | ), 6 | 7 | aggregated as ( 8 | 9 | select 10 | 11 | {{ dbt_utils.generate_surrogate_key([ 12 | 'event_model', 13 | 'invocation_id' 14 | ]) }} as model_deployment_id, 15 | 16 | invocation_id, 17 | event_model as model, 18 | event_schema as schema, 19 | event_target as target, 20 | event_is_full_refresh as is_full_refresh, 21 | 22 | min(case 23 | when event_name = 'model deployment started' then event_timestamp 24 | end) as deployment_started_at, 25 | 26 | min(case 27 | when event_name = 'model deployment completed' then event_timestamp 28 | end) as deployment_completed_at 29 | 30 | from events 31 | 32 | where event_name like '%model%' 33 | 34 | {{ dbt_utils.group_by(n=6) }} 35 | 36 | ) 37 | 38 | select * from aggregated 39 | -------------------------------------------------------------------------------- /models/default/stg_dbt_deployments.sql: -------------------------------------------------------------------------------- 1 | with events as ( 2 | 3 | select * from {{ref('stg_dbt_audit_log')}} 4 | 5 | ), 6 | 7 | aggregated as ( 8 | 9 | select 10 | 11 | invocation_id, 12 | event_user as user, 13 | event_target as target, 14 | event_is_full_refresh as is_full_refresh, 15 | 16 | min(case 17 | when event_name = 'run started' then event_timestamp 18 | end) as deployment_started_at, 19 | 20 | min(case 21 | when event_name = 'run completed' then event_timestamp 22 | end) as deployment_completed_at, 23 | 24 | count(distinct case 25 | when event_name ilike '%model%' then event_model 26 | end) as models_deployed 27 | 28 | from events 29 | 30 | {{ dbt_utils.group_by(n=4) }} 31 | 32 | ) 33 | 34 | select * from aggregated 35 | -------------------------------------------------------------------------------- /models/default/stg_dbt_model_deployments.sql: -------------------------------------------------------------------------------- 1 | with events as ( 2 | 3 | select * from {{ ref('stg_dbt_audit_log') }} 4 | 5 | ), 6 | 7 | aggregated as ( 8 | 9 | select 10 | 11 | {{ dbt_utils.generate_surrogate_key([ 12 | 'event_model', 13 | 'invocation_id' 14 | ]) }} as model_deployment_id, 15 | 16 | invocation_id, 17 | event_model as model, 18 | event_schema as schema, 19 | event_user as user, 20 | event_target as target, 21 | event_is_full_refresh as is_full_refresh, 22 | 23 | min(case 24 | when event_name = 'model deployment started' then event_timestamp 25 | end) as deployment_started_at, 26 | 27 | min(case 28 | when event_name = 'model deployment completed' then event_timestamp 29 | end) as deployment_completed_at 30 | 31 | from events 32 | 33 | where event_name ilike '%model%' 34 | 35 | {{ dbt_utils.group_by(n=7) }} 36 | 37 | ) 38 | 39 | select * from aggregated 40 | -------------------------------------------------------------------------------- /models/stg_dbt_audit_log.sql: -------------------------------------------------------------------------------- 1 | with audit as ( 2 | 3 | select * from {{ get_audit_relation() }} 4 | 5 | ), 6 | 7 | with_id as ( 8 | 9 | select 10 | 11 | *, 12 | 13 | {{ dbt_utils.generate_surrogate_key([ 14 | 'event_name', 15 | 'event_model', 16 | 'invocation_id' 17 | ]) }} as event_id 18 | 19 | from audit 20 | 21 | ) 22 | 23 | select * from with_id 24 | -------------------------------------------------------------------------------- /packages.yml: -------------------------------------------------------------------------------- 1 | packages: 2 | - package: dbt-labs/dbt_utils 3 | version: [">=1.0.0-rc1", "<2.0.0"] 4 | --------------------------------------------------------------------------------