Module: Datadog::Tracing::Contrib::Rack::RouteInference

Defined in:
lib/datadog/tracing/contrib/rack/route_inference.rb

Overview

This module provides logic for inferring HTTP route pattern from an HTTP path.

Constant Summary collapse

MAX_NUMBER_OF_SEGMENTS =
8
INT_PARAM_REGEX =
/\A[0-9]+\z/.freeze
INT_ID_PARAM_REGEX =
/\A(?=.*\d)[\d._-]{3,}\z/.freeze
HEX_PARAM_REGEX =
/\A(?=.*\d)[A-Fa-f0-9]{6,}\z/.freeze
HEX_ID_PARAM_REGEX =
/\A(?=.*\d)[A-Fa-f0-9._-]{6,}\z/.freeze
STRING_PARAM_REGEX =
/\A.{20,}|.*[%&'()*+,:=@].*\z/.freeze
DATADOG_INFERRED_ROUTE_ENV_KEY =
'datadog.inferred_route'

Class Method Summary collapse

Class Method Details

.infer(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 27

def infer(path)
  segments = path.delete_prefix('/').split('/', MAX_NUMBER_OF_SEGMENTS + 1).first(MAX_NUMBER_OF_SEGMENTS)

  segments.map! do |segment| #: Array[String?]
    next if segment.empty?

    case segment
    when INT_PARAM_REGEX then '{param:int}'
    when INT_ID_PARAM_REGEX then '{param:int_id}'
    when HEX_PARAM_REGEX then '{param:hex}'
    when HEX_ID_PARAM_REGEX then '{param:hex_id}'
    when STRING_PARAM_REGEX then '{param:str}'
    else segment
    end
  end

  segments.compact! #: Array[String]

  "/#{segments.join("/")}"
rescue
  nil
end

.read_or_infer(request_env) ⇒ Object



22
23
24
25
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 22

def read_or_infer(request_env)
  request_env[DATADOG_INFERRED_ROUTE_ENV_KEY] ||=
    infer(request_env['SCRIPT_NAME'].to_s + request_env['PATH_INFO'].to_s)
end