Add server span kind to otel cowboy (#116)

* Add server span kind to otel cowboy

* Upgrade deps
This commit is contained in:
Alberto Sartori 2022-11-10 12:15:15 +01:00 committed by GitHub
parent 4358c75b99
commit a72305f574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 13 deletions

View File

@ -2,7 +2,7 @@
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.9.0">>},1},
{<<"cowboy_telemetry">>,{pkg,<<"cowboy_telemetry">>,<<"0.4.0">>},0},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},2},
{<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.0.2">>},0},
{<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.1.1">>},0},
{<<"opentelemetry_telemetry">>,
{pkg,<<"opentelemetry_telemetry">>,<<"1.0.0">>},
0},
@ -14,7 +14,7 @@
{<<"cowboy">>, <<"865DD8B6607E14CF03282E10E934023A1BD8BE6F6BACF921A7E2A96D800CD452">>},
{<<"cowboy_telemetry">>, <<"F239F68B588EFA7707ABCE16A84D0D2ACF3A0F50571F8BB7F56A15865AAE820C">>},
{<<"cowlib">>, <<"0B9FF9C346629256C42EBE1EEB769A83C6CB771A6EE5960BD110AB0B9B872063">>},
{<<"opentelemetry_api">>, <<"91353EE40583B1D4F07D7B13ED62642ABFEC6AAA0D8A2114F07EDAFB2DF781C5">>},
{<<"opentelemetry_api">>, <<"3B43877C456C8A7F5448A95D9BF4FB4BB8CC2ABBBEA2C62D5F8E8C538B4AF14F">>},
{<<"opentelemetry_telemetry">>, <<"D5982A319E725FCD2305B306B65C18A86AFDCF7D96821473CF0649FF88877615">>},
{<<"ranch">>, <<"8C7A100A139FD57F17327B6413E4167AC559FBC04CA7448E9BE9057311597A1D">>},
{<<"telemetry">>, <<"A589817034A27EAB11144AD24D5C0F9FAB1F58173274B1E9BAE7074AF9CBEE51">>},
@ -23,7 +23,7 @@
{<<"cowboy">>, <<"2C729F934B4E1AA149AFF882F57C6372C15399A20D54F65C8D67BEF583021BDE">>},
{<<"cowboy_telemetry">>, <<"7D98BAC1EE4565D31B62D59F8823DFD8356A169E7FCBB83831B8A5397404C9DE">>},
{<<"cowlib">>, <<"2B3E9DA0B21C4565751A6D4901C20D1B4CC25CBB7FD50D91D2AB6DD287BC86A9">>},
{<<"opentelemetry_api">>, <<"2A8247F85C44216B883900067478D59955D11E58E5CFCA7C884CD4F203ACE3AC">>},
{<<"opentelemetry_api">>, <<"A9554B3208B60A70043318D051EA78FBBC7A1B8F4C418EBC16CCB40015995675">>},
{<<"opentelemetry_telemetry">>, <<"3401D13A1D4B7AA941A77E6B3EC074F0AE77F83B5B2206766CE630123A9291A9">>},
{<<"ranch">>, <<"49FBCFD3682FAB1F5D109351B61257676DA1A2FDBE295904176D5E521A2DDFE5">>},
{<<"telemetry">>, <<"B727B2A1F75614774CFF2D7565B64D0DFA5BD52BA517F16543E6FC7EFCC0DF48">>},

View File

@ -46,7 +46,8 @@ handle_event([cowboy, request, start], _Measurements, #{req := Req} = Meta, _Con
'net.transport' => 'IP.TCP'
},
SpanName = iolist_to_binary([<<"HTTP ">>, Method]),
otel_telemetry:start_telemetry_span(?TRACER_ID, SpanName, Meta, #{attributes => Attributes});
Opts = #{attributes => Attributes, kind => ?SPAN_KIND_SERVER},
otel_telemetry:start_telemetry_span(?TRACER_ID, SpanName, Meta, Opts);
handle_event([cowboy, request, stop], Measurements, Meta, _Config) ->
Ctx = otel_telemetry:set_current_telemetry_span(?TRACER_ID, Meta),
@ -107,7 +108,8 @@ handle_event([cowboy, request, early_error], Measurements, Meta, _Config) ->
'http.status_code' => StatusCode,
'http.response_content_length' => maps:get(resp_body_length, Measurements)
},
Ctx = otel_telemetry:start_telemetry_span(?TRACER_ID, <<"HTTP Error">>, Meta, #{attributes => Attributes}),
Opts = #{attributes => Attributes, kind => ?SPAN_KIND_SERVER},
Ctx = otel_telemetry:start_telemetry_span(?TRACER_ID, <<"HTTP Error">>, Meta, Opts),
otel_span:add_events(Ctx, [opentelemetry:event(ErrorType, #{error => Error, reason => Reason})]),
otel_span:set_status(Ctx, opentelemetry:status(?OTEL_STATUS_ERROR, Reason)),
otel_telemetry:end_telemetry_span(?TRACER_ID, Meta),

View File

@ -71,9 +71,10 @@ successful_request(_Config) ->
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, _Body}} =
httpc:request(get, {"http://localhost:8080/success", Headers}, [], []),
receive
{span, #span{name=Name,attributes=Attributes,parent_span_id=ParentSpanId}} ->
{span, #span{name=Name,attributes=Attributes,parent_span_id=ParentSpanId,kind=Kind}} ->
?assertEqual(<<"HTTP GET">>, Name),
?assertEqual(13235353014750950193, ParentSpanId),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"203.0.133.195">>,
'http.flavor' => '1.1',
@ -97,8 +98,9 @@ chunked_request(_Config) ->
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, _Body}} =
httpc:request(get, {"http://localhost:8080/chunked", []}, [], []),
receive
{span, #span{name=Name,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
?assertEqual(<<"HTTP GET">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"127.0.0.1">>,
'http.flavor' => '1.1',
@ -122,10 +124,11 @@ failed_request(_Config) ->
{ok, {{_Version, 500, _ReasonPhrase}, _Headers, _Body}} =
httpc:request(get, {"http://localhost:8080/failure", []}, [], []),
receive
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
[Event] = otel_events:list(Events),
#event{name= <<"exception">>} = Event,
?assertEqual(<<"HTTP GET">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"127.0.0.1">>,
'http.flavor' => '1.1',
@ -149,7 +152,7 @@ client_timeout_request(_Config) ->
{error, timeout} =
httpc:request(get, {"http://localhost:8080/slow", []}, [{timeout, 50}], []),
receive
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
[Event] = otel_events:list(Events),
#event{name='socket_error',attributes = EventAttributes} = Event,
ExpectedEventAttrs = #{
@ -158,6 +161,7 @@ client_timeout_request(_Config) ->
},
?assertMatch(ExpectedEventAttrs, otel_attributes:map(EventAttributes)),
?assertEqual(<<"HTTP GET">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"127.0.0.1">>,
'http.flavor' => '1.1',
@ -180,7 +184,7 @@ idle_timeout_request(_Config) ->
{error, socket_closed_remotely} =
httpc:request(head, {"http://localhost:8080/slow", []}, [], []),
receive
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
[Event] = otel_events:list(Events),
#event{name= 'connection_error',attributes = EventAttributes} = Event,
ExpectedEventAttrs = #{
@ -189,6 +193,7 @@ idle_timeout_request(_Config) ->
},
?assertMatch(ExpectedEventAttrs, otel_attributes:map(EventAttributes)),
?assertEqual(<<"HTTP HEAD">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"127.0.0.1">>,
'http.flavor' => '1.1',
@ -210,8 +215,9 @@ idle_timeout_request(_Config) ->
chunk_timeout_request(_Config) ->
httpc:request(head, {"http://localhost:8080/chunked_slow", []}, [], []),
receive
{span, #span{name=Name,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
?assertEqual(<<"HTTP HEAD">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.client_ip' => <<"127.0.0.1">>,
'http.flavor' => '1.1',
@ -239,7 +245,7 @@ bad_request(_Config) ->
{ok, {{_Version, 501, _ReasonPhrase}, _Headers, _Body}} =
httpc:request(trace, {"http://localhost:8080/", Headers}, [], []),
receive
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined}} ->
{span, #span{name=Name,events=Events,attributes=Attributes,parent_span_id=undefined,kind=Kind}} ->
[Event] = otel_events:list(Events),
#event{name='connection_error',attributes = EventAttributes} = Event,
ExpectedEventAttrs = #{
@ -248,6 +254,7 @@ bad_request(_Config) ->
},
?assertMatch(ExpectedEventAttrs, otel_attributes:map(EventAttributes)),
?assertEqual(<<"HTTP Error">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
ExpectedAttrs = #{
'http.status_code' => 501,
'http.response_content_length' => 0},
@ -265,8 +272,9 @@ binary_status_code_request(_Config) ->
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, _Body}} =
httpc:request(get, {"http://localhost:8080/binary_status_code", Headers}, [], []),
receive
{span, #span{name=Name,attributes=Attributes,parent_span_id=ParentSpanId}} ->
{span, #span{name=Name,attributes=Attributes,parent_span_id=ParentSpanId,kind=Kind}} ->
?assertEqual(<<"HTTP GET">>, Name),
?assertEqual(?SPAN_KIND_SERVER, Kind),
?assertEqual(13235353014750950193, ParentSpanId),
ExpectedAttrs = #{
'http.client_ip' => <<"203.0.133.195">>,