Skip to content

Commit

Permalink
Improve path groups handling
Browse files Browse the repository at this point in the history
The only functional difference here is that empty paths will be logged
as "/", otherwise this is just a refactor.
  • Loading branch information
sulami committed Feb 1, 2024
1 parent 6b24d2c commit b460e85
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/degica_datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "degica_datadog/config"
require_relative "degica_datadog/statsd"
require_relative "degica_datadog/tracing"
require_relative "degica_datadog/util"
require_relative "degica_datadog/version"

# Standardised interactions with Datadog.
Expand Down
9 changes: 3 additions & 6 deletions lib/degica_datadog/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

module DegicaDatadog
# Tracing related functionality.
module Tracing # rubocop:disable Metrics/ModuleLength
module Tracing
class << self
# Initialize Datadog tracing. Call this in from config/application.rb.
def init(rake_tasks: []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def init(rake_tasks: []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
return unless Config.enabled?

require "ddtrace/auto_instrument"
Expand Down Expand Up @@ -69,10 +69,7 @@ def init(rake_tasks: []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,
# ID. The logic seems to be at least vaguely to replace any path
# segment that contains a digit with a ?, so we're reproducing
# that here.
path_group = span.get_tag("http.url")
.split("/")
.map { |segment| segment =~ /\d/ ? "?" : segment }
.join("/")
path_group = DegicaDatadog::Util.path_group(span.get_tag("http.url"))
span.resource = "#{span.get_tag("http.method")} #{path_group}"
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/degica_datadog/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module DegicaDatadog
# Utility methods.
module Util
class << self
# Return a path group for a given path.
def path_group(path)
return unless path.is_a?(String)
return "/" if path.empty?

path
.split("/")
.map(&method(:process_path_segment))
.join("/")
end

private

def process_path_segment(segment)
return segment if segment =~ /^v\d+$/
return "?" if segment =~ /\d/

segment
end
end
end
end
30 changes: 30 additions & 0 deletions spec/util_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

RSpec.describe DegicaDatadog::Util do
describe ".path_group" do
it "returns nil for non-strings" do
expect(described_class.path_group(nil)).to be_nil
expect(described_class.path_group(42)).to be_nil
expect(described_class.path_group([])).to be_nil
end

it "returns '/' for empty strings" do
expect(described_class.path_group("")).to eq("/")
end

it "returns the path for strings without numbers" do
expect(described_class.path_group("/foo/bar")).to eq("/foo/bar")
end

it "returns the path with segments containing digits replaced with ?" do
expect(described_class.path_group("/foo/42")).to eq("/foo/?")
expect(described_class.path_group("/foo/42/bar")).to eq("/foo/?/bar")
expect(described_class.path_group("/foo/v42uuid/bar")).to eq("/foo/?/bar")
end

it "keeps segments that look like API versioning" do
expect(described_class.path_group("/api/v1/foo")).to eq("/api/v1/foo")
expect(described_class.path_group("/api/v1/foo/42")).to eq("/api/v1/foo/?")
end
end
end

0 comments on commit b460e85

Please sign in to comment.