Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Oct 10, 2023
1 parent 0b1b9e8 commit ac3109c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Default
def initialize(tracer, mapper)
@tracer = tracer
@mapper = mapper
@config = ActiveJob::Instrumentation.instance.config
end

# Invoked by ActiveSupport::Notifications at the start of the instrumentation block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ class Enqueue < Default
# @param payload [Hash] containing job run information
# @return [Hash] with the span and generated context tokens
def start_span(name, _id, payload)
otel_config = ActiveJob::Instrumentation.instance.config
job = payload.fetch(:job)
span_name = "#{otel_config[:span_naming] == :job_class ? job.class.name : job.queue_name} publish"
span = @tracer.start_span(span_name, kind: :producer, attributes: @mapper.call(payload))
span = @tracer.start_span(span_name_from(job), kind: :producer, attributes: @mapper.call(payload))
tokens = [OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))]
OpenTelemetry.propagation.inject(job.__otel_headers) # This must be transmitted over the wire
{ span: span, ctx_tokens: tokens }
end

# TODO: extract strategy
def span_name_from(job)
"#{@config[:span_naming] == :job_class ? job.class.name : job.queue_name} publish"
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class Perform < Default
# @return [Hash] with the span and generated context tokens
def start_span(name, _id, payload)
tokens = []
parent_context = OpenTelemetry.propagation.extract(payload.fetch(:job).__otel_headers)
job = payload.fetch(:job)
parent_context = OpenTelemetry.propagation.extract(job.__otel_headers)

span_name = span_name_from(payload)
span_name = span_name_from(job)

# TODO: Refactor into a propagation strategy
propagation_style = otel_config[:propagation_style]
propagation_style = @config[:propagation_style]
if propagation_style == :child
tokens << OpenTelemetry::Context.attach(parent_context)
span = @tracer.start_span(span_name, kind: :consumer, attributes: @mapper.call(payload))
Expand Down Expand Up @@ -53,13 +54,8 @@ def attach_consumer_context(span)
end

# TODO: refactor into a strategy
def span_name_from(payload)
"#{otel_config[:span_naming] == :job_class ? payload.fetch(:job).class.name : payload.fetch(:job).queue_name} process"
end

# TODO: use dependency injection
def otel_config
ActiveJob::Instrumentation.instance.config
def span_name_from(job)
"#{@config[:span_naming] == :job_class ? job.class.name : job.queue_name} process"
end
end
end
Expand Down

0 comments on commit ac3109c

Please sign in to comment.