1717-export ([init /1 , report /2 ]).
1818
1919-include_lib (" opencensus/include/opencensus.hrl" ).
20+
21+ -ifdef (OTP_RELEASE ).
2022-include_lib (" kernel/include/logger.hrl" ).
23+ -else .
24+ -define (LOG_ERROR (Format , Data ), error_logger :error_msg (Format , Data )).
25+ -endif .
2126
2227-define (TRACER_VERSION , " OC/0.1.0" ).
2328
@@ -32,14 +37,20 @@ init(Options) ->
3237 Port = proplists :get_value (port , Options , ? DEFAULT_PORT ),
3338 Service = proplists :get_value (service , Options , ? DEFAULT_SERVICE ),
3439 Type = proplists :get_value (type , Options , ? DEFAULT_TYPE ),
35- #{host => Host , port => Port , service => Service , type => Type }.
40+ Client = proplists :get_value (http_client , Options , fun default_client /3 ),
41+ #{host => Host ,
42+ port => Port ,
43+ service => Service ,
44+ type => Type ,
45+ client => Client }.
3646
3747-spec report (nonempty_list (opencensus :span ()), oc_reporter :opts ()) -> ok .
3848report (Spans , #{
3949 service := Service ,
4050 host := Host ,
4151 port := Port ,
42- type := Type }) ->
52+ type := Type ,
53+ client := Client }) ->
4354 Sorted = lists :sort (fun (A , B ) ->
4455 A # span .trace_id =< B # span .trace_id
4556 end , Spans ),
@@ -55,15 +66,9 @@ report(Spans, #{
5566 {" Datadog-Meta-Lang-Interpreter" , interpreter_version ()},
5667 {" Datadog-Meta-Tracer-Version" , ? TRACER_VERSION }
5768 ],
58- case httpc :request (
59- put ,
60- {Address , Headers , " application/json" , JSON },
61- [],
62- []
63- ) of
64- {ok , {{_ , Code , _ }, _ , _ }} when Code >= 200 , Code =< 299 ->
65- ok ;
66- {ok , {{_ , Code , _ }, _ , Message }} ->
69+ case Client (Address , Headers , JSON ) of
70+ ok -> ok ;
71+ {error , {http_error , Code , Message }} ->
6772 ? LOG_ERROR (" DD: Unable to send spans,"
6873 " DD reported an error: ~p : ~p " ,
6974 [Code , Message ]);
@@ -76,8 +81,23 @@ report(Spans, #{
7681 ? LOG_ERROR (" DD: Can't spans encode to json: ~p " , [Error ])
7782 end .
7883
84+ default_client (Address , Headers , JSON ) ->
85+ case httpc :request (
86+ put ,
87+ {Address , Headers , " application/json" , JSON },
88+ [],
89+ []
90+ ) of
91+ {ok , {{_ , Code , _ }, _ , _ }} when Code >= 200 , Code =< 299 ->
92+ ok ;
93+ {ok , {{_ , Code , _ }, _ , Body }} ->
94+ {error , {http_error , Code , Body }};
95+ {error , Reason } ->
96+ {error , Reason }
97+ end .
98+
7999lang_version () ->
80- erlang :system_info (otp_version ).
100+ erlang :system_info (otp_release ).
81101
82102interpreter_version () ->
83103 io_lib :format ('~s-~s' , [erlang :system_info (version ),
0 commit comments