Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add invocation_id to artifact metadata #2784

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## dbt 0.19.0 (Release TBD)

### Breaking changes
- The format for sources.json, run-results.json, manifest.json, and catalog.json has changed to include a common metadata field ([#2761](https:/fishtown-analytics/dbt/issues/2761), [#2778](https:/fishtown-analytics/dbt/pull/2778))
- The format for sources.json, run-results.json, manifest.json, and catalog.json has changed to include a common metadata field ([#2761](https:/fishtown-analytics/dbt/issues/2761), [#2778](https:/fishtown-analytics/dbt/pull/2778), [#2763](https:/fishtown-analytics/dbt/issues/2763), [#2784](https:/fishtown-analytics/dbt/pull/2784))

### Features
- dbt will compare configurations using the un-rendered form of the config block in dbt_project.yml ([#2713](https:/fishtown-analytics/dbt/issues/2713), [#2735](https:/fishtown-analytics/dbt/pull/2735))
Expand Down
6 changes: 5 additions & 1 deletion core/dbt/contracts/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses
from datetime import datetime
from typing import (
List, Tuple, ClassVar, Type, TypeVar, Dict, Any
List, Tuple, ClassVar, Type, TypeVar, Dict, Any, Optional
)

from dbt.clients.system import write_json, read_json
Expand All @@ -11,6 +11,7 @@
RuntimeException,
)
from dbt.version import __version__
from dbt.tracking import get_invocation_id
from hologram import JsonSchemaMixin

MacroKey = Tuple[str, str]
Expand Down Expand Up @@ -129,6 +130,9 @@ class BaseArtifactMetadata(JsonSchemaMixin):
generated_at: datetime = dataclasses.field(
default_factory=datetime.utcnow
)
invocation_id: Optional[str] = dataclasses.field(
default_factory=get_invocation_id
)


def schema_version(name: str, version: int):
Expand Down
7 changes: 7 additions & 0 deletions core/dbt/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ def initialize_tracking(cookie_dir):
active_user = User(None)


def get_invocation_id() -> Optional[str]:
if active_user is None:
return None
else:
return active_user.invocation_id


class InvocationProcessor(logbook.Processor):
def __init__(self):
super().__init__()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import shutil
import tempfile
import time
import uuid
from datetime import datetime
from unittest.mock import ANY, patch

from pytest import mark
from test.integration.base import DBTIntegrationTest, use_profile, AnyFloat, \
AnyString, AnyStringWith, normalize, Normalized

import dbt.tracking
import dbt.version
from dbt.exceptions import CompilationException

Expand Down Expand Up @@ -508,8 +510,6 @@ def expected_postgres_references_catalog(self):
},
}
return {
'dbt_schema_version': 'https://schemas.getdbt.com/dbt/catalog/v1.json',
'dbt_version': dbt.version.__version__,
'nodes': {
'seed.test.seed': {
'unique_id': 'seed.test.seed',
Expand Down Expand Up @@ -689,8 +689,6 @@ def expected_bigquery_complex_catalog(self):
}

return {
'dbt_schema_version': 'https://schemas.getdbt.com/dbt/catalog/v1.json',
'dbt_version': dbt.version.__version__,
'nodes': {
'model.test.clustered': {
'unique_id': 'model.test.clustered',
Expand Down Expand Up @@ -776,8 +774,6 @@ def expected_redshift_incremental_catalog(self):
my_schema_name = self.unique_schema()
role = self.get_role()
return {
'dbt_schema_version': 'https://schemas.getdbt.com/dbt/catalog/v1.json',
'dbt_version': dbt.version.__version__,
'nodes': {
'model.test.model': {
'unique_id': 'model.test.model',
Expand Down Expand Up @@ -2566,6 +2562,7 @@ def verify_metadata(self, metadata, dbt_schema_version):
assert metadata['dbt_version'] == dbt.version.__version__
assert 'dbt_schema_version' in metadata
assert metadata['dbt_schema_version'] == dbt_schema_version
assert metadata['invocation_id'] == dbt.tracking.active_user.invocation_id

def verify_manifest(self, expected_manifest):
self.assertTrue(os.path.exists('./target/manifest.json'))
Expand Down
2 changes: 2 additions & 0 deletions test/integration/042_sources_test/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml

from dbt.exceptions import CompilationException
import dbt.tracking
import dbt.version
from test.integration.base import DBTIntegrationTest, use_profile, AnyFloat, \
AnyStringWith
Expand Down Expand Up @@ -242,6 +243,7 @@ def _assert_freshness_results(self, path, state):
self.freshness_start_time)
assert data['metadata']['dbt_schema_version'] == 'https://schemas.getdbt.com/dbt/sources/v1.json'
assert data['metadata']['dbt_version'] == dbt.version.__version__
assert data['metadata']['invocation_id'] == dbt.tracking.active_user.invocation_id


last_inserted_time = self.last_inserted_time
Expand Down
10 changes: 9 additions & 1 deletion test/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def test__no_nodes(self):
'generated_at': '2018-02-14T09:15:13Z',
'dbt_schema_version': 'https://schemas.getdbt.com/dbt/manifest/v1.json',
'dbt_version': dbt.version.__version__,
# invocation_id is None, so it will not be present
},
'docs': {},
'disabled': [],
Expand Down Expand Up @@ -322,6 +323,7 @@ def test__build_flat_graph(self):
@mock.patch.object(tracking, 'active_user')
def test_metadata(self, mock_user):
mock_user.id = 'cfc9500f-dc7f-4c83-9ea7-2c581c1b38cf'
mock_user.invocation_id = '01234567-0123-0123-0123-0123456789ab'
mock_user.do_not_track = True
now = datetime.utcnow()
self.assertEqual(
Expand All @@ -336,13 +338,15 @@ def test_metadata(self, mock_user):
send_anonymous_usage_stats=False,
adapter_type='postgres',
generated_at=now,
invocation_id='01234567-0123-0123-0123-0123456789ab',
)
)

@mock.patch.object(tracking, 'active_user')
@freezegun.freeze_time('2018-02-14T09:15:13Z')
def test_no_nodes_with_metadata(self, mock_user):
mock_user.id = 'cfc9500f-dc7f-4c83-9ea7-2c581c1b38cf'
mock_user.invocation_id = '01234567-0123-0123-0123-0123456789ab'
mock_user.do_not_track = True
metadata = ManifestMetadata(
project_id='098f6bcd4621d373cade4e832627b4f6',
Expand Down Expand Up @@ -371,6 +375,7 @@ def test_no_nodes_with_metadata(self, mock_user):
'user_id': 'cfc9500f-dc7f-4c83-9ea7-2c581c1b38cf',
'send_anonymous_usage_stats': False,
'adapter_type': 'postgres',
'invocation_id': '01234567-0123-0123-0123-0123456789ab',
},
'disabled': [],
}
Expand Down Expand Up @@ -584,9 +589,10 @@ def setUp(self):

@freezegun.freeze_time('2018-02-14T09:15:13Z')
def test__no_nodes(self):
metadata = ManifestMetadata(generated_at=datetime.utcnow(), invocation_id='01234567-0123-0123-0123-0123456789ab')
manifest = Manifest(nodes={}, sources={}, macros={}, docs={},
disabled=[], files={}, reports={},
metadata=ManifestMetadata(generated_at=datetime.utcnow()))
metadata=metadata)
self.assertEqual(
manifest.writable_manifest().to_dict(),
{
Expand All @@ -600,6 +606,7 @@ def test__no_nodes(self):
'generated_at': '2018-02-14T09:15:13Z',
'dbt_schema_version': 'https://schemas.getdbt.com/dbt/manifest/v1.json',
'dbt_version': dbt.version.__version__,
'invocation_id': '01234567-0123-0123-0123-0123456789ab',
},
'docs': {},
'disabled': [],
Expand Down Expand Up @@ -696,6 +703,7 @@ class TestManifestSearch(unittest.TestCase):
_macros = []
_models = []
_docs = []

@property
def macros(self):
return self._macros
Expand Down