Sha256: 84d894fb0656a8dfe8fe08c395d9d25c384ccd3006fae1501ad53890cbd58e3d

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

# typed: true

require 'time'

require_relative '../../metadata/ext'
require_relative 'ext'

module Datadog
  module Tracing
    module Contrib
      module Sinatra
        # Gets and sets trace information from a Rack Env
        module Env
          module_function

          def datadog_span(env, app)
            request_span = env[Ext::RACK_ENV_REQUEST_SPAN]
            request_span && request_span[app]
          end

          def set_datadog_span(env, app, span)
            hash = (env[Ext::RACK_ENV_REQUEST_SPAN] ||= {})
            hash[app] = span
          end

          def request_header_tags(env, headers)
            headers ||= []

            {}.tap do |result|
              headers.each do |header|
                rack_header = header_to_rack_header(header)
                if env.key?(rack_header)
                  result[Tracing::Metadata::Ext::HTTP::RequestHeaders.to_tag(header)] = env[rack_header]
                end
              end
            end
          end

          def header_to_rack_header(name)
            "HTTP_#{name.to_s.upcase.gsub(/[-\s]/, '_')}"
          end

          # Was a Sinatra already traced in this request?
          # We don't want to create spans for intermediate Sinatra
          # middlewares that don't match the request at hand.
          def middleware_traced?(env)
            env[Ext::RACK_ENV_MIDDLEWARE_TRACED]
          end

          def set_middleware_traced(env, bool)
            env[Ext::RACK_ENV_MIDDLEWARE_TRACED] = bool
          end

          # The start time of the top-most Sinatra middleware.
          def middleware_start_time(env)
            env[Ext::RACK_ENV_MIDDLEWARE_START_TIME]
          end

          def set_middleware_start_time(env, time = Time.now.utc)
            env[Ext::RACK_ENV_MIDDLEWARE_START_TIME] = time
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddtrace-1.5.2 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.5.1 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.5.0 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.4.2 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.4.1 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.4.0 lib/datadog/tracing/contrib/sinatra/env.rb
ddtrace-1.3.0 lib/datadog/tracing/contrib/sinatra/env.rb