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
- .infer(path) ⇒ Object
- .read_or_infer(request_env) ⇒ Object
- .split(path, pattern = nil, &block) ⇒ Object
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 49 |
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 27 def self.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
22 23 24 25 |
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 22 def self.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 |
.split(path, pattern = nil, &block) ⇒ Object
52 53 54 |
# File 'lib/datadog/tracing/contrib/rack/route_inference.rb', line 52 def self.split(path, pattern = nil, &block) path.split(pattern, &block) end |