From f04340aff47ba172c265f3dc4770cd9239323a7a Mon Sep 17 00:00:00 2001 From: Ty Bekiares <107002404+ty-elastic@users.noreply.github.com> Date: Thu, 1 Dec 2022 16:37:47 -0600 Subject: [PATCH] [grpcbox] add grpc conventions (#130) Co-authored-by: Tristan Sloughter --- instrumentation/opentelemetry_grpcbox/rebar.config | 4 +++- instrumentation/opentelemetry_grpcbox/rebar.lock | 11 ++++++++--- .../src/otel_grpcbox_interceptor.erl | 13 ++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry_grpcbox/rebar.config b/instrumentation/opentelemetry_grpcbox/rebar.config index bf0e602..3e0e171 100644 --- a/instrumentation/opentelemetry_grpcbox/rebar.config +++ b/instrumentation/opentelemetry_grpcbox/rebar.config @@ -1,7 +1,8 @@ {erl_opts, [debug_info]}. {deps, [grpcbox, - {opentelemetry_api, "~> 1.0"}]}. + {opentelemetry_api, "~> 1.0"}, + {opentelemetry_semantic_conventions, "~> 0.2.0"}]}. {project_plugins, [{rebar_covertool, "1.1.0"}, {grpcbox_plugin, "~> 0.7.0"}]}. @@ -16,6 +17,7 @@ {module_name_suffix, "_pb"}]}]}, {deps, [{opentelemetry, "~> 1.0"}, + {opentelemetry_semantic_conventions, "~> 0.2.0"}, jsx]}]}]}. {cover_enabled, true}. diff --git a/instrumentation/opentelemetry_grpcbox/rebar.lock b/instrumentation/opentelemetry_grpcbox/rebar.lock index 4ba15f9..d5d7f73 100644 --- a/instrumentation/opentelemetry_grpcbox/rebar.lock +++ b/instrumentation/opentelemetry_grpcbox/rebar.lock @@ -5,7 +5,10 @@ {<<"gproc">>,{pkg,<<"gproc">>,<<"0.8.0">>},1}, {<<"grpcbox">>,{pkg,<<"grpcbox">>,<<"0.14.0">>},0}, {<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},2}, - {<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.0.2">>},0}]}. + {<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.0.2">>},0}, + {<<"opentelemetry_semantic_conventions">>, + {pkg,<<"opentelemetry_semantic_conventions">>,<<"0.2.0">>}, + 0}]}. [ {pkg_hash,[ {<<"acceptor_pool">>, <<"43C20D2ACAE35F0C2BCD64F9D2BDE267E459F0F3FD23DAB26485BF518C281B21">>}, @@ -14,7 +17,8 @@ {<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>}, {<<"grpcbox">>, <<"3EB321BCD2275BAF8B54CF381FEB7B0559A50C02544DE28FDA039C7F2F9D1A7A">>}, {<<"hpack">>, <<"17670F83FF984AE6CD74B1C456EDDE906D27FF013740EE4D9EFAA4F1BF999633">>}, - {<<"opentelemetry_api">>, <<"91353EE40583B1D4F07D7B13ED62642ABFEC6AAA0D8A2114F07EDAFB2DF781C5">>}]}, + {<<"opentelemetry_api">>, <<"91353EE40583B1D4F07D7B13ED62642ABFEC6AAA0D8A2114F07EDAFB2DF781C5">>}, + {<<"opentelemetry_semantic_conventions">>, <<"B67FE459C2938FCAB341CB0951C44860C62347C005ACE1B50F8402576F241435">>}]}, {pkg_hash_ext,[ {<<"acceptor_pool">>, <<"0CBCD83FDC8B9AD2EEE2067EF8B91A14858A5883CB7CD800E6FCD5803E158788">>}, {<<"chatterbox">>, <<"722FE2BAD52913AB7E87D849FC6370375F0C961FFB2F0B5E6D647C9170C382A6">>}, @@ -22,5 +26,6 @@ {<<"gproc">>, <<"580ADAFA56463B75263EF5A5DF4C86AF321F68694E7786CB057FD805D1E2A7DE">>}, {<<"grpcbox">>, <<"E24159B7B6D3F9869BBE528845C0125FED2259366BA908FD04A1F45FE81D0660">>}, {<<"hpack">>, <<"06F580167C4B8B8A6429040DF36CC93BBA6D571FAEAEC1B28816523379CBB23A">>}, - {<<"opentelemetry_api">>, <<"2A8247F85C44216B883900067478D59955D11E58E5CFCA7C884CD4F203ACE3AC">>}]} + {<<"opentelemetry_api">>, <<"2A8247F85C44216B883900067478D59955D11E58E5CFCA7C884CD4F203ACE3AC">>}, + {<<"opentelemetry_semantic_conventions">>, <<"D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895">>}]} ]. diff --git a/instrumentation/opentelemetry_grpcbox/src/otel_grpcbox_interceptor.erl b/instrumentation/opentelemetry_grpcbox/src/otel_grpcbox_interceptor.erl index 8973395..add711d 100644 --- a/instrumentation/opentelemetry_grpcbox/src/otel_grpcbox_interceptor.erl +++ b/instrumentation/opentelemetry_grpcbox/src/otel_grpcbox_interceptor.erl @@ -30,13 +30,18 @@ recv_msg/3]). -include_lib("opentelemetry_api/include/otel_tracer.hrl"). +-include_lib("opentelemetry_api/include/opentelemetry.hrl"). +-include_lib("opentelemetry_semantic_conventions/include/trace.hrl"). + +-define(RPC_SYSTEM_GRPC, 'grpc'). unary_client(Ctx, _Channel, Handler, FullMethod, Input, _Def, _Options) -> Metadata = otel_propagator_text_map:inject(opentelemetry:get_text_map_injector(), #{}, fun set_metadata/3), Ctx1 = grpcbox_metadata:append_to_outgoing_ctx(Ctx, Metadata), - ?with_span(FullMethod, #{}, fun(_) -> + ?with_span(FullMethod, #{kind => ?SPAN_KIND_CLIENT, + attributes => #{?RPC_SYSTEM => ?RPC_SYSTEM_GRPC}}, fun(_) -> Handler(Ctx1, Input) end). @@ -52,14 +57,16 @@ recv_msg(#{client_stream := ClientStream}, Streamer, Input) -> unary(Ctx, Message, _ServerInfo=#{full_method := FullMethod}, Handler) -> otel_ctx_from_ctx(Ctx), - ?with_span(FullMethod, #{}, fun(_) -> + ?with_span(FullMethod, #{kind => ?SPAN_KIND_SERVER, + attributes => #{?RPC_SYSTEM => ?RPC_SYSTEM_GRPC}}, fun(_) -> Handler(Ctx, Message) end). stream(Ref, Stream, _ServerInfo=#{full_method := FullMethod}, Handler) -> Ctx = grpcbox_stream:ctx(Stream), otel_ctx_from_ctx(Ctx), - ?with_span(FullMethod, #{}, fun(_) -> + ?with_span(FullMethod, #{kind => ?SPAN_KIND_SERVER, + attributes => #{?RPC_SYSTEM => ?RPC_SYSTEM_GRPC}}, fun(_) -> Handler(Ref, Stream) end).