garf-executors 0.2.3__py3-none-any.whl → 1.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- garf/executors/__init__.py +25 -0
- garf/executors/api_executor.py +228 -0
- garf/executors/bq_executor.py +179 -0
- garf/executors/config.py +52 -0
- garf/executors/entrypoints/__init__.py +0 -0
- garf/executors/entrypoints/cli.py +164 -0
- {garf_executors → garf/executors}/entrypoints/grpc_server.py +22 -9
- garf/executors/entrypoints/server.py +174 -0
- garf/executors/entrypoints/tracer.py +82 -0
- garf/executors/entrypoints/utils.py +140 -0
- garf/executors/exceptions.py +17 -0
- garf/executors/execution_context.py +117 -0
- garf/executors/executor.py +124 -0
- garf/executors/fetchers.py +128 -0
- garf/executors/garf_pb2.py +51 -0
- {garf_executors → garf/executors}/garf_pb2_grpc.py +45 -2
- garf/executors/query_processor.py +79 -0
- garf/executors/setup.py +58 -0
- garf/executors/sql_executor.py +144 -0
- garf/executors/telemetry.py +20 -0
- garf/executors/workflows/__init__.py +0 -0
- garf/executors/workflows/gcp_workflow.yaml +49 -0
- garf/executors/workflows/workflow.py +164 -0
- garf/executors/workflows/workflow_runner.py +172 -0
- garf_executors/__init__.py +9 -44
- garf_executors/api_executor.py +9 -121
- garf_executors/bq_executor.py +9 -161
- garf_executors/config.py +9 -37
- garf_executors/entrypoints/__init__.py +25 -0
- garf_executors/entrypoints/cli.py +9 -148
- garf_executors/entrypoints/grcp_server.py +25 -0
- garf_executors/entrypoints/server.py +9 -102
- garf_executors/entrypoints/tracer.py +8 -40
- garf_executors/entrypoints/utils.py +9 -124
- garf_executors/exceptions.py +11 -3
- garf_executors/execution_context.py +9 -100
- garf_executors/executor.py +9 -108
- garf_executors/fetchers.py +9 -63
- garf_executors/sql_executor.py +9 -125
- garf_executors/telemetry.py +10 -5
- garf_executors/workflow.py +8 -79
- {garf_executors-0.2.3.dist-info → garf_executors-1.1.3.dist-info}/METADATA +18 -5
- garf_executors-1.1.3.dist-info/RECORD +46 -0
- {garf_executors-0.2.3.dist-info → garf_executors-1.1.3.dist-info}/WHEEL +1 -1
- garf_executors-1.1.3.dist-info/entry_points.txt +2 -0
- {garf_executors-0.2.3.dist-info → garf_executors-1.1.3.dist-info}/top_level.txt +1 -0
- garf_executors/garf_pb2.py +0 -45
- garf_executors-0.2.3.dist-info/RECORD +0 -24
- garf_executors-0.2.3.dist-info/entry_points.txt +0 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -11,130 +11,15 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
-
"""Module for various helpers for executing Garf as CLI tool."""
|
|
15
14
|
|
|
16
|
-
from __future__ import annotations
|
|
17
15
|
|
|
18
|
-
import
|
|
19
|
-
import logging
|
|
20
|
-
import sys
|
|
21
|
-
from collections.abc import Sequence
|
|
22
|
-
from typing import Any
|
|
16
|
+
import warnings
|
|
23
17
|
|
|
24
|
-
from
|
|
18
|
+
from garf.executors.entrypoints.utils import *
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
identifier: self._parse_params(identifier, params)
|
|
34
|
-
for identifier in self.identifiers
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
def _parse_params(self, identifier: str, params: Sequence[Any]) -> dict:
|
|
38
|
-
parsed_params = {}
|
|
39
|
-
if params:
|
|
40
|
-
raw_params = [param.split('=', maxsplit=1) for param in params]
|
|
41
|
-
for param in raw_params:
|
|
42
|
-
param_pair = self._identify_param_pair(identifier, param)
|
|
43
|
-
if param_pair:
|
|
44
|
-
parsed_params.update(param_pair)
|
|
45
|
-
return parsed_params
|
|
46
|
-
|
|
47
|
-
def _identify_param_pair(
|
|
48
|
-
self, identifier: str, param: Sequence[str]
|
|
49
|
-
) -> dict[str, Any] | None:
|
|
50
|
-
key = param[0]
|
|
51
|
-
if not identifier or identifier not in key:
|
|
52
|
-
return None
|
|
53
|
-
provided_identifier, *keys = key.split('.')
|
|
54
|
-
if not keys:
|
|
55
|
-
return None
|
|
56
|
-
if len(keys) > 1:
|
|
57
|
-
raise GarfParamsException(
|
|
58
|
-
f'{key} is invalid format,'
|
|
59
|
-
f'`--{identifier}.key=value` or `--{identifier}.key` '
|
|
60
|
-
'are the correct formats'
|
|
61
|
-
)
|
|
62
|
-
provided_identifier = provided_identifier.replace('--', '')
|
|
63
|
-
if provided_identifier not in self.identifiers:
|
|
64
|
-
supported_arguments = ', '.join(self.identifiers)
|
|
65
|
-
raise GarfParamsException(
|
|
66
|
-
f'CLI argument {provided_identifier} is not supported'
|
|
67
|
-
f', supported arguments {supported_arguments}'
|
|
68
|
-
)
|
|
69
|
-
if provided_identifier != identifier:
|
|
70
|
-
return None
|
|
71
|
-
key = keys[0].replace('-', '_')
|
|
72
|
-
if not key:
|
|
73
|
-
raise GarfParamsException(
|
|
74
|
-
f'{identifier} {key} is invalid,'
|
|
75
|
-
f'`--{identifier}.key=value` or `--{identifier}.key` '
|
|
76
|
-
'are the correct formats'
|
|
77
|
-
)
|
|
78
|
-
if len(param) == 2:
|
|
79
|
-
return {key: param[1]}
|
|
80
|
-
if len(param) == 1:
|
|
81
|
-
return {key: True}
|
|
82
|
-
raise GarfParamsException(
|
|
83
|
-
f'{identifier} {key} is invalid,'
|
|
84
|
-
f'`--{identifier}.key=value` or `--{identifier}.key` '
|
|
85
|
-
'are the correct formats'
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
class GarfParamsException(Exception):
|
|
90
|
-
"""Defines exception for incorrect parameters."""
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
class LoggerEnum(str, enum.Enum):
|
|
94
|
-
local = 'local'
|
|
95
|
-
rich = 'rich'
|
|
96
|
-
gcloud = 'gcloud'
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def init_logging(
|
|
100
|
-
loglevel: str = 'INFO',
|
|
101
|
-
logger_type: str | LoggerEnum = 'local',
|
|
102
|
-
name: str = __name__,
|
|
103
|
-
) -> logging.Logger:
|
|
104
|
-
loglevel = getattr(logging, loglevel)
|
|
105
|
-
if logger_type == 'rich':
|
|
106
|
-
logging.basicConfig(
|
|
107
|
-
format='%(message)s',
|
|
108
|
-
level=loglevel,
|
|
109
|
-
datefmt='%Y-%m-%d %H:%M:%S',
|
|
110
|
-
handlers=[
|
|
111
|
-
rich_logging.RichHandler(rich_tracebacks=True),
|
|
112
|
-
],
|
|
113
|
-
)
|
|
114
|
-
elif logger_type == 'gcloud':
|
|
115
|
-
try:
|
|
116
|
-
import google.cloud.logging as glogging
|
|
117
|
-
except ImportError as e:
|
|
118
|
-
raise ImportError(
|
|
119
|
-
'Please install garf-executors with Cloud logging support - '
|
|
120
|
-
'`pip install garf-executors[bq]`'
|
|
121
|
-
) from e
|
|
122
|
-
|
|
123
|
-
client = glogging.Client()
|
|
124
|
-
handler = glogging.handlers.CloudLoggingHandler(client, name=name)
|
|
125
|
-
handler.close()
|
|
126
|
-
glogging.handlers.setup_logging(handler, log_level=loglevel)
|
|
127
|
-
logging.basicConfig(
|
|
128
|
-
level=loglevel,
|
|
129
|
-
handlers=[handler],
|
|
130
|
-
)
|
|
131
|
-
else:
|
|
132
|
-
logging.basicConfig(
|
|
133
|
-
format='[%(asctime)s][%(name)s][%(levelname)s] %(message)s',
|
|
134
|
-
stream=sys.stdout,
|
|
135
|
-
level=loglevel,
|
|
136
|
-
datefmt='%Y-%m-%d %H:%M:%S',
|
|
137
|
-
)
|
|
138
|
-
logging.getLogger('smart_open.smart_open_lib').setLevel(logging.WARNING)
|
|
139
|
-
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
|
|
140
|
-
return logging.getLogger(name)
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/exceptions.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -13,5 +13,13 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
import warnings
|
|
17
|
+
|
|
18
|
+
from garf.executors.exceptions import *
|
|
19
|
+
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -12,105 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
# pylint: disable=C0330, g-bad-import-order, g-multiple-import
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
import warnings
|
|
18
17
|
|
|
19
|
-
from
|
|
18
|
+
from garf.executors.execution_context import *
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
from garf_core import query_editor
|
|
28
|
-
from garf_io import writer
|
|
29
|
-
from garf_io.writers import abs_writer
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class ExecutionContext(pydantic.BaseModel):
|
|
33
|
-
"""Common context for executing one or more queries.
|
|
34
|
-
|
|
35
|
-
Attributes:
|
|
36
|
-
query_parameters: Parameters to dynamically change query text.
|
|
37
|
-
fetcher_parameters: Parameters to specify fetching setup.
|
|
38
|
-
writer: Type of writer to use. Can be a single writer string or list of writers.
|
|
39
|
-
writer_parameters: Optional parameters to setup writer.
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
query_parameters: query_editor.GarfQueryParameters | None = pydantic.Field(
|
|
43
|
-
default_factory=dict
|
|
44
|
-
)
|
|
45
|
-
fetcher_parameters: dict[str, str | bool | int | list[str | int]] | None = (
|
|
46
|
-
pydantic.Field(default_factory=dict)
|
|
47
|
-
)
|
|
48
|
-
writer: str | list[str] | None = None
|
|
49
|
-
writer_parameters: dict[str, str] | None = pydantic.Field(
|
|
50
|
-
default_factory=dict
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
def model_post_init(self, __context__) -> None:
|
|
54
|
-
if self.fetcher_parameters is None:
|
|
55
|
-
self.fetcher_parameters = {}
|
|
56
|
-
if self.writer_parameters is None:
|
|
57
|
-
self.writer_parameters = {}
|
|
58
|
-
if not self.query_parameters:
|
|
59
|
-
self.query_parameters = query_editor.GarfQueryParameters()
|
|
60
|
-
|
|
61
|
-
@classmethod
|
|
62
|
-
def from_file(
|
|
63
|
-
cls, path: str | pathlib.Path | os.PathLike[str]
|
|
64
|
-
) -> ExecutionContext:
|
|
65
|
-
"""Builds context from local or remote yaml file."""
|
|
66
|
-
with smart_open.open(path, 'r', encoding='utf-8') as f:
|
|
67
|
-
data = yaml.safe_load(f)
|
|
68
|
-
return ExecutionContext(**data)
|
|
69
|
-
|
|
70
|
-
def save(self, path: str | pathlib.Path | os.PathLike[str]) -> str:
|
|
71
|
-
"""Saves context to local or remote yaml file."""
|
|
72
|
-
with smart_open.open(path, 'w', encoding='utf-8') as f:
|
|
73
|
-
yaml.dump(self.model_dump(), f, encoding='utf-8')
|
|
74
|
-
return f'ExecutionContext is saved to {str(path)}'
|
|
75
|
-
|
|
76
|
-
@property
|
|
77
|
-
def writer_client(self) -> abs_writer.AbsWriter:
|
|
78
|
-
"""Returns single writer client."""
|
|
79
|
-
if isinstance(self.writer, list) and len(self.writer) > 0:
|
|
80
|
-
writer_type = self.writer[0]
|
|
81
|
-
else:
|
|
82
|
-
writer_type = self.writer
|
|
83
|
-
|
|
84
|
-
writer_params = self.writer_parameters or {}
|
|
85
|
-
|
|
86
|
-
if not writer_type:
|
|
87
|
-
raise ValueError('No writer specified')
|
|
88
|
-
|
|
89
|
-
writer_client = writer.create_writer(writer_type, **writer_params)
|
|
90
|
-
if writer_type == 'bq':
|
|
91
|
-
_ = writer_client.create_or_get_dataset()
|
|
92
|
-
if writer_type == 'sheet':
|
|
93
|
-
writer_client.init_client()
|
|
94
|
-
return writer_client
|
|
95
|
-
|
|
96
|
-
@property
|
|
97
|
-
def writer_clients(self) -> list[abs_writer.AbsWriter]:
|
|
98
|
-
"""Returns list of writer clients."""
|
|
99
|
-
if not self.writer:
|
|
100
|
-
return []
|
|
101
|
-
|
|
102
|
-
# Convert single writer to list for uniform processing
|
|
103
|
-
writers_to_use = (
|
|
104
|
-
self.writer if isinstance(self.writer, list) else [self.writer]
|
|
105
|
-
)
|
|
106
|
-
writer_params = self.writer_parameters or {}
|
|
107
|
-
|
|
108
|
-
clients = []
|
|
109
|
-
for writer_type in writers_to_use:
|
|
110
|
-
writer_client = writer.create_writer(writer_type, **writer_params)
|
|
111
|
-
if writer_type == 'bq':
|
|
112
|
-
_ = writer_client.create_or_get_dataset()
|
|
113
|
-
if writer_type == 'sheet':
|
|
114
|
-
writer_client.init_client()
|
|
115
|
-
clients.append(writer_client)
|
|
116
|
-
return clients
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/executor.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -12,113 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
"""Defines common functionality between executors."""
|
|
16
15
|
|
|
17
|
-
import
|
|
18
|
-
import inspect
|
|
19
|
-
from typing import Optional
|
|
16
|
+
import warnings
|
|
20
17
|
|
|
21
|
-
from
|
|
22
|
-
from opentelemetry import trace
|
|
18
|
+
from garf.executors.executor import *
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def __init__(
|
|
32
|
-
self,
|
|
33
|
-
preprocessors: Optional[dict[str, report_fetcher.Processor]] = None,
|
|
34
|
-
postprocessors: Optional[dict[str, report_fetcher.Processor]] = None,
|
|
35
|
-
) -> None:
|
|
36
|
-
self.preprocessors = preprocessors or {}
|
|
37
|
-
self.postprocessors = postprocessors or {}
|
|
38
|
-
|
|
39
|
-
@tracer.start_as_current_span('api.execute_batch')
|
|
40
|
-
def execute_batch(
|
|
41
|
-
self,
|
|
42
|
-
batch: dict[str, str],
|
|
43
|
-
context: execution_context.ExecutionContext,
|
|
44
|
-
parallel_threshold: int = 10,
|
|
45
|
-
) -> list[str]:
|
|
46
|
-
"""Executes batch of queries for a common context.
|
|
47
|
-
|
|
48
|
-
If an executor has any pre/post processors, executes them first while
|
|
49
|
-
modifying the context.
|
|
50
|
-
|
|
51
|
-
Args:
|
|
52
|
-
batch: Mapping between query_title and its text.
|
|
53
|
-
context: Execution context.
|
|
54
|
-
parallel_threshold: Number of queries to execute in parallel.
|
|
55
|
-
|
|
56
|
-
Returns:
|
|
57
|
-
Results of execution.
|
|
58
|
-
"""
|
|
59
|
-
span = trace.get_current_span()
|
|
60
|
-
span.set_attribute('api.parallel_threshold', parallel_threshold)
|
|
61
|
-
_handle_processors(processors=self.preprocessors, context=context)
|
|
62
|
-
results = asyncio.run(
|
|
63
|
-
self._run(
|
|
64
|
-
batch=batch, context=context, parallel_threshold=parallel_threshold
|
|
65
|
-
)
|
|
66
|
-
)
|
|
67
|
-
_handle_processors(processors=self.postprocessors, context=context)
|
|
68
|
-
return results
|
|
69
|
-
|
|
70
|
-
def add_preprocessor(
|
|
71
|
-
self, preprocessors: dict[str, report_fetcher.Processor]
|
|
72
|
-
) -> None:
|
|
73
|
-
self.preprocessors.update(preprocessors)
|
|
74
|
-
|
|
75
|
-
async def aexecute(
|
|
76
|
-
self,
|
|
77
|
-
query: str,
|
|
78
|
-
title: str,
|
|
79
|
-
context: execution_context.ExecutionContext,
|
|
80
|
-
) -> str:
|
|
81
|
-
"""Performs query execution asynchronously.
|
|
82
|
-
|
|
83
|
-
Args:
|
|
84
|
-
query: Location of the query.
|
|
85
|
-
title: Name of the query.
|
|
86
|
-
context: Query execution context.
|
|
87
|
-
|
|
88
|
-
Returns:
|
|
89
|
-
Result of writing the report.
|
|
90
|
-
"""
|
|
91
|
-
return await asyncio.to_thread(self.execute, query, title, context)
|
|
92
|
-
|
|
93
|
-
async def _run(
|
|
94
|
-
self,
|
|
95
|
-
batch: dict[str, str],
|
|
96
|
-
context: execution_context.ExecutionContext,
|
|
97
|
-
parallel_threshold: int,
|
|
98
|
-
):
|
|
99
|
-
semaphore = asyncio.Semaphore(value=parallel_threshold)
|
|
100
|
-
|
|
101
|
-
async def run_with_semaphore(fn):
|
|
102
|
-
async with semaphore:
|
|
103
|
-
return await fn
|
|
104
|
-
|
|
105
|
-
tasks = [
|
|
106
|
-
self.aexecute(query=query, title=title, context=context)
|
|
107
|
-
for title, query in batch.items()
|
|
108
|
-
]
|
|
109
|
-
return await asyncio.gather(*(run_with_semaphore(task) for task in tasks))
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def _handle_processors(
|
|
113
|
-
processors: dict[str, report_fetcher.Processor],
|
|
114
|
-
context: execution_context.ExecutionContext,
|
|
115
|
-
) -> None:
|
|
116
|
-
for k, processor in processors.items():
|
|
117
|
-
processor_signature = list(inspect.signature(processor).parameters.keys())
|
|
118
|
-
if k in context.fetcher_parameters:
|
|
119
|
-
processor_parameters = {
|
|
120
|
-
k: v
|
|
121
|
-
for k, v in context.fetcher_parameters.items()
|
|
122
|
-
if k in processor_signature
|
|
123
|
-
}
|
|
124
|
-
context.fetcher_parameters[k] = processor(**processor_parameters)
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/fetchers.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -12,68 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
import inspect
|
|
16
|
-
import logging
|
|
17
|
-
import sys
|
|
18
|
-
from importlib.metadata import entry_points
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
import warnings
|
|
21
17
|
|
|
22
|
-
from
|
|
18
|
+
from garf.executors.fetchers import *
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if entrypoints := _get_entrypoints('garf'):
|
|
31
|
-
return {fetcher.name for fetcher in entrypoints}
|
|
32
|
-
return set()
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
@tracer.start_as_current_span('get_report_fetcher')
|
|
36
|
-
def get_report_fetcher(source: str) -> type[report_fetcher.ApiReportFetcher]:
|
|
37
|
-
"""Loads report fetcher for a given source.
|
|
38
|
-
|
|
39
|
-
Args:
|
|
40
|
-
source: Alias for a source associated with a fetcher.
|
|
41
|
-
|
|
42
|
-
Returns:
|
|
43
|
-
Class for a found report fetcher.
|
|
44
|
-
|
|
45
|
-
Raises:
|
|
46
|
-
ApiReportFetcherError: When fetcher cannot be loaded.
|
|
47
|
-
MissingApiReportFetcherError: When fetcher not found.
|
|
48
|
-
"""
|
|
49
|
-
if source not in find_fetchers():
|
|
50
|
-
raise report_fetcher.MissingApiReportFetcherError(source)
|
|
51
|
-
for fetcher in _get_entrypoints('garf'):
|
|
52
|
-
if fetcher.name == source:
|
|
53
|
-
try:
|
|
54
|
-
with tracer.start_as_current_span('load_fetcher_module') as span:
|
|
55
|
-
fetcher_module = fetcher.load()
|
|
56
|
-
span.set_attribute('loaded_module', fetcher_module.__name__)
|
|
57
|
-
for name, obj in inspect.getmembers(fetcher_module):
|
|
58
|
-
if inspect.isclass(obj) and issubclass(
|
|
59
|
-
obj, report_fetcher.ApiReportFetcher
|
|
60
|
-
):
|
|
61
|
-
return getattr(fetcher_module, name)
|
|
62
|
-
except ModuleNotFoundError as e:
|
|
63
|
-
raise report_fetcher.ApiReportFetcherError(
|
|
64
|
-
f'Failed to load fetcher for source {source}, reason: {e}'
|
|
65
|
-
)
|
|
66
|
-
raise report_fetcher.ApiReportFetcherError(
|
|
67
|
-
f'No fetcher available for the source "{source}"'
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def _get_entrypoints(group='garf'):
|
|
72
|
-
if sys.version_info.major == 3 and sys.version_info.minor == 9:
|
|
73
|
-
try:
|
|
74
|
-
fetchers = entry_points()[group]
|
|
75
|
-
except KeyError:
|
|
76
|
-
fetchers = []
|
|
77
|
-
else:
|
|
78
|
-
fetchers = entry_points(group=group)
|
|
79
|
-
return fetchers
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/sql_executor.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -11,131 +11,15 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
-
"""Defines mechanism for executing queries via SqlAlchemy."""
|
|
15
14
|
|
|
16
|
-
from __future__ import annotations
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
import sqlalchemy
|
|
20
|
-
except ImportError as e:
|
|
21
|
-
raise ImportError(
|
|
22
|
-
'Please install garf-executors with sqlalchemy support '
|
|
23
|
-
'- `pip install garf-executors[sqlalchemy]`'
|
|
24
|
-
) from e
|
|
16
|
+
import warnings
|
|
25
17
|
|
|
26
|
-
import
|
|
27
|
-
import re
|
|
28
|
-
import uuid
|
|
18
|
+
from garf.executors.sql_executor import *
|
|
29
19
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
logger = logging.getLogger(__name__)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
class SqlAlchemyQueryExecutorError(exceptions.GarfExecutorError):
|
|
41
|
-
"""Error when SqlAlchemyQueryExecutor fails to run query."""
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class SqlAlchemyQueryExecutor(
|
|
45
|
-
executor.Executor, query_editor.TemplateProcessorMixin
|
|
46
|
-
):
|
|
47
|
-
"""Handles query execution via SqlAlchemy.
|
|
48
|
-
|
|
49
|
-
Attributes:
|
|
50
|
-
engine: Initialized Engine object to operated on a given database.
|
|
51
|
-
"""
|
|
52
|
-
|
|
53
|
-
def __init__(self, engine: sqlalchemy.engine.base.Engine) -> None:
|
|
54
|
-
"""Initializes executor with a given engine.
|
|
55
|
-
|
|
56
|
-
Args:
|
|
57
|
-
engine: Initialized Engine object to operated on a given database.
|
|
58
|
-
"""
|
|
59
|
-
self.engine = engine
|
|
60
|
-
super().__init__()
|
|
61
|
-
|
|
62
|
-
@classmethod
|
|
63
|
-
def from_connection_string(
|
|
64
|
-
cls, connection_string: str
|
|
65
|
-
) -> SqlAlchemyQueryExecutor:
|
|
66
|
-
"""Creates executor from SqlAlchemy connection string.
|
|
67
|
-
|
|
68
|
-
https://docs.sqlalchemy.org/en/20/core/engines.html
|
|
69
|
-
"""
|
|
70
|
-
engine = sqlalchemy.create_engine(connection_string)
|
|
71
|
-
return cls(engine)
|
|
72
|
-
|
|
73
|
-
@tracer.start_as_current_span('sql.execute')
|
|
74
|
-
def execute(
|
|
75
|
-
self,
|
|
76
|
-
query: str,
|
|
77
|
-
title: str,
|
|
78
|
-
context: execution_context.ExecutionContext = (
|
|
79
|
-
execution_context.ExecutionContext()
|
|
80
|
-
),
|
|
81
|
-
) -> report.GarfReport:
|
|
82
|
-
"""Executes query in a given database via SqlAlchemy.
|
|
83
|
-
|
|
84
|
-
Args:
|
|
85
|
-
query: Location of the query.
|
|
86
|
-
title: Name of the query.
|
|
87
|
-
context: Query execution context.
|
|
88
|
-
|
|
89
|
-
Returns:
|
|
90
|
-
Report with data if query returns some data otherwise empty Report.
|
|
91
|
-
"""
|
|
92
|
-
span = trace.get_current_span()
|
|
93
|
-
logger.info('Executing script: %s', title)
|
|
94
|
-
query_text = self.replace_params_template(query, context.query_parameters)
|
|
95
|
-
with self.engine.begin() as conn:
|
|
96
|
-
if re.findall(r'(create|update) ', query_text.lower()):
|
|
97
|
-
try:
|
|
98
|
-
conn.connection.executescript(query_text)
|
|
99
|
-
results = report.GarfReport()
|
|
100
|
-
except Exception as e:
|
|
101
|
-
raise SqlAlchemyQueryExecutorError(
|
|
102
|
-
f'Failed to execute query {title}: Reason: {e}'
|
|
103
|
-
) from e
|
|
104
|
-
else:
|
|
105
|
-
temp_table_name = f'temp_{uuid.uuid4().hex}'
|
|
106
|
-
query_text = f'CREATE TABLE {temp_table_name} AS {query_text}'
|
|
107
|
-
conn.connection.executescript(query_text)
|
|
108
|
-
try:
|
|
109
|
-
results = report.GarfReport.from_pandas(
|
|
110
|
-
pd.read_sql(f'SELECT * FROM {temp_table_name}', conn)
|
|
111
|
-
)
|
|
112
|
-
except Exception as e:
|
|
113
|
-
raise SqlAlchemyQueryExecutorError(
|
|
114
|
-
f'Failed to execute query {title}: Reason: {e}'
|
|
115
|
-
) from e
|
|
116
|
-
finally:
|
|
117
|
-
conn.connection.execute(f'DROP TABLE {temp_table_name}')
|
|
118
|
-
if context.writer and results:
|
|
119
|
-
writer_clients = context.writer_clients
|
|
120
|
-
if not writer_clients:
|
|
121
|
-
logger.warning('No writers configured, skipping write operation')
|
|
122
|
-
else:
|
|
123
|
-
writing_results = []
|
|
124
|
-
for writer_client in writer_clients:
|
|
125
|
-
logger.debug(
|
|
126
|
-
'Start writing data for query %s via %s writer',
|
|
127
|
-
title,
|
|
128
|
-
type(writer_client),
|
|
129
|
-
)
|
|
130
|
-
writing_result = writer_client.write(results, title)
|
|
131
|
-
logger.debug(
|
|
132
|
-
'Finish writing data for query %s via %s writer',
|
|
133
|
-
title,
|
|
134
|
-
type(writer_client),
|
|
135
|
-
)
|
|
136
|
-
writing_results.append(writing_result)
|
|
137
|
-
logger.info('%s executed successfully', title)
|
|
138
|
-
# Return the last writer's result for backward compatibility
|
|
139
|
-
return writing_results[-1] if writing_results else None
|
|
140
|
-
span.set_attribute('execute.num_results', len(results))
|
|
141
|
-
return results
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/telemetry.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
2
|
#
|
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
# you may not use this file except in compliance with the License.
|
|
@@ -12,9 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
# pylint: disable=C0330, g-bad-import-order, g-multiple-import
|
|
16
|
-
from opentelemetry import trace
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
import warnings
|
|
17
|
+
|
|
18
|
+
from garf.executors.telemetry import *
|
|
19
|
+
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
20
25
|
)
|