|
29 | 29 | %% Test cases |
30 | 30 | -export([ t_normal_flow/1 |
31 | 31 | , t_no_required_acks/1 |
| 32 | + , t_produce_request_telemetry/1 |
32 | 33 | , t_retry_on_same_connection/1 |
33 | 34 | , t_retry_on_kafka_storage_error/1 |
34 | 35 | , t_connection_down_retry/1 |
35 | 36 | , t_leader_migration/1 |
36 | 37 | ]). |
37 | 38 |
|
| 39 | +%% Telemetry test handler |
| 40 | +-export([ handle_telemetry_event/4 ]). |
| 41 | + |
38 | 42 |
|
39 | 43 | -include_lib("eunit/include/eunit.hrl"). |
40 | 44 | -include("brod_int.hrl"). |
@@ -168,6 +172,39 @@ t_no_required_acks(Config) when is_list(Config) -> |
168 | 172 | result = brod_produce_req_acked}, ok, 2000), |
169 | 173 | ok = brod_producer:stop(Producer). |
170 | 174 |
|
| 175 | +t_produce_request_telemetry(Config) when is_list(Config) -> |
| 176 | + Tester = self(), |
| 177 | + HandlerId = <<"t-produce-request-telemetry">>, |
| 178 | + ok = telemetry:attach(HandlerId, [brod, produce_request_sent], |
| 179 | + fun ?MODULE:handle_telemetry_event/4, |
| 180 | + #{pid => Tester}), |
| 181 | + try |
| 182 | + meck:expect(brod_client, get_leader_connection, |
| 183 | + fun(_, <<"topic">>, 0) -> {ok, Tester} end), |
| 184 | + meck:expect(kpro, request_async, |
| 185 | + fun(Connection, KafkaReq) -> |
| 186 | + Connection ! {request_async, KafkaReq}, |
| 187 | + ok |
| 188 | + end), |
| 189 | + ProducerConfig = [{max_linger_ms, 1000}, |
| 190 | + {max_linger_count, 3}], |
| 191 | + {ok, Producer} = brod_producer:start_link(client, <<"topic">>, 0, |
| 192 | + ProducerConfig), |
| 193 | + {ok, _} = brod_producer:produce(Producer, <<"k1">>, <<"v1">>), |
| 194 | + {ok, _} = brod_producer:produce(Producer, <<"k2">>, <<"v2">>), |
| 195 | + {ok, _} = brod_producer:produce(Producer, <<"k3">>, <<"v3">>), |
| 196 | + ?WAIT({request_async, _}, ok, 2000), |
| 197 | + ?WAIT({telemetry, [brod, produce_request_sent], Measurements, Metadata}, |
| 198 | + begin |
| 199 | + ?assertMatch(#{count := 3, bytes := Bytes} when Bytes > 0, |
| 200 | + Measurements), |
| 201 | + ?assertEqual(#{topic => <<"topic">>, partition => 0}, Metadata) |
| 202 | + end, 2000), |
| 203 | + ok = brod_producer:stop(Producer) |
| 204 | + after |
| 205 | + telemetry:detach(HandlerId) |
| 206 | + end. |
| 207 | + |
171 | 208 | t_retry_on_same_connection(Config) when is_list(Config) -> |
172 | 209 | Tester = self(), |
173 | 210 | %% A mocked connection process which expects 2 requests to be sent |
@@ -413,6 +450,9 @@ t_leader_migration(Config) when is_list(Config) -> |
413 | 450 |
|
414 | 451 | %%%_* Help functions =========================================================== |
415 | 452 |
|
| 453 | +handle_telemetry_event(Event, Measurements, Metadata, #{pid := Pid}) -> |
| 454 | + Pid ! {telemetry, Event, Measurements, Metadata}. |
| 455 | + |
416 | 456 | meck_module(Module) -> |
417 | 457 | meck:new(Module, [passthrough, no_passthrough_cover, no_history]). |
418 | 458 |
|
|
0 commit comments