-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07_postgresql_storage.py
More file actions
66 lines (48 loc) · 1.77 KB
/
Copy path07_postgresql_storage.py
File metadata and controls
66 lines (48 loc) · 1.77 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
"""
07 — PostgresStorage (on-premise enterprise backend).
Requires psycopg2. Install:
pip3 install sentinel-kernel[postgres]
Requires a running PostgreSQL (or compatible) instance. For a local
throwaway database:
docker run -d --name sentinel-pg \
-e POSTGRES_PASSWORD=test -e POSTGRES_USER=sentinel \
-e POSTGRES_DB=sentinel -p 5432:5432 postgres:15-alpine
export SENTINEL_PG="postgresql://sentinel:test@localhost:5432/sentinel"
This example skips itself gracefully if psycopg2 is not installed
or if SENTINEL_PG is unset, so it is safe to run in CI without a
real database.
Run:
python examples/07_postgresql_storage.py
"""
from __future__ import annotations
import os
import sys
def main() -> int:
try:
import psycopg2 # noqa: F401
except ImportError:
print("psycopg2 not installed. Install: pip3 install sentinel-kernel[postgres]")
print("Skipping.")
return 0
dsn = os.environ.get("SENTINEL_PG")
if not dsn:
print("SENTINEL_PG env var not set. Set it to a PostgreSQL DSN to run.")
print(" export SENTINEL_PG='postgresql://user:pass@host:5432/db'")
print("Skipping.")
return 0
from sentinel import Sentinel
from sentinel.storage.postgres import PostgresStorage
storage = PostgresStorage(dsn)
sentinel = Sentinel(storage=storage, project="pg-demo")
@sentinel.trace
def record(value: int) -> dict:
return {"value": value, "squared": value * value}
for v in range(3):
record(v)
traces = sentinel.query(limit=10)
print(f"Wrote {len(traces)} traces to PostgreSQL")
for t in traces:
print(f" {t.trace_id[:12]} {t.agent} {t.started_at.isoformat()}")
return 0
if __name__ == "__main__":
sys.exit(main())