Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter_metadata config option #973

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changesets/add-filter_metadata-config-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add `filter_metadata` config option to filter metadata set on Transactions set by default. Metadata like `path`, (request) `method`, `request_id`, `hostname`, etc. This can be useful if there's PII or other sensitive data in any of the app's metadata.
3 changes: 3 additions & 0 deletions lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Config
:enable_rails_error_reporter => true,
:endpoint => "https://push.appsignal.com",
:files_world_accessible => true,
:filter_metadata => [],
:filter_parameters => [],
:filter_session_data => [],
:ignore_actions => [],
Expand Down Expand Up @@ -77,6 +78,7 @@ class Config
"APPSIGNAL_ENABLE_GVL_WAITING_THREADS" => :enable_gvl_waiting_threads,
"APPSIGNAL_ENABLE_RAILS_ERROR_REPORTER" => :enable_rails_error_reporter,
"APPSIGNAL_FILES_WORLD_ACCESSIBLE" => :files_world_accessible,
"APPSIGNAL_FILTER_METADATA" => :filter_metadata,
"APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
"APPSIGNAL_FILTER_SESSION_DATA" => :filter_session_data,
"APPSIGNAL_HOSTNAME" => :hostname,
Expand Down Expand Up @@ -150,6 +152,7 @@ class Config
# @api private
ENV_ARRAY_KEYS = %w[
APPSIGNAL_DNS_SERVERS
APPSIGNAL_FILTER_METADATA
APPSIGNAL_FILTER_PARAMETERS
APPSIGNAL_FILTER_SESSION_DATA
APPSIGNAL_IGNORE_ACTIONS
Expand Down
16 changes: 11 additions & 5 deletions lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def set_http_or_background_queue_start

def set_metadata(key, value)
return unless key && value
return if Appsignal.config[:filter_metadata].include?(key.to_s)

@ext.set_metadata(key, value)
end
Expand Down Expand Up @@ -337,7 +338,7 @@ def sample_data
:params => sanitized_params,
:environment => sanitized_environment,
:session_data => sanitized_session_data,
:metadata => metadata,
:metadata => sanitized_metadata,
:tags => sanitized_tags,
:breadcrumbs => breadcrumbs
}.each do |key, data|
Expand Down Expand Up @@ -522,12 +523,17 @@ def sanitized_session_data
)
end

# Returns metadata from the environment.
# Returns sanitized metadata set by {#set_metadata} and from the
# {#environment}.
#
# @return [nil] if no `:metadata` key is present in the {#environment}.
# @return [Hash<String, Object>]
def metadata
environment[:metadata]
def sanitized_metadata
metadata = environment[:metadata]
return unless metadata

metadata
.transform_keys(&:to_s)
.except(*Appsignal.config[:filter_metadata])
end

# Returns the environment for a transaction.
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/diagnose
1 change: 1 addition & 0 deletions spec/lib/appsignal/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
:enable_rails_error_reporter => true,
:endpoint => "https://push.appsignal.com",
:files_world_accessible => true,
:filter_metadata => [],
:filter_parameters => [],
:filter_session_data => [],
:ignore_actions => [],
Expand Down
1 change: 1 addition & 0 deletions spec/lib/appsignal/rack/streaming_listener_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "appsignal/rack/streaming_listener"

describe Appsignal::Rack::StreamingListener do
before(:context) { start_agent }
let(:headers) { {} }
let(:env) do
{
Expand Down
29 changes: 25 additions & 4 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,18 @@ def current_transaction
expect(transaction.to_h["metadata"]).to eq("request_method" => "GET")
end

context "when filter_metadata includes metadata key" do
before { Appsignal.config[:filter_metadata] = ["filter_key"] }
after { Appsignal.config[:filter_metadata] = [] }

it "does not set the metadata on the transaction" do
transaction.set_metadata(:filter_key, "filtered value")
transaction.set_metadata("filter_key", "filtered value")

expect(transaction.to_h["metadata"].keys).to_not include("filter_key")
end
end

context "when the key is nil" do
it "does not update the metadata on the transaction" do
transaction.set_metadata(nil, "GET")
Expand Down Expand Up @@ -1275,8 +1287,8 @@ def session_exists?(_env)
end
end

describe "#metadata" do
subject { transaction.send(:metadata) }
describe "#sanitized_metadata" do
subject { transaction.send(:sanitized_metadata) }

context "when request is nil" do
let(:request) { nil }
Expand All @@ -1291,9 +1303,18 @@ def session_exists?(_env)
end

context "when env is present" do
let(:env) { { :metadata => { :key => "value" } } }
let(:env) { { "key" => "value" } }

it { is_expected.to eq env[:metadata] }
it { is_expected.to eq("key" => "value") }

context "with filter_metadata option set" do
before { Appsignal.config[:filter_metadata] = ["key"] }
after { Appsignal.config[:filter_metadata] = [] }

it "filters out keys listed in the filter_metadata option" do
expect(subject.keys).to_not include("key")
end
end
end
end

Expand Down