Skip to content

Commit 007cffd

Browse files
[io] feat: add telemetry support
1 parent 70bbc0f commit 007cffd

File tree

13 files changed

+52
-6
lines changed

13 files changed

+52
-6
lines changed

libs/io/garf_io/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Write GarfReport to anywhere."""
1415

15-
"""Writing GarfReport to anywhere."""
16-
17-
__version__ = '0.0.16'
16+
__version__ = '0.1.0'

libs/io/garf_io/formatter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from garf_core import report as garf_report
2323
from typing_extensions import TypeAlias
2424

25+
from garf_io.telemetry import tracer
26+
2527
_NESTED_FIELD: TypeAlias = Union[list, tuple]
2628

2729

@@ -70,6 +72,7 @@ def __init__(
7072
self.type_ = self._cast_to_enum(ArrayHandling, type_)
7173
self.delimiter = delimiter
7274

75+
@tracer.start_as_current_span('apply_transformations')
7376
def apply_transformations(
7477
self, report: garf_report.GarfReport
7578
) -> garf_report.GarfReport:
@@ -121,6 +124,7 @@ def _delimiter_join(self, field: _NESTED_FIELD) -> str:
121124
return self.delimiter.join([str(element) for element in field])
122125

123126

127+
@tracer.start_as_current_span('format_report_for_writing')
124128
def format_report_for_writing(
125129
report: garf_report.GarfReport,
126130
formatting_strategies: list[FormattingStrategy],

libs/io/garf_io/reader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import smart_open
1818
from typing_extensions import override
1919

20+
from garf_io.telemetry import tracer
21+
2022

2123
class AbsReader(abc.ABC):
2224
"""Base class for reading queries."""
@@ -30,6 +32,7 @@ class FileReader(AbsReader):
3032
"""Reads from file."""
3133

3234
@override
35+
@tracer.start_as_current_span('file.read')
3336
def read(self, query_path, **kwargs):
3437
with smart_open.open(query_path, 'r') as f:
3538
return f.read()
@@ -39,6 +42,7 @@ class ConsoleReader(AbsReader):
3942
"""Read query from standard input."""
4043

4144
@override
45+
@tracer.start_as_current_span('console.read')
4246
def read(self, query_path, **kwargs):
4347
return query_path
4448

libs/io/garf_io/telemetry.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 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+
# pylint: disable=C0330, g-bad-import-order, g-multiple-import
16+
from opentelemetry import trace
17+
18+
tracer = trace.get_tracer(
19+
instrumenting_module_name='garf_io',
20+
)

libs/io/garf_io/writers/abs_writer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from garf_core.report import GarfReport
2323

2424
from garf_io import formatter
25+
from garf_io.telemetry import tracer
2526

2627
logger = logging.getLogger(__name__)
2728

@@ -43,6 +44,7 @@ def __init__(
4344
def write(self, report: GarfReport, destination: str) -> str | None:
4445
"""Writes report to destination."""
4546

47+
@tracer.start_as_current_span('format_for_write')
4648
def format_for_write(self, report: GarfReport) -> GarfReport:
4749
"""Prepares report for writing."""
4850
array_handling_strategy = formatter.ArrayHandlingStrategy(

libs/io/garf_io/writers/bigquery_writer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from google.cloud import exceptions as google_cloud_exceptions
3535

3636
from garf_io import exceptions, formatter
37+
from garf_io.telemetry import tracer
3738
from garf_io.writers import abs_writer
3839

3940
logger = logging.getLogger(__name__)
@@ -110,6 +111,7 @@ def client(self) -> bigquery.Client:
110111
"""Instantiated BigQuery client."""
111112
return bigquery.Client(self.project)
112113

114+
@tracer.start_as_current_span('bq.create_or_get_dataset')
113115
def create_or_get_dataset(self) -> bigquery.Dataset:
114116
"""Gets existing dataset or create a new one."""
115117
try:
@@ -120,6 +122,7 @@ def create_or_get_dataset(self) -> bigquery.Dataset:
120122
bq_dataset = self.client.create_dataset(bq_dataset, timeout=30)
121123
return bq_dataset
122124

125+
@tracer.start_as_current_span('bq.write')
123126
def write(self, report: garf_report.GarfReport, destination: str) -> str:
124127
"""Writes Garf report to a BigQuery table.
125128

libs/io/garf_io/writers/console_writer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from garf_core import report as garf_report
2222
from rich import console, table
2323

24+
from garf_io.telemetry import tracer
2425
from garf_io.writers import abs_writer
2526

2627

@@ -49,6 +50,7 @@ def __init__(
4950
self.page_size = int(page_size)
5051
self.format = format
5152

53+
@tracer.start_as_current_span('console.write')
5254
def write(self, report: garf_report.GarfReport, destination: str) -> None:
5355
"""Writes Garf report to standard output.
5456

libs/io/garf_io/writers/csv_writer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from garf_core import report as garf_report
2929

3030
from garf_io import formatter
31+
from garf_io.telemetry import tracer
3132
from garf_io.writers import file_writer
3233

3334
logger = logging.getLogger(__name__)
@@ -72,6 +73,7 @@ def __str__(self):
7273
f'[CSV] - data are saved to {self.destination_folder} destination_folder.'
7374
)
7475

76+
@tracer.start_as_current_span('csv.write')
7577
def write(self, report: garf_report.GarfReport, destination: str) -> str:
7678
"""Writes Garf report to a CSV file.
7779

libs/io/garf_io/writers/file_writer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pathlib
1818
from typing import Union
1919

20+
from garf_io.telemetry import tracer
2021
from garf_io.writers.abs_writer import AbsWriter
2122

2223

@@ -38,6 +39,7 @@ def __init__(
3839
super().__init__(**kwargs)
3940
self.destination_folder = str(destination_folder)
4041

42+
@tracer.start_as_current_span('file.create_dir')
4143
def create_dir(self) -> None:
4244
"""Creates folders if needed or destination is not remote."""
4345
if (

libs/io/garf_io/writers/json_writer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from garf_core import report as garf_report
2525

2626
from garf_io import formatter
27+
from garf_io.telemetry import tracer
2728
from garf_io.writers import file_writer
2829

2930
logger = logging.getLogger(__name__)
@@ -54,6 +55,7 @@ def __init__(
5455
super().__init__(destination_folder=destination_folder, **kwargs)
5556
self.format = format
5657

58+
@tracer.start_as_current_span('json.write')
5759
def write(self, report: garf_report.GarfReport, destination: str) -> str:
5860
"""Writes Garf report to a JSON file.
5961

0 commit comments

Comments
 (0)