From cd2a6cca91f8d2b5813b39e817492a08073c3eb5 Mon Sep 17 00:00:00 2001 From: Tom Taylor Date: Fri, 14 Jul 2023 00:15:26 +0100 Subject: [PATCH] Redis: propagate the span context if available (#180) Behave in a similar way to Ecto, which is to attach the context of the connection, falling back to the calling process context if available. Co-authored-by: Tristan Sloughter --- .../lib/opentelemetry_redix.ex | 20 +++++++++++++++++++ instrumentation/opentelemetry_redix/mix.exs | 1 + instrumentation/opentelemetry_redix/mix.lock | 1 + 3 files changed, 22 insertions(+) diff --git a/instrumentation/opentelemetry_redix/lib/opentelemetry_redix.ex b/instrumentation/opentelemetry_redix/lib/opentelemetry_redix.ex index 21333be..b12d34d 100644 --- a/instrumentation/opentelemetry_redix/lib/opentelemetry_redix.ex +++ b/instrumentation/opentelemetry_redix/lib/opentelemetry_redix.ex @@ -63,6 +63,22 @@ defmodule OpentelemetryRedix do |> Map.merge(net_attributes(connection)) |> Map.merge(redix_attributes(meta)) + parent_context = + case OpentelemetryProcessPropagator.fetch_ctx(self()) do + :undefined -> + OpentelemetryProcessPropagator.fetch_parent_ctx(1, :"$callers") + + ctx -> + ctx + end + + parent_token = + if parent_context != :undefined do + OpenTelemetry.Ctx.attach(parent_context) + else + :undefined + end + s = OpenTelemetry.Tracer.start_span(operation, %{ start_time: start_time, @@ -75,6 +91,10 @@ defmodule OpentelemetryRedix do end OpenTelemetry.Span.end_span(s) + + if parent_token != :undefined do + OpenTelemetry.Ctx.detach(parent_token) + end end defp net_attributes(%{address: address}) when is_binary(address) do diff --git a/instrumentation/opentelemetry_redix/mix.exs b/instrumentation/opentelemetry_redix/mix.exs index 26d0789..5d69af4 100644 --- a/instrumentation/opentelemetry_redix/mix.exs +++ b/instrumentation/opentelemetry_redix/mix.exs @@ -57,6 +57,7 @@ defmodule OpentelemetryRedix.MixProject do {:ex_doc, "~> 0.29.0", only: [:dev], runtime: false}, {:opentelemetry, "~> 1.0", only: [:dev, :test]}, {:opentelemetry_api, "~> 1.0"}, + {:opentelemetry_process_propagator, "~> 0.2"}, {:opentelemetry_semantic_conventions, "~> 0.2"}, {:opentelemetry_exporter, "~> 1.0", only: [:dev, :test]}, {:redix, "~> 1.0", only: [:dev, :test]}, diff --git a/instrumentation/opentelemetry_redix/mix.lock b/instrumentation/opentelemetry_redix/mix.lock index e3a9fa7..6845ad8 100644 --- a/instrumentation/opentelemetry_redix/mix.lock +++ b/instrumentation/opentelemetry_redix/mix.lock @@ -16,6 +16,7 @@ "opentelemetry": {:hex, :opentelemetry, "1.1.2", "77ba2fd2fee67bebde590851a4afeda45b3f298310aa410a2a3804b364cb598a", [:rebar3], [{:opentelemetry_api, "~> 1.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "5c60be189d6aed64a9fd17055f72c93eab144be441e625276c3c95533e6bb4c7"}, "opentelemetry_api": {:hex, :opentelemetry_api, "1.1.1", "3b43877c456c8a7f5448a95d9bf4fb4bb8cc2abbbea2c62d5f8e8c538b4af14f", [:mix, :rebar3], [], "hexpm", "a9554b3208b60a70043318d051ea78fbbc7a1b8f4c418ebc16ccb40015995675"}, "opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.2.2", "3966c56656627ef7db6c34c4ce28d44ac8629dcd065a310d7c33712fc2a1cfe3", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.1", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.11", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "5c11adeda19e0d203a04efe92cdd7a183da4b09ae8acaba7608e9303fa258e74"}, + "opentelemetry_process_propagator": {:hex, :opentelemetry_process_propagator, "0.2.2", "85244a49f0c32ae1e2f3d58c477c265bd6125ee3480ade82b0fa9324b85ed3f0", [:mix, :rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "04db13302a34bea8350a13ed9d49c22dfd32c4bc590d8aa88b6b4b7e4f346c61"}, "opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "0.2.0", "b67fe459c2938fcab341cb0951c44860c62347c005ace1b50f8402576f241435", [:mix, :rebar3], [], "hexpm", "d61fa1f5639ee8668d74b527e6806e0503efc55a42db7b5f39939d84c07d6895"}, "redix": {:hex, :redix, "1.2.0", "0d7eb3ccb7b82c461a6ea28b65c2c04486093d816dd6d901a09164800e004df1", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e1e0deb14599da07c77e66956a12863e85ee270ada826804a0ba8e61657e22a3"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},