Skip to content

Commit 977fd66

Browse files
sylaneziopio
andauthored
Change logging API from push to pull (#55)
* Change logging API from push to pull * Upgrade to grisp_cryptoauth 2.4.1 * Update jarl version to 1.0.1 * Add reboot flags in grisp.ini * Prevent crash if grisp_connect log handler is not setup properly * Update log configuration in README * Cleanup changelog --------- Co-authored-by: Luca Succi <[email protected]>
1 parent b4e28c8 commit 977fd66

17 files changed

+346
-235
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ road for namespaces. foo.bar.Buz is parsed into [foo, bar, <<"Buz">>] (if foo
2121
and bar are already existing atoms, but 'Buz' is not).
2222
- Upgrade grisp dependency to 2.8.0.
2323
- Add jittered exponential backoff for reconnection.
24+
- Changed logging API from push to pull. Instead of the client (grisp_connect)
25+
pushing batches of log event to the server (grisp.io), the server is now pulling
26+
them with the request `log.get`. In order to synchronize the client ring buffer,
27+
the server sends `log.sync` notifications.
2428

2529
## Fixed
2630

@@ -45,8 +49,6 @@ disconnected from the server.
4549

4650
## [1.0.0] - 2024-09-26
4751

48-
### What's Changed
49-
5052
#### Added
5153
- NTP handling
5254
- Connect to GRiSP.io
@@ -57,8 +59,6 @@ disconnected from the server.
5759
- Use grisp_cryptoauth TLS helper to generate TLS options.
5860
- Start integrating grisp updater.
5961

60-
**Full Changelog**: https://github.com/grisp/grisp_connect/commits/1.0.0
61-
6262
[Unreleased]: https://github.com/grisp/grisp_connect/compare/1.1.0...HEAD
6363
[1.1.0]: https://github.com/grisp/grisp_connect/compare/1.0.0...1.1.0
6464
[1.0.0]: https://github.com/grisp/grisp_connect/compare/6b59d16383b3e5154ef839bcf5c77a6b770aada5...1.0.0

README.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,34 +153,44 @@ grisp_connect sets the following options as default values if no `tls_server_tru
153153
## See all Logs on GRiSP.io
154154

155155
Once this app is started, it forwards all logs to GRiSP.io without the need of setting up anything. The only logs that we do not catch are the ones generated before `grisp_connect` boots.
156-
If you want to see ALL logs, even from applications that boot before `grisp_connect`, you need to disable the default logger handler and set the grisp_connect handler as the default one. This involves changing the `kernel` and `grisp_connect` app configuration settings in your sys.config file.
156+
If you want to see ALL logs, even from applications that boot before `grisp_connect`, you need to add the log handler in the kernel configuration and disable the one defined in the `grisp_connect` application configuration. This involves changing the `kernel` and `grisp_connect` app configuration settings in your sys.config file.
157157

158-
You can copy paste these settings. Here we both swap the default logger handler with the grisp_connect logger handler and also request it to print logs to stdout.
158+
You can copy paste these settings:
159159

160160
```erlang
161161
% sys.config
162162
[
163163
{kernel, [
164-
% Disable 'default' handler (which buffers all log events in logger).
165-
{logger, [{handler, default, undefined}]}
166-
]},
167-
{grisp_connect,[
164+
{logger_level, notice},
168165
{logger, [
169-
% Enable the grisp_connect handler as default,
170-
% so that it will receive all events from boot
171-
{handler,
172-
default, % name
173-
grisp_connect_logger_bin, % module
174-
#{
175-
formatter => {grisp_connect_logger_bin, #{
176-
% To see logs printed on the USB serial appoint a logger
177-
% formatter module of your choice and set the stdout
178-
% configuration stdout => {Formatter, FormatterConfig}
179-
stdout => {logger_formatter, #{}}
180-
}}
166+
{handler, default, logger_std_h, #{
167+
level => notice,
168+
filter_default => log,
169+
filters => [
170+
% Filter out supervisor progress reports so TLS certificates
171+
% are not swamping the console if the level is set to info...
172+
{disable_progress, {fun logger_filters:progress/2, stop}}
173+
]
174+
}}
175+
{handler, grisp_connect_log_handler, grisp_connect_logger_bin, #{
176+
level => notice,
177+
filter_default => log,
178+
formatter => {grisp_connect_logger_bin, #{}},
179+
filters => [
180+
% Filter out supervisor progress reports so TLS certificates
181+
% are not swamping grisp.io if level is set to info...
182+
{disable_progress, {fun logger_filters:progress/2, stop}}
183+
]
181184
}
182185
}
183186
]}
187+
]},
188+
{grisp_connect, [
189+
% Disable the log handler defined in grisp_connect application default
190+
% configuration, as it was explicitly started in kernel configuration
191+
% in order to catch the log entries before grisp_connect is started.
192+
{logger, []}
193+
]}
184194
]}
185195
].
186196
```

docs/grisp_connect_api.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ This should only be called if the new software is functioning as expected.
137137
</p>
138138
</details>
139139

140+
<details><summary><code>log.get</code> - retrieve a batch of log entry from the device </summary>
141+
<p>
142+
143+
**`params`:**
144+
| key (required *) | value | description |
145+
| ----------------- | -------- | -------------------------------------- |
146+
| `"max_batch_size"`| integer | Maximum number of events in the result |
147+
| `"max_byte_size"` | integer | Maximum byte size of the result |
148+
149+
**`result`**: JSON Object
150+
| key(required *) | value | description |
151+
|-----------------|----------------|-------------------------------|
152+
| dropped * | integer | Number of dropped log entries |
153+
| events * | list of Events | The list of log events |
154+
155+
**`event format`:**
156+
Each log event is a list of two elements, first the sequence number of the
157+
event, and then an object describing the log event with the following fields:
158+
- `meta`: meta data of the log entry as an object:
159+
- `time`: log time in microseconds.
160+
- `file`: `null` or a filename as a string.
161+
- `mfa`: `null` or the function the log is from as a list with module name
162+
as a tring, function name as a string and arity as an integer.
163+
- `msg`: the log entry message, either as a string, or as a json object if it
164+
is a report entry.
165+
- `level`: the log level as a string.
166+
167+
140168
### Notifications
141169

142170
<details><summary><code>update</code> <code>{"type":"software_update_event"}</code> - notify the current progess of grisp_updater </summary>
@@ -154,6 +182,16 @@ This should only be called if the new software is functioning as expected.
154182
</p>
155183
</details>
156184

185+
<details><summary><code>log.sync</code> - synchronize the device log buffer, truncating the entries the server is aware of </summary>
186+
<p>
187+
188+
**`params`:**
189+
| key (required *) | value | description |
190+
| ----------------- | -------- | ------------------------------------------------ |
191+
| `"seq"` * | integer | The sequence number of the last stored log event |
192+
| `"dropped"` * | integer | The number of "confirmed dropped log events |
193+
194+
157195
## Error Codes
158196

159197
### Default error codes

docs/grisp_connect_architecture.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ graph TD
66
77
subgraph GrispConnectApp[Grisp Connect Application]
88
GrispConnectRootSup[Root Supervisor<br>grisp_connect_sup]
9-
GrispConnectLogServer[Log Server<br>grisp_connect_log_server]
109
GrispConnectClient[Client<br>grisp_connect_client]
1110
12-
GrispConnectRootSup --Supervise--> GrispConnectLogServer
1311
GrispConnectRootSup --Supervise--> GrispConnectClient
1412
GrispConnectClient --Spawn and Monitor--> JarlConnection
1513
end

grisp/grisp2/common/deploy/files/grisp.ini.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[erlang]
22
args = erl.rtems -C multi_time_warp -- -mode embedded -home . -pa . -root {{release_name}} -bindir {{release_name}}/erts-{{erts_vsn}}/bin -boot {{release_name}}/releases/{{release_version}}/start -config {{release_name}}/releases/{{release_version}}/sys.config -kernel inetrc "./erl_inetrc"
3+
shell = erlang
4+
on_exit = reboot
5+
on_crash = reboot
36

47
[network]
58
ip_self=dhcp

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{erl_opts, [debug_info]}.
22
{deps, [
33
jsx,
4-
{jarl, {git, "https://github.com/grisp/jarl.git"}},
4+
jarl,
55
{grisp, "~> 2.7"},
66
{grisp_cryptoauth, "~> 2.4"},
77
{certifi, "2.13.0"}

rebar.lock

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22
[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.13.0">>},0},
33
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.13.0">>},2},
44
{<<"grisp">>,{pkg,<<"grisp">>,<<"2.8.0">>},0},
5-
{<<"grisp_cryptoauth">>,{pkg,<<"grisp_cryptoauth">>,<<"2.4.0">>},0},
5+
{<<"grisp_cryptoauth">>,{pkg,<<"grisp_cryptoauth">>,<<"2.4.1">>},0},
66
{<<"gun">>,{pkg,<<"gun">>,<<"2.1.0">>},1},
7-
{<<"jarl">>,
8-
{git,"https://github.com/grisp/jarl.git",
9-
{ref,"8d20c3ca314aa5c448d5e16f52a2b887e11b26f7"}},
10-
0},
7+
{<<"jarl">>,{pkg,<<"jarl">>,<<"1.0.1">>},0},
118
{<<"jsx">>,{pkg,<<"jsx">>,<<"3.1.0">>},0},
129
{<<"mapz">>,{pkg,<<"mapz">>,<<"2.4.0">>},1}]}.
1310
[
1411
{pkg_hash,[
1512
{<<"certifi">>, <<"E52BE248590050B2DD33B0BB274B56678F9068E67805DCA8AA8B1CCDB016BBF6">>},
1613
{<<"cowlib">>, <<"DB8F7505D8332D98EF50A3EF34B34C1AFDDEC7506E4EE4DD4A3A266285D282CA">>},
1714
{<<"grisp">>, <<"E0B7D4C3464702DBD62A867EDE76488226B0772B29DF7DBAE4E08E474F2B81F5">>},
18-
{<<"grisp_cryptoauth">>, <<"60773DFCB597893A1E98BFE2974A74C277D9FF6CE16BD918BFD906D43AAB86C0">>},
15+
{<<"grisp_cryptoauth">>, <<"AA623B135F000E03DBA0FCBB64C08B5F31E29B1DAC2B4E7A106BAFCA3878141C">>},
1916
{<<"gun">>, <<"B4E4CBBF3026D21981C447E9E7CA856766046EFF693720BA43114D7F5DE36E87">>},
17+
{<<"jarl">>, <<"B92ECC182498C33E9ED31DF7A0EF4BECAFAC0E1CB1C118C618E662BCC45CE7E9">>},
2018
{<<"jsx">>, <<"D12516BAA0BB23A59BB35DCCAF02A1BD08243FCBB9EFE24F2D9D056CCFF71268">>},
2119
{<<"mapz">>, <<"77A8E38B69BAB16C5D3EBD44E6C619F8AF1F1598B0CAAE301D266605A0865756">>}]},
2220
{pkg_hash_ext,[
2321
{<<"certifi">>, <<"8F3D9533A0F06070AFDFD5D596B32E21C6580667A492891851B0E2737BC507A1">>},
2422
{<<"cowlib">>, <<"E1E1284DC3FC030A64B1AD0D8382AE7E99DA46C3246B815318A4B848873800A4">>},
2523
{<<"grisp">>, <<"4BF7D6E31B03884C13402694D8CDC9B5ECADB53EA1B405453581FD2248D38928">>},
26-
{<<"grisp_cryptoauth">>, <<"D9BD51BC877986404FCF6DB1E3DF196C919BD6F55398FA03262D1C4323410AB9">>},
24+
{<<"grisp_cryptoauth">>, <<"A3B997EB54C6FC96B9FFCBB6207F453CD28CD86B4B44D4A6DE703174B4AAE065">>},
2725
{<<"gun">>, <<"52FC7FC246BFC3B00E01AEA1C2854C70A366348574AB50C57DFE796D24A0101D">>},
26+
{<<"jarl">>, <<"6786787231E0E966BB01B30EEFEC353D6760F5C490B419A126726AFB46BB1437">>},
2827
{<<"jsx">>, <<"0C5CC8FDC11B53CC25CF65AC6705AD39E54ECC56D1C22E4ADB8F5A53FB9427F3">>},
2928
{<<"mapz">>, <<"4B68DF5CF0522E0D6545DF7B681BC052865CDB78405AD4CC9C55FE45EE7B25BE">>}]}
3029
].

src/grisp_connect.app.src

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
{ws_request_timeout, 5_000},
2727
{ws_ping_timeout, 60_000},
2828
{ws_max_retries, infinity},
29-
{logs_interval, 2_000},
3029
{logs_batch_size, 100},
3130
{logger, [
3231
% Enable our own default handler,

src/grisp_connect_api.erl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@
1919
when Msg :: {request, Method :: jarl:method(), Params :: map() | list(), ReqRef :: binary() | integer()}
2020
| {notification, jarl:method(), Params :: map() | list()}.
2121
handle_msg({notification, M, Params}) ->
22-
?LOG_ERROR("Received unexpected notification ~p: ~p", [M, Params]),
23-
ok;
24-
handle_msg({request, M, Params, ID})
25-
when M == [?method_post]; M == [?method_get] ->
22+
handle_notification(M, Params);
23+
handle_msg({request, M, Params, ID}) ->
2624
handle_request(M, Params, ID).
2725

2826

2927
%--- Internal Funcitons --------------------------------------------------------
3028

29+
handle_notification([log, sync], Params) ->
30+
grisp_connect_log:sync(Params);
31+
handle_notification(Method, Params) ->
32+
?LOG_ERROR("Received unexpected notification ~p: ~p", [Method, Params]),
33+
ok.
34+
3135
handle_request([?method_get], #{type := <<"system_info">>} = _Params, ID) ->
3236
Info = grisp_connect_updater:system_info(),
3337
{reply, Info, ID};
@@ -73,5 +77,8 @@ handle_request([?method_post], #{type := <<"cancel">>}, ID) ->
7377
ok ->
7478
{reply, ok, ID}
7579
end;
76-
handle_request(_T, _P, ID) ->
80+
handle_request([log, get], Params, ID) ->
81+
{reply, grisp_connect_log:get(Params), ID};
82+
handle_request(Method, Params, ID) ->
83+
?LOG_ERROR("Received unexpected request ~p: ~p", [Method, Params]),
7784
{error, method_not_found, undefined, undefined, ID}.

src/grisp_connect_client.erl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ connecting(info, {jarl, Conn, {connected, _}}, Data = #data{conn = Conn}) ->
213213
connected(enter, _OldState, Data = #data{wait_calls = WaitCalls}) ->
214214
% When entering connected, we reply to all wait_connected calls with ok
215215
gen_statem:reply([{reply, F, ok} || F <- WaitCalls]),
216-
grisp_connect_log_server:start(),
217216
{keep_state, Data#data{wait_calls = [], last_error = undefined}};
218217
connected({call, From}, is_connected, _) ->
219218
{keep_state_and_data, [{reply, From, true}]};
@@ -371,13 +370,11 @@ conn_start(Data = #data{conn = undefined,
371370
conn_close(Data = #data{conn = undefined}, _Reason) ->
372371
Data;
373372
conn_close(Data = #data{conn = Conn}, _Reason) ->
374-
grisp_connect_log_server:stop(),
375373
jarl:disconnect(Conn),
376374
Data#data{conn = undefined}.
377375

378376
% Safe to call in any state
379377
conn_died(Data) ->
380-
grisp_connect_log_server:stop(),
381378
Data#data{conn = undefined}.
382379

383380
-spec conn_request(data(), jarl:method(), atom(), map(),

0 commit comments

Comments
 (0)