Skip to content

Commit

Permalink
filter ip addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Sep 28, 2023
1 parent 1e562f7 commit 1b92c4f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
19 changes: 19 additions & 0 deletions lib/network_resiliency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ def timestamp
Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1_000
end

# private

IP_ADDRESS_REGEX = Regexp.new(/\d{1,3}(\.\d{1,3}){3}/)

def record(adapter:, action:, destination:, duration:, error: nil)
# filter raw IP addresses
return if IP_ADDRESS_REGEX.match?(destination)

NetworkResiliency.statsd&.distribution(
"network_resiliency.#{action}",
duration,
tags: {
adapter: adapter,
destination: destination,
error: error,
}.compact,
)
end

def reset
@enabled = nil
end
Expand Down
14 changes: 6 additions & 8 deletions lib/network_resiliency/adapter/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ def connect
ensure
ts += NetworkResiliency.timestamp

NetworkResiliency.statsd&.distribution(
"network_resiliency.connect",
ts,
tags: {
adapter: "http",
destination: address,
error: e&.class,
}.compact,
NetworkResiliency.record(
adapter: "http",
action: "connect",
destination: address,
error: e&.class,
duration: ts,
)
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,15 @@
end
end
end

context "when host is a raw IP address" do
let(:http) { Net::HTTP.new("127.0.0.1") }

it "does not call datadog" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)

http.connect
end
end
end
end
43 changes: 43 additions & 0 deletions spec/network_resiliency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,47 @@ def expect_enabled
expect_enabled.to be false
end
end

describe ".record" do
subject do
NetworkResiliency.record(
adapter: "adapter",
action: action,
destination: host,
duration: duration,
error: error,
)

NetworkResiliency.statsd
end

let(:action) { "connect" }
let(:error) { Net::OpenTimeout }
let(:duration) { 10 }
let(:host) { "example.com" }

before do
allow(NetworkResiliency.statsd).to receive(:distribution)
end

it "calls Datadog" do
is_expected.to have_received(:distribution)
end

it "captures metric info" do
is_expected.to have_received(:distribution).with(
"network_resiliency.#{action}",
duration,
tags: include(destination: host, error: error),
)
end

context "when host is a raw IP address" do
let(:host) { "127.0.0.1" }

it "does not call Datadog" do
is_expected.not_to have_received(:distribution)
end
end
end
end

0 comments on commit 1b92c4f

Please sign in to comment.