forked from temporalio/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.py
More file actions
656 lines (532 loc) · 21.2 KB
/
Copy pathruntime.py
File metadata and controls
656 lines (532 loc) · 21.2 KB
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
"""Runtime for clients and workers. (experimental)
This module is currently experimental. The API may change.
"""
from __future__ import annotations
import logging
import time
from dataclasses import dataclass, field
from datetime import timedelta
from enum import Enum
from typing import ClassVar, Mapping, NewType, Optional, Sequence, Union
from typing_extensions import Protocol
import temporalio.bridge.metric
import temporalio.bridge.runtime
import temporalio.common
_default_runtime: Optional[Runtime] = None
class Runtime:
"""Runtime for Temporal Python SDK.
Users are encouraged to use :py:meth:`default`. It can be set with
:py:meth:`set_default`. Every time a new runtime is created, a new internal
thread pool is created.
Runtimes do not work across forks.
"""
@staticmethod
def default() -> Runtime:
"""Get the default runtime, creating if not already created.
If the default runtime needs to be different, it should be done with
:py:meth:`set_default` before this is called or ever used.
Returns:
The default runtime.
"""
global _default_runtime
if not _default_runtime:
_default_runtime = Runtime(telemetry=TelemetryConfig())
return _default_runtime
@staticmethod
def set_default(runtime: Runtime, *, error_if_already_set: bool = True) -> None:
"""Set the default runtime to the given runtime.
This should be called before any Temporal client is created, but can
change the existing one. Any clients and workers created with the
previous runtime will stay on that runtime.
Args:
runtime: The runtime to set.
error_if_already_set: If True and default is already set, this will
raise a RuntimeError.
"""
global _default_runtime
if _default_runtime and error_if_already_set:
raise RuntimeError("Runtime default already set")
_default_runtime = runtime
def __init__(self, *, telemetry: TelemetryConfig) -> None:
"""Create a default runtime with the given telemetry config.
Each new runtime creates a new internal thread pool, so use sparingly.
"""
self._core_runtime = temporalio.bridge.runtime.Runtime(
telemetry=telemetry._to_bridge_config()
)
if isinstance(telemetry.metrics, MetricBuffer):
telemetry.metrics._runtime = self
core_meter = temporalio.bridge.metric.MetricMeter.create(self._core_runtime)
if not core_meter:
self._metric_meter = temporalio.common.MetricMeter.noop
else:
self._metric_meter = _MetricMeter(core_meter, core_meter.default_attributes)
@property
def metric_meter(self) -> temporalio.common.MetricMeter:
"""Metric meter for this runtime. This is a no-op metric meter if no
metrics were configured.
"""
return self._metric_meter
@dataclass
class TelemetryFilter:
"""Filter for telemetry use."""
core_level: str
"""Level for Core. Can be ``ERROR``, ``WARN``, ``INFO``, ``DEBUG``, or
``TRACE``.
"""
other_level: str
"""Level for non-Core. Can be ``ERROR``, ``WARN``, ``INFO``, ``DEBUG``, or
``TRACE``.
"""
def formatted(self) -> str:
"""Return a formatted form of this filter."""
# We intentionally aren't using __str__ or __format__ so they can keep
# their original dataclass impls
return f"{self.other_level},temporal_sdk_core={self.core_level},temporal_client={self.core_level},temporal_sdk={self.core_level}"
@dataclass(frozen=True)
class LoggingConfig:
"""Configuration for runtime logging."""
filter: Union[TelemetryFilter, str]
"""Filter for logging. Can use :py:class:`TelemetryFilter` or raw string."""
forwarding: Optional[LogForwardingConfig] = None
"""If present, Core logger messages will be forwarded to a Python logger.
See the :py:class:`LogForwardingConfig` docs for more info.
.. warning::
This API is experimental
"""
default: ClassVar[LoggingConfig]
"""Default logging configuration of Core WARN level and other ERROR
level.
"""
def _to_bridge_config(self) -> temporalio.bridge.runtime.LoggingConfig:
return temporalio.bridge.runtime.LoggingConfig(
filter=self.filter
if isinstance(self.filter, str)
else self.filter.formatted(),
forward_to=None if not self.forwarding else self.forwarding._on_logs,
)
LoggingConfig.default = LoggingConfig(
filter=TelemetryFilter(core_level="WARN", other_level="ERROR")
)
_module_start_time = time.time()
@dataclass
class LogForwardingConfig:
"""Configuration for log forwarding from Core.
Configuring this will send logs from Core to the given Python logger. By
default, log timestamps are overwritten and internally throttled/buffered
for a few milliseconds to prevent overloading Python. This means those log
records may have a time in the past and technically may appear out of order
with Python-originated log messages by a few milliseconds.
If for some reason lots of logs occur within the buffered time (i.e.
thousands), they may be sent earlier. Users are discouraged from using this
with ``TRACE`` Core logging.
All log records produced have a ``temporal_log`` attribute that contains a
representation of the Core log. This representation has a ``fields``
attribute which has arbitrary extra data from Core. By default a string
representation of this extra ``fields`` attribute is appended to the
message.
.. warning::
This API is experimental
"""
logger: logging.Logger
"""Core logger messages will be sent to this logger."""
append_target_to_name: bool = True
"""If true, the default, the target is appended to the name."""
prepend_target_on_message: bool = True
"""If true, the default, the target is appended to the name."""
overwrite_log_record_time: bool = True
"""If true, the default, the log record time is overwritten with the core
log time."""
append_log_fields_to_message: bool = True
"""If true, the default, the extra fields dict is appended to the
message."""
def _on_logs(
self, logs: Sequence[temporalio.bridge.runtime.BufferedLogEntry]
) -> None:
for log in logs:
# Don't go further if not enabled
level = log.level
if not self.logger.isEnabledFor(level):
continue
# Create the record
name = self.logger.name
if self.append_target_to_name:
name += f"-sdk_core::{log.target}"
message = log.message
if self.prepend_target_on_message:
message = f"[sdk_core::{log.target}] {message}"
if self.append_log_fields_to_message:
# Swallow error converting fields (should never happen, but
# just in case)
try:
message += f" {log.fields}"
except:
pass
record = self.logger.makeRecord(
name,
level,
"(sdk-core)",
0,
message,
(),
None,
"(sdk-core)",
{"temporal_log": log},
None,
)
if self.overwrite_log_record_time:
record.created = log.time
record.msecs = (record.created - int(record.created)) * 1000
# We can't access logging module's start time and it's not worth
# doing difference math to get relative time right here, so
# we'll make time relative to _our_ module's start time
self.relativeCreated = (record.created - _module_start_time) * 1000
# Log the record
self.logger.handle(record)
class OpenTelemetryMetricTemporality(Enum):
"""Temporality for OpenTelemetry metrics."""
CUMULATIVE = 1
DELTA = 2
@dataclass(frozen=True)
class OpenTelemetryConfig:
"""Configuration for OpenTelemetry collector."""
url: str
headers: Optional[Mapping[str, str]] = None
metric_periodicity: Optional[timedelta] = None
metric_temporality: OpenTelemetryMetricTemporality = (
OpenTelemetryMetricTemporality.CUMULATIVE
)
def _to_bridge_config(self) -> temporalio.bridge.runtime.OpenTelemetryConfig:
return temporalio.bridge.runtime.OpenTelemetryConfig(
url=self.url,
headers=self.headers or {},
metric_periodicity_millis=None
if not self.metric_periodicity
else round(self.metric_periodicity.total_seconds() * 1000),
metric_temporality_delta=self.metric_temporality
== OpenTelemetryMetricTemporality.DELTA,
)
@dataclass(frozen=True)
class PrometheusConfig:
"""Configuration for Prometheus metrics endpoint."""
bind_address: str
counters_total_suffix: bool = False
unit_suffix: bool = False
def _to_bridge_config(self) -> temporalio.bridge.runtime.PrometheusConfig:
return temporalio.bridge.runtime.PrometheusConfig(
bind_address=self.bind_address,
counters_total_suffix=self.counters_total_suffix,
unit_suffix=self.unit_suffix,
)
class MetricBuffer:
"""A buffer that can be set on :py:class:`TelemetryConfig` to record
metrics instead of ignoring/exporting them.
.. warning::
It is important that the buffer size is set to a high number and that
:py:meth:`retrieve_updates` is called regularly to drain the buffer. If
the buffer is full, metric updates will be dropped and an error will be
logged.
"""
def __init__(self, buffer_size: int) -> None:
"""Create a buffer with the given size.
.. warning::
It is important that the buffer size is set to a high number and is
drained regularly. See :py:class:`MetricBuffer` warning.
Args:
buffer_size: Size of the buffer. Set this to a large value. A value
in the tens of thousands or higher is plenty reasonable.
"""
self._buffer_size = buffer_size
self._runtime: Optional[Runtime] = None
def retrieve_updates(self) -> Sequence[BufferedMetricUpdate]:
"""Drain the buffer and return all metric updates.
.. warning::
It is important that this is called regularly. See
:py:class:`MetricBuffer` warning.
Returns:
A sequence of metric updates.
"""
if not self._runtime:
raise RuntimeError("Attempting to retrieve updates before runtime created")
return self._runtime._core_runtime.retrieve_buffered_metrics()
@dataclass(frozen=True)
class TelemetryConfig:
"""Configuration for Core telemetry."""
logging: Optional[LoggingConfig] = LoggingConfig.default
"""Logging configuration."""
metrics: Optional[Union[OpenTelemetryConfig, PrometheusConfig, MetricBuffer]] = None
"""Metrics configuration or buffer."""
global_tags: Mapping[str, str] = field(default_factory=dict)
"""OTel resource tags to be applied to all metrics."""
attach_service_name: bool = True
"""Whether to put the service_name on every metric."""
metric_prefix: Optional[str] = None
"""Prefix to put on every Temporal metric. If unset, defaults to
``temporal_``."""
def _to_bridge_config(self) -> temporalio.bridge.runtime.TelemetryConfig:
return temporalio.bridge.runtime.TelemetryConfig(
logging=None if not self.logging else self.logging._to_bridge_config(),
metrics=None
if not self.metrics
else temporalio.bridge.runtime.MetricsConfig(
opentelemetry=None
if not isinstance(self.metrics, OpenTelemetryConfig)
else self.metrics._to_bridge_config(),
prometheus=None
if not isinstance(self.metrics, PrometheusConfig)
else self.metrics._to_bridge_config(),
buffered_with_size=0
if not isinstance(self.metrics, MetricBuffer)
else self.metrics._buffer_size,
attach_service_name=self.attach_service_name,
global_tags=self.global_tags or None,
metric_prefix=self.metric_prefix,
),
)
BufferedMetricKind = NewType("BufferedMetricKind", int)
"""Representation of a buffered metric kind."""
BUFFERED_METRIC_KIND_COUNTER = BufferedMetricKind(0)
"""Buffered metric is a counter which means values are deltas."""
BUFFERED_METRIC_KIND_GAUGE = BufferedMetricKind(1)
"""Buffered metric is a gauge."""
BUFFERED_METRIC_KIND_HISTOGRAM = BufferedMetricKind(2)
"""Buffered metric is a histogram."""
# WARNING: This must match Rust metric::BufferedMetric
class BufferedMetric(Protocol):
"""A metric for a buffered update.
The same metric for the same name and runtime is guaranteed to be the exact
same object for performance reasons. This means py:func:`id` will be the
same for the same metric across updates.
"""
@property
def name(self) -> str:
"""Get the name of the metric."""
...
@property
def description(self) -> Optional[str]:
"""Get the description of the metric if any."""
...
@property
def unit(self) -> Optional[str]:
"""Get the unit of the metric if any."""
...
@property
def kind(self) -> BufferedMetricKind:
"""Get the metric kind.
This is one of :py:const:`BUFFERED_METRIC_KIND_COUNTER`,
:py:const:`BUFFERED_METRIC_KIND_GAUGE`, or
:py:const:`BUFFERED_METRIC_KIND_HISTOGRAM`.
"""
...
# WARNING: This must match Rust metric::BufferedMetricUpdate
class BufferedMetricUpdate(Protocol):
"""A single metric value update."""
@property
def metric(self) -> BufferedMetric:
"""Metric being updated.
For performance reasons, this is the same object across updates for the
same metric. This means py:func:`id` will be the same for the same
metric across updates.
"""
...
@property
def value(self) -> Union[int, float]:
"""Value for the update.
For counters this is a delta, for gauges and histograms this is just the
value.
"""
...
@property
def attributes(self) -> temporalio.common.MetricAttributes:
"""Attributes for the update.
For performance reasons, this is the same object across updates for the
same attribute set. This means py:func:`id` will be the same for the
same attribute set across updates. Note this is for same "attribute set"
as created by the metric creator, but different attribute sets may have
the same values.
Do not mutate this.
"""
...
class _MetricMeter(temporalio.common.MetricMeter):
def __init__(
self,
core_meter: temporalio.bridge.metric.MetricMeter,
core_attrs: temporalio.bridge.metric.MetricAttributes,
) -> None:
self._core_meter = core_meter
self._core_attrs = core_attrs
def create_counter(
self, name: str, description: Optional[str] = None, unit: Optional[str] = None
) -> temporalio.common.MetricCounter:
return _MetricCounter(
name,
description,
unit,
temporalio.bridge.metric.MetricCounter(
self._core_meter, name, description, unit
),
self._core_attrs,
)
def create_histogram(
self, name: str, description: Optional[str] = None, unit: Optional[str] = None
) -> temporalio.common.MetricHistogram:
return _MetricHistogram(
name,
description,
unit,
temporalio.bridge.metric.MetricHistogram(
self._core_meter, name, description, unit
),
self._core_attrs,
)
def create_gauge(
self, name: str, description: Optional[str] = None, unit: Optional[str] = None
) -> temporalio.common.MetricGauge:
return _MetricGauge(
name,
description,
unit,
temporalio.bridge.metric.MetricGauge(
self._core_meter, name, description, unit
),
self._core_attrs,
)
def with_additional_attributes(
self, additional_attributes: temporalio.common.MetricAttributes
) -> temporalio.common.MetricMeter:
return _MetricMeter(
self._core_meter,
self._core_attrs.with_additional_attributes(additional_attributes),
)
class _MetricCounter(temporalio.common.MetricCounter):
def __init__(
self,
name: str,
description: Optional[str],
unit: Optional[str],
core_metric: temporalio.bridge.metric.MetricCounter,
core_attrs: temporalio.bridge.metric.MetricAttributes,
) -> None:
self._name = name
self._description = description
self._unit = unit
self._core_metric = core_metric
self._core_attrs = core_attrs
@property
def name(self) -> str:
return self._name
@property
def description(self) -> Optional[str]:
return self._description
@property
def unit(self) -> Optional[str]:
return self._unit
def add(
self,
value: int,
additional_attributes: Optional[temporalio.common.MetricAttributes] = None,
) -> None:
if value < 0:
raise ValueError("Metric value cannot be negative")
core_attrs = self._core_attrs
if additional_attributes:
core_attrs = core_attrs.with_additional_attributes(additional_attributes)
self._core_metric.add(value, core_attrs)
def with_additional_attributes(
self, additional_attributes: temporalio.common.MetricAttributes
) -> temporalio.common.MetricCounter:
return _MetricCounter(
self._name,
self._description,
self._unit,
self._core_metric,
self._core_attrs.with_additional_attributes(additional_attributes),
)
class _MetricHistogram(temporalio.common.MetricHistogram):
def __init__(
self,
name: str,
description: Optional[str],
unit: Optional[str],
core_metric: temporalio.bridge.metric.MetricHistogram,
core_attrs: temporalio.bridge.metric.MetricAttributes,
) -> None:
self._name = name
self._description = description
self._unit = unit
self._core_metric = core_metric
self._core_attrs = core_attrs
@property
def name(self) -> str:
return self._name
@property
def description(self) -> Optional[str]:
return self._description
@property
def unit(self) -> Optional[str]:
return self._unit
def record(
self,
value: int,
additional_attributes: Optional[temporalio.common.MetricAttributes] = None,
) -> None:
if value < 0:
raise ValueError("Metric value cannot be negative")
core_attrs = self._core_attrs
if additional_attributes:
core_attrs = core_attrs.with_additional_attributes(additional_attributes)
self._core_metric.record(value, core_attrs)
def with_additional_attributes(
self, additional_attributes: temporalio.common.MetricAttributes
) -> temporalio.common.MetricHistogram:
return _MetricHistogram(
self._name,
self._description,
self._unit,
self._core_metric,
self._core_attrs.with_additional_attributes(additional_attributes),
)
class _MetricGauge(temporalio.common.MetricGauge):
def __init__(
self,
name: str,
description: Optional[str],
unit: Optional[str],
core_metric: temporalio.bridge.metric.MetricGauge,
core_attrs: temporalio.bridge.metric.MetricAttributes,
) -> None:
self._name = name
self._description = description
self._unit = unit
self._core_metric = core_metric
self._core_attrs = core_attrs
@property
def name(self) -> str:
return self._name
@property
def description(self) -> Optional[str]:
return self._description
@property
def unit(self) -> Optional[str]:
return self._unit
def set(
self,
value: int,
additional_attributes: Optional[temporalio.common.MetricAttributes] = None,
) -> None:
if value < 0:
raise ValueError("Metric value cannot be negative")
core_attrs = self._core_attrs
if additional_attributes:
core_attrs = core_attrs.with_additional_attributes(additional_attributes)
self._core_metric.set(value, core_attrs)
def with_additional_attributes(
self, additional_attributes: temporalio.common.MetricAttributes
) -> temporalio.common.MetricGauge:
return _MetricGauge(
self._name,
self._description,
self._unit,
self._core_metric,
self._core_attrs.with_additional_attributes(additional_attributes),
)