From 211f16c46f9813edad3ce8dad002881760b0925b Mon Sep 17 00:00:00 2001 From: Aleksey Kashapov Date: Thu, 26 Oct 2023 13:45:45 +0300 Subject: [PATCH] TD-686: Adopts opentelemetry API (#19) --- Makefile | 3 +-- apps/akm/src/akm.app.src | 5 +++- apps/akm/src/akm_handler.erl | 26 ++++++++++++++++--- compose.tracing.yaml | 21 ++++++++++++++++ compose.yaml | 24 ------------------ rebar.config | 10 ++++++-- rebar.lock | 48 +++++++++++++++++++++++++++++++----- 7 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 compose.tracing.yaml diff --git a/Makefile b/Makefile index 403f9be..2c391a2 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ DEV_IMAGE_ID = $(file < .image.dev) DOCKER ?= docker DOCKERCOMPOSE ?= docker-compose -DOCKERCOMPOSE_W_ENV = DEV_IMAGE_TAG=$(DEV_IMAGE_TAG) $(DOCKERCOMPOSE) +DOCKERCOMPOSE_W_ENV = DEV_IMAGE_TAG=$(DEV_IMAGE_TAG) $(DOCKERCOMPOSE) -f compose.yaml -f compose.tracing.yaml REBAR ?= rebar3 TEST_CONTAINER_NAME ?= testrunner @@ -44,7 +44,6 @@ DOCKER_WC_OPTIONS := -v $(PWD):$(PWD) --workdir $(PWD) DOCKER_WC_EXTRA_OPTIONS ?= --rm DOCKER_RUN = $(DOCKER) run -t $(DOCKER_WC_OPTIONS) $(DOCKER_WC_EXTRA_OPTIONS) - DOCKERCOMPOSE_RUN = $(DOCKERCOMPOSE_W_ENV) run --rm $(DOCKER_WC_OPTIONS) # Utility tasks diff --git a/apps/akm/src/akm.app.src b/apps/akm/src/akm.app.src index c4be209..4f80038 100644 --- a/apps/akm/src/akm.app.src +++ b/apps/akm/src/akm.app.src @@ -29,7 +29,10 @@ woody_user_identity, erlydtl, gen_smtp, - canal + canal, + opentelemetry_api, + opentelemetry_exporter, + opentelemetry ]}, {env, []} ]}. diff --git a/apps/akm/src/akm_handler.erl b/apps/akm/src/akm_handler.erl index ddbd2d0..f684f7f 100644 --- a/apps/akm/src/akm_handler.erl +++ b/apps/akm/src/akm_handler.erl @@ -1,5 +1,8 @@ -module(akm_handler). +-include_lib("opentelemetry_api/include/otel_tracer.hrl"). +-include_lib("opentelemetry_api/include/opentelemetry.hrl"). + -behaviour(swag_server_apikeys_logic_handler). %% swag_server_apikeys_logic_handler callbacks @@ -53,7 +56,8 @@ map_error_type(wrong_body) -> <<"WrongBody">>. opts() ) -> Result :: false | {true, akm_auth:preauth_context()}. -authorize_api_key(OperationID, ApiKey, _Context, _HandlerOpts) -> +authorize_api_key(OperationID, ApiKey, Context, _HandlerOpts) -> + ok = set_otel_context(Context), %% Since we require the request id field to create a woody context for our trip to token_keeper %% it seems it is no longer possible to perform any authorization in this method. %% To gain this ability back be would need to rewrite the swagger generator to perform its @@ -62,8 +66,8 @@ authorize_api_key(OperationID, ApiKey, _Context, _HandlerOpts) -> %% it is probably easier to move it there in its entirety. ok = scoper:add_scope('swag.server', #{api => apikeymgmt, operation_id => OperationID}), case akm_auth:preauthorize_api_key(ApiKey) of - {ok, Context} -> - {true, Context}; + {ok, Context1} -> + {true, Context1}; {error, Error} -> _ = logger:info("API Key preauthorization failed for ~p due to ~p", [OperationID, Error]), false @@ -77,6 +81,14 @@ authorize_api_key(OperationID, ApiKey, _Context, _HandlerOpts) -> ) -> akm_apikeys_handler:request_result(). handle_request(OperationID, Req, SwagContext, Opts) -> + SpanName = <<"server ", (atom_to_binary(OperationID))/binary>>, + ?with_span(SpanName, #{kind => ?SPAN_KIND_SERVER}, fun(_SpanCtx) -> + scoper:scope(swagger, fun() -> + handle_request_(OperationID, Req, SwagContext, Opts) + end) + end). + +handle_request_(OperationID, Req, SwagContext, Opts) -> #{'X-Request-Deadline' := Header} = Req, case akm_utils:parse_deadline(Header) of {ok, Deadline} -> @@ -136,6 +148,14 @@ collect_user_identity(AuthContext) -> email => akm_auth:get_user_email(AuthContext) }). +set_otel_context(#{cowboy_req := Req}) -> + Headers = cowboy_req:headers(Req), + %% Implicitly puts OTEL context into process dictionary. + %% Since cowboy does not reuse process for other requests, we don't care + %% about cleaning it up. + _OtelCtx = otel_propagator_text_map:extract(maps:to_list(Headers)), + ok. + -spec set_context_meta(akm_handler_utils:handler_context()) -> ok. set_context_meta(Context) -> AuthContext = akm_handler_utils:get_auth_context(Context), diff --git a/compose.tracing.yaml b/compose.tracing.yaml new file mode 100644 index 0000000..f712b3c --- /dev/null +++ b/compose.tracing.yaml @@ -0,0 +1,21 @@ +services: + testrunner: + depends_on: + jaeger: + condition: service_healthy + + jaeger: + image: jaegertracing/all-in-one:1.47 + environment: + - COLLECTOR_OTLP_ENABLED=true + healthcheck: + test: "/go/bin/all-in-one-linux status" + interval: 2s + timeout: 1s + retries: 20 + ports: + - 4317:4317 # OTLP gRPC receiver + - 4318:4318 # OTLP http receiver + - 5778:5778 + - 14250:14250 + - 16686:16686 diff --git a/compose.yaml b/compose.yaml index e3a03d8..7b69936 100644 --- a/compose.yaml +++ b/compose.yaml @@ -35,27 +35,3 @@ services: interval: 5s timeout: 5s retries: 5 - - token-keeper: - image: ghcr.io/valitydev/token-keeper:sha-42d4a27 - command: /opt/token-keeper/bin/token-keeper foreground - depends_on: - machinegun: - condition: service_healthy - healthcheck: - test: "/opt/token-keeper/bin/token-keeper ping" - interval: 10s - timeout: 5s - retries: 10 - - machinegun: - image: ghcr.io/valitydev/machinegun:sha-058bada - command: /opt/machinegun/bin/machinegun foreground - volumes: - - ./test/machinegun/config.yaml:/opt/machinegun/etc/config.yaml - - ./test/machinegun/cookie:/opt/machinegun/etc/cookie - healthcheck: - test: "/opt/machinegun/bin/machinegun ping" - interval: 5s - timeout: 1s - retries: 20 diff --git a/rebar.config b/rebar.config index beedc05..a9502a1 100644 --- a/rebar.config +++ b/rebar.config @@ -62,7 +62,12 @@ %% For db migrations {envloader, {git, "https://github.com/nuex/envloader.git", {branch, "master"}}}, eql, - getopt + getopt, + + %% OpenTelemetry deps + {opentelemetry_api, "1.2.1"}, + {opentelemetry, "1.3.0"}, + {opentelemetry_exporter, "1.3.0"} ]}. %% XRef checks @@ -97,7 +102,7 @@ % Introspect a node running in production {recon, "2.5.2"}, {logger_logstash_formatter, - {git, "https://github.com/valitydev/logger_logstash_formatter.git", {ref, "2c7b716"}}}, + {git, "https://github.com/valitydev/logger_logstash_formatter.git", {ref, "08a66a6"}}}, {iosetopts, {git, "https://github.com/valitydev/iosetopts.git", {ref, "edb445c"}}} ]}, {relx, [ @@ -107,6 +112,7 @@ {runtime_tools, load}, % profiler {tools, load}, + {opentelemetry, temporary}, {recon, load}, {logger_logstash_formatter, load}, prometheus, diff --git a/rebar.lock b/rebar.lock index 3f1adfd..5839785 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,5 +1,6 @@ {"1.2.0", -[{<<"bender_client">>, +[{<<"acceptor_pool">>,{pkg,<<"acceptor_pool">>,<<"1.0.0">>},2}, + {<<"bender_client">>, {git,"https://github.com/valitydev/bender-client-erlang.git", {ref,"4e15070a194ed2f3f033891eb2da935982a06c30"}}, 0}, @@ -9,7 +10,7 @@ 0}, {<<"bouncer_client">>, {git,"https://github.com/valitydev/bouncer-client-erlang", - {ref,"79d9d0144ed66537ec25302aeba8f133bddb05d7"}}, + {ref,"4f89634d9a009cac807cf86a0eff9e80d33318ea"}}, 0}, {<<"bouncer_proto">>, {git,"https://github.com/valitydev/bouncer-proto.git", @@ -25,6 +26,7 @@ {git,"https://github.com/rbkmoney/cg_mon.git", {ref,"5a87a37694e42b6592d3b4164ae54e0e87e24e18"}}, 1}, + {<<"chatterbox">>,{pkg,<<"ts_chatterbox">>,<<"0.13.0">>},2}, {<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.9.0">>},1}, {<<"cowboy_access_log">>, {git,"https://github.com/valitydev/cowboy_access_log.git", @@ -39,6 +41,7 @@ {ref,"186cf4d0722d4ad79afe73d371df6b1371e51905"}}, 0}, {<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.12.1">>},1}, + {<<"ctx">>,{pkg,<<"ctx">>,<<"0.6.0">>},2}, {<<"damsel">>, {git,"https://github.com/valitydev/damsel.git", {ref,"bfedcb9dbb0bfdbd7a06a86417b49be6e807b98d"}}, @@ -67,7 +70,7 @@ {<<"eql">>,{pkg,<<"eql">>,<<"0.2.0">>},0}, {<<"erl_health">>, {git,"https://github.com/valitydev/erlang-health.git", - {ref,"5958e2f35cd4d09f40685762b82b82f89b4d9333"}}, + {ref,"7ffbc855bdbe79e23efad1803b0b185c9ea8d2f1"}}, 0}, {<<"erlydtl">>,{pkg,<<"erlydtl">>,<<"0.14.0">>},0}, {<<"file_storage_proto">>, @@ -85,16 +88,18 @@ {<<"gen_smtp">>,{pkg,<<"gen_smtp">>,<<"1.2.0">>},0}, {<<"genlib">>, {git,"https://github.com/valitydev/genlib.git", - {ref,"b08ef4d61e0dde98995ec3d2f69a4447255e79ef"}}, + {ref,"f6074551d6586998e91a97ea20acb47241254ff3"}}, 0}, {<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.2">>},0}, {<<"gproc">>,{pkg,<<"gproc">>,<<"0.9.0">>},1}, + {<<"grpcbox">>,{pkg,<<"grpcbox">>,<<"0.16.0">>},1}, {<<"gun">>,{pkg,<<"gun">>,<<"2.0.1">>},0}, {<<"hackney">>,{pkg,<<"hackney">>,<<"1.17.4">>},1}, {<<"herd">>, {git,"https://github.com/wgnet/herd.git", {ref,"934847589dcf5a6d2b02a1f546ffe91c04066f17"}}, 0}, + {<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},3}, {<<"identdocstore_proto">>, {git,"https://github.com/valitydev/identdocstore-proto.git", {ref,"0ab676da2bb23eb04c42e02325c40c413d74856e"}}, @@ -116,6 +121,14 @@ {git,"https://github.com/valitydev/msgpack-proto.git", {ref,"7e447496aa5df4a5f1ace7ef2e3c31248b2a3ed0"}}, 1}, + {<<"opentelemetry">>,{pkg,<<"opentelemetry">>,<<"1.3.0">>},0}, + {<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.2.1">>},0}, + {<<"opentelemetry_exporter">>, + {pkg,<<"opentelemetry_exporter">>,<<"1.3.0">>}, + 0}, + {<<"opentelemetry_semantic_conventions">>, + {pkg,<<"opentelemetry_semantic_conventions">>,<<"0.2.0">>}, + 1}, {<<"org_management_proto">>, {git,"https://github.com/valitydev/org-management-proto", {ref,"03a269df4805fa604e8fd2d04241619a739e2ae3"}}, @@ -129,7 +142,7 @@ {<<"ranch">>,{pkg,<<"ranch">>,<<"2.1.0">>},1}, {<<"scoper">>, {git,"https://github.com/valitydev/scoper.git", - {ref,"87110f5bd72c0e39ba9b7d6eca88fea91b8cd357"}}, + {ref,"41a14a558667316998af9f49149ee087ffa8bef2"}}, 0}, {<<"snowflake">>, {git,"https://github.com/valitydev/snowflake.git", @@ -152,9 +165,12 @@ {git,"https://github.com/valitydev/thrift-erlang.git", {ref,"3f3e11246d90aefa8f58b35e4f2eab14c0c28bd2"}}, 0}, + {<<"tls_certificate_check">>, + {pkg,<<"tls_certificate_check">>,<<"1.19.0">>}, + 1}, {<<"token_keeper_client">>, {git,"https://github.com/valitydev/token-keeper-client", - {ref,"de4f666c93a71c8fcab2cfb3b77f2e91225b650c"}}, + {ref,"417916aab09baf0cd7f1a2a174945446203ea0bf"}}, 0}, {<<"token_keeper_proto">>, {git,"https://github.com/valitydev/token-keeper-proto.git", @@ -175,49 +191,69 @@ 0}]}. [ {pkg_hash,[ + {<<"acceptor_pool">>, <<"43C20D2ACAE35F0C2BCD64F9D2BDE267E459F0F3FD23DAB26485BF518C281B21">>}, {<<"cache">>, <<"B23A5FE7095445A88412A6E614C933377E0137B44FFED77C9B3FEF1A731A20B2">>}, {<<"certifi">>, <<"DBAB8E5E155A0763EEA978C913CA280A6B544BFA115633FA20249C3D396D9493">>}, + {<<"chatterbox">>, <<"6F059D97BCAA758B8EA6FFFE2B3B81362BD06B639D3EA2BB088335511D691EBF">>}, {<<"cowboy">>, <<"865DD8B6607E14CF03282E10E934023A1BD8BE6F6BACF921A7E2A96D800CD452">>}, {<<"cowlib">>, <<"A9FA9A625F1D2025FE6B462CB865881329B5CAFF8F1854D1CBC9F9533F00E1E1">>}, + {<<"ctx">>, <<"8FF88B70E6400C4DF90142E7F130625B82086077A45364A78D208ED3ED53C7FE">>}, {<<"email_validator">>, <<"7E09A862E9AA99AE2CA6FD2A718D2B94360E32940A1339B53DFEE6B774BCDB03">>}, {<<"eql">>, <<"598ABC19A1CF6AFB8EF89FFEA869F43BAEBB1CEC3260DD5065112FEE7D8CE3E2">>}, {<<"erlydtl">>, <<"964B2DC84F8C17ACFAA69C59BA129EF26AC45D2BA898C3C6AD9B5BDC8BA13CED">>}, {<<"gen_smtp">>, <<"9CFC75C72A8821588B9B9FE947AE5AB2AED95A052B81237E0928633A13276FD3">>}, {<<"getopt">>, <<"33D9B44289FE7AD08627DDFE1D798E30B2DA0033B51DA1B3A2D64E72CD581D02">>}, {<<"gproc">>, <<"853CCB7805E9ADA25D227A157BA966F7B34508F386A3E7E21992B1B484230699">>}, + {<<"grpcbox">>, <<"B83F37C62D6EECA347B77F9B1EC7E9F62231690CDFEB3A31BE07CD4002BA9C82">>}, {<<"gun">>, <<"160A9A5394800FCBA41BC7E6D421295CF9A7894C2252C0678244948E3336AD73">>}, {<<"hackney">>, <<"99DA4674592504D3FB0CFEF0DB84C3BA02B4508BAE2DFF8C0108BAA0D6E0977C">>}, + {<<"hpack">>, <<"17670F83FF984AE6CD74B1C456EDDE906D27FF013740EE4D9EFAA4F1BF999633">>}, {<<"idna">>, <<"8A63070E9F7D0C62EB9D9FCB360A7DE382448200FBBD1B106CC96D3D8099DF8D">>}, {<<"jsone">>, <<"347FF1FA700E182E1F9C5012FA6D737B12C854313B9AE6954CA75D3987D6C06D">>}, {<<"jsx">>, <<"D12516BAA0BB23A59BB35DCCAF02A1BD08243FCBB9EFE24F2D9D056CCFF71268">>}, {<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>}, {<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>}, + {<<"opentelemetry">>, <<"988AC3C26ACAC9720A1D4FB8D9DC52E95B45ECFEC2D5B5583276A09E8936BC5E">>}, + {<<"opentelemetry_api">>, <<"7B69ED4F40025C005DE0B74FCE8C0549625D59CB4DF12D15C32FE6DC5076FF42">>}, + {<<"opentelemetry_exporter">>, <<"1D8809C0D4F4ACF986405F7700ED11992BCBDB6A4915DD11921E80777FFA7167">>}, + {<<"opentelemetry_semantic_conventions">>, <<"B67FE459C2938FCAB341CB0951C44860C62347C005ACE1B50F8402576F241435">>}, {<<"parse_trans">>, <<"6E6AA8167CB44CC8F39441D05193BE6E6F4E7C2946CB2759F015F8C56B76E5FF">>}, {<<"pooler">>, <<"898CD1FA301FC42D4A8ED598CE139B71CA85B54C16AB161152B5CC5FBDCFA1A8">>}, {<<"ranch">>, <<"2261F9ED9574DCFCC444106B9F6DA155E6E540B2F82BA3D42B339B93673B72A3">>}, {<<"ssl_verify_fun">>, <<"354C321CF377240C7B8716899E182CE4890C5938111A1296ADD3EC74CF1715DF">>}, + {<<"tls_certificate_check">>, <<"C76C4C5D79EE79A2B11C84F910C825D6F024A78427C854F515748E9BD025E987">>}, {<<"unicode_util_compat">>, <<"BC84380C9AB48177092F43AC89E4DFA2C6D62B40B8BD132B1059ECC7232F9A78">>}]}, {pkg_hash_ext,[ + {<<"acceptor_pool">>, <<"0CBCD83FDC8B9AD2EEE2067EF8B91A14858A5883CB7CD800E6FCD5803E158788">>}, {<<"cache">>, <<"44516CE6FA03594D3A2AF025DD3A87BFE711000EB730219E1DDEFC816E0AA2F4">>}, {<<"certifi">>, <<"524C97B4991B3849DD5C17A631223896272C6B0AF446778BA4675A1DFF53BB7E">>}, + {<<"chatterbox">>, <<"B93D19104D86AF0B3F2566C4CBA2A57D2E06D103728246BA1AC6C3C0FF010AA7">>}, {<<"cowboy">>, <<"2C729F934B4E1AA149AFF882F57C6372C15399A20D54F65C8D67BEF583021BDE">>}, {<<"cowlib">>, <<"163B73F6367A7341B33C794C4E88E7DBFE6498AC42DCD69EF44C5BC5507C8DB0">>}, + {<<"ctx">>, <<"A14ED2D1B67723DBEBBE423B28D7615EB0BDCBA6FF28F2D1F1B0A7E1D4AA5FC2">>}, {<<"email_validator">>, <<"2B1E6DF7BB14155C8D7D131F1C95CF4676200BC056EEBA82123396833FF94DA2">>}, {<<"eql">>, <<"513BE6B36EE86E8292B2B7475C257ABB66CED5AAD40CBF7AD21E233D0A3BF51D">>}, {<<"erlydtl">>, <<"D80EC044CD8F58809C19D29AC5605BE09E955040911B644505E31E9DD8143431">>}, {<<"gen_smtp">>, <<"5EE0375680BCA8F20C4D85F58C2894441443A743355430FF33A783FE03296779">>}, {<<"getopt">>, <<"A0029AEA4322FB82A61F6876A6D9C66DC9878B6CB61FAA13DF3187384FD4EA26">>}, {<<"gproc">>, <<"587E8AF698CCD3504CF4BA8D90F893EDE2B0F58CABB8A916E2BF9321DE3CF10B">>}, + {<<"grpcbox">>, <<"294DF743AE20A7E030889F00644001370A4F7CE0121F3BBDAF13CF3169C62913">>}, {<<"gun">>, <<"A10BC8D6096B9502205022334F719CC9A08D9ADCFBFC0DBEE9EF31B56274A20B">>}, {<<"hackney">>, <<"DE16FF4996556C8548D512F4DBE22DD58A587BF3332E7FD362430A7EF3986B16">>}, + {<<"hpack">>, <<"06F580167C4B8B8A6429040DF36CC93BBA6D571FAEAEC1B28816523379CBB23A">>}, {<<"idna">>, <<"92376EB7894412ED19AC475E4A86F7B413C1B9FBB5BD16DCCD57934157944CEA">>}, {<<"jsone">>, <<"08560B78624A12E0B5E7EC0271EC8CA38EF51F63D84D84843473E14D9B12618C">>}, {<<"jsx">>, <<"0C5CC8FDC11B53CC25CF65AC6705AD39E54ECC56D1C22E4ADB8F5A53FB9427F3">>}, {<<"metrics">>, <<"69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16">>}, {<<"mimerl">>, <<"F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323">>}, + {<<"opentelemetry">>, <<"8E09EDC26AAD11161509D7ECAD854A3285D88580F93B63B0B1CF0BAC332BFCC0">>}, + {<<"opentelemetry_api">>, <<"6D7A27B7CAD2AD69A09CABF6670514CAFCEC717C8441BEB5C96322BAC3D05350">>}, + {<<"opentelemetry_exporter">>, <<"2B40007F509D38361744882FD060A8841AF772AB83BB542AA5350908B303AD65">>}, + {<<"opentelemetry_semantic_conventions">>, <<"D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895">>}, {<<"parse_trans">>, <<"620A406CE75DADA827B82E453C19CF06776BE266F5A67CFF34E1EF2CBB60E49A">>}, {<<"pooler">>, <<"058D85C5081289B90E97E4DDDBC3BB5A3B4A19A728AB3BC88C689EFCC36A07C7">>}, {<<"ranch">>, <<"244EE3FA2A6175270D8E1FC59024FD9DBC76294A321057DE8F803B1479E76916">>}, {<<"ssl_verify_fun">>, <<"FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8">>}, + {<<"tls_certificate_check">>, <<"4083B4A298ADD534C96125337CB01161C358BB32DD870D5A893AAE685FD91D70">>}, {<<"unicode_util_compat">>, <<"25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521">>}]} ].