@usherlabs/cex-broker 0.1.20 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -65,33 +65,43 @@ CEX_BROKER_BINANCE_API_KEY_1=your_secondary_binance_api_key
65
65
  CEX_BROKER_BINANCE_API_SECRET_1=your_secondary_binance_api_secret
66
66
  CEX_BROKER_BINANCE_API_KEY_2=your_tertiary_binance_api_key
67
67
  CEX_BROKER_BINANCE_API_SECRET_2=your_tertiary_binance_api_secret
68
+
69
+ # OpenTelemetry Metrics (Optional)
70
+ # Send metrics via OTLP to a collector
71
+ OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
72
+ OTEL_SERVICE_NAME=cex-broker
73
+ # Or use CEX_BROKER_OTEL_* (default port 4318). Legacy: CEX_BROKER_CLICKHOUSE_* also supported.
74
+ # CEX_BROKER_OTEL_HOST=otel-collector
75
+ # CEX_BROKER_OTEL_PORT=4318
76
+ # CEX_BROKER_OTEL_PROTOCOL=http
68
77
  ```
69
78
 
70
79
  **Note**: Only configure API keys for exchanges you plan to use. The system will automatically detect and initialize configured exchanges.
71
80
 
81
+ **Metrics (OpenTelemetry)**: Metrics are exported via OTLP. If neither `OTEL_EXPORTER_OTLP_ENDPOINT` nor `CEX_BROKER_OTEL_HOST` (or legacy `CEX_BROKER_CLICKHOUSE_HOST`) is set, metrics are disabled. When enabled, the broker sends metrics to the configured OTLP endpoint (e.g. an OpenTelemetry Collector).
82
+
72
83
  ### Policy Configuration
73
84
 
74
- Configure trading policies in `policy/policy.json`:
85
+ Configure trading policies in `policy/policy.json`.
86
+
87
+ - **Full reference**: see `POLICY.md` (supported options, matching rules, reload behaviour, and troubleshooting)
88
+ - **Example policy**: `policy/policy.json`
75
89
 
76
90
  ```json
77
91
  {
78
92
  "withdraw": {
79
- "rule": {
80
- "networks": ["BEP20", "ARBITRUM", "ETHEREUM"],
81
- "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"],
82
- "amounts": [
83
- {
84
- "ticker": "USDC",
85
- "max": 100000,
86
- "min": 1
87
- },
88
- {
89
- "ticker": "USDT",
90
- "max": 100000,
91
- "min": 1
92
- }
93
- ]
94
- }
93
+ "rule": [
94
+ {
95
+ "exchange": "BINANCE",
96
+ "network": "BEP20",
97
+ "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"]
98
+ },
99
+ {
100
+ "exchange": "BINANCE",
101
+ "network": "ARBITRUM",
102
+ "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"]
103
+ }
104
+ ]
95
105
  },
96
106
  "deposit": {},
97
107
  "order": {
@@ -103,10 +113,7 @@ Configure trading policies in `policy/policy.json`:
103
113
  "BINANCE:BTC/ETH"
104
114
  ],
105
115
  "limits": [
106
- { "from": "USDT", "to": "ETH", "min": 1, "max": 100000 },
107
- { "from": "ETH", "to": "USDT", "min": 0.5, "max": 5 },
108
- { "from": "ARB", "to": "USDC", "min": 1, "max": 1000 },
109
- { "from": "USDC", "to": "ARB", "min": 1, "max": 10000 }
116
+ { "from": "USDT", "to": "ETH", "min": 1, "max": 100000 }
110
117
  ]
111
118
  }
112
119
  }
@@ -394,6 +401,45 @@ const response = await client.ExecuteAction(request, metadata);
394
401
  - Monitor API usage and set appropriate rate limits
395
402
  - Use secondary brokers for redundancy and load distribution
396
403
 
