diff --git a/lib/http_client_builder.ex b/lib/http_client_builder.ex index a025788..5cab733 100755 --- a/lib/http_client_builder.ex +++ b/lib/http_client_builder.ex @@ -38,6 +38,8 @@ defmodule HttpClientBuilder do raise ":runtime_headers_getter option is not a nullary anonymous function or function capture " end + otel_headers? = Keyword.get(client_opts, :otel_headers?, true) + quote do def child_spec(opts) do default_pools = %{:default => [size: 50]} @@ -102,6 +104,8 @@ defmodule HttpClientBuilder do compile_time_headers end + headers = otel_headers(headers) + method |> Finch.build(url, headers, body) |> Finch.request(__MODULE__, request_opts) @@ -121,6 +125,15 @@ defmodule HttpClientBuilder do |> Map.to_list() end + # https://github.com/open-telemetry/opentelemetry-erlang-contrib/blob/main/instrumentation/opentelemetry_finch/README.md + if Code.ensure_loaded?(:otel_propagator_text_map) && unquote(otel_headers?) do + defp otel_headers(headers) when is_list(headers) do + :otel_propagator_text_map.inject([]) ++ headers + end + end + + defp otel_headers(headers), do: headers + defoverridable get: 2, post: 2, put: 2, delete: 2, patch: 2, do_request: 3, build_url: 2 end end diff --git a/mix.exs b/mix.exs index 0f580e0..934358d 100755 --- a/mix.exs +++ b/mix.exs @@ -27,6 +27,7 @@ defmodule HttpClientBuilder.MixProject do {:finch, "~> 0.9"}, # for encode plug compat query string {:plug, "~> 1.12"}, + {:opentelemetry_api, "~> 1.3", optional: true}, {:jason, "~> 1.2", only: [:test]} ] end diff --git a/mix.lock b/mix.lock index 24eca95..fbe4d4b 100755 --- a/mix.lock +++ b/mix.lock @@ -7,6 +7,8 @@ "mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"}, "nimble_options": {:hex, :nimble_options, "0.4.0", "c89babbab52221a24b8d1ff9e7d838be70f0d871be823165c94dd3418eea728f", [:mix], [], "hexpm", "e6701c1af326a11eea9634a3b1c62b475339ace9456c1a23ec3bc9a847bca02d"}, "nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"}, + "opentelemetry_api": {:hex, :opentelemetry_api, "1.3.1", "83b4713593f80562d9643c4ab0b6f80f3c5fa4c6d0632c43e11b2ccb6b04dfa7", [:mix, :rebar3], [{:opentelemetry_semantic_conventions, "~> 0.2", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}], "hexpm", "9e8a5cc38671e3ac61be48abe5f6b3afdbbb50a1dc08b7950c56f169611505c1"}, + "opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "0.2.0", "b67fe459c2938fcab341cb0951c44860c62347c005ace1b50f8402576f241435", [:mix, :rebar3], [], "hexpm", "d61fa1f5639ee8668d74b527e6806e0503efc55a42db7b5f39939d84c07d6895"}, "plug": {:hex, :plug, "1.13.6", "187beb6b67c6cec50503e940f0434ea4692b19384d47e5fdfd701e93cadb4cc2", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "02b9c6b9955bce92c829f31d6284bf53c591ca63c4fb9ff81dfd0418667a34ff"}, "plug_crypto": {:hex, :plug_crypto, "1.2.3", "8f77d13aeb32bfd9e654cb68f0af517b371fb34c56c9f2b58fe3df1235c1251a", [:mix], [], "hexpm", "b5672099c6ad5c202c45f5a403f21a3411247f164e4a8fab056e5cd8a290f4a2"}, "telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},