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

Instance Method Summary collapse

Class Method Details

.infer(path) ⇒ Object



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

def infer(path)
  count = 0
  result = []

  split(path, "/") do |segment|
    next if segment.empty?
    break if count >= MAX_NUMBER_OF_SEGMENTS
    count += 1

    result << 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

  result.empty? ? "/" : "/#{result.join("/")}"
rescue
  nil
end

.read_or_infer(request_env) ⇒ Object



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

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

Instance Method Details

#split(path, pattern = nil, &block) ⇒ Object



54
55
56
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 54

def split(path, pattern = nil, &block)
  path.split(pattern, &block)
end