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
garf_executors/bq_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,167 +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
|
-
"""Executes queries in BigQuery."""
|
|
15
14
|
|
|
16
|
-
from __future__ import annotations
|
|
17
15
|
|
|
18
|
-
import
|
|
19
|
-
import os
|
|
16
|
+
import warnings
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
from google.cloud import bigquery # type: ignore
|
|
23
|
-
except ImportError as e:
|
|
24
|
-
raise ImportError(
|
|
25
|
-
'Please install garf-executors with BigQuery support '
|
|
26
|
-
'- `pip install garf-executors[bq]`'
|
|
27
|
-
) from e
|
|
18
|
+
from garf.executors.bq_executor import *
|
|
28
19
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
from garf_executors import exceptions, execution_context, executor
|
|
36
|
-
from garf_executors.telemetry import tracer
|
|
37
|
-
|
|
38
|
-
logger = logging.getLogger(__name__)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
class BigQueryExecutorError(exceptions.GarfExecutorError):
|
|
42
|
-
"""Error when BigQueryExecutor fails to run query."""
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
class BigQueryExecutor(executor.Executor, query_editor.TemplateProcessorMixin):
|
|
46
|
-
"""Handles query execution in BigQuery.
|
|
47
|
-
|
|
48
|
-
Attributes:
|
|
49
|
-
project_id: Google Cloud project id.
|
|
50
|
-
location: BigQuery dataset location.
|
|
51
|
-
client: BigQuery client.
|
|
52
|
-
"""
|
|
53
|
-
|
|
54
|
-
def __init__(
|
|
55
|
-
self,
|
|
56
|
-
project_id: str | None = os.getenv('GOOGLE_CLOUD_PROJECT'),
|
|
57
|
-
location: str | None = None,
|
|
58
|
-
) -> None:
|
|
59
|
-
"""Initializes BigQueryExecutor.
|
|
60
|
-
|
|
61
|
-
Args:
|
|
62
|
-
project_id: Google Cloud project id.
|
|
63
|
-
location: BigQuery dataset location.
|
|
64
|
-
"""
|
|
65
|
-
if not project_id:
|
|
66
|
-
raise BigQueryExecutorError(
|
|
67
|
-
'project_id is required. Either provide it as project_id parameter '
|
|
68
|
-
'or GOOGLE_CLOUD_PROJECT env variable.'
|
|
69
|
-
)
|
|
70
|
-
self.project_id = project_id
|
|
71
|
-
self.location = location
|
|
72
|
-
super().__init__()
|
|
73
|
-
|
|
74
|
-
@property
|
|
75
|
-
def client(self) -> bigquery.Client:
|
|
76
|
-
"""Instantiates bigquery client."""
|
|
77
|
-
return bigquery.Client(self.project_id)
|
|
78
|
-
|
|
79
|
-
@tracer.start_as_current_span('bq.execute')
|
|
80
|
-
def execute(
|
|
81
|
-
self,
|
|
82
|
-
query: str,
|
|
83
|
-
title: str,
|
|
84
|
-
context: execution_context.ExecutionContext = (
|
|
85
|
-
execution_context.ExecutionContext()
|
|
86
|
-
),
|
|
87
|
-
) -> report.GarfReport:
|
|
88
|
-
"""Executes query in BigQuery.
|
|
89
|
-
|
|
90
|
-
Args:
|
|
91
|
-
query: Location of the query.
|
|
92
|
-
title: Name of the query.
|
|
93
|
-
context: Query execution context.
|
|
94
|
-
|
|
95
|
-
Returns:
|
|
96
|
-
Report with data if query returns some data otherwise empty Report.
|
|
97
|
-
"""
|
|
98
|
-
span = trace.get_current_span()
|
|
99
|
-
logger.info('Executing script: %s', title)
|
|
100
|
-
query_text = self.replace_params_template(query, context.query_parameters)
|
|
101
|
-
self.create_datasets(context.query_parameters.macro)
|
|
102
|
-
job = self.client.query(query_text)
|
|
103
|
-
try:
|
|
104
|
-
result = job.result()
|
|
105
|
-
except google_cloud_exceptions.GoogleCloudError as e:
|
|
106
|
-
raise BigQueryExecutorError(
|
|
107
|
-
f'Failed to execute query {title}: Reason: {e}'
|
|
108
|
-
) from e
|
|
109
|
-
logger.debug('%s launched successfully', title)
|
|
110
|
-
if result.total_rows:
|
|
111
|
-
results = report.GarfReport.from_pandas(result.to_dataframe())
|
|
112
|
-
else:
|
|
113
|
-
results = report.GarfReport()
|
|
114
|
-
if context.writer and results:
|
|
115
|
-
writer_clients = context.writer_clients
|
|
116
|
-
if not writer_clients:
|
|
117
|
-
logger.warning('No writers configured, skipping write operation')
|
|
118
|
-
else:
|
|
119
|
-
writing_results = []
|
|
120
|
-
for writer_client in writer_clients:
|
|
121
|
-
logger.debug(
|
|
122
|
-
'Start writing data for query %s via %s writer',
|
|
123
|
-
title,
|
|
124
|
-
type(writer_client),
|
|
125
|
-
)
|
|
126
|
-
writing_result = writer_client.write(results, title)
|
|
127
|
-
logger.debug(
|
|
128
|
-
'Finish writing data for query %s via %s writer',
|
|
129
|
-
title,
|
|
130
|
-
type(writer_client),
|
|
131
|
-
)
|
|
132
|
-
writing_results.append(writing_result)
|
|
133
|
-
# Return the last writer's result for backward compatibility
|
|
134
|
-
logger.info('%s executed successfully', title)
|
|
135
|
-
return writing_results[-1] if writing_results else None
|
|
136
|
-
logger.info('%s executed successfully', title)
|
|
137
|
-
span.set_attribute('execute.num_results', len(results))
|
|
138
|
-
return results
|
|
139
|
-
|
|
140
|
-
@tracer.start_as_current_span('bq.create_datasets')
|
|
141
|
-
def create_datasets(self, macros: dict | None) -> None:
|
|
142
|
-
"""Creates datasets in BQ based on values in a dict.
|
|
143
|
-
|
|
144
|
-
If dict contains keys with 'dataset' in them, then values for such keys
|
|
145
|
-
are treated as dataset names.
|
|
146
|
-
|
|
147
|
-
Args:
|
|
148
|
-
macros: Mapping containing data for query execution.
|
|
149
|
-
"""
|
|
150
|
-
if macros and (datasets := extract_datasets(macros)):
|
|
151
|
-
for dataset in datasets:
|
|
152
|
-
dataset_id = f'{self.project_id}.{dataset}'
|
|
153
|
-
try:
|
|
154
|
-
self.client.get_dataset(dataset_id)
|
|
155
|
-
except google_cloud_exceptions.NotFound:
|
|
156
|
-
bq_dataset = bigquery.Dataset(dataset_id)
|
|
157
|
-
bq_dataset.location = self.location
|
|
158
|
-
with contextlib.suppress(google_cloud_exceptions.Conflict):
|
|
159
|
-
self.client.create_dataset(bq_dataset, timeout=30)
|
|
160
|
-
logger.info('Created new dataset %s', dataset_id)
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
def extract_datasets(macros: dict | None) -> list[str]:
|
|
164
|
-
"""Finds dataset-related keys based on values in a dict.
|
|
165
|
-
|
|
166
|
-
If dict contains keys with 'dataset' in them, then values for such keys
|
|
167
|
-
are treated as dataset names.
|
|
168
|
-
|
|
169
|
-
Args:
|
|
170
|
-
macros: Mapping containing data for query execution.
|
|
171
|
-
|
|
172
|
-
Returns:
|
|
173
|
-
Possible names of datasets.
|
|
174
|
-
"""
|
|
175
|
-
if not macros:
|
|
176
|
-
return []
|
|
177
|
-
return [value for macro, value in macros.items() if 'dataset' in macro]
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
garf_executors/config.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,42 +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.config import *
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
from garf_executors.execution_context import ExecutionContext
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class Config(pydantic.BaseModel):
|
|
32
|
-
"""Stores necessary parameters for one or multiple API sources.
|
|
33
|
-
|
|
34
|
-
Attributes:
|
|
35
|
-
source: Mapping between API source alias and execution parameters.
|
|
36
|
-
"""
|
|
37
|
-
|
|
38
|
-
sources: dict[str, ExecutionContext]
|
|
39
|
-
|
|
40
|
-
@classmethod
|
|
41
|
-
def from_file(cls, path: str | pathlib.Path | os.PathLike[str]) -> Config:
|
|
42
|
-
"""Builds config from local or remote yaml file."""
|
|
43
|
-
with smart_open.open(path, 'r', encoding='utf-8') as f:
|
|
44
|
-
data = yaml.safe_load(f)
|
|
45
|
-
return Config(sources=data)
|
|
46
|
-
|
|
47
|
-
def save(self, path: str | pathlib.Path | os.PathLike[str]) -> str:
|
|
48
|
-
"""Saves config to local or remote yaml file."""
|
|
49
|
-
with smart_open.open(path, 'w', encoding='utf-8') as f:
|
|
50
|
-
yaml.dump(
|
|
51
|
-
self.model_dump(exclude_none=True).get('sources'), f, encoding='utf-8'
|
|
52
|
-
)
|
|
53
|
-
return f'Config is saved to {str(path)}'
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import warnings
|
|
17
|
+
|
|
18
|
+
from garf.executors.entrypoints import *
|
|
19
|
+
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' 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.
|
|
@@ -11,154 +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 defining `garf` CLI utility.
|
|
15
14
|
|
|
16
|
-
`garf` allows to execute queries and store results in local/remote
|
|
17
|
-
storage.
|
|
18
|
-
"""
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
import warnings
|
|
21
17
|
|
|
22
|
-
import
|
|
23
|
-
import logging
|
|
24
|
-
import sys
|
|
18
|
+
from garf.executors.entrypoints.cli import *
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
from garf_executors.entrypoints.tracer import initialize_tracer
|
|
33
|
-
from garf_executors.telemetry import tracer
|
|
34
|
-
|
|
35
|
-
initialize_tracer()
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
@tracer.start_as_current_span('garf.entrypoints.cli')
|
|
39
|
-
def main():
|
|
40
|
-
parser = argparse.ArgumentParser()
|
|
41
|
-
parser.add_argument('query', nargs='*')
|
|
42
|
-
parser.add_argument('-c', '--config', dest='config', default=None)
|
|
43
|
-
parser.add_argument('-w', '--workflow', dest='workflow', default=None)
|
|
44
|
-
parser.add_argument('--source', dest='source', default=None)
|
|
45
|
-
parser.add_argument('--output', dest='output', default='console')
|
|
46
|
-
parser.add_argument('--input', dest='input', default='file')
|
|
47
|
-
parser.add_argument('--log', '--loglevel', dest='loglevel', default='info')
|
|
48
|
-
parser.add_argument('--logger', dest='logger', default='local')
|
|
49
|
-
parser.add_argument('--log-name', dest='log_name', default='garf')
|
|
50
|
-
parser.add_argument(
|
|
51
|
-
'--parallel-queries', dest='parallel_queries', action='store_true'
|
|
52
|
-
)
|
|
53
|
-
parser.add_argument(
|
|
54
|
-
'--no-parallel-queries', dest='parallel_queries', action='store_false'
|
|
55
|
-
)
|
|
56
|
-
parser.add_argument('--dry-run', dest='dry_run', action='store_true')
|
|
57
|
-
parser.add_argument('-v', '--version', dest='version', action='store_true')
|
|
58
|
-
parser.add_argument(
|
|
59
|
-
'--parallel-threshold', dest='parallel_threshold', default=10, type=int
|
|
60
|
-
)
|
|
61
|
-
parser.add_argument(
|
|
62
|
-
'--enable-cache', dest='enable_cache', action='store_true'
|
|
63
|
-
)
|
|
64
|
-
parser.add_argument(
|
|
65
|
-
'--cache-ttl-seconds',
|
|
66
|
-
dest='cache_ttl_seconds',
|
|
67
|
-
default=3600,
|
|
68
|
-
type=int,
|
|
69
|
-
)
|
|
70
|
-
parser.set_defaults(parallel_queries=True)
|
|
71
|
-
parser.set_defaults(enable_cache=False)
|
|
72
|
-
parser.set_defaults(dry_run=False)
|
|
73
|
-
args, kwargs = parser.parse_known_args()
|
|
74
|
-
|
|
75
|
-
span = trace.get_current_span()
|
|
76
|
-
command_args = ' '.join(sys.argv[1:])
|
|
77
|
-
span.set_attribute('cli.command', f'garf {command_args}')
|
|
78
|
-
if args.version:
|
|
79
|
-
print(garf_executors.__version__)
|
|
80
|
-
sys.exit()
|
|
81
|
-
logger = utils.init_logging(
|
|
82
|
-
loglevel=args.loglevel.upper(), logger_type=args.logger, name=args.log_name
|
|
83
|
-
)
|
|
84
|
-
reader_client = reader.create_reader(args.input)
|
|
85
|
-
if workflow_file := args.workflow:
|
|
86
|
-
execution_workflow = workflow.Workflow.from_file(workflow_file)
|
|
87
|
-
for i, step in enumerate(execution_workflow.steps, 1):
|
|
88
|
-
with tracer.start_as_current_span(f'{i}-{step.fetcher}'):
|
|
89
|
-
query_executor = garf_executors.setup_executor(
|
|
90
|
-
source=step.fetcher,
|
|
91
|
-
fetcher_parameters=step.fetcher_parameters,
|
|
92
|
-
enable_cache=args.enable_cache,
|
|
93
|
-
cache_ttl_seconds=args.cache_ttl_seconds,
|
|
94
|
-
)
|
|
95
|
-
batch = {}
|
|
96
|
-
if not (queries := step.queries):
|
|
97
|
-
logger.error('Please provide one or more queries to run')
|
|
98
|
-
raise exceptions.GarfExecutorError(
|
|
99
|
-
'Please provide one or more queries to run'
|
|
100
|
-
)
|
|
101
|
-
for query in queries:
|
|
102
|
-
if isinstance(query, garf_executors.workflow.QueryPath):
|
|
103
|
-
batch[query.path] = reader_client.read(query.path)
|
|
104
|
-
else:
|
|
105
|
-
batch[query.query.title] = query.query.text
|
|
106
|
-
query_executor.execute_batch(
|
|
107
|
-
batch, step.context, args.parallel_threshold
|
|
108
|
-
)
|
|
109
|
-
sys.exit()
|
|
110
|
-
|
|
111
|
-
if not args.query:
|
|
112
|
-
logger.error('Please provide one or more queries to run')
|
|
113
|
-
raise exceptions.GarfExecutorError(
|
|
114
|
-
'Please provide one or more queries to run'
|
|
115
|
-
)
|
|
116
|
-
if config_file := args.config:
|
|
117
|
-
execution_config = config.Config.from_file(config_file)
|
|
118
|
-
if not (context := execution_config.sources.get(args.source)):
|
|
119
|
-
raise exceptions.GarfExecutorError(
|
|
120
|
-
f'No execution context found for source {args.source} in {config_file}'
|
|
121
|
-
)
|
|
122
|
-
else:
|
|
123
|
-
param_types = ['source', 'macro', 'template']
|
|
124
|
-
outputs = args.output.split(',')
|
|
125
|
-
extra_parameters = utils.ParamsParser([*param_types, *outputs]).parse(
|
|
126
|
-
kwargs
|
|
127
|
-
)
|
|
128
|
-
source_parameters = extra_parameters.get('source', {})
|
|
129
|
-
writer_parameters = {}
|
|
130
|
-
for output in outputs:
|
|
131
|
-
writer_parameters.update(extra_parameters.get(output))
|
|
132
|
-
|
|
133
|
-
context = garf_executors.api_executor.ApiExecutionContext(
|
|
134
|
-
query_parameters={
|
|
135
|
-
'macro': extra_parameters.get('macro'),
|
|
136
|
-
'template': extra_parameters.get('template'),
|
|
137
|
-
},
|
|
138
|
-
writer=outputs,
|
|
139
|
-
writer_parameters=writer_parameters,
|
|
140
|
-
fetcher_parameters=source_parameters,
|
|
141
|
-
)
|
|
142
|
-
query_executor = garf_executors.setup_executor(
|
|
143
|
-
source=args.source,
|
|
144
|
-
fetcher_parameters=context.fetcher_parameters,
|
|
145
|
-
enable_cache=args.enable_cache,
|
|
146
|
-
cache_ttl_seconds=args.cache_ttl_seconds,
|
|
147
|
-
)
|
|
148
|
-
batch = {query: reader_client.read(query) for query in args.query}
|
|
149
|
-
if args.parallel_queries and len(args.query) > 1:
|
|
150
|
-
logger.info('Running queries in parallel')
|
|
151
|
-
batch = {query: reader_client.read(query) for query in args.query}
|
|
152
|
-
query_executor.execute_batch(batch, context, args.parallel_threshold)
|
|
153
|
-
else:
|
|
154
|
-
if len(args.query) > 1:
|
|
155
|
-
logger.info('Running queries sequentially')
|
|
156
|
-
for query in args.query:
|
|
157
|
-
query_executor.execute(
|
|
158
|
-
query=reader_client.read(query), title=query, context=context
|
|
159
|
-
)
|
|
160
|
-
logging.shutdown()
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if __name__ == '__main__':
|
|
164
|
-
main()
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import warnings
|
|
17
|
+
|
|
18
|
+
from garf.executors.entrypoints.grpc_server import *
|
|
19
|
+
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' 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,107 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
"""FastAPI endpoint for executing queries."""
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
import warnings
|
|
18
17
|
|
|
19
|
-
import
|
|
20
|
-
import pydantic
|
|
21
|
-
import typer
|
|
22
|
-
import uvicorn
|
|
23
|
-
from garf_io import reader
|
|
24
|
-
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
|
25
|
-
from typing_extensions import Annotated
|
|
18
|
+
from garf.executors.entrypoints.server import *
|
|
26
19
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
FastAPIInstrumentor.instrument_app(app)
|
|
34
|
-
typer_app = typer.Typer()
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class ApiExecutorRequest(pydantic.BaseModel):
|
|
38
|
-
"""Request for executing a query.
|
|
39
|
-
|
|
40
|
-
Attributes:
|
|
41
|
-
source: Type of API to interact with.
|
|
42
|
-
title: Name of the query used as an output for writing.
|
|
43
|
-
query: Query to execute.
|
|
44
|
-
query_path: Local or remote path to query.
|
|
45
|
-
context: Execution context.
|
|
46
|
-
"""
|
|
47
|
-
|
|
48
|
-
source: str
|
|
49
|
-
title: Optional[str] = None
|
|
50
|
-
query: Optional[str] = None
|
|
51
|
-
query_path: Optional[Union[str, list[str]]] = None
|
|
52
|
-
context: garf_executors.api_executor.ApiExecutionContext
|
|
53
|
-
|
|
54
|
-
@pydantic.model_validator(mode='after')
|
|
55
|
-
def check_query_specified(self):
|
|
56
|
-
if not self.query_path and not self.query:
|
|
57
|
-
raise exceptions.GarfExecutorError(
|
|
58
|
-
'Missing one of required parameters: query, query_path'
|
|
59
|
-
)
|
|
60
|
-
return self
|
|
61
|
-
|
|
62
|
-
def model_post_init(self, __context__) -> None:
|
|
63
|
-
if self.query_path and isinstance(self.query_path, str):
|
|
64
|
-
self.query = reader.FileReader().read(self.query_path)
|
|
65
|
-
if not self.title:
|
|
66
|
-
self.title = str(self.query_path)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
class ApiExecutorResponse(pydantic.BaseModel):
|
|
70
|
-
"""Response after executing a query.
|
|
71
|
-
|
|
72
|
-
Attributes:
|
|
73
|
-
results: Results of query execution.
|
|
74
|
-
"""
|
|
75
|
-
|
|
76
|
-
results: list[str]
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
@app.get('/api/version')
|
|
80
|
-
async def version() -> str:
|
|
81
|
-
return garf_executors.__version__
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
@app.get('/api/fetchers')
|
|
85
|
-
async def get_fetchers() -> list[str]:
|
|
86
|
-
"""Shows all available API sources."""
|
|
87
|
-
return list(garf_executors.fetchers.find_fetchers())
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
@app.post('/api/execute')
|
|
91
|
-
async def execute(request: ApiExecutorRequest) -> ApiExecutorResponse:
|
|
92
|
-
query_executor = garf_executors.setup_executor(
|
|
93
|
-
request.source, request.context.fetcher_parameters
|
|
94
|
-
)
|
|
95
|
-
result = query_executor.execute(request.query, request.title, request.context)
|
|
96
|
-
return ApiExecutorResponse(results=[result])
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
@app.post('/api/execute:batch')
|
|
100
|
-
def execute_batch(request: ApiExecutorRequest) -> ApiExecutorResponse:
|
|
101
|
-
query_executor = garf_executors.setup_executor(
|
|
102
|
-
request.source, request.context.fetcher_parameters
|
|
103
|
-
)
|
|
104
|
-
reader_client = reader.FileReader()
|
|
105
|
-
batch = {query: reader_client.read(query) for query in request.query_path}
|
|
106
|
-
results = query_executor.execute_batch(batch, request.context)
|
|
107
|
-
return ApiExecutorResponse(results=results)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
@typer_app.command()
|
|
111
|
-
def main(
|
|
112
|
-
port: Annotated[int, typer.Option(help='Port to start the server')] = 8000,
|
|
113
|
-
):
|
|
114
|
-
uvicorn.run(app, port=port)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if __name__ == '__main__':
|
|
118
|
-
typer_app()
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|
|
@@ -12,46 +12,14 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
import os
|
|
16
|
-
|
|
17
|
-
from opentelemetry import trace
|
|
18
|
-
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
|
|
19
|
-
OTLPSpanExporter,
|
|
20
|
-
)
|
|
21
|
-
from opentelemetry.sdk.resources import Resource
|
|
22
|
-
from opentelemetry.sdk.trace import TracerProvider
|
|
23
|
-
from opentelemetry.sdk.trace.export import (
|
|
24
|
-
BatchSpanProcessor,
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
DEFAULT_SERVICE_NAME = 'garf'
|
|
28
15
|
|
|
16
|
+
import warnings
|
|
29
17
|
|
|
30
|
-
|
|
31
|
-
resource = Resource.create(
|
|
32
|
-
{'service.name': os.getenv('OTLP_SERVICE_NAME', DEFAULT_SERVICE_NAME)}
|
|
33
|
-
)
|
|
18
|
+
from garf.executors.entrypoints.tracer import *
|
|
34
19
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
except ImportError as e:
|
|
42
|
-
raise ImportError(
|
|
43
|
-
'Please install garf_executors with GCP support '
|
|
44
|
-
'- `pip install garf_executors[gcp]`'
|
|
45
|
-
) from e
|
|
46
|
-
|
|
47
|
-
cloud_span_processor = BatchSpanProcessor(
|
|
48
|
-
CloudTraceSpanExporter(project_id=gcp_project_id)
|
|
49
|
-
)
|
|
50
|
-
tracer_provider.add_span_processor(cloud_span_processor)
|
|
51
|
-
else:
|
|
52
|
-
otlp_processor = BatchSpanProcessor(
|
|
53
|
-
OTLPSpanExporter(endpoint=otel_endpoint, insecure=True)
|
|
54
|
-
)
|
|
55
|
-
tracer_provider.add_span_processor(otlp_processor)
|
|
56
|
-
|
|
57
|
-
trace.set_tracer_provider(tracer_provider)
|
|
20
|
+
warnings.warn(
|
|
21
|
+
"The 'garf_executors.entrypoints' namespace is deprecated. "
|
|
22
|
+
"Please use 'garf.executors.entrypoints' instead.",
|
|
23
|
+
DeprecationWarning,
|
|
24
|
+
stacklevel=2,
|
|
25
|
+
)
|