Abstract DatadogStatsClient to ForemStatsClient (#12304)

* This change abstracts the DatadogStatsClient into a ForemStatsClient.
The purpose of this abstraction is to set the foundation for a subsequent PR that will allow one to use New Relic for recording Forem stats, instead of Datadog, if there is a New Relic configuration found.
This specific change creates an abstraction layer that can be built upon, without changing any actual default behavior. All specs still pass.

* Use delegate instead of explicit methods.

* Delegate instead of explicit methods.

* Fix the error.

* Refactor according to the suggestions in the comments.

* Ooops.  Stats work better when all of the code is committed.

* Removing the alias of count to increment since that was done in error.
This commit is contained in:
Kirk Haines 2021-01-27 09:25:44 -07:00 committed by GitHub
parent 23f3386f3f
commit ed74f2f245
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 184 additions and 146 deletions

View file

@ -105,7 +105,7 @@ class FollowsController < ApplicationController
Notification.send_new_follower_notification(user_follow) if need_notification
"followed"
rescue ActiveRecord::RecordInvalid
DatadogStatsClient.increment("users.invalid_follow")
ForemStatsClient.increment("users.invalid_follow")
"already followed"
end

View file

@ -23,7 +23,7 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
error = request.env["omniauth.error"]
class_name = error.present? ? error.class.name : ""
DatadogStatsClient.increment(
ForemStatsClient.increment(
"omniauth.failure",
tags: [
"class:#{class_name}",

View file

@ -14,13 +14,13 @@ class StripeActiveCardsController < ApplicationController
flash[:settings_notice] = "Your billing information has been updated"
audit_log("add")
else
DatadogStatsClient.increment("stripe.errors", tags: ["action:create_card", "user_id:#{current_user.id}"])
ForemStatsClient.increment("stripe.errors", tags: ["action:create_card", "user_id:#{current_user.id}"])
flash[:error] = "There was a problem updating your billing info."
end
redirect_to user_settings_path(:billing)
rescue Payments::CardError, Payments::InvalidRequestError => e
DatadogStatsClient.increment("stripe.errors", tags: ["action:create_card", "user_id:#{current_user.id}"])
ForemStatsClient.increment("stripe.errors", tags: ["action:create_card", "user_id:#{current_user.id}"])
redirect_to user_settings_path(:billing), flash: { error: e.message }
end
@ -36,13 +36,13 @@ class StripeActiveCardsController < ApplicationController
flash[:settings_notice] = "Your billing information has been updated"
audit_log("update")
else
DatadogStatsClient.increment("stripe.errors", tags: ["action:update_card", "user_id:#{current_user.id}"])
ForemStatsClient.increment("stripe.errors", tags: ["action:update_card", "user_id:#{current_user.id}"])
flash[:error] = "There was a problem updating your billing info."
end
redirect_to user_settings_path(:billing)
rescue Payments::CardError, Payments::InvalidRequestError => e
DatadogStatsClient.increment("stripe.errors", tags: ["action:update_card", "user_id:#{current_user.id}"])
ForemStatsClient.increment("stripe.errors", tags: ["action:update_card", "user_id:#{current_user.id}"])
redirect_to user_settings_path(:billing), flash: { error: e.message }
end
@ -65,7 +65,7 @@ class StripeActiveCardsController < ApplicationController
redirect_to user_settings_path(:billing)
rescue Payments::InvalidRequestError => e
DatadogStatsClient.increment("stripe.errors")
ForemStatsClient.increment("stripe.errors")
redirect_to user_settings_path(:billing), flash: { error: e.message }
end

View file

@ -0,0 +1,15 @@
module ActsAsForemStatsDriver
extend ActiveSupport::Concern
class_methods do
def setup_driver
define_method(:initialize) do
@driver = yield
end
end
end
included do
delegate :count, :increment, :time, :gauge, to: :@driver
end
end

View file

@ -0,0 +1,16 @@
class ForemStatsDriver
def initialize(*_args)
@driver = select_driver.new
end
delegate(:count, :increment, :time, :gauge, to: :@driver)
private
def select_driver
# Currently, this only supports the default Datadog driver.
# Logic will be added here for selecting the correct driver based on
# the existence of configuration files for the desired stats recipient.
ForemStatsDrivers::DatadogDriver
end
end

View file

@ -0,0 +1,19 @@
module ForemStatsDrivers
class DatadogDriver
include ActsAsForemStatsDriver
setup_driver do
Datadog.configure do |c|
c.tracer env: Rails.env
c.tracer enabled: ENV["DD_API_KEY"].present?
c.tracer partial_flush: true
c.tracer priority_sampling: true
c.use :elasticsearch
c.use :sidekiq
c.use :redis
c.use :rails
c.use :http
end
Datadog::Statsd.new
end
end
end

View file

@ -59,7 +59,7 @@ module Authentication
if log_to_datadog
# Notify DataDog if a new identity was successfully created.
DatadogStatsClient.increment("identity.created", tags: ["provider:#{id_provider}"])
ForemStatsClient.increment("identity.created", tags: ["provider:#{id_provider}"])
end
# Return the successfully-authed used from the transaction.
@ -67,7 +67,7 @@ module Authentication
rescue StandardError => e
# Notify DataDog if something goes wrong in the transaction,
# and then ensure that we re-raise and bubble up the error.
DatadogStatsClient.increment("identity.errors", tags: ["error:#{e.class}", "message:#{e.message}"])
ForemStatsClient.increment("identity.errors", tags: ["error:#{e.class}", "message:#{e.message}"])
raise e
end

View file

@ -25,8 +25,8 @@ module EdgeCache
true
else
Rails.logger.warn("#{provider_class} cannot be used without a #call implementation!")
DatadogStatsClient.increment("edgecache_bust.invalid_provider_class",
tags: ["provider_class:#{provider_class}"])
ForemStatsClient.increment("edgecache_bust.invalid_provider_class",
tags: ["provider_class:#{provider_class}"])
false
end
end

View file

@ -29,8 +29,8 @@ module EdgeCache
# If we can't connect to OpenResty, alert ourselves that
# it is unavailable and return false.
Rails.logger.error("Could not connect to OpenResty via #{ApplicationConfig['OPENRESTY_URL']}!")
DatadogStatsClient.increment("edgecache_bust.service_unavailable",
tags: ["path:#{ApplicationConfig['OPENRESTY_URL']}"])
ForemStatsClient.increment("edgecache_bust.service_unavailable",
tags: ["path:#{ApplicationConfig['OPENRESTY_URL']}"])
false
end
end

View file

@ -56,7 +56,7 @@ module FastlyConfig
"new_version:#{new_version.number}",
]
DatadogStatsClient.increment("fastly.snippets", tags: tags)
ForemStatsClient.increment("fastly.snippets", tags: tags)
end
end
end

View file

@ -47,7 +47,7 @@ module FastlyConfig
"configs_updated:#{configs.join(', ')}",
]
DatadogStatsClient.increment("fastly.update", tags: tags)
ForemStatsClient.increment("fastly.update", tags: tags)
end
def validate_configs(configs)

View file

@ -21,10 +21,10 @@ module Feeds
users.in_batches(of: users_batch_size) do |batch_of_users|
feeds_per_user_id = fetch_feeds(batch_of_users)
DatadogStatsClient.count("feeds::import::fetch_feeds.count", feeds_per_user_id.length)
ForemStatsClient.count("feeds::import::fetch_feeds.count", feeds_per_user_id.length)
feedjira_objects = parse_feeds(feeds_per_user_id)
DatadogStatsClient.count("feeds::import::parse_feeds.count", feedjira_objects.length)
ForemStatsClient.count("feeds::import::parse_feeds.count", feedjira_objects.length)
# NOTE: doing this sequentially to avoid locking problems with the DB
# and unnecessary conflicts
@ -33,7 +33,7 @@ module Feeds
# only actually uses feed.url
user = batch_of_users.detect { |u| u.id == user_id }
DatadogStatsClient.time("feeds::import::create_articles_from_user_feed", tags: ["user_id:#{user_id}"]) do
ForemStatsClient.time("feeds::import::create_articles_from_user_feed", tags: ["user_id:#{user_id}"]) do
create_articles_from_user_feed(user, feed)
end
end
@ -46,7 +46,7 @@ module Feeds
batch_of_users.update_all(feed_fetched_at: Time.current)
end
DatadogStatsClient.count("feeds::import::articles.count", total_articles_count)
ForemStatsClient.count("feeds::import::articles.count", total_articles_count)
total_articles_count
end
@ -70,7 +70,7 @@ module Feeds
cleaned_url = url.to_s.strip
next if cleaned_url.blank?
response = DatadogStatsClient.time("feeds::import::fetch_feed", tags: ["user_id:#{user_id}", "url:#{url}"]) do
response = ForemStatsClient.time("feeds::import::fetch_feed", tags: ["user_id:#{user_id}", "url:#{url}"]) do
HTTParty.get(cleaned_url, timeout: 10)
end
@ -97,7 +97,7 @@ module Feeds
# TODO: put this in separate service object
def parse_feeds(feeds_per_user_id)
result = Parallel.map(feeds_per_user_id, in_threads: num_parsers) do |user_id, feed_xml|
parsed_feed = DatadogStatsClient.time("feeds::import::parse_feed", tags: ["user_id:#{user_id}"]) do
parsed_feed = ForemStatsClient.time("feeds::import::parse_feed", tags: ["user_id:#{user_id}"]) do
Feedjira.parse(feed_xml)
end

View file

@ -73,7 +73,7 @@ module Github
Honeycomb.add_field("github.result", "error")
Honeycomb.add_field("github.error", class_name)
DatadogStatsClient.increment(
ForemStatsClient.increment(
"github.errors",
tags: ["error:#{class_name}", "message:#{exception.message}"],
)

View file

@ -27,7 +27,7 @@ module Loggers
end
def log_to_datadog(metric_name, value, tags = [])
DatadogStatsClient.gauge(metric_name, value, tags: tags)
ForemStatsClient.gauge(metric_name, value, tags: tags)
end
end
end

View file

@ -39,7 +39,7 @@ module Notifications
attr_reader :receiver_id, :welcome_broadcast
def log_to_datadog
DatadogStatsClient.increment(
ForemStatsClient.increment(
"notifications.welcome",
tags: ["user_id:#{receiver_id}", "title:#{welcome_broadcast.title}"],
)

View file

@ -65,14 +65,14 @@ module Payments
def request
yield
rescue Stripe::InvalidRequestError => e
DatadogStatsClient.increment("stripe.errors", tags: ["error:InvalidRequestError"])
ForemStatsClient.increment("stripe.errors", tags: ["error:InvalidRequestError"])
raise InvalidRequestError, e.message
rescue Stripe::CardError => e
DatadogStatsClient.increment("stripe.errors", tags: ["error:CardError"])
ForemStatsClient.increment("stripe.errors", tags: ["error:CardError"])
raise CardError, e.message
rescue Stripe::StripeError => e
Honeybadger.notify(e)
DatadogStatsClient.increment("stripe.errors", tags: ["error:StripeError"])
ForemStatsClient.increment("stripe.errors", tags: ["error:StripeError"])
raise PaymentsError, e.message
end
end

View file

@ -110,6 +110,6 @@ class RateLimitChecker
end
def log_to_datadog
DatadogStatsClient.increment("rate_limit.limit_reached", tags: ["user:#{user.id}", "action:#{action}"])
ForemStatsClient.increment("rate_limit.limit_reached", tags: ["user:#{user.id}", "action:#{action}"])
end
end

View file

@ -48,7 +48,7 @@ module Search
def record_error(error_message, class_name)
Honeycomb.add_field("elasticsearch.result", "error")
Honeycomb.add_field("elasticsearch.error", class_name)
DatadogStatsClient.increment("elasticsearch.errors", tags: ["error:#{class_name}", "message:#{error_message}"])
ForemStatsClient.increment("elasticsearch.errors", tags: ["error:#{class_name}", "message:#{error_message}"])
end
def target

View file

@ -31,7 +31,7 @@ module TwitterClient
Honeycomb.add_field("twitter.result", "error")
Honeycomb.add_field("twitter.error", class_name)
DatadogStatsClient.increment(
ForemStatsClient.increment(
"twitter.errors",
tags: ["error:#{class_name}", "message:#{exception.message}"],
)

View file

@ -37,6 +37,6 @@ class DataUpdateWorker
"time=#{Time.current.rfc3339}, script=#{file_name}, status=#{status}",
)
DatadogStatsClient.increment("data_update_scripts.status", tags: ["status:#{status}", "script_name:#{file_name}"])
ForemStatsClient.increment("data_update_scripts.status", tags: ["status:#{status}", "script_name:#{file_name}"])
end
end

View file

@ -6,7 +6,7 @@ module Metrics
def perform
failed_scripts = DataUpdateScript.failed.where(created_at: 1.day.ago..Time.current)
failed_scripts.find_each do |script|
DatadogStatsClient.count(
ForemStatsClient.count(
"data_update_scripts.failures",
1,
tags: ["file_name:#{script.file_name}"],

View file

@ -23,7 +23,7 @@ module Metrics
.where("time > ?", 1.day.ago)
.where("properties->>'title' = ?", title)
DatadogStatsClient.count(
ForemStatsClient.count(
"ahoy_events",
event.size,
tags: ["title:#{title}"],

View file

@ -6,7 +6,7 @@ module Metrics
def perform
# Articles published in the past 24 hours with at least 15 "score" (positive/negative reactions)
articles_min_15_score_past_24h = Article.published.where("score >= ? AND published_at > ?", 15, 1.day.ago).size
DatadogStatsClient.count(
ForemStatsClient.count(
"articles.min_15_score_past_24h",
articles_min_15_score_past_24h,
tags: ["resource:articles"],
@ -16,7 +16,7 @@ module Metrics
articles_min_15_comment_score_past_24h = Article.published
.where("comment_score >= ? AND published_at > ?", 15, 1.day.ago)
.size
DatadogStatsClient.count(
ForemStatsClient.count(
"articles.min_15_comment_score_past_24h",
articles_min_15_comment_score_past_24h,
tags: ["resource:articles"],
@ -24,11 +24,11 @@ module Metrics
# Articles published in the past 24 which were that user's first article
first_articles_past_24h = Article.where(nth_published_by_author: 1).where("published_at > ?", 1.day.ago).size
DatadogStatsClient.count("articles.first_past_24h", first_articles_past_24h, tags: ["resource:articles"])
ForemStatsClient.count("articles.first_past_24h", first_articles_past_24h, tags: ["resource:articles"])
# Users who signed up in the past 24 hours who have made at least 1 comment so far
new_users_min_1_comment_past_24h = User.where("comments_count >= ? AND registered_at > ?", 1, 24.hours.ago).size
DatadogStatsClient.count(
ForemStatsClient.count(
"users.new_min_1_comment_past_24h",
new_users_min_1_comment_past_24h,
tags: ["resource:users"],
@ -36,12 +36,12 @@ module Metrics
# Total negative reactions in the past 24 hours
negative_reactions_past_24h = Reaction.where("points < 0").where("created_at > ?", 1.day.ago).size
DatadogStatsClient.count("reactions.negative_past_24h", negative_reactions_past_24h, tags: ["resource:reactions"])
ForemStatsClient.count("reactions.negative_past_24h", negative_reactions_past_24h, tags: ["resource:reactions"])
# Total abuse (etc.) reports in the past 24 hours
categories = ["spam", "other", "rude or vulgar", "harassment"]
reports_past_24_hours = FeedbackMessage.where(category: categories).where("created_at > ?", 1.day.ago).size
DatadogStatsClient.count(
ForemStatsClient.count(
"feedback_messages.reports_past_24_hours",
reports_past_24_hours,
tags: ["resource:feedback_messages"],
@ -76,8 +76,8 @@ module Metrics
distinct_user_values = user_ids_by_day.flatten.group_by(&:itself).transform_values(&:count).values
distinct_counts = distinct_user_values.group_by(&:itself).transform_values(&:count)
distinct_counts.each_key do |key|
DatadogStatsClient.count("users.active_days_past_week", distinct_counts[key],
tags: ["resource:users", "group:#{group}", "day_count:#{key}"])
ForemStatsClient.count("users.active_days_past_week", distinct_counts[key],
tags: ["resource:users", "group:#{group}", "day_count:#{key}"])
end
end
end

View file

@ -13,7 +13,7 @@ module Metrics
end
Rails.logger.info(message: "db_table_size", table_info: { table_name: model.table_name, table_size: db_count })
DatadogStatsClient.gauge("postgres.db_table_size", db_count, tags: ["table_name:#{model.table_name}"])
ForemStatsClient.gauge("postgres.db_table_size", db_count, tags: ["table_name:#{model.table_name}"])
next unless model.const_defined?(:SEARCH_CLASS)
@ -22,8 +22,8 @@ module Metrics
else
model::SEARCH_CLASS.document_count
end
DatadogStatsClient.gauge("elasticsearch.document_count", document_count,
tags: ["table_name:#{model.table_name}"])
ForemStatsClient.gauge("elasticsearch.document_count", document_count,
tags: ["table_name:#{model.table_name}"])
end
end
end

View file

@ -9,7 +9,7 @@ module Moderator
admin = User.find(admin_id)
Moderator::BanishUser.call(admin: admin, user: abuser)
rescue StandardError => e
DatadogStatsClient.count("moderators.banishuser", 1, tags: ["action:failed", "user_id:#{abuser.id}"])
ForemStatsClient.count("moderators.banishuser", 1, tags: ["action:failed", "user_id:#{abuser.id}"])
Honeybadger.notify(e)
end
end

View file

@ -13,7 +13,7 @@ module Moderator
new_score = reactions.sum(:points) + Reaction.where(reactable: user).sum(:points)
articles.update_all(score: new_score)
rescue StandardError => e
DatadogStatsClient.count("moderators.sink", 1, tags: ["action:failed", "user_id:#{user.id}"])
ForemStatsClient.count("moderators.sink", 1, tags: ["action:failed", "user_id:#{user.id}"])
Honeybadger.notify(e)
end
end

View file

@ -19,7 +19,7 @@ module Users
# whole object
NotifyMailer.with(name: user.name, email: user.email).account_deleted_email.deliver_now
rescue StandardError => e
DatadogStatsClient.count("users.delete", 1, tags: ["action:failed", "user_id:#{user.id}"])
ForemStatsClient.count("users.delete", 1, tags: ["action:failed", "user_id:#{user.id}"])
Honeybadger.context({ user_id: user.id })
Honeybadger.notify(e)
end

View file

@ -1,13 +0,0 @@
Datadog.configure do |c|
c.tracer env: Rails.env
c.tracer enabled: ENV["DD_API_KEY"].present?
c.tracer partial_flush: true
c.tracer priority_sampling: true
c.use :elasticsearch
c.use :sidekiq
c.use :redis
c.use :rails
c.use :http
end
DatadogStatsClient = Datadog::Statsd.new

View file

@ -0,0 +1 @@
ForemStatsClient = ForemStatsDriver.new

View file

@ -31,7 +31,7 @@ module DataUpdateScripts
# Sending IDs of deleted articles to Datadog
result.map { |row| row["id"] }.in_groups_of(1000) do |ids|
DatadogStatsClient.event(
ForemStatsClient.event(
"DataUpdateScripts::RemoveDraftArticlesWithDuplicateCanonicalUrl",
"deleted draft articles with the same canonical_url and same body_markdown",
tags: ids,
@ -67,7 +67,7 @@ module DataUpdateScripts
# Sending IDs of deleted articles to Datadog
result.map { |row| row["id"] }.in_groups_of(1000) do |ids|
DatadogStatsClient.event(
ForemStatsClient.event(
"DataUpdateScripts::RemoveDraftArticlesWithDuplicateCanonicalUrl",
"deleted draft articles with the same canonical_url and different body_markdown",
tags: ids,

View file

@ -11,7 +11,7 @@ module Sidekiq
"retry:#{job['retry']}",
"retry_count:#{job['retry_count']}",
]
DatadogStatsClient.increment(
ForemStatsClient.increment(
"sidekiq.worker.retries_exhausted", tags: tags
)
end

View file

@ -4,9 +4,9 @@ describe Sidekiq::WorkerRetriesExhaustedReporter, type: :labor do
let(:job_hash) { { "class" => "TempWorker", "retry" => 15, "retry_count" => 1, "jid" => "123abc" } }
it "increments worker retries exhausted in Datadog" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
described_class.report_final_failure(job_hash)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"sidekiq.worker.retries_exhausted",
tags: [
"action:dead",

View file

@ -50,7 +50,7 @@ RSpec.describe "StripeActiveCards", type: :request do
end
it "increments sidekiq.errors in Datadog on failure" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
invalid_error = Stripe::InvalidRequestError.new("message", "param")
allow(Stripe::Customer).to receive(:create).and_raise(invalid_error)
@ -59,7 +59,7 @@ RSpec.describe "StripeActiveCards", type: :request do
expect(flash[:error]).to eq(invalid_error.message)
tags = hash_including(tags: array_including("error:InvalidRequestError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
it "updates the user's updated_at" do
@ -73,13 +73,13 @@ RSpec.describe "StripeActiveCards", type: :request do
end
it "increments sidekiq.errors.new_subscription in Datadog on failure" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
invalid_error = Stripe::InvalidRequestError.new(nil, nil)
allow(Stripe::Customer).to receive(:create).and_raise(invalid_error)
post "/stripe_active_cards", params: { stripe_token: stripe_helper.generate_card_token }
tags = hash_including(tags: array_including("action:create_card", "user_id:#{user.id}"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
end
@ -124,7 +124,7 @@ RSpec.describe "StripeActiveCards", type: :request do
_, source = create_user_with_card(user, card_token)
original_card_id = source.id
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
card_error = Stripe::CardError.new("message", "param")
allow(Stripe::Customer).to receive(:retrieve).and_raise(card_error)
@ -133,7 +133,7 @@ RSpec.describe "StripeActiveCards", type: :request do
expect(flash[:error]).to eq(card_error.message)
tags = hash_including(tags: array_including("error:CardError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
it "updates the user's updated_at" do
@ -151,7 +151,7 @@ RSpec.describe "StripeActiveCards", type: :request do
_, source = create_user_with_card(user, card_token)
original_card_id = source.id
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
card_error = Stripe::CardError.new("message", "param")
allow(Stripe::Customer).to receive(:retrieve).and_raise(card_error)
@ -159,7 +159,7 @@ RSpec.describe "StripeActiveCards", type: :request do
expect(response).to redirect_to(user_settings_path(:billing))
expect(flash[:error]).to eq(card_error.message)
tags = hash_including(tags: array_including("action:update_card", "user_id:#{user.id}"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
end

View file

@ -83,10 +83,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "records successful identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"identity.created", tags: ["provider:apple"]
)
end
@ -122,10 +122,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "does not record an identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).not_to have_received(:increment)
expect(ForemStatsClient).not_to have_received(:increment)
end
it "updates the proper data from the auth payload" do
@ -281,10 +281,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "records successful identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"identity.created", tags: ["provider:github"]
)
end
@ -293,12 +293,12 @@ RSpec.describe Authentication::Authenticator, type: :service do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(Identity).to receive(:save!).and_raise(StandardError)
# rubocop:enable RSpec/AnyInstance
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.call(auth_payload) }.to raise_error(StandardError)
tags = hash_including(tags: array_including("error:StandardError"))
expect(DatadogStatsClient).to have_received(:increment).with("identity.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("identity.errors", tags)
end
end
@ -332,10 +332,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "does not record an identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).not_to have_received(:increment)
expect(ForemStatsClient).not_to have_received(:increment)
end
it "sets remember_me for the existing user" do
@ -391,12 +391,12 @@ RSpec.describe Authentication::Authenticator, type: :service do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(Identity).to receive(:save!).and_raise(StandardError)
# rubocop:enable RSpec/AnyInstance
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.call(auth_payload) }.to raise_error(StandardError)
tags = hash_including(tags: array_including("error:StandardError"))
expect(DatadogStatsClient).to have_received(:increment).with("identity.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("identity.errors", tags)
end
end
@ -492,10 +492,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "records successful identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"identity.created", tags: ["provider:facebook"]
)
end
@ -504,12 +504,12 @@ RSpec.describe Authentication::Authenticator, type: :service do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(Identity).to receive(:save!).and_raise(StandardError)
# rubocop:enable RSpec/AnyInstance
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.call(auth_payload) }.to raise_error(StandardError)
tags = hash_including(tags: array_including("error:StandardError"))
expect(DatadogStatsClient).to have_received(:increment).with("identity.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("identity.errors", tags)
end
end
end
@ -584,10 +584,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "records successful identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"identity.created", tags: ["provider:twitter"]
)
end
@ -623,10 +623,10 @@ RSpec.describe Authentication::Authenticator, type: :service do
end
it "does not record an identity creation metric" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
service.call
expect(DatadogStatsClient).not_to have_received(:increment)
expect(ForemStatsClient).not_to have_received(:increment)
end
it "updates the proper data from the auth payload" do

View file

@ -35,7 +35,7 @@ RSpec.describe FastlyConfig::Snippets, type: :service do
end
it "logs success messages" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
allow(fastly_version).to receive(:number).and_return(1)
allow(fastly).to receive(:get_snippet).and_return(fastly_snippet)
allow(fastly_snippet).to receive(:content).and_return("test")
@ -48,7 +48,7 @@ RSpec.describe FastlyConfig::Snippets, type: :service do
tags = hash_including(tags: array_including("snippet_update_type:update", "snippet_name:test",
"new_version:#{fastly_version.number}"))
expect(DatadogStatsClient).to have_received(:increment).with("fastly.snippets", tags).at_least(:once)
expect(ForemStatsClient).to have_received(:increment).with("fastly.snippets", tags).at_least(:once)
end
end
end

View file

@ -53,7 +53,7 @@ RSpec.describe FastlyConfig::Update, type: :service do
it "logs success messages" do
allow(Rails.logger).to receive(:info)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
stub_const("#{described_class}::FASTLY_CONFIGS", ["Snippets"])
snippet_handler = instance_double FastlyConfig::Snippets
@ -66,7 +66,7 @@ RSpec.describe FastlyConfig::Update, type: :service do
tags = hash_including(tags: array_including("new_version:#{fastly_version.number}", "configs_updated:Snippets"))
expect(DatadogStatsClient).to have_received(:increment).with("fastly.update", tags)
expect(ForemStatsClient).to have_received(:increment).with("fastly.update", tags)
end
end
end

View file

@ -6,7 +6,7 @@ RSpec.describe Notifications::WelcomeNotification::Send, type: :service do
before do
allow(User).to receive(:mascot_account).and_return(create(:user))
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
end
it "creates a new welcome notification", :aggregate_failures do
@ -26,7 +26,7 @@ RSpec.describe Notifications::WelcomeNotification::Send, type: :service do
welcome_notification = Notification.find_by(notifiable_id: welcome_broadcast.id)
tags = ["user_id:#{welcome_notification.user_id}", "title:#{welcome_notification.notifiable.title}"]
expect(DatadogStatsClient).to have_received(:increment).with("notifications.welcome", tags: tags)
expect(ForemStatsClient).to have_received(:increment).with("notifications.welcome", tags: tags)
end
end
end

View file

@ -21,11 +21,11 @@ RSpec.describe Payments::Customer, type: :service do
end
it "increments stripe.errors if the customer does not exist" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.get("foobar") }.to raise_error(Payments::InvalidRequestError)
tags = hash_including(tags: array_including("error:InvalidRequestError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
it "raises Payments::PaymentsError for any other known error" do
@ -34,12 +34,12 @@ RSpec.describe Payments::Customer, type: :service do
end
it "increments stripe.errors for any other known error" do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
allow(Stripe::Customer).to receive(:retrieve).with("foobar").and_raise(Stripe::StripeError)
expect { described_class.get("foobar") }.to raise_error(Payments::PaymentsError)
tags = hash_including(tags: array_including("error:StripeError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
end
@ -76,11 +76,11 @@ RSpec.describe Payments::Customer, type: :service do
it "increments stripe.errors if anything in the params is invalid" do
error = Stripe::InvalidRequestError.new("message", :token)
allow(Stripe::Customer).to receive(:create_source).and_raise(error)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.create_source("customer_id", "token") }.to raise_error(Payments::InvalidRequestError)
tags = hash_including(tags: array_including("error:InvalidRequestError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
it "raises Payments::PaymentsError for any other known error" do
@ -90,11 +90,11 @@ RSpec.describe Payments::Customer, type: :service do
it "increments stripe.errors for any other known error" do
allow(Stripe::Customer).to receive(:create_source).and_raise(Stripe::StripeError)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
expect { described_class.create_source("customer_id", "token") }.to raise_error(Payments::PaymentsError)
tags = hash_including(tags: array_including("error:StripeError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
end
@ -166,7 +166,7 @@ RSpec.describe Payments::Customer, type: :service do
it "increments stripe.errors if the card has any troubles" do
StripeMock.prepare_card_error(:expired_card)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
customer = Stripe::Customer.create
token = stripe_helper.generate_card_token
@ -176,7 +176,7 @@ RSpec.describe Payments::Customer, type: :service do
described_class.charge(customer: customer, amount: 1, description: "Test charge", card_id: card.id)
end.to raise_error(Payments::CardError)
tags = hash_including(tags: array_including("error:CardError"))
expect(DatadogStatsClient).to have_received(:increment).with("stripe.errors", tags)
expect(ForemStatsClient).to have_received(:increment).with("stripe.errors", tags)
end
end
end

View file

@ -110,10 +110,10 @@ RSpec.describe RateLimitChecker, type: :service do
allow(Rails.cache)
.to receive(:read).with("#{user.id}_organization_creation", raw: true)
.and_return(SiteConfig.rate_limit_organization_creation + 1)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
described_class.new(user).limit_by_action("organization_creation")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"rate_limit.limit_reached",
tags: ["user:#{user.id}", "action:organization_creation"],
)

View file

@ -61,7 +61,7 @@ RSpec.describe "Authenticating with Apple", vcr: { cassette_name: "fastly_sloan"
before do
omniauth_setup_invalid_credentials(:apple)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
end
after do
@ -86,7 +86,7 @@ RSpec.describe "Authenticating with Apple", vcr: { cassette_name: "fastly_sloan"
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "apple", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -102,7 +102,7 @@ RSpec.describe "Authenticating with Apple", vcr: { cassette_name: "fastly_sloan"
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "apple", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -115,7 +115,7 @@ RSpec.describe "Authenticating with Apple", vcr: { cassette_name: "fastly_sloan"
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "apple", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end

View file

@ -92,7 +92,7 @@ RSpec.describe "Authenticating with Facebook" do
before do
omniauth_setup_invalid_credentials(:facebook)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
end
after do
@ -125,7 +125,7 @@ RSpec.describe "Authenticating with Facebook" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "facebook", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -141,7 +141,7 @@ RSpec.describe "Authenticating with Facebook" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "facebook", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -154,7 +154,7 @@ RSpec.describe "Authenticating with Facebook" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "facebook", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end

View file

@ -59,7 +59,7 @@ RSpec.describe "Authenticating with GitHub" do
before do
omniauth_setup_invalid_credentials(:github)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
end
after do
@ -92,7 +92,7 @@ RSpec.describe "Authenticating with GitHub" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -108,7 +108,7 @@ RSpec.describe "Authenticating with GitHub" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -121,7 +121,7 @@ RSpec.describe "Authenticating with GitHub" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end

View file

@ -55,7 +55,7 @@ RSpec.describe "Authenticating with Twitter" do
before do
omniauth_setup_invalid_credentials(:twitter)
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
end
after do
@ -88,7 +88,7 @@ RSpec.describe "Authenticating with Twitter" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "twitter", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -104,7 +104,7 @@ RSpec.describe "Authenticating with Twitter" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "twitter", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
@ -117,7 +117,7 @@ RSpec.describe "Authenticating with Twitter" do
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "twitter", "{}")
expect(DatadogStatsClient).to have_received(:increment).with(
expect(ForemStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end

View file

@ -51,7 +51,7 @@ RSpec.describe DataUpdateWorker, type: :worker do
end
it "logs data to Datadog", :aggregate_failures do
allow(DatadogStatsClient).to receive(:increment)
allow(ForemStatsClient).to receive(:increment)
worker.perform
statuses.each do |status|
@ -59,7 +59,7 @@ RSpec.describe DataUpdateWorker, type: :worker do
"data_update_scripts.status",
{ tags: ["status:#{status}", "script_name:20200214151804_data_update_test_script"] },
]
expect(DatadogStatsClient).to have_received(:increment).once.with(*expected_args)
expect(ForemStatsClient).to have_received(:increment).once.with(*expected_args)
end
end
end

View file

@ -8,12 +8,12 @@ RSpec.describe Metrics::CheckDataUpdateScriptStatuses, type: :worker do
create(:data_update_script)
create(:data_update_script, status: :failed, created_at: 1.month.ago)
failed_script = create(:data_update_script, status: :failed)
allow(DatadogStatsClient).to receive(:count)
allow(ForemStatsClient).to receive(:count)
described_class.new.perform
expect(DatadogStatsClient).to have_received(:count).once
expect(ForemStatsClient).to have_received(:count).once
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with(
"data_update_scripts.failures", 1, { tags: ["file_name:#{failed_script.file_name}"] }
).at_least(1)

View file

@ -9,15 +9,15 @@ RSpec.describe Metrics::RecordDailyNotificationsWorker, type: :worker do
let(:event_title_count) { Metrics::RecordDailyNotificationsWorker::EVENT_TITLES.count }
before do
allow(DatadogStatsClient).to receive(:count)
allow(ForemStatsClient).to receive(:count)
ahoy_event
described_class.new.perform
end
it "logs welcome notification click events created in the past day" do
expect(DatadogStatsClient).to have_received(:count).exactly(event_title_count).times
expect(ForemStatsClient).to have_received(:count).exactly(event_title_count).times
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count)
.with("ahoy_events", 1, { tags: ["title:welcome_notification_welcome_thread"] })
.at_least(1)

View file

@ -27,43 +27,43 @@ RSpec.describe Metrics::RecordDailyUsageWorker, type: :worker do
describe "#perform" do
before do
allow(DatadogStatsClient).to receive(:count)
allow(ForemStatsClient).to receive(:count)
described_class.new.perform
end
it "logs articles with at least 15 score" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("articles.min_15_score_past_24h", 2, tags: Array).at_least(1)
end
it "logs articles with at least comment 15 score" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("articles.min_15_comment_score_past_24h", 1, tags: Array).at_least(1)
end
it "records first articles" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("articles.first_past_24h", 1, tags: Array).at_least(1)
end
it "records new users with at least one comment" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("users.new_min_1_comment_past_24h", 2, tags: Array).at_least(1)
end
it "records negative reactions" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("reactions.negative_past_24h", 1, tags: Array).at_least(1)
end
it "records report feedback_messages" do
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:count).with("feedback_messages.reports_past_24_hours", 1, tags: Array).at_least(1)
end
end

View file

@ -21,20 +21,20 @@ RSpec.describe Metrics::RecordDataCountsWorker, type: :worker do
end
it "logs estimated counts in Datadog" do
allow(DatadogStatsClient).to receive(:gauge)
allow(ForemStatsClient).to receive(:gauge)
described_class.new.perform
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:gauge).with("postgres.db_table_size", 0, tags: Array).at_least(1)
end
it "logs index counts in Datadog" do
allow(DatadogStatsClient).to receive(:gauge)
allow(ForemStatsClient).to receive(:gauge)
described_class.new.perform
expect(
DatadogStatsClient,
ForemStatsClient,
).to have_received(:gauge).with("elasticsearch.document_count", Integer, tags: Array).at_least(1)
end
end