-
Notifications
You must be signed in to change notification settings - Fork 1.8k
DBT model instrumentation #11268
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
DBT model instrumentation #11268
Changes from all commits
75ab71e
80a9602
cb2405a
de4582e
e444f33
b61494e
f0e0320
7b7808a
c1954ab
d16e8a9
fcbf71f
8f21d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
from pathlib import Path | ||
from typing import AbstractSet, Dict, Iterable, List, Optional, Set, Tuple, Type, Union | ||
|
||
from opentelemetry import context, trace | ||
from opentelemetry.trace import Link, SpanContext, StatusCode | ||
|
||
import dbt.exceptions | ||
import dbt.tracking | ||
import dbt.utils | ||
|
@@ -91,6 +94,8 @@ def __init__(self, args: Flags, config: RuntimeConfig, manifest: Manifest) -> No | |
self.previous_defer_state: Optional[PreviousState] = None | ||
self.run_count: int = 0 | ||
self.started_at: float = 0 | ||
self._node_span_context_mapping: Dict[str, SpanContext] = {} | ||
self._dbt_tracer = trace.get_tracer("dbt.runner") | ||
|
||
if self.args.state: | ||
self.previous_state = PreviousState( | ||
|
@@ -222,14 +227,28 @@ def get_runner(self, node) -> BaseRunner: | |
|
||
return cls(self.config, adapter, node, run_count, num_nodes) | ||
|
||
def call_runner(self, runner: BaseRunner) -> RunResult: | ||
with log_contextvars(node_info=runner.node.node_info): | ||
def call_runner(self, runner: BaseRunner, parent_context=None) -> RunResult: | ||
node_info = runner.node.node_info | ||
links = [] | ||
if hasattr(runner.node.depends_on, "nodes"): | ||
for parent_node in runner.node.depends_on.nodes: | ||
if parent_node in self._node_span_context_mapping: | ||
links.append( | ||
Link( | ||
self._node_span_context_mapping[parent_node], | ||
{"upstream.name": parent_node}, | ||
), | ||
) | ||
with log_contextvars(node_info=node_info), self._dbt_tracer.start_as_current_span( | ||
node_info["unique_id"], context=parent_context, links=links | ||
) as node_span: | ||
self._node_span_context_mapping[node_info["unique_id"]] = node_span.get_span_context() | ||
runner.node.update_event_status( | ||
started_at=datetime.utcnow().isoformat(), node_status=RunningStatus.Started | ||
) | ||
fire_event( | ||
NodeStart( | ||
node_info=runner.node.node_info, | ||
node_info=node_info, | ||
) | ||
) | ||
try: | ||
|
@@ -242,10 +261,16 @@ def call_runner(self, runner: BaseRunner) -> RunResult: | |
result = None | ||
thread_exception = e | ||
finally: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Java I know that the resource (in try-with-resource pattern) is closed before calling finally (would be the span in this case). Is this the case in Python as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this try-catch-finally is different inner scope within with block correct? |
||
if result.status in (NodeStatus.Error, NodeStatus.Fail, NodeStatus.PartialSuccess): | ||
node_span.set_status(StatusCode.ERROR) | ||
node_span.set_attribute("node.status", result.status.value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this as the description for the status? |
||
node_span.set_attribute("node.materialization", node_info["materialized"]) | ||
node_span.set_attribute("node.database", node_info["node_relation"]["database"]) | ||
node_span.set_attribute("node.schema", node_info["node_relation"]["schema"]) | ||
if result is not None: | ||
fire_event( | ||
NodeFinished( | ||
node_info=runner.node.node_info, | ||
node_info=node_info, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to refer some attributes in node_info at different place and using full name seems lengthy and difficult to read hence shortened it. |
||
run_result=result.to_msg_dict(), | ||
) | ||
) | ||
|
@@ -256,7 +281,7 @@ def call_runner(self, runner: BaseRunner) -> RunResult: | |
GenericExceptionOnRun( | ||
unique_id=runner.node.unique_id, | ||
exc=str(thread_exception), | ||
node_info=runner.node.node_info, | ||
node_info=node_info, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
) | ||
) | ||
|
||
|
@@ -304,6 +329,7 @@ def _submit(self, pool, args, callback): | |
|
||
This does still go through the callback path for result collection. | ||
""" | ||
args.append(context.get_current()) | ||
if self.config.args.single_threaded: | ||
callback(self.call_runner(*args)) | ||
else: | ||
|
@@ -501,7 +527,8 @@ def populate_adapter_cache( | |
def before_run(self, adapter: BaseAdapter, selected_uids: AbstractSet[str]) -> RunStatus: | ||
with adapter.connection_named("master"): | ||
self.defer_to_manifest() | ||
self.populate_adapter_cache(adapter) | ||
with self._dbt_tracer.start_as_current_span("metadata.setup"): | ||
self.populate_adapter_cache(adapter) | ||
return RunStatus.Success | ||
|
||
def after_run(self, adapter, results) -> None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
git+https://github.com/dbt-labs/dbt-adapters.git@main#subdirectory=dbt-adapters | ||
git+https://github.com/sfc-gh-vguttha/dbt-adapters.git@vguttha-add-telemetry#subdirectory=dbt-adapters | ||
git+https://github.com/dbt-labs/dbt-adapters.git@main#subdirectory=dbt-tests-adapter | ||
git+https://github.com/dbt-labs/dbt-common.git@main | ||
git+https://github.com/dbt-labs/dbt-adapters.git@main#subdirectory=dbt-postgres | ||
git+https://github.com/sfc-gh-vguttha/dbt-adapters.git@vguttha-add-telemetry#subdirectory=dbt-snowflake | ||
# black must match what's in .pre-commit-config.yaml to be sure local env matches CI | ||
Comment on lines
+1
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: will revert these changes once I am fully complete with my testing. |
||
black==24.3.0 | ||
bumpversion | ||
|
@@ -38,3 +39,5 @@ types-pytz | |
types-requests | ||
types-setuptools | ||
mocker | ||
opentelemetry-api | ||
opentelemetry-sdk |
Uh oh!
There was an error while loading. Please reload this page.