Skip to content

Commit 084eb64

Browse files
authored
Merge pull request #203 from Point72/tkp/logfire
Logfire integration
2 parents bff952c + 39077a3 commit 084eb64

File tree

16 files changed

+1534
-101
lines changed

16 files changed

+1534
-101
lines changed

csp_gateway/server/config/gateway/omnibus.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ gateway:
4343
UI: True
4444
API_KEY: "12345"
4545
modules:
46+
- /modules/logfire
4647
- /modules/example_module
4748
- /modules/example_module_feedback
4849
- /modules/example_custom_table
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# @package _global_
2+
defaults:
3+
- /gateway: omnibus
4+
- _self_
5+
6+
logfire:
7+
_target_: csp_gateway.server.modules.logging.Logfire
8+
project_name: "csp-gateway-poc"
9+
service_name: "CSP Gateway"
10+
instrument_fastapi: True
11+
capture_logging: True
12+
log_level: DEBUG
13+
14+
publish_logfire:
15+
_target_: csp_gateway.server.modules.logging.PublishLogfire
16+
selection:
17+
include:
18+
- example
19+
service_name: "CSP Gateway"
20+
include_metadata: True
21+
use_spans: True
22+
log_level: DEBUG
23+
24+
gateway:
25+
_target_: csp_gateway.Gateway
26+
settings:
27+
PORT: ${port}
28+
AUTHENTICATE: ${authenticate}
29+
UI: True
30+
API_KEY: "12345"
31+
modules:
32+
- /modules/example_module
33+
- /modules/mount_outputs
34+
- /modules/mount_perspective_tables
35+
- /modules/mount_rest_routes
36+
- /logfire
37+
- /publish_logfire
38+
channels:
39+
_target_: csp_gateway.server.demo.ExampleGatewayChannels
40+
41+
# csp-gateway-start --config-dir=csp_gateway/server/demo +config=logfire
42+
43+
authenticate: False
44+
port: 8000

csp_gateway/server/modules/controls/controls.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import threading
55
from datetime import datetime, timezone
66

7+
try:
8+
import psutil
9+
except ImportError:
10+
# Hold and raise in model validator
11+
psutil = None
12+
713
import csp
8-
import psutil
914
from csp import ts
1015

1116
from csp_gateway.server import GatewayChannels, GatewayModule
@@ -53,16 +58,16 @@ def manage_controls(self, data: ts[Controls]):
5358
stats = {}
5459

5560
# Machine information
56-
stats["cpu"] = psutil.cpu_percent()
57-
stats["memory"] = psutil.virtual_memory().percent
61+
stats["cpu"] = psutil.cpu_percent() if psutil else None
62+
stats["memory"] = psutil.virtual_memory().percent if psutil else None
5863
stats["memory-total"] = round(
59-
psutil.virtual_memory().available * 100 / psutil.virtual_memory().total,
64+
psutil.virtual_memory().available * 100 / psutil.virtual_memory().total if psutil else 0,
6065
2,
6166
)
6267

6368
# Process and thread information
64-
current_process = psutil.Process()
65-
stats["pid"] = current_process.pid
69+
current_process = psutil.Process() if psutil else None
70+
stats["pid"] = current_process.pid if current_process else None
6671
stats["active_threads"] = threading.active_count()
6772

6873
# Get max threads from ulimit
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
import sys
2-
3-
if sys.platform.startswith("linux"):
4-
try:
5-
from .adapter import *
6-
from .filedrop import *
7-
except ImportError:
8-
pass
9-
else:
10-
pass
1+
from .adapter import *
2+
from .filedrop import *

csp_gateway/server/modules/filedrop/adapter.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
from enum import Enum, auto
66
from typing import Any, Callable, Dict, List, Optional, TypeVar
77

8-
import csp
98
import orjson
109
import pyarrow.parquet as pq
1110
from csp.impl.pushadapter import PushInputAdapter
1211
from csp.impl.types.container_type_normalizer import ContainerTypeNormalizer
1312
from csp.impl.wiring import py_push_adapter_def
1413
from pydantic import TypeAdapter
15-
from watchdog.events import FileSystemEvent, FileSystemEventHandler
16-
from watchdog.observers import Observer
14+
15+
try:
16+
from watchdog.events import FileSystemEvent, FileSystemEventHandler
17+
from watchdog.observers import Observer
18+
except ImportError:
19+
# Hold and raise in model validator
20+
FileSystemEvent = object
21+
FileSystemEventHandler = object
22+
Observer = None
23+
24+
import csp
1725