404
+ ## 📊 OpenTelemetry Metrics
405
+
406
+ The broker exports metrics via **OpenTelemetry (OTLP)** for monitoring and analytics. Metrics are collected for:
407
+
408
+ - **ExecuteAction requests**: Request counts, success/failure rates, latency histograms
409
+ - **Subscribe streams**: Subscription counts, duration, error rates
410
+ - **Action-specific metrics**: Tagged by action type, CEX, and symbol
411
+
412
+ ### Metrics (OTLP)
413
+
414
+ The following metrics are exported as OTLP counters and histograms:
415
+
416
+ - `execute_action_requests_total` (counter): Total ExecuteAction requests
417
+ - `execute_action_success_total` (counter): Successful ExecuteAction requests
418
+ - `execute_action_errors_total` (counter): Failed ExecuteAction requests
419
+ - `execute_action_duration_ms` (histogram): ExecuteAction latency
420
+ - `subscribe_requests_total` (counter): Total Subscribe requests
421
+ - `subscribe_errors_total` (counter): Failed Subscribe requests
422
+ - `subscribe_duration_ms` (histogram): Subscribe stream duration
423
+
424
+ All metrics include attributes: `action`, `cex`, `symbol`, `error_type`, `service`.
425
+
426
+ ### Setting Up Metrics
427
+
428
+ 1. **Run an OTLP receiver** (e.g. [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/)):
429
+ - Default endpoint: `http://localhost:4318/v1/metrics`
430
+ - To store in ClickHouse or other backends, use the appropriate exporter in the collector pipeline (e.g. [ClickHouse exporter](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/clickhouseexporter)).
431
+
432
+ 2. **Configure the broker**:
433
+ - Set `OTEL_EXPORTER_OTLP_ENDPOINT` (e.g. `http://localhost:4318`) or use `CEX_BROKER_OTEL_HOST` (and optional `CEX_BROKER_OTEL_PORT`, `CEX_BROKER_OTEL_PROTOCOL`). Legacy `CEX_BROKER_CLICKHOUSE_*` env vars are also supported.
434
+
435
+ 3. Metrics are pushed periodically to the configured endpoint; no database schema is created by the broker (handled by the collector/backend).
436
+
437
+ **Local or Docker: OTLP → ClickHouse (no Prometheus)**
438
+ Use the included OpenTelemetry + ClickHouse stack so metrics go only to ClickHouse (no Prometheus exporter):
439
+
440
+ - **Docker**: `docker compose -f docker-compose.otel.yaml up -d`, then set `OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318` and start the broker.
441
+ - **Config**: `otel/collector-config.yaml` (OTLP receiver → batch → ClickHouse exporter). See [otel/README.md](otel/README.md) for full instructions (Docker and local).
442
+
397
443
  ## 🏗️ Architecture
398
444
 
399
445
  ### Project Structure
@@ -527,17 +573,18 @@ for (const [currency, info] of Object.entries(currencies)) {
527
573
  ```json
528
574
  {
529
575
  "withdraw": {
530
- "rule": {
531
- "networks": ["BEP20", "ARBITRUM", "ETH"], // Networks supported by your exchanges
532
- "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"],
533
- "amounts": [
534
- {
535
- "ticker": "USDT",
536
- "max": 100000,
537
- "min": 1
538
- }
539
- ]
540
- }
576
+ "rule": [
577
+ {
578
+ "exchange": "BINANCE",
579
+ "network": "BEP20",
580
+ "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"]
581
+ },
582
+ {
583
+ "exchange": "BINANCE",
584
+ "network": "ARBITRUM",
585
+ "whitelist": ["0x9d467fa9062b6e9b1a46e26007ad82db116c67cb"]
586
+ }
587
+ ]
541
588
  }
542
589
  }
543
590
  ```
@@ -572,6 +619,7 @@ bun run check
572
619
 
573
620
  ### Core Dependencies
574
621
 
622
+ - `@opentelemetry/*`: OpenTelemetry API, SDK metrics, OTLP HTTP exporter for metrics
575
623
  - `@grpc/grpc-js`: gRPC server implementation
576
624
  - `@grpc/proto-loader`: Protocol buffer loading
577
625
  - `@usherlabs/ccxt`: Enhanced CCXT library with Verity support