-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.py
70 lines (54 loc) · 1.69 KB
/
script.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
import logging
import os
import random
import time
import sys
from dotenv import load_dotenv
import elasticapm
import ecs_logging
load_dotenv()
logger = logging.getLogger("script")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
if os.getenv("ELASTIC_APM_ENABLED", "") not in ["false" or "no"]:
handler.setFormatter(
ecs_logging.StdlibFormatter(
exclude_fields=["log.original"],
extra={"event": {"dataset": "demo.script"}},
stack_trace_limit=0, # Stack traces are in APM
)
)
logger.addHandler(handler)
@elasticapm.capture_span()
def process(payload):
# Simulate errors (~ 10% calls)
if random.randint(1, 100) > 90:
raise Exception("Simulated processing error")
with elasticapm.capture_span("Sleep"):
# Simulate duration
time.sleep(random.uniform(0, 1.0))
# Custom logging
logger.info(f"PROCESSED: Job #{payload}", extra={"iteration": payload})
return True
def main():
for i in range(1, 11):
try:
elasticapm.get_client().begin_transaction("cronjob")
apm_result = "unknown"
# Custom logging
logger.debug(f"--> Processing job [{i}]")
process(i)
apm_result = "success"
except Exception as e:
logger.error(f"[!] Error when processing job [{i}]")
elasticapm.get_client().capture_exception()
apm_result = "failure"
finally:
elasticapm.get_client().end_transaction("cronjob", apm_result)
if __name__ == "__main__":
elasticapm.instrument()
elasticapm.Client()
try:
main()
finally:
elasticapm.get_client().close()