Skip to content

Commit

Permalink
Added support for google-apis-monitoring_v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 2, 2022
1 parent bb3f2d0 commit 71773b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.8.3 (unreleased)

- Added support for `google-apis-monitoring_v3`
- Added experimental support for Propshaft
- Fixed error with walsender queries on live queries page

Expand Down
36 changes: 31 additions & 5 deletions lib/pghero/methods/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ def azure_stats(metric_name, duration: nil, period: nil, offset: nil, series: fa
private

def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: false)
require "google/cloud/monitoring/v3"

# TODO DRY with RDS stats
duration = (duration || 1.hour).to_i
period = (period || 1.minute).to_i
Expand All @@ -146,10 +144,20 @@ def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: fals
raise Error, "Invalid metric name" unless metric_name =~ /\A[a-z\/_]+\z/i
raise Error, "Invalid database id" unless gcp_database_id =~ /\A[a-z0-9\-:]+\z/i

# we handle three situations:
# we handle four situations:
# 1. google-cloud-monitoring-v3
# 2. google-cloud-monitoring >= 1
# 3. google-cloud-monitoring < 1
# 4. google-apis-monitoring_v3
begin
require "google/cloud/monitoring/v3"
rescue LoadError
begin
require "google/cloud/monitoring"
rescue LoadError
require "google/apis/monitoring_v3"
end
end

# for situations 1 and 2
# Google::Cloud::Monitoring.metric_service is documented
Expand All @@ -175,7 +183,7 @@ def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: fals
view: Google::Cloud::Monitoring::V3::ListTimeSeriesRequest::TimeSeriesView::FULL,
aggregation: aggregation
})
else
elsif defined?(Google::Cloud::Monitoring)
require "google/cloud/monitoring"

client = Google::Cloud::Monitoring::Metric.new
Expand All @@ -198,13 +206,31 @@ def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: fals
Google::Monitoring::V3::ListTimeSeriesRequest::TimeSeriesView::FULL,
aggregation: aggregation
)
else
client = Google::Apis::MonitoringV3::MonitoringService.new

scope = Google::Apis::MonitoringV3::AUTH_MONITORING_READ
client.authorization = Google::Auth.get_application_default([scope])

# default logging is very verbose, but use app default
results = client.list_project_time_series(
"projects/#{gcp_database_id.split(":").first}",
filter: "metric.type = \"cloudsql.googleapis.com/database/#{metric_name}\" AND resource.label.database_id = \"#{gcp_database_id}\"",
interval_start_time: (start_time - period).iso8601,
interval_end_time: end_time.iso8601,
view: 0, # full
aggregation_alignment_period: "#{period}s",
aggregation_per_series_aligner: 12 # mean
).time_series
end

data = {}
result = results.first
if result
result.points.each do |point|
time = Time.at(point.interval.start_time.seconds)
time = point.interval.start_time
# string with google-apis-monitoring_v3
time = time.is_a?(String) ? Time.parse(time) : Time.at(time.seconds)
value = point.value.double_value
value *= 100 if metric_name == "cpu/utilization"
data[time] = value
Expand Down

0 comments on commit 71773b9

Please sign in to comment.