Skip to content

Commit 3bdb44c

Browse files
AgeManningmichaelsproul
authored andcommitted
Logging via the HTTP API (sigp#4074)
This PR adds the ability to read the Lighthouse logs from the HTTP API for both the BN and the VC. This is done in such a way to as minimize any kind of performance hit by adding this feature. The current design creates a tokio broadcast channel and mixes is into a form of slog drain that combines with our main global logger drain, only if the http api is enabled. The drain gets the logs, checks the log level and drops them if they are below INFO. If they are INFO or higher, it sends them via a broadcast channel only if there are users subscribed to the HTTP API channel. If not, it drops the logs. If there are more than one subscriber, the channel clones the log records and converts them to json in their independent HTTP API tasks. Co-authored-by: Michael Sproul <[email protected]>
1 parent 369151d commit 3bdb44c

File tree

20 files changed

+561
-42
lines changed

20 files changed

+561
-42
lines changed

beacon_node/client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2021"
66

77
[dev-dependencies]
88
serde_yaml = "0.8.13"
9-
logging = { path = "../../common/logging" }
109
state_processing = { path = "../../consensus/state_processing" }
1110
operation_pool = { path = "../operation_pool" }
1211
tokio = "1.14.0"
@@ -17,6 +16,7 @@ store = { path = "../store" }
1716
network = { path = "../network" }
1817
timer = { path = "../timer" }
1918
lighthouse_network = { path = "../lighthouse_network" }
19+
logging = { path = "../../common/logging" }
2020
parking_lot = "0.12.0"
2121
types = { path = "../../consensus/types" }
2222
eth2_config = { path = "../../common/eth2_config" }

beacon_node/client/src/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ where
486486
network_globals: None,
487487
eth1_service: Some(genesis_service.eth1_service.clone()),
488488
log: context.log().clone(),
489+
sse_logging_components: runtime_context.sse_logging_components.clone(),
489490
});
490491

491492
// Discard the error from the oneshot.
@@ -706,6 +707,7 @@ where
706707
network_senders: self.network_senders.clone(),
707708
network_globals: self.network_globals.clone(),
708709
eth1_service: self.eth1_service.clone(),
710+
sse_logging_components: runtime_context.sse_logging_components.clone(),
709711
log: log.clone(),
710712
});
711713

beacon_node/http_api/src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ pub async fn create_api_server_on_port<T: BeaconChainTypes>(
195195
network_senders: Some(network_senders),
196196
network_globals: Some(network_globals),
197197
eth1_service: Some(eth1_service),
198+
sse_logging_components: None,
198199
log,
199200
});
200201

beacon_node/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
11231123
.long("gui")
11241124
.hidden(true)
11251125
.help("Enable the graphical user interface and all its requirements. \
1126-
This is equivalent to --http and --validator-monitor-auto.")
1126+
This enables --http and --validator-monitor-auto and enables SSE logging.")
11271127
.takes_value(false)
11281128
)
11291129
.arg(

book/src/api-lighthouse.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,31 @@ Caveats:
678678
This is because the state _prior_ to the `start_epoch` needs to be loaded from the database, and
679679
loading a state on a boundary is most efficient.
680680

681+
682+
### `/lighthouse/logs`
683+
684+
This is a Server Side Event subscription endpoint. This allows a user to read
685+
the Lighthouse logs directly from the HTTP API endpoint. This currently
686+
exposes INFO and higher level logs. It is only enabled when the `--gui` flag is set in the CLI.
687+
688+
Example:
689+
690+
```bash
691+
curl -N "http://localhost:5052/lighthouse/logs"
692+
```
693+
694+
Should provide an output that emits log events as they occur:
695+
```json
696+
{
697+
"data": {
698+
"time": "Mar 13 15:28:41",
699+
"level": "INFO",
700+
"msg": "Syncing",
701+
"service": "slot_notifier",
702+
"est_time": "1 hr 27 mins",
703+
"speed": "5.33 slots/sec",
704+
"distance": "28141 slots (3 days 21 hrs)",
705+
"peers": "8"
706+
}
707+
}
708+
```

book/src/api-vc-endpoints.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,33 @@ The following fields may be omitted or nullified to obtain default values:
578578
### Example Response Body
579579

580580
*No data is included in the response body.*
581+
582+
## `GET /lighthouse/logs`
583+
584+
Provides a subscription to receive logs as Server Side Events. Currently the
585+
logs emitted are INFO level or higher.
586+
587+
### HTTP Specification
588+
589+
| Property | Specification |
590+
|-------------------|--------------------------------------------|
591+
| Path | `/lighthouse/logs` |
592+
| Method | GET |
593+
| Required Headers | None |
594+
| Typical Responses | 200 |
595+
596+
### Example Response Body
597+
598+
```json
599+
{
600+
"data": {
601+
"time": "Mar 13 15:26:53",
602+
"level": "INFO",
603+
"msg": "Connected to beacon node(s)",
604+
"service": "notifier",
605+
"synced": 1,
606+
"available": 1,
607+
"total": 1
608+
}
609+
}
610+
```

common/logging/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ test_logger = [] # Print log output to stderr when running tests instead of drop
1010
[dependencies]
1111
slog = "2.5.2"
1212
slog-term = "2.6.0"
13+
tokio = { version = "1.26.0", features = ["sync"] }
1314
lighthouse_metrics = { path = "../lighthouse_metrics" }
1415
lazy_static = "1.4.0"
1516
sloggers = { version = "2.1.1", features = ["json"] }
17+
slog-async = "2.7.0"
18+
take_mut = "0.2.2"
19+
parking_lot = "0.12.1"
20+
serde = "1.0.153"
21+
serde_json = "1.0.94"
22+
chrono = "0.4.23"

0 commit comments

Comments
 (0)