From c0b2afb7d4a7c09aa59aa62542fd4cc763c12f67 Mon Sep 17 00:00:00 2001 From: alejandro-fr <99.alejandro.flores@gmail.com> Date: Thu, 6 Apr 2023 12:20:08 -0600 Subject: [PATCH 1/2] try to first fetch self ctx in run start --- .../lib/opentelemetry_dataloader.ex | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex b/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex index 238fb652..68d2df0e 100644 --- a/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex +++ b/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex @@ -45,10 +45,17 @@ defmodule OpentelemetryDataloader do def handle_event(event, measurements, metadata, config) def handle_event(@run_start, _measurements, metadata, config) do - parent_ctx = OpentelemetryProcessPropagator.fetch_parent_ctx(4, :"$callers") + parent_ctx = + case OpentelemetryProcessPropagator.fetch_ctx(self()) do + :undefined -> + OpentelemetryProcessPropagator.fetch_parent_ctx(4, :"$callers") + + ctx -> + ctx + end if parent_ctx != :undefined do - OpenTelemetry.Ctx.attach(parent_ctx) + attach_parent_ctx(parent_ctx) end OpentelemetryTelemetry.start_telemetry_span( @@ -65,6 +72,8 @@ defmodule OpentelemetryDataloader do OpentelemetryTelemetry.set_current_telemetry_span(config.tracer_id, metadata) OpentelemetryTelemetry.end_telemetry_span(config.tracer_id, metadata) + + detach_parent_ctx() end def handle_event(@batch_start, _measurements, metadata, config) do @@ -96,4 +105,22 @@ defmodule OpentelemetryDataloader do OpentelemetryTelemetry.end_telemetry_span(config.tracer_id, metadata) end + + @ctx_key {__MODULE__, :parent_ctx} + + defp attach_parent_ctx(parent_ctx) do + parent_token = OpenTelemetry.Ctx.attach(parent_ctx) + + Process.put(@ctx_key, parent_token) + end + + defp detach_parent_ctx() do + parent_ctx = Process.get(@ctx_key, :undefined) + + if parent_ctx != :undefined do + @ctx_key + |> Process.delete() + |> OpenTelemetry.Ctx.detach() + end + end end From 22885e1d3ccd10f72db4f18605b004b9d92a199b Mon Sep 17 00:00:00 2001 From: alejandro-fr <99.alejandro.flores@gmail.com> Date: Mon, 10 Jul 2023 13:43:07 -0600 Subject: [PATCH 2/2] remove self attach and parent detach --- .../lib/opentelemetry_dataloader.ex | 34 +++---------------- 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex b/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex index 68d2df0e..f1e757f4 100644 --- a/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex +++ b/instrumentation/opentelemetry_dataloader/lib/opentelemetry_dataloader.ex @@ -45,17 +45,11 @@ defmodule OpentelemetryDataloader do def handle_event(event, measurements, metadata, config) def handle_event(@run_start, _measurements, metadata, config) do - parent_ctx = - case OpentelemetryProcessPropagator.fetch_ctx(self()) do - :undefined -> - OpentelemetryProcessPropagator.fetch_parent_ctx(4, :"$callers") - - ctx -> - ctx - end + self_ctx = OpentelemetryProcessPropagator.fetch_ctx(self()) + parent_ctx = OpentelemetryProcessPropagator.fetch_parent_ctx(4, :"$callers") - if parent_ctx != :undefined do - attach_parent_ctx(parent_ctx) + if self_ctx == :undefined and parent_ctx != :undefined do + OpenTelemetry.Ctx.attach(parent_ctx) end OpentelemetryTelemetry.start_telemetry_span( @@ -72,8 +66,6 @@ defmodule OpentelemetryDataloader do OpentelemetryTelemetry.set_current_telemetry_span(config.tracer_id, metadata) OpentelemetryTelemetry.end_telemetry_span(config.tracer_id, metadata) - - detach_parent_ctx() end def handle_event(@batch_start, _measurements, metadata, config) do @@ -105,22 +97,4 @@ defmodule OpentelemetryDataloader do OpentelemetryTelemetry.end_telemetry_span(config.tracer_id, metadata) end - - @ctx_key {__MODULE__, :parent_ctx} - - defp attach_parent_ctx(parent_ctx) do - parent_token = OpenTelemetry.Ctx.attach(parent_ctx) - - Process.put(@ctx_key, parent_token) - end - - defp detach_parent_ctx() do - parent_ctx = Process.get(@ctx_key, :undefined) - - if parent_ctx != :undefined do - @ctx_key - |> Process.delete() - |> OpenTelemetry.Ctx.detach() - end - end end