diff --git a/spec/controllers/graphql_controller_spec.rb b/spec/controllers/graphql_controller_spec.rb index 165bbf34b50..cd28cadea20 100644 --- a/spec/controllers/graphql_controller_spec.rb +++ b/spec/controllers/graphql_controller_spec.rb @@ -88,7 +88,7 @@ describe GraphQLController do before { allow(InstStatsd::Statsd).to receive(:increment).and_call_original } def expect_increment(metric, tags) - expect(InstStatsd::Statsd).to receive(:increment).with(metric, tags: tags) + expect(InstStatsd::Statsd).to have_received(:increment).with(metric, tags: tags) end context "for first-party queries" do @@ -107,11 +107,11 @@ describe GraphQLController do } } GQL + post :execute, params: {query: test_query}, format: :json expect_increment("graphql.operation.count", operation_name: 'GetStuff', domain: 'test.host', operation_md5: String) expect_increment("graphql.query.count", operation_name: 'GetStuff', field: 'course', operation_md5: String) expect_increment("graphql.query.count", operation_name: 'GetStuff', field: 'assignment', operation_md5: String) expect_increment("graphql.query.count", operation_name: 'GetStuff', field: 'legacyNode', operation_md5: String) - post :execute, params: {query: test_query}, format: :json end it "counts unnamed operations" do @@ -122,10 +122,10 @@ describe GraphQLController do assignment(id: "1") { name } } GQL + post :execute, params: {query: test_query}, format: :json expect_increment("graphql.operation.count", operation_name: 'unnamed', domain: 'test.host', operation_md5: String) expect_increment("graphql.query.count", operation_name: 'unnamed', field: 'course', operation_md5: String) expect_increment("graphql.query.count", operation_name: 'unnamed', field: 'assignment', operation_md5: String) - post :execute, params: {query: test_query}, format: :json end it "counts each mutation top-level field" do @@ -140,10 +140,10 @@ describe GraphQLController do } } GQL + post :execute, params: {query: test_query}, format: :json expect_increment("graphql.operation.count", operation_name: 'unnamed', domain: 'test.host', operation_md5: String) expect_increment("graphql.mutation.count", operation_name: 'unnamed', field: 'createAssignment', operation_md5: String) expect_increment("graphql.mutation.count", operation_name: 'unnamed', field: 'updateAssignment', operation_md5: String) - post :execute, params: {query: test_query}, format: :json end end @@ -154,9 +154,9 @@ describe GraphQLController do course(id: "1") { name } } GQL + post :execute, params: {query: test_query}, format: :json expect_increment("graphql.operation.count", operation_name: '3rdparty', domain: 'test.host') expect_increment("graphql.query.count", operation_name: '3rdparty', field: 'course') - post :execute, params: {query: test_query}, format: :json end end end diff --git a/spec/lib/canvas/error_stats_spec.rb b/spec/lib/canvas/error_stats_spec.rb index a3c8815bc95..66e9a211298 100644 --- a/spec/lib/canvas/error_stats_spec.rb +++ b/spec/lib/canvas/error_stats_spec.rb @@ -39,19 +39,19 @@ module Canvas let(:data){ {} } it "increments the error level by default" do - expect(InstStatsd::Statsd).to receive(:increment) do |key, data| + described_class.capture("something", data) + expect(InstStatsd::Statsd).to have_received(:increment) do |key, data| expect(key).to eq("errors.error") expect(data[:tags][:category]).to eq("something") end - described_class.capture("something", data) end it "uses the exception name for the category tag" do - expect(InstStatsd::Statsd).to receive(:increment) do |key, data| + described_class.capture(StandardError.new, data, :warn) + expect(InstStatsd::Statsd).to have_received(:increment) do |key, data| expect(key).to eq("errors.warn") expect(data[:tags][:category]).to eq("StandardError") end - described_class.capture(StandardError.new, data, :warn) end it "increments the inner exception too" do diff --git a/spec/lib/canvas/twilio_spec.rb b/spec/lib/canvas/twilio_spec.rb index 532c8526044..06eb1f10abc 100644 --- a/spec/lib/canvas/twilio_spec.rb +++ b/spec/lib/canvas/twilio_spec.rb @@ -134,22 +134,27 @@ describe 'Canvas::Twilio' do end it 'pings StatsD about outgoing messages' do + allow(InstStatsd::Statsd).to receive(:increment) stub_twilio(['+18015550100', '+18015550102'], '+18015550102' => 'CA', '+18015550103' => 'CA', '+18015550104' => 'GB') expect(Canvas::Twilio.client.api.account.messages).to receive(:create).exactly(3).times - - expect(InstStatsd::Statsd).to receive(:increment).with('notifications.twilio.message_sent_from_number.US.+18015550100', - short_stat: 'notifications.twilio.message_sent', - tags: {country: 'US', number: '+18015550100'}).twice - expect(InstStatsd::Statsd).to receive(:increment).with('notifications.twilio.message_sent_from_number.CA.+18015550102', - short_stat: 'notifications.twilio.message_sent', - tags: {country: 'CA', number: '+18015550102'}) - expect(InstStatsd::Statsd).to receive(:increment).with('notifications.twilio.no_outbound_numbers_for.GB', - short_stat: 'notifications.twilio.no_outbound_numbers', - tags: {country: 'GB'}) - Canvas::Twilio.deliver('+18015550101', 'message text') Canvas::Twilio.deliver('+18015550103', 'message text') Canvas::Twilio.deliver('+18015550104', 'message text') + expect(InstStatsd::Statsd).to have_received(:increment) + .with( + 'notifications.twilio.message_sent_from_number.US.+18015550100', + short_stat: 'notifications.twilio.message_sent', + tags: {country: 'US', number: '+18015550100'} + ).twice + expect(InstStatsd::Statsd).to have_received(:increment) + .with( + 'notifications.twilio.message_sent_from_number.CA.+18015550102', + short_stat: 'notifications.twilio.message_sent', + tags: {country: 'CA', number: '+18015550102'}) + expect(InstStatsd::Statsd).to have_received(:increment) + .with('notifications.twilio.no_outbound_numbers_for.GB', + short_stat: 'notifications.twilio.no_outbound_numbers', + tags: {country: 'GB'}) end end end diff --git a/spec/lib/feature_flags_spec.rb b/spec/lib/feature_flags_spec.rb index df6b5665cad..428ec997ae1 100644 --- a/spec/lib/feature_flags_spec.rb +++ b/spec/lib/feature_flags_spec.rb @@ -33,6 +33,7 @@ describe FeatureFlags do before do silence_undefined_feature_flag_errors allow_any_instance_of(User).to receive(:set_default_feature_flags) + allow(InstStatsd::Statsd).to receive(:increment) allow(Feature).to receive(:definitions).and_return({ 'site_admin_feature' => Feature.new(feature: 'site_admin_feature', applies_to: 'SiteAdmin', state: 'allowed'), 'root_account_feature' => Feature.new(feature: 'root_account_feature', applies_to: 'RootAccount', state: 'off'), @@ -58,17 +59,17 @@ describe FeatureFlags do end it "should log feature enablement" do - expect(InstStatsd::Statsd).to receive(:increment).with("feature_flag_check", tags: { + t_sub_account.feature_enabled?(:course_feature) + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", tags: { feature: :course_feature, enabled: 'false' }).exactly(:once) - t_sub_account.feature_enabled?(:course_feature) - expect(InstStatsd::Statsd).to receive(:increment).with("feature_flag_check", tags: { + t_sub_account.feature_enabled?(:account_feature) + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", tags: { feature: :account_feature, enabled: 'true' }).exactly(:once) - t_sub_account.feature_enabled?(:account_feature) end end diff --git a/spec/lib/microsoft_sync/graph_service_http_spec.rb b/spec/lib/microsoft_sync/graph_service_http_spec.rb index 060af8ae99d..f99dc0dab95 100644 --- a/spec/lib/microsoft_sync/graph_service_http_spec.rb +++ b/spec/lib/microsoft_sync/graph_service_http_spec.rb @@ -199,26 +199,16 @@ describe MicrosoftSync::GraphServiceHttp do end it 'increments a "retried" statsd counter' do - expect { - subject.request(:post, 'foo/bar', body: {hello: 'world'}) - }.to have_incremented_statsd_stats([ - { - stat: "microsoft_sync.graph_service.retried", - options: { - tags: { - msft_endpoint: 'post_foo', - extra_tag: 'abc', - status_code: status_code_statsd_tag - } - } - }, - { - stat: "microsoft_sync.graph_service.success", - options: { - tags: {msft_endpoint: 'post_foo', extra_tag: 'abc', status_code: '200'} - } - } - ]) + subject.request(:post, 'foo/bar', body: {hello: 'world'}) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "microsoft_sync.graph_service.retried", + tags: {msft_endpoint: 'post_foo', extra_tag: 'abc', + status_code: status_code_statsd_tag} + ) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "microsoft_sync.graph_service.success", + tags: {msft_endpoint: 'post_foo', extra_tag: 'abc', status_code: '200'} + ) end it 'logs the outcome of each request' do @@ -237,30 +227,17 @@ describe MicrosoftSync::GraphServiceHttp do let(:requests) { [:bad, :bad] } it 'fails and increments "retried" and "error" statsd counters' do - expect { - subject.request(:post, 'foo/bar') - }.to have_incremented_statsd_stats([ - { - stat: "microsoft_sync.graph_service.retried", - options: { - tags: { - msft_endpoint: 'post_foo', - extra_tag: 'abc', - status_code: status_code_statsd_tag - } - } - }, - { - stat: "microsoft_sync.graph_service.intermittent", - options: { - tags: { - msft_endpoint: 'post_foo', - extra_tag: 'abc', - status_code: status_code_statsd_tag - } - } - } - ]).and raise_error(error_class) + expect { subject.request(:post, 'foo/bar', body: {hello: 'world'}) }.to raise_error(error_class) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "microsoft_sync.graph_service.retried", + tags: {msft_endpoint: 'post_foo', extra_tag: 'abc', + status_code: status_code_statsd_tag} + ) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "microsoft_sync.graph_service.intermittent", + tags: {msft_endpoint: 'post_foo', extra_tag: 'abc', + status_code: status_code_statsd_tag} + ) end it 'logs the outcome of each request' do diff --git a/spec/lib/microsoft_sync/login_service_spec.rb b/spec/lib/microsoft_sync/login_service_spec.rb index 212eece71e7..782d5e69072 100644 --- a/spec/lib/microsoft_sync/login_service_spec.rb +++ b/spec/lib/microsoft_sync/login_service_spec.rb @@ -45,6 +45,10 @@ describe MicrosoftSync::LoginService do context 'when configured' do subject { described_class.new_token('mytenant') } + before :each do + allow(InstStatsd::Statsd).to receive(:increment) + end + context 'when Microsoft returns a response' do before do allow(Canvas::DynamicSettings).to receive(:find).with(any_args).and_call_original @@ -78,10 +82,11 @@ describe MicrosoftSync::LoginService do it { is_expected.to eq(response_body) } it 'increments a statsd metric' do - expect(InstStatsd::Statsd).to \ - receive(:increment).with('microsoft_sync.login_service', tags: {status_code: '200'}) - .and_call_original subject + expect(InstStatsd::Statsd).to have_received(:increment).with( + 'microsoft_sync.login_service', + tags: {status_code: '200'} + ) end end @@ -90,13 +95,14 @@ describe MicrosoftSync::LoginService do let(:response_body) { {} } it 'increments a statsd metric and raises an HTTPInvalidStatus' do - expect(InstStatsd::Statsd).to \ - receive(:increment).with('microsoft_sync.login_service', tags: {status_code: '401'}) - .and_call_original expect { subject }.to raise_error( MicrosoftSync::Errors::HTTPInvalidStatus, /Login service returned 401 for tenant mytenant/ ) + expect(InstStatsd::Statsd).to have_received(:increment).with( + 'microsoft_sync.login_service', + tags: {status_code: '401'} + ) end end @@ -150,10 +156,11 @@ describe MicrosoftSync::LoginService do it 'increments a statsd metric and bubbles up the error' do error = SocketError.new expect(HTTParty).to receive(:post).and_raise error - expect(InstStatsd::Statsd).to \ - receive(:increment).with('microsoft_sync.login_service', tags: {status_code: 'error'}) - .and_call_original expect { subject }.to raise_error(error) + expect(InstStatsd::Statsd).to have_received(:increment).with( + 'microsoft_sync.login_service', + tags: {status_code: 'error'} + ) end end end diff --git a/spec/lib/sis/csv/import_refactored_spec.rb b/spec/lib/sis/csv/import_refactored_spec.rb index 276fd7517cb..ab10f33c8c2 100644 --- a/spec/lib/sis/csv/import_refactored_spec.rb +++ b/spec/lib/sis/csv/import_refactored_spec.rb @@ -369,17 +369,19 @@ describe SIS::CSV::ImportRefactored do expect_any_instance_of(SIS::CSV::ImportRefactored).to receive(:fail_with_error!).once.and_call_original expect_any_instance_of(SIS::CSV::ImportRefactored).to receive(:job_args).once.with(:term, attempt: 1).and_call_original expect_any_instance_of(SIS::CSV::ImportRefactored).to receive(:job_args).once.with(:term, attempt: 2).and_call_original + + csv_importer_double = instance_double(SIS::CSV::TermImporter) + allow(SIS::CSV::TermImporter).to receive(:new).and_return(csv_importer_double) + allow(csv_importer_double).to receive(:process).and_raise("error") + + process_csv_data("term_id,name,status", "T001,Winter13,active") + [0, 1, 2].each do |i| - expect(InstStatsd::Statsd).to receive(:increment).once.with('sis_parallel_worker', + expect(InstStatsd::Statsd).to have_received(:increment).once.with('sis_parallel_worker', tags: { attempt: i, retry: false}) - expect(InstStatsd::Statsd).to receive(:increment).once.with('sis_parallel_worker', + expect(InstStatsd::Statsd).to have_received(:increment).once.with('sis_parallel_worker', tags: { attempt: i, retry: true}) end - allow_any_instance_of(SIS::CSV::TermImporter).to receive(:process).and_raise("error") - process_csv_data( - "term_id,name,status", - "T001,Winter13,active" - ) end it 'should only run an importer once if successful' do diff --git a/spec/lib/turnitin/outcome_response_processor_spec.rb b/spec/lib/turnitin/outcome_response_processor_spec.rb index d925f30cfab..478356f6735 100644 --- a/spec/lib/turnitin/outcome_response_processor_spec.rb +++ b/spec/lib/turnitin/outcome_response_processor_spec.rb @@ -168,16 +168,20 @@ module Turnitin describe "#update_originality_data" do it 'raises an error and sends stat if max attempts are not exceeded' do allow_any_instance_of(subject.class).to receive(:attempt_number).and_return(subject.class.max_attempts-1) + allow(InstStatsd::Statsd).to receive(:increment) mock_turnitin_client = double('turnitin_client') allow(mock_turnitin_client).to receive(:scored?).and_return(false) allow(subject).to receive(:turnitin_client).and_return(mock_turnitin_client) submission = lti_assignment.submit_homework(lti_student, attachments:[attachment], submission_type: 'online_upload') - expect(InstStatsd::Statsd).to receive(:increment).with("submission_not_scored.account_#{lti_assignment.root_account.global_id}", - short_stat: 'submission_not_scored', - tags: { root_account_id: lti_assignment.root_account.global_id }).once expect do subject.update_originality_data(submission, attachment.asset_string) end.to raise_error Turnitin::Errors::SubmissionNotScoredError + expect(InstStatsd::Statsd).to have_received(:increment) + .with( + "submission_not_scored.account_#{lti_assignment.root_account.global_id}", + short_stat: 'submission_not_scored', + tags: { root_account_id: lti_assignment.root_account.global_id } + ).once end it 'sets an error message if max attempts are exceeded' do diff --git a/spec/models/attachments/verification_spec.rb b/spec/models/attachments/verification_spec.rb index 9fa4556043b..b43c2c7c602 100644 --- a/spec/models/attachments/verification_spec.rb +++ b/spec/models/attachments/verification_spec.rb @@ -71,52 +71,38 @@ describe Attachments::Verification do end context "verifying a verifier" do + before :each do + allow(InstStatsd::Statsd).to receive(:increment) + end + it "should verify a legacy verifier for read and download" do - expect(InstStatsd::Statsd).to receive(:increment).with("attachments.legacy_verifier_success").twice expect(v.valid_verifier_for_permission?(attachment.uuid, :read)).to eq(true) expect(v.valid_verifier_for_permission?(attachment.uuid, :download)).to eq(true) + expect(InstStatsd::Statsd).to have_received(:increment).with("attachments.legacy_verifier_success").twice end it "accepts the uuid of another copy of the file" do - expect(InstStatsd::Statsd).to receive(:increment).with("attachments.related_verifier_success").twice - expect(InstStatsd::Statsd).to receive(:increment).with("feature_flag_check", any_args).at_least(:once) clone = attachment.clone_for(course_factory) clone.save! v2 = Attachments::Verification.new(clone) expect(v2.valid_verifier_for_permission?(attachment.uuid, :read)).to eq true expect(v2.valid_verifier_for_permission?(attachment.uuid, :download)).to eq true - end - - it "should return false on an expired verifier" do - expect(CanvasSecurity).to receive(:decode_jwt).with("token").and_raise(CanvasSecurity::TokenExpired) - - result = true - expect { - result = v.valid_verifier_for_permission?("token", :read) - }.to have_incremented_statsd_stat("attachments.token_verifier_expired") - expect(result).to eq(false) + expect(InstStatsd::Statsd).to have_received(:increment).with("attachments.related_verifier_success").twice + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", any_args).at_least(:once) end it "should return false on an invalid verifier" do expect(CanvasSecurity).to receive(:decode_jwt).with("token").and_raise(CanvasSecurity::InvalidToken) - - result = true - expect { - result = v.valid_verifier_for_permission?("token", :read) - }.to have_incremented_statsd_stat("attachments.token_verifier_invalid") - expect(result).to eq(false) + expect(v.valid_verifier_for_permission?("token", :read)).to eq(false) + expect(InstStatsd::Statsd).to have_received(:increment).with("attachments.token_verifier_invalid") end it "should return false on token id mismatch" do expect(CanvasSecurity).to receive(:decode_jwt).with("token").and_return({ id: attachment.global_id + 1 }) - - result = true - expect { - result = v.valid_verifier_for_permission?("token", :read) - }.to have_incremented_statsd_stat("attachments.token_verifier_id_mismatch") - expect(result).to eq(false) + expect(v.valid_verifier_for_permission?("token", :read)).to eq(false) + expect(InstStatsd::Statsd).to have_received(:increment).with("attachments.token_verifier_id_mismatch") end it "should not let a student download an attachment that's locked" do @@ -126,11 +112,10 @@ describe Attachments::Verification do expect(CanvasSecurity).to receive(:decode_jwt).with("token").and_return({ id: att2.global_id, user_id: student.global_id }).twice - expect(InstStatsd::Statsd).to receive(:increment).with("attachments.token_verifier_success").twice - expect(InstStatsd::Statsd).to receive(:increment).with("feature_flag_check", any_args).at_least(:once) - expect(v2.valid_verifier_for_permission?("token", :read)).to eq(true) expect(v2.valid_verifier_for_permission?("token", :download)).to eq(false) + expect(InstStatsd::Statsd).to have_received(:increment).with("attachments.token_verifier_success").twice + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", any_args).at_least(:once) end it "follows custom permissions" do diff --git a/spec/models/authentication_provider/ldap_spec.rb b/spec/models/authentication_provider/ldap_spec.rb index 08eac9567b1..f8c978c713e 100644 --- a/spec/models/authentication_provider/ldap_spec.rb +++ b/spec/models/authentication_provider/ldap_spec.rb @@ -74,13 +74,13 @@ describe AuthenticationProvider::LDAP do allow(@aac).to receive(:account_id).and_return(1) allow(@aac).to receive(:global_id).and_return(2) allow(@aac).to receive(:should_send_to_statsd?).and_return(true) + allow(InstStatsd::Statsd).to receive(:increment) end it "should send to statsd on success" do allow(@ldap).to receive(:bind_as).and_return(true) - expect { - @aac.ldap_bind_result('user', 'pass') - }.to have_incremented_statsd_stat( + @aac.ldap_bind_result('user', 'pass') + expect(InstStatsd::Statsd).to have_received(:increment).with( "#{@aac.send(:statsd_prefix)}.ldap_success", short_stat: "ldap_success", tags: {account_id: Shard.global_id_for(@aac.account_id), auth_provider_id: @aac.global_id} @@ -89,9 +89,8 @@ describe AuthenticationProvider::LDAP do it "should send to statsd on failure" do allow(@ldap).to receive(:bind_as).and_return(false) - expect { - @aac.ldap_bind_result('user', 'pass') - }.to have_incremented_statsd_stat( + @aac.ldap_bind_result('user', 'pass') + expect(InstStatsd::Statsd).to have_received(:increment).with( "#{@aac.send(:statsd_prefix)}.ldap_failure", short_stat: "ldap_failure", tags: {account_id: Shard.global_id_for(@aac.account_id), auth_provider_id: @aac.global_id} @@ -100,28 +99,28 @@ describe AuthenticationProvider::LDAP do it "should send to statsd on timeout" do allow(@ldap).to receive(:bind_as).and_raise(Timeout::Error) - expect { - @aac.ldap_bind_result('user', 'pass') - }.to have_incremented_statsd_stat("#{@aac.send(:statsd_prefix)}.ldap_timeout", { + @aac.ldap_bind_result('user', 'pass') + expect(InstStatsd::Statsd).to have_received(:increment).with( + "#{@aac.send(:statsd_prefix)}.ldap_timeout", short_stat: "ldap_timeout", tags: { account_id: Shard.global_id_for(@aac.account_id), auth_provider_id: @aac.global_id } - }) + ) end it "should send to statsd on exception" do allow(@ldap).to receive(:bind_as).and_raise(StandardError) - expect { - @aac.ldap_bind_result('user', 'pass') - }.to have_incremented_statsd_stat("#{@aac.send(:statsd_prefix)}.ldap_error", { + @aac.ldap_bind_result('user', 'pass') + expect(InstStatsd::Statsd).to have_received(:increment).with( + "#{@aac.send(:statsd_prefix)}.ldap_error", short_stat: "ldap_error", tags: { account_id: Shard.global_id_for(@aac.account_id), auth_provider_id: @aac.global_id } - }) + ) end end end diff --git a/spec/models/bounce_notification_processor_spec.rb b/spec/models/bounce_notification_processor_spec.rb index 6c1f6816ea9..fdca2d08606 100644 --- a/spec/models/bounce_notification_processor_spec.rb +++ b/spec/models/bounce_notification_processor_spec.rb @@ -124,25 +124,16 @@ describe BounceNotificationProcessor do expect(queue).to expectation allow(bnp).to receive(:bounce_queue).and_return(queue) allow(CommunicationChannel).to receive(:bounce_for_path) - - expect { bnp.process }.to have_incremented_statsd_stats([ - { - stat: 'bounce_notification_processor.processed.transient', - count: 1 - }, - { - stat: 'bounce_notification_processor.processed.no_bounce', - count: 2 - }, - { - stat: 'bounce_notification_processor.processed.suppression', - count: 3 - }, - { - stat: 'bounce_notification_processor.processed.permanent', - count: 4 - } - ]) + allow(InstStatsd::Statsd).to receive(:increment) + bnp.process + expect(InstStatsd::Statsd).to have_received(:increment) + .with('bounce_notification_processor.processed.transient').once + expect(InstStatsd::Statsd).to have_received(:increment) + .with('bounce_notification_processor.processed.no_bounce').twice + expect(InstStatsd::Statsd).to have_received(:increment) + .with('bounce_notification_processor.processed.suppression').exactly(3).times + expect(InstStatsd::Statsd).to have_received(:increment) + .with('bounce_notification_processor.processed.permanent').exactly(4).times end end end diff --git a/spec/models/communication_channel_spec.rb b/spec/models/communication_channel_spec.rb index 12c7c48ec8a..5ae47785d81 100644 --- a/spec/models/communication_channel_spec.rb +++ b/spec/models/communication_channel_spec.rb @@ -737,6 +737,7 @@ describe CommunicationChannel do it "sends directly via SMS if configured" do expect(cc.e164_path).to eq '+18015555555' + allow(InstStatsd::Statsd).to receive(:increment) account = double() allow(account).to receive(:feature_enabled?).and_return(true) allow(account).to receive(:global_id).and_return('totes_an_ID') @@ -748,24 +749,22 @@ describe CommunicationChannel do true ) expect(cc).to receive(:send_otp_via_sms_gateway!).never - expect { - cc.send_otp!('123456', account) - }.to have_incremented_statsd_stats([ + cc.send_otp!('123456', account) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "message.deliver.sms.one_time_password", { - stat: "message.deliver.sms.one_time_password", - options: { - short_stat: "message.deliver", - tags: {path_type: "sms", notification_name: 'one_time_password'} - } - }, - { - stat: "message.deliver.sms.totes_an_ID", - options: { - short_stat: "message.deliver_per_account", - tags: {path_type: "sms", root_account_id: 'totes_an_ID'} - } + short_stat: "message.deliver", + tags: { path_type: "sms", notification_name: 'one_time_password' } } - ]) + ) + + expect(InstStatsd::Statsd).to have_received(:increment).with( + "message.deliver.sms.totes_an_ID", + { + short_stat: "message.deliver_per_account", + tags: { path_type: "sms", root_account_id: 'totes_an_ID' } + } + ) end it "sends via email if not configured" do diff --git a/spec/models/group_and_membership_importer_spec.rb b/spec/models/group_and_membership_importer_spec.rb index b8610238a70..d3b3c682b17 100644 --- a/spec/models/group_and_membership_importer_spec.rb +++ b/spec/models/group_and_membership_importer_spec.rb @@ -195,14 +195,17 @@ describe GroupAndMembershipImporter do end it 'should log stat on new groups' do - expect { - import_csv_data(%{user_id,group_name - user_4,anugroup}) - }.to have_incremented_statsd_stat('groups.auto_create', tags: { - split_type: 'csv', - root_account_id: gc1.root_account&.global_id, - root_account_name: gc1.root_account&.name - }) + allow(InstStatsd::Statsd).to receive(:increment) + import_csv_data(%{user_id,group_name + user_4,anugroup}) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "groups.auto_create", + tags: { + split_type: 'csv', + root_account_id: gc1.root_account&.global_id, + root_account_name: gc1.root_account&.name + } + ) end end end diff --git a/spec/models/learning_outcome_spec.rb b/spec/models/learning_outcome_spec.rb index cf310922414..8634be24f59 100644 --- a/spec/models/learning_outcome_spec.rb +++ b/spec/models/learning_outcome_spec.rb @@ -115,6 +115,7 @@ describe LearningOutcome do end it "adding outcomes to a rubric should increment datadog counter" do + allow(InstStatsd::Statsd).to receive(:increment) @rubric = Rubric.new(:context => @course) @rubric.data = [ { @@ -138,9 +139,9 @@ describe LearningOutcome do :learning_outcome_id => @outcome.id } ] - expect { - @rubric.save! - }.to have_incremented_statsd_stat('learning_outcome.align') + @rubric.save! + expect(InstStatsd::Statsd).to have_received(:increment).with('learning_outcome.align') + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", any_args).at_least(:once) end it "should allow learning outcome rows in the rubric" do diff --git a/spec/models/mailer_spec.rb b/spec/models/mailer_spec.rb index d1ebabfeab8..ecde0f9bf6f 100644 --- a/spec/models/mailer_spec.rb +++ b/spec/models/mailer_spec.rb @@ -72,15 +72,16 @@ describe Mailer do end it 'sends stat to stat service' do + allow(InstStatsd::Statsd).to receive(:increment) message = message_model(to: "someemail@example.com") mail = Mailer.create_message(message) expect(mail).to receive(:deliver_now) - expect { - Mailer.deliver(mail) - }.to have_incremented_statsd_stat('message.deliver', { - short_stat: "message.deliver", - tags: { path_type: "mailer_emails", notification_name: 'mailer_delivery' } - }) + Mailer.deliver(mail) + expect(InstStatsd::Statsd).to have_received(:increment).with( + "message.deliver", + { short_stat: "message.deliver", + tags: { path_type: "mailer_emails", notification_name: 'mailer_delivery' } } + ) end it 'calls the notification service if configured' do diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 68125325be6..fe80b45ab7e 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -313,6 +313,7 @@ describe Message do end it "logs stats on deliver" do + allow(InstStatsd::Statsd).to receive(:increment) account = account_model @message = message_model(dispatch_at: Time.now - 1, notification_name: 'my_name', @@ -322,26 +323,23 @@ describe Message do path_type: 'email', user: @user, root_account: account) - expect(@message).to receive(:dispatch).and_return(true) - expect { - @message.deliver - }.to have_incremented_statsd_stats([ + @message.deliver + expect(InstStatsd::Statsd).to have_received(:increment).with( + "message.deliver.email.my_name", { - stat: "message.deliver.email.my_name", - options: { - short_stat: "message.deliver", - tags: {path_type: "email", notification_name: 'my_name'} - } - }, - { - stat: "message.deliver.email.#{@message.root_account.global_id}", - options: { - short_stat: "message.deliver_per_account", - tags: {path_type: "email", root_account_id: @message.root_account.global_id} - } + short_stat: "message.deliver", + tags: {path_type: "email", notification_name: 'my_name'} } - ]) + ) + + expect(InstStatsd::Statsd).to have_received(:increment).with( + "message.deliver.email.#{@message.root_account.global_id}", + { + short_stat: "message.deliver_per_account", + tags: {path_type: "email", root_account_id: @message.root_account.global_id} + } + ) end context 'push' do diff --git a/spec/models/rubric_assessment_spec.rb b/spec/models/rubric_assessment_spec.rb index f9f904a495a..0c55bfd3ecb 100644 --- a/spec/models/rubric_assessment_spec.rb +++ b/spec/models/rubric_assessment_spec.rb @@ -338,21 +338,22 @@ describe RubricAssessment do end it 'assessing a rubric with outcome criterion should increment datadog counter' do + allow(InstStatsd::Statsd).to receive(:increment) @outcome.update!(data: nil) criterion_id = "criterion_#{@rubric.data[0][:id]}".to_sym - expect { - @association.assess({ - :user => @student, - :assessor => @teacher, - :artifact => @assignment.find_or_create_submission(@student), - :assessment => { - :assessment_type => 'grading', - criterion_id => { - :points => '3' - } + @association.assess({ + :user => @student, + :assessor => @teacher, + :artifact => @assignment.find_or_create_submission(@student), + :assessment => { + :assessment_type => 'grading', + criterion_id => { + :points => '3' } - }) - }.to have_incremented_statsd_stat('learning_outcome_result.create') + } + }) + expect(InstStatsd::Statsd).to have_received(:increment).with("feature_flag_check", any_args).at_least(:once) + expect(InstStatsd::Statsd).to have_received(:increment).with('learning_outcome_result.create') end it 'should use default ratings for scoring' do diff --git a/spec/models/sis_batch_spec.rb b/spec/models/sis_batch_spec.rb index 171692f467a..970bb0e591b 100644 --- a/spec/models/sis_batch_spec.rb +++ b/spec/models/sis_batch_spec.rb @@ -97,10 +97,9 @@ describe SisBatch do it 'should log stats' do allow(InstStatsd::Statsd).to receive(:increment) - expect(InstStatsd::Statsd).to receive(:increment).with("sis_batch_completed", tags: { failed: false }) process_csv_data([%{user_id,login_id,status user_1,user_1,active}]) - + expect(InstStatsd::Statsd).to have_received(:increment).with("sis_batch_completed", tags: { failed: false }) end it 'should restore linked observers when restoring enrollments' do @@ -117,12 +116,11 @@ describe SisBatch do expect(student_enrollment.reload.workflow_state).to eq 'deleted' expect(observer_enrollment.reload.workflow_state).to eq 'deleted' tags = { undelete_only: false, unconclude_only: false, batch_mode: false } - expect(InstStatsd::Statsd).to receive(:increment).with("sis_batch_restored", tags: tags) - batch.restore_states_for_batch run_jobs expect(student_enrollment.reload.workflow_state).to eq 'active' expect(observer_enrollment.reload.workflow_state).to eq 'active' + expect(InstStatsd::Statsd).to have_received(:increment).with("sis_batch_restored", tags: tags) end it 'should create new linked observer enrollments when restoring enrollments' do diff --git a/spec/support/custom_matchers/have_incremented_statsd_stat.rb b/spec/support/custom_matchers/have_incremented_statsd_stat.rb deleted file mode 100644 index 402bfc5aba2..00000000000 --- a/spec/support/custom_matchers/have_incremented_statsd_stat.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -# -# Copyright (C) 2021 - present Instructure, Inc. -# -# This file is part of Canvas. -# -# Canvas is free software: you can redistribute it and/or modify it under -# the terms of the GNU Affero General Public License as published by the Free -# Software Foundation, version 3 of the License. -# -# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more -# details. -# -# You should have received a copy of the GNU Affero General Public License along -# with this program. If not, see . -# -RSpec::Matchers.define :have_incremented_statsd_stat do |stat, *options| - match do |action| - allow(InstStatsd::Statsd).to receive(:increment).and_call_original - # expect increment to be called with stat and options if provided - expect(InstStatsd::Statsd).to( - receive(:increment).with(*[stat, *options].compact), - "expected stat: #{stat} with options: #{options} to be incremented but wasn't" - ) - action.call - RSpec::Mocks.verify # run mock verifications - end - - supports_block_expectations -end diff --git a/spec/support/custom_matchers/have_incremented_statsd_stats.rb b/spec/support/custom_matchers/have_incremented_statsd_stats.rb deleted file mode 100644 index 909a2fe6ac5..00000000000 --- a/spec/support/custom_matchers/have_incremented_statsd_stats.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -# -# Copyright (C) 2021 - present Instructure, Inc. -# -# This file is part of Canvas. -# -# Canvas is free software: you can redistribute it and/or modify it under -# the terms of the GNU Affero General Public License as published by the Free -# Software Foundation, version 3 of the License. -# -# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more -# details. -# -# You should have received a copy of the GNU Affero General Public License along -# with this program. If not, see . -# -RSpec::Matchers.define :have_incremented_statsd_stats do |stats| - match do |action| - allow(InstStatsd::Statsd).to receive(:increment).and_call_original - stats.each do |obj| - stat = obj[:stat] - options = obj[:options] - count = obj[:count] || 1 - # expect increment to be called with stat and options if provided - expect(InstStatsd::Statsd).to( - receive(:increment).with(*[stat, options].compact).exactly(count).times, - "expected stat: #{stat} with options: #{options} to be incremented exactly #{count} times but wasn't" - ) - end - action.call - RSpec::Mocks.verify # run mock verifications - end - - supports_block_expectations -end