1826
__all__ = (
1927
"FileDropType",

csp_gateway/server/modules/filedrop/filedrop.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
from .adapter import FileDropAdapterConfiguration, FileDropType, filedrop_adapter_def
1818

19+
try:
20+
import watchdog
21+
except ImportError:
22+
# Hold and raise in model validator
23+
watchdog = None
24+
1925
__all__ = (
2026
"ReadFileDropConfiguration",
2127
"ReadFileDrop",
@@ -44,6 +50,12 @@ class ReadFileDropConfiguration(BaseModel):
4450
description=("If False, replaces the timestamp field on the GatewayStruct with a timestamp autogenerated by the current Gateway."),
4551
)
4652

53+
@model_validator(mode="before")
54+
def check_import(cls, values):
55+
if watchdog is None:
56+
raise ImportError("watchdog is required for ReadFileDrop. Install it with: pip install watchdog")
57+
return values
58+
4759
@model_validator(mode="after")
4860
def check_filedrop_type(self) -> "ReadFileDropConfiguration":
4961
if isinstance(self.filedrop_type, str):
@@ -65,6 +77,12 @@ class ReadFileDrop(GatewayModule):
6577

6678
configs: List[ReadFileDropConfiguration] = Field(description="List of configs for the directories to monitor")
6779

80+
@model_validator(mode="before")
81+
def check_import(cls, values):
82+
if watchdog is None:
83+
raise ImportError("watchdog is required for ReadFileDrop. Install it with: pip install watchdog")
84+
return values
85+
6886
@csp.node
6987
def handle_list_basket(self, data: csp.ts[List[T]], list_size: int) -> csp.OutputBasket(List[csp.ts[T]], shape="list_size"):
7088
if csp.ticked(data):
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
from .logging import LogChannels
1+
from .datadog import PublishDatadog
2+
from .logfire import (
3+
Logfire,
4+
PublishLogfire,
5+
configure_logfire_early,
6+
is_logfire_configured,
7+
)
8+
from .opsgenie import PublishOpsGenie
29
from .printing import PrintChannels
3-
4-
try:
5-
from .symphony import PublishSymphony
6-
except ImportError:
7-
pass
8-
9-
try:
10-
from .datadog import PublishDatadog
11-
except ImportError:
12-
pass
13-
14-
try:
15-
from .opsgenie import PublishOpsGenie
16-
except ImportError:
17-
pass
10+
from .stdlib import LogChannels
11+
from .symphony import PublishSymphony

csp_gateway/server/modules/logging/datadog.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import csp
88
from csp import ts
9-
from pydantic import Field
9+
from pydantic import Field, model_validator
1010

1111
from csp_gateway.server import ChannelSelection, GatewayModule
1212
from csp_gateway.server.modules.logging.util import (
@@ -15,6 +15,12 @@
1515
)
1616
from csp_gateway.utils import get_thread
1717

18+
try:
19+
from datadog import api
20+
except ImportError:
21+
# Hold and raise in model validator
22+
api = None
23+
1824
log = logging.getLogger(__name__)
1925
logging.getLogger("urllib3.connectionpool").setLevel(logging.ERROR)
2026
logging.getLogger("datadog.api").setLevel(logging.ERROR)
@@ -54,8 +60,6 @@ def _log_result(
5460

5561

5662
def _send_to_datadog(queue: Queue, dd_latency_log_threshold_seconds: int) -> None:
57-
from datadog import api
58-
5963
while True:
6064
data = queue.get()
6165
if data is None:
@@ -98,6 +102,12 @@ class PublishDatadog(GatewayModule):
98102

99103
dd_tags: Optional[Dict[str, str]] = Field(default=None, description="Tags to be included with Datadog submissions.")
100104

105+
@model_validator(mode="before")
106+
def check_import(cls, values):
107+
if api is None:
108+
raise ImportError("datadog is required for PublishDatadog. Install it with: pip install datadog")
109+
return values
110+
101111
def connect(self, channels):
102112
"""
103113
Channels to be connected to graph

0 commit comments

Comments
 (0)