-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1546 from department-of-veterans-affairs/release/…
…FY24Q2.6.2 Release/fy24 q2.6.2
- Loading branch information
Showing
14 changed files
with
149 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module CollectCustomMetrics | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :collect_custom_metrics | ||
end | ||
|
||
def collect_custom_metrics | ||
collect_postgres_metrics | ||
end | ||
|
||
def collect_postgres_metrics | ||
conns = ActiveRecord::Base.connection_pool.connections | ||
|
||
active = conns.count { |conn| conn.in_use? && conn.owner.alive? } | ||
dead = conns.count { |conn| conn.in_use? && !conn.owner.alive? } | ||
idle = conns.count { |conn| !conn.in_use? } | ||
|
||
emit_metrics_service_point("postgres", "active", active) | ||
emit_metrics_service_point("postgres", "dead", dead) | ||
emit_metrics_service_point("postgres", "idle", idle) | ||
end | ||
|
||
def emit_metrics_service_point(db_name, type, count) | ||
MetricsService.emit_gauge( | ||
metric_group: "database", | ||
metric_name: "#{type}_connections", | ||
metric_value: count, | ||
app_name: "efolder", | ||
attrs: { | ||
database: db_name | ||
} | ||
) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,124 @@ | ||
# frozen_string_literal: true | ||
|
||
require "benchmark" | ||
require "datadog/statsd" | ||
require "statsd-instrument" | ||
|
||
# see https://dropwizard.github.io/metrics/3.1.0/getting-started/ for abstractions on metric types | ||
# :nocov: | ||
class MetricsService | ||
# rubocop:disable Metrics/MethodLength | ||
@app = "eFolder" | ||
def self.record(description, service: nil, name: "unknown") | ||
@statsd = Datadog::Statsd.new | ||
|
||
# :reek:LongParameterList | ||
def self.increment_counter(metric_group:, metric_name:, app_name:, attrs: {}, by: 1) | ||
tags = get_tags(app_name, attrs) | ||
stat_name = get_stat_name(metric_group, metric_name) | ||
@statsd.increment(stat_name, tags: tags, by: by) | ||
|
||
# Dynatrace statD implementation | ||
StatsD.increment(stat_name, tags: tags) | ||
end | ||
|
||
def self.record_runtime(metric_group:, app_name:, start_time: Time.zone.now) | ||
metric_name = "runtime" | ||
job_duration_seconds = Time.zone.now - start_time | ||
|
||
emit_gauge( | ||
app_name: app_name, | ||
metric_group: metric_group, | ||
metric_name: metric_name, | ||
metric_value: job_duration_seconds | ||
) | ||
end | ||
|
||
# :reek:LongParameterList | ||
def self.emit_gauge(metric_group:, metric_name:, metric_value:, app_name:, attrs: {}) | ||
tags = get_tags(app_name, attrs) | ||
stat_name = get_stat_name(metric_group, metric_name) | ||
@statsd.gauge(stat_name, metric_value, tags: tags) | ||
|
||
# Dynatrace statD implementation | ||
StatsD.gauge(stat_name, metric_value, tags: tags) | ||
end | ||
|
||
# :nocov: | ||
# :reek:LongParameterList | ||
def self.histogram(metric_group:, metric_name:, metric_value:, app_name:, attrs: {}) | ||
tags = get_tags(app_name, attrs) | ||
stat_name = get_stat_name(metric_group, metric_name) | ||
@statsd.histogram(stat_name, metric_value, tags: tags) | ||
|
||
# Dynatrace statD implementation | ||
StatsD.histogram(stat_name, metric_value, tags: tags) | ||
end | ||
# :nocov: | ||
|
||
private_class_method def self.get_stat_name(metric_group, metric_name) | ||
"dsva-appeals.#{metric_group}.#{metric_name}" | ||
end | ||
|
||
private_class_method def self.get_tags(app_name, attrs) | ||
extra_tags = attrs.reduce([]) do |tags, (key, val)| | ||
tags + ["#{key}:#{val}"] | ||
end | ||
[ | ||
"app:#{app_name}", | ||
"env:#{Rails.current_env}" | ||
] + extra_tags | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength | ||
# :reek:LongParameterList | ||
def self.record(description, service: nil, name: "unknown", caller: nil) | ||
return_value = nil | ||
app = RequestStore[:application] || "other" | ||
|
||
Rails.logger.info("STARTED #{description}") | ||
stopwatch = Benchmark.measure do | ||
return_value = yield | ||
end | ||
|
||
if service && Rails.env.production? | ||
if service | ||
latency = stopwatch.real | ||
DataDogService.emit_gauge( | ||
sent_to_info = { | ||
metric_group: "service", | ||
metric_name: "request_latency", | ||
metric_value: latency, | ||
app_name: @app, | ||
app_name: app, | ||
attrs: { | ||
service: service, | ||
endpoint: name | ||
service: service ||= app, | ||
endpoint: name, | ||
uuid: SecureRandom.uuid | ||
} | ||
) | ||
} | ||
MetricsService.emit_gauge(sent_to_info) | ||
end | ||
|
||
Rails.logger.info("FINISHED #{description}: #{stopwatch}") | ||
|
||
return_value | ||
rescue StandardError | ||
increment_datadog_counter("request_error", service, name) if service | ||
rescue StandardError => error | ||
Rails.logger.error("#{error.message}\n#{error.backtrace.join("\n")}") | ||
Raven.capture_exception(error, extra: { type: "request_error", service: service, name: name, app: app }) | ||
|
||
Rails.logger.info("RESCUED #{description}") | ||
increment_metrics_service_counter("request_error", service, name, app) if service | ||
|
||
# Re-raise the same error. We don't want to interfere at all in normal error handling. | ||
# This is just to capture the metric. | ||
raise | ||
ensure | ||
increment_datadog_counter("request_attempt", service, name) if service | ||
increment_metrics_service_counter("request_attempt", service, name, app) if service | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength | ||
|
||
private_class_method def self.increment_datadog_counter(metric_name, service, endpoint_name) | ||
DataDogService.increment_counter( | ||
private_class_method def self.increment_metrics_service_counter(metric_name, service, endpoint_name, app_name) | ||
increment_counter( | ||
metric_group: "service", | ||
metric_name: metric_name, | ||
app_name: @app, | ||
app_name: app_name, | ||
attrs: { | ||
service: service, | ||
endpoint: endpoint_name | ||
} | ||
) | ||
end | ||
end | ||
# :nocov: | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,4 +54,7 @@ | |
config.s3_enabled = false | ||
|
||
config.api_key = "token" | ||
end | ||
|
||
# Dynatrace variables | ||
ENV["STATSD_ENV"] = "test" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters