-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcore.py
1174 lines (955 loc) · 39.8 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Stream abstract class."""
import abc
import copy
import datetime
import json
import logging
from os import PathLike
from pathlib import Path
from types import MappingProxyType
from typing import (
Any,
Callable,
Dict,
Generator,
Iterable,
List,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
cast,
)
import pendulum
import requests
import singer
from singer import RecordMessage, SchemaMessage, StateMessage
from singer.schema import Schema
from singer_sdk.exceptions import InvalidStreamSortException, MaxRecordsLimitException
from singer_sdk.helpers._catalog import pop_deselected_record_properties
from singer_sdk.helpers._compat import final
from singer_sdk.helpers._flattening import get_flattening_options
from singer_sdk.helpers._singer import (
Catalog,
CatalogEntry,
MetadataMapping,
SelectionMask,
)
from singer_sdk.helpers._state import (
finalize_state_progress_markers,
get_starting_replication_value,
get_state_partitions_list,
get_writeable_state_dict,
increment_state,
log_sort_error,
reset_state_progress_markers,
write_replication_key_signpost,
write_starting_replication_value,
)
from singer_sdk.helpers._typing import conform_record_data_types, is_datetime_type
from singer_sdk.helpers._util import utc_now
from singer_sdk.mapper import RemoveRecordTransform, SameRecordTransform, StreamMap
from singer_sdk.plugin_base import PluginBase as TapBaseClass
# Replication methods
REPLICATION_FULL_TABLE = "FULL_TABLE"
REPLICATION_INCREMENTAL = "INCREMENTAL"
REPLICATION_LOG_BASED = "LOG_BASED"
FactoryType = TypeVar("FactoryType", bound="Stream")
METRICS_LOG_LEVEL_SETTING = "metrics_log_level"
class Stream(metaclass=abc.ABCMeta):
"""Abstract base class for tap streams."""
STATE_MSG_FREQUENCY = 10000 # Number of records between state messages
_MAX_RECORDS_LIMIT: Optional[int] = None
# Used for nested stream relationships
parent_stream_type: Optional[Type["Stream"]] = None
ignore_parent_replication_key: bool = False
# Internal API cost aggregator
_sync_costs: Dict[str, int] = {}
def __init__(
self,
tap: TapBaseClass,
schema: Optional[Union[str, PathLike, Dict[str, Any], Schema]] = None,
name: Optional[str] = None,
) -> None:
"""Init tap stream.
Args:
tap: Singer Tap this stream belongs to.
schema: JSON schema for records in this stream.
name: Name of this stream.
Raises:
ValueError: TODO
FileNotFoundError: TODO
"""
if name:
self.name: str = name
if not self.name:
raise ValueError("Missing argument or class variable 'name'.")
self.logger: logging.Logger = tap.logger
self.tap_name: str = tap.name
self._config: dict = dict(tap.config)
self._tap = tap
self._tap_state = tap.state
self._tap_input_catalog: Optional[Catalog] = None
self._stream_maps: Optional[List[StreamMap]] = None
self.forced_replication_method: Optional[str] = None
self._replication_key: Optional[str] = None
self._primary_keys: Optional[List[str]] = None
self._state_partitioning_keys: Optional[List[str]] = None
self._schema_filepath: Optional[Path] = None
self._metadata: Optional[MetadataMapping] = None
self._mask: Optional[SelectionMask] = None
self._schema: dict
self.child_streams: List[Stream] = []
if schema:
if isinstance(schema, (PathLike, str)):
if not Path(schema).is_file():
raise FileNotFoundError(
f"Could not find schema file '{self.schema_filepath}'."
)
self._schema_filepath = Path(schema)
elif isinstance(schema, dict):
self._schema = schema
elif isinstance(schema, Schema):
self._schema = schema.to_dict()
else:
raise ValueError(
f"Unexpected type {type(schema).__name__} for arg 'schema'."
)
if self.schema_filepath:
self._schema = json.loads(Path(self.schema_filepath).read_text())
if not self.schema:
raise ValueError(
f"Could not initialize schema for stream '{self.name}'. "
"A valid schema object or filepath was not provided."
)
@property
def stream_maps(self) -> List[StreamMap]:
"""Get stream transformation maps.
The 0th item is the primary stream map. List should not be empty.
Returns:
A list of one or more map transformations for this stream.
"""
if self._stream_maps:
return self._stream_maps
if self._tap.mapper:
self._stream_maps = self._tap.mapper.stream_maps[self.name]
self.logger.info(
f"Tap has custom mapper. Using {len(self.stream_maps)} provided map(s)."
)
else:
self.logger.info(
f"No custom mapper provided for '{self.name}'. "
"Using SameRecordTransform."
)
self._stream_maps = [
SameRecordTransform(
stream_alias=self.name,
raw_schema=self.schema,
key_properties=self.primary_keys,
flattening_options=get_flattening_options(self.config),
)
]
return self._stream_maps
@property
def is_timestamp_replication_key(self) -> bool:
"""Check is replication key is a timestamp.
Developers can override to `True` in order to force this value, although this
should not be required in most use cases since the type can generally be
accurately detected from the JSON Schema.
Returns:
True if the stream uses a timestamp-based replication key.
"""
if not self.replication_key:
return False
type_dict = self.schema.get("properties", {}).get(self.replication_key)
return is_datetime_type(type_dict)
def get_starting_replication_key_value(
self, context: Optional[dict]
) -> Optional[Any]:
"""Get starting replication key.
Will return the value of the stream's replication key when `--state` is passed.
If no prior state exists, will return `None`.
Developers should use this method to seed incremental processing for
non-datetime replication keys. For datetime and date replication keys, use
:meth:`~singer_sdk.Stream.get_starting_timestamp()`
Args:
context: Stream partition or context dictionary.
Returns:
Starting replication value.
"""
state = self.get_context_state(context)
return get_starting_replication_value(state)
def get_starting_timestamp(
self, context: Optional[dict]
) -> Optional[datetime.datetime]:
"""Get starting replication timestamp.
Will return the value of the stream's replication key when `--state` is passed.
If no state exists, will return `start_date` if set, or `None` if neither
the stream state nor `start_date` is set.
Developers should use this method to seed incremental processing for date
and datetime replication keys. For non-datetime replication keys, use
:meth:`~singer_sdk.Stream.get_starting_replication_key_value()`
Args:
context: Stream partition or context dictionary.
Returns:
`start_date` from config, or state value if using timestamp replication.
Raises:
ValueError: If the replication value is not a valid timestamp.
"""
value = self.get_starting_replication_key_value(context)
if value is None:
return None
if not self.is_timestamp_replication_key:
raise ValueError(
f"The replication key {self.replication_key} is not of timestamp type"
)
return cast(datetime.datetime, pendulum.parse(value))
@final
@property
def selected(self) -> bool:
"""Check if stream is selected.
Returns:
True if the stream is selected.
"""
return self.mask.get((), True)
@final
@property
def has_selected_descendents(self) -> bool:
"""Check descendents.
Returns:
True if any child streams are selected, recursively.
"""
for child in self.child_streams or []:
if child.selected or child.has_selected_descendents:
return True
return False
@final
@property
def descendent_streams(self) -> List["Stream"]:
"""Get child streams.
Returns:
A list of all children, recursively.
"""
result: List[Stream] = list(self.child_streams) or []
for child in self.child_streams:
result += child.descendent_streams or []
return result
def _write_replication_key_signpost(
self,
context: Optional[dict],
value: Union[datetime.datetime, str, int, float],
) -> None:
"""Write the signpost value, if available.
Args:
context: Stream partition or context dictionary.
value: TODO
Returns:
TODO
"""
if not value:
return
state = self.get_context_state(context)
write_replication_key_signpost(state, value)
def _write_starting_replication_value(self, context: Optional[dict]) -> None:
"""Write the starting replication value, if available.
Args:
context: Stream partition or context dictionary.
"""
value = None
state = self.get_context_state(context)
if self.replication_key:
replication_key_value = state.get("replication_key_value")
if replication_key_value and self.replication_key == state.get(
"replication_key"
):
value = replication_key_value
elif "start_date" in self.config:
value = self.config["start_date"]
write_starting_replication_value(state, value)
def get_replication_key_signpost(
self, context: Optional[dict]
) -> Optional[Union[datetime.datetime, Any]]:
"""Get the replication signpost.
For timestamp-based replication keys, this defaults to `utc_now()`. For
non-timestamp replication keys, default to `None`. For consistency in subsequent
calls, the value will be frozen (cached) at its initially called state, per
partition argument if applicable.
Developers may optionally override this method in advanced use cases such
as unsorted incremental streams or complex hierarchical stream scenarios.
For more info: :doc:`/implementation/state`
Args:
context: Stream partition or context dictionary.
Returns:
Max allowable bookmark value for this stream's replication key.
"""
if self.is_timestamp_replication_key:
return utc_now()
return None
@property
def schema_filepath(self) -> Optional[Path]:
"""Get path to schema file.
Returns:
Path to a schema file for the stream or `None` if n/a.
"""
return self._schema_filepath
@property
def schema(self) -> dict:
"""Get schema.
Returns:
JSON Schema dictionary for this stream.
"""
return self._schema
@property
def primary_keys(self) -> Optional[List[str]]:
"""Get primary keys.
Returns:
A list of primary key(s) for the stream.
"""
if not self._primary_keys:
return []
return self._primary_keys
@primary_keys.setter
def primary_keys(self, new_value: List[str]) -> None:
"""Set primary key(s) for the stream.
Args:
new_value: TODO
"""
self._primary_keys = new_value
@property
def state_partitioning_keys(self) -> Optional[List[str]]:
"""Get state partition keys.
If not set, a default partitioning will be inherited from the stream's context.
If an empty list is set (`[]`), state will be held in one bookmark per stream.
Returns:
Partition keys for the stream state bookmarks.
"""
return self._state_partitioning_keys
@state_partitioning_keys.setter
def state_partitioning_keys(self, new_value: Optional[List[str]]) -> None:
"""Set partition keys for the stream state bookmarks.
If not set, a default partitioning will be inherited from the stream's context.
If an empty list is set (`[]`), state will be held in one bookmark per stream.
Args:
new_value: the new list of keys
"""
self._state_partitioning_keys = new_value
@property
def replication_key(self) -> Optional[str]:
"""Get replication key.
Returns:
Replication key for the stream.
"""
if not self._replication_key:
return None
return self._replication_key
@replication_key.setter
def replication_key(self, new_value: str) -> None:
"""Set replication key for the stream.
Args:
new_value: TODO
"""
self._replication_key = new_value
@property
def is_sorted(self) -> bool:
"""Expect stream to be sorted.
When `True`, incremental streams will attempt to resume if unexpectedly
interrupted.
Returns:
`True` if stream is sorted. Defaults to `False`.
"""
return False
@property
def check_sorted(self) -> bool:
"""Check if stream is sorted.
This setting enables additional checks which may trigger
`InvalidStreamSortException` if records are found which are unsorted.
Returns:
`True` if sorting is checked. Defaults to `True`.
"""
return True
@property
def metadata(self) -> MetadataMapping:
"""Get stream metadata.
Metadata attributes (`inclusion`, `selected`, etc.) are part of the Singer spec.
Metadata from an input catalog will override standard metadata.
Returns:
A mapping from property breadcrumbs to metadata objects.
"""
if self._metadata is not None:
return self._metadata
if self._tap_input_catalog:
catalog_entry = self._tap_input_catalog.get_stream(self.tap_stream_id)
if catalog_entry:
self._metadata = catalog_entry.metadata
return self._metadata
self._metadata = MetadataMapping.get_standard_metadata(
schema=self.schema,
replication_method=self.forced_replication_method,
key_properties=self.primary_keys or [],
valid_replication_keys=(
[self.replication_key] if self.replication_key else None
),
schema_name=None,
)
# If there's no input catalog, select all streams
if self._tap_input_catalog is None:
self._metadata.root.selected = True
return self._metadata
@property
def _singer_catalog_entry(self) -> CatalogEntry:
"""Return catalog entry as specified by the Singer catalog spec.
Returns:
TODO
"""
return CatalogEntry(
tap_stream_id=self.tap_stream_id,
stream=self.name,
schema=Schema.from_dict(self.schema),
metadata=self.metadata,
key_properties=self.primary_keys or [],
replication_key=self.replication_key,
replication_method=self.replication_method,
is_view=None,
database=None,
table=None,
row_count=None,
stream_alias=None,
)
@property
def _singer_catalog(self) -> Catalog:
"""TODO.
Returns:
TODO
"""
return Catalog([(self.tap_stream_id, self._singer_catalog_entry)])
@property
def config(self) -> Mapping[str, Any]:
"""Get stream configuration.
Returns:
A frozen (read-only) config dictionary map.
"""
return MappingProxyType(self._config)
@property
def tap_stream_id(self) -> str:
"""Return a unique stream ID.
Default implementations will return `self.name` but this behavior may be
overridden if required by the developer.
Returns:
Unique stream ID.
"""
return self.name
@property
def replication_method(self) -> str:
"""Get replication method.
Returns:
Replication method to be used for this stream.
"""
if self.forced_replication_method:
return str(self.forced_replication_method)
if self.replication_key:
return REPLICATION_INCREMENTAL
return REPLICATION_FULL_TABLE
# State properties:
@property
def tap_state(self) -> dict:
"""Return a writeable state dict for the entire tap.
Note: This dictionary is shared (and writable) across all streams.
This method is internal to the SDK and should not need to be overridden.
Developers may access this property but this is not recommended except in
advanced use cases. Instead, developers should access the latest stream
replication key values using :meth:`~singer_sdk.Stream.get_starting_timestamp()`
for timestamp keys, or
:meth:`~singer_sdk.Stream.get_starting_replication_key_value()` for
non-timestamp keys.
Returns:
A writeable state dict for the entire tap.
"""
return self._tap_state
def get_context_state(self, context: Optional[dict]) -> dict:
"""Return a writable state dict for the given context.
Gives a partitioned context state if applicable; else returns stream state.
A blank state will be created in none exists.
This method is internal to the SDK and should not need to be overridden.
Developers may access this property but this is not recommended except in
advanced use cases. Instead, developers should access the latest stream
replication key values using
:meth:`~singer_sdk.Stream.get_starting_timestamp()` for timestamp keys, or
:meth:`~singer_sdk.Stream.get_starting_replication_key_value()` for
non-timestamp keys.
Partition level may be overridden by
:attr:`~singer_sdk.Stream.state_partitioning_keys` if set.
Args:
context: Stream partition or context dictionary.
Returns:
A partitioned context state if applicable; else returns stream state.
A blank state will be created in none exists.
"""
state_partition_context = self._get_state_partition_context(context)
if state_partition_context:
return get_writeable_state_dict(
self.tap_state,
self.name,
state_partition_context=state_partition_context,
)
return self.stream_state
@property
def stream_state(self) -> dict:
"""Get writable state.
This method is internal to the SDK and should not need to be overridden.
Developers may access this property but this is not recommended except in
advanced use cases. Instead, developers should access the latest stream
replication key values using :meth:`~singer_sdk.Stream.get_starting_timestamp()`
for timestamp keys, or
:meth:`~singer_sdk.Stream.get_starting_replication_key_value()` for
non-timestamp keys.
A blank state entry will be created if one doesn't already exist.
Returns:
A writable state dict for this stream.
"""
return get_writeable_state_dict(self.tap_state, self.name)
# Partitions
@property
def partitions(self) -> Optional[List[dict]]:
"""Get stream partitions.
Developers may override this property to provide a default partitions list.
By default, this method returns a list of any partitions which are already
defined in state, otherwise None.
Returns:
A list of partition key dicts (if applicable), otherwise `None`.
"""
result: List[dict] = []
for partition_state in (
get_state_partitions_list(self.tap_state, self.name) or []
):
result.append(partition_state["context"])
return result or None
# Private bookmarking methods
def _increment_stream_state(
self, latest_record: Dict[str, Any], *, context: Optional[dict] = None
) -> None:
"""Update state of stream or partition with data from the provided record.
Raises InvalidStreamSortException is self.is_sorted = True and unsorted data is
detected.
Args:
latest_record: TODO
context: Stream partition or context dictionary.
Raises:
ValueError: TODO
"""
state_dict = self.get_context_state(context)
if latest_record:
if self.replication_method in [
REPLICATION_INCREMENTAL,
REPLICATION_LOG_BASED,
]:
if not self.replication_key:
raise ValueError(
f"Could not detect replication key for '{self.name}' stream"
f"(replication method={self.replication_method})"
)
treat_as_sorted = self.is_sorted
if not treat_as_sorted and self.state_partitioning_keys is not None:
# Streams with custom state partitioning are not resumable.
treat_as_sorted = False
increment_state(
state_dict,
replication_key=self.replication_key,
latest_record=latest_record,
is_sorted=treat_as_sorted,
check_sorted=self.check_sorted,
)
# Private message authoring methods:
def _write_state_message(self) -> None:
"""Write out a STATE message with the latest state."""
singer.write_message(StateMessage(value=self.tap_state))
def _generate_schema_messages(self) -> Generator[SchemaMessage, None, None]:
"""Generate schema messages from stream maps.
Yields:
Schema message objects.
"""
bookmark_keys = [self.replication_key] if self.replication_key else None
for stream_map in self.stream_maps:
if isinstance(stream_map, RemoveRecordTransform):
# Don't emit schema if the stream's records are all ignored.
continue
schema_message = SchemaMessage(
stream_map.stream_alias,
stream_map.transformed_schema,
stream_map.transformed_key_properties,
bookmark_keys,
)
yield schema_message
def _write_schema_message(self) -> None:
"""Write out a SCHEMA message with the stream schema."""
for schema_message in self._generate_schema_messages():
singer.write_message(schema_message)
@property
def mask(self) -> SelectionMask:
"""Get a boolean mask for stream and property selection.
Returns:
A mapping of breadcrumbs to boolean values, representing stream and field
selection.
"""
if self._mask is None:
self._mask = self.metadata.resolve_selection()
return self._mask
def _generate_record_messages(
self,
record: dict,
) -> Generator[RecordMessage, None, None]:
"""Write out a RECORD message.
Args:
record: A single stream record.
Yields:
Record message objects.
"""
pop_deselected_record_properties(record, self.schema, self.mask, self.logger)
record = conform_record_data_types(
stream_name=self.name,
row=record,
schema=self.schema,
logger=self.logger,
)
for stream_map in self.stream_maps:
mapped_record = stream_map.transform(record)
# Emit record if not filtered
if mapped_record is not None:
record_message = RecordMessage(
stream=stream_map.stream_alias,
record=mapped_record,
version=None,
time_extracted=utc_now(),
)
yield record_message
def _write_record_message(self, record: dict) -> None:
"""Write out a RECORD message.
Args:
record: A single stream record.
"""
for record_message in self._generate_record_messages(record):
singer.write_message(record_message)
@property
def _metric_logging_function(self) -> Optional[Callable]:
"""Return the metrics logging function.
Returns:
The logging function for emitting metrics.
Raises:
ValueError: If logging level setting is an unsupported value.
"""
if METRICS_LOG_LEVEL_SETTING not in self.config:
return self.logger.info
if self.config[METRICS_LOG_LEVEL_SETTING].upper() == "INFO":
return self.logger.info
if self.config[METRICS_LOG_LEVEL_SETTING].upper() == "DEBUG":
return self.logger.debug
if self.config[METRICS_LOG_LEVEL_SETTING].upper() == "NONE":
return None
raise ValueError(
"Unexpected logging level for metrics: "
+ self.config[METRICS_LOG_LEVEL_SETTING]
)
def _write_metric_log(self, metric: dict, extra_tags: Optional[dict]) -> None:
"""Emit a metric log. Optionally with appended tag info.
Args:
metric: TODO
extra_tags: TODO
Returns:
None
"""
if not self._metric_logging_function:
return None
if extra_tags:
metric["tags"].update(extra_tags)
self._metric_logging_function(f"INFO METRIC: {str(metric)}")
def _write_record_count_log(
self, record_count: int, context: Optional[dict]
) -> None:
"""Emit a metric log. Optionally with appended tag info.
Args:
record_count: TODO
context: Stream partition or context dictionary.
"""
extra_tags = {} if not context else {"context": context}
counter_metric: Dict[str, Any] = {
"type": "counter",
"metric": "record_count",
"value": record_count,
"tags": {"stream": self.name},
}
self._write_metric_log(counter_metric, extra_tags=extra_tags)
def _write_request_duration_log(
self,
endpoint: str,
response: requests.Response,
context: Optional[dict],
extra_tags: Optional[dict],
) -> None:
"""TODO.
Args:
endpoint: TODO
response: TODO
context: Stream partition or context dictionary.
extra_tags: TODO
"""
request_duration_metric: Dict[str, Any] = {
"type": "timer",
"metric": "http_request_duration",
"value": response.elapsed.total_seconds(),
"tags": {
"endpoint": endpoint,
"http_status_code": response.status_code,
"status": "succeeded" if response.status_code < 400 else "failed",
},
}
extra_tags = extra_tags or {}
if context:
extra_tags["context"] = context
self._write_metric_log(metric=request_duration_metric, extra_tags=extra_tags)
def log_sync_costs(self) -> None:
"""Log a summary of Sync costs.
The costs are calculated via `calculate_sync_cost`.
This method can be overridden to log results in a custom
format. It is only called once at the end of the life of
the stream.
"""
if len(self._sync_costs) > 0:
msg = f"Total Sync costs for stream {self.name}: {self._sync_costs}"
self.logger.info(msg)
def _check_max_record_limit(self, record_count: int) -> None:
"""TODO.
Args:
record_count: TODO.
Raises:
MaxRecordsLimitException: TODO.
"""
if (
self._MAX_RECORDS_LIMIT is not None
and record_count >= self._MAX_RECORDS_LIMIT
):
raise MaxRecordsLimitException(
"Stream prematurely aborted due to the stream's max record "
f"limit ({self._MAX_RECORDS_LIMIT}) being reached."
)
# Handle interim stream state
def reset_state_progress_markers(self, state: Optional[dict] = None) -> None:
"""Reset progress markers. If all=True, all state contexts will be set.
This method is internal to the SDK and should not need to be overridden.
Args:
state: State object to promote progress markers with.
"""
if state is None or state == {}:
context: Optional[dict]
for context in self.partitions or [{}]:
context = context or None
state = self.get_context_state(context)
reset_state_progress_markers(state)
return
reset_state_progress_markers(state)
def finalize_state_progress_markers(self, state: Optional[dict] = None) -> None:
"""Reset progress markers. If all=True, all state contexts will be finalized.
This method is internal to the SDK and should not need to be overridden.
If all=True and the stream has children, child streams will also be finalized.
Args:
state: State object to promote progress markers with.
"""
if state is None or state == {}:
for child_stream in self.child_streams or []:
child_stream.finalize_state_progress_markers()
context: Optional[dict]
for context in self.partitions or [{}]:
context = context or None
state = self.get_context_state(context)
finalize_state_progress_markers(state)
return
finalize_state_progress_markers(state)
# Private sync methods:
def _sync_records( # noqa C901 # too complex
self, context: Optional[dict] = None
) -> None:
"""Sync records, emitting RECORD and STATE messages.
Args:
context: Stream partition or context dictionary.
Raises:
InvalidStreamSortException: TODO
"""
record_count = 0
current_context: Optional[dict]
context_list: Optional[List[dict]]
context_list = [context] if context is not None else self.partitions
selected = self.selected
for current_context in context_list or [{}]:
partition_record_count = 0
current_context = current_context or None
state = self.get_context_state(current_context)
state_partition_context = self._get_state_partition_context(current_context)
self._write_starting_replication_value(current_context)
child_context: Optional[dict] = (
None if current_context is None else copy.copy(current_context)
)
for record_result in self.get_records(current_context):
if isinstance(record_result, tuple):
# Tuple items should be the record and the child context
record, child_context = record_result
else:
record = record_result
child_context = copy.copy(
self.get_child_context(record=record, context=child_context)
)
for key, val in (state_partition_context or {}).items():
# Add state context to records if not already present
if key not in record:
record[key] = val
# Sync children, except when primary mapper filters out the record
if self.stream_maps[0].get_filter_result(record):
self._sync_children(child_context)
self._check_max_record_limit(record_count)
if selected:
if (record_count - 1) % self.STATE_MSG_FREQUENCY == 0:
self._write_state_message()
self._write_record_message(record)
try:
self._increment_stream_state(record, context=current_context)
except InvalidStreamSortException as ex:
log_sort_error(
log_fn=self.logger.error,
ex=ex,
record_count=record_count + 1,
partition_record_count=partition_record_count + 1,