Sha256: 7836c86ff25a7df675ef8dcb9d50d3127ae1bce4a31acbfc4e157c5d414d5b61

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require_relative '../../metadata/ext'
require_relative '../analytics'
require_relative 'ext'
require_relative 'request_queue'

module Datadog
  module Tracing
    module Contrib
      module Rack
        # Module to create virtual proxy span
        module TraceProxyMiddleware
          module_function

          def call(env, configuration)
            return yield unless configuration[:request_queuing]

            # parse the request queue time
            start_time = Contrib::Rack::QueueTime.get_request_start(env)
            return yield unless start_time

            options = {
              service: configuration[:web_service_name],
              start_time: start_time,
              type: Tracing::Metadata::Ext::HTTP::TYPE_PROXY
            }

            request_span = Tracing.trace(Ext::SPAN_HTTP_PROXY_REQUEST, **options)

            request_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT_HTTP_PROXY)
            request_span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_HTTP_PROXY_REQUEST)
            request_span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_PROXY)

            queue_span = Tracing.trace(Ext::SPAN_HTTP_PROXY_QUEUE, **options)

            queue_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT_HTTP_PROXY)
            queue_span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_HTTP_PROXY_QUEUE)
            queue_span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_PROXY)

            Contrib::Analytics.set_measured(queue_span)
            # finish the `queue` span now to record only the time spent *in queue*,
            # excluding the time spent processing the request itself
            queue_span.finish

            yield.tap { request_span.finish }
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
datadog-2.7.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.6.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.5.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.4.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.3.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.2.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.1.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.0.0 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.0.0.beta2 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb
datadog-2.0.0.beta1 lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb