Skip to content

Commit ca96afd

Browse files
authored
Merge pull request #249 from emilymye/monitoring
Migrate monitoring code to V3 API
2 parents a5dbf20 + 3df4758 commit ca96afd

26 files changed

+431
-462
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ Fog implements [v1](https://cloud.google.com/dns/api/v1/) of the Google Cloud DN
3434

3535
## Monitoring
3636

37-
Fog implements [v2beta2](https://cloud.google.com/monitoring/v2beta2/) of the Google Cloud Monitoring API. As of 2016-03-15, we believe Fog for Google Cloud Monitoring is feature complete. We are always looking for people to improve our code and test coverage, so please [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like.
37+
Fog mostly implements [v3](https://cloud.google.com/monitoring/api/ref_v3/rest/) of the Google Cloud Monitoring API.
38+
39+
As of 2017-09-26, some less common API methods are missing and may be added as requested. Feature requests or pull requests for
40+
additions are welcome.
41+
42+
We are always looking for people to improve our code and test coverage, so please [file issues](https://github.com/fog/fog-google/issues) for any anomalies you see or features you would like.
3843

3944
## Pubsub
4045

examples/monitoring/metric_descriptors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test
1717

1818
puts "\nListing all MetricDescriptors related to Google Compute Engine..."
1919
puts "-----------------------------------------------------------------"
20-
md = connection.metric_descriptors.all(:query => "compute")
20+
md = connection.metric_descriptors.all(:filter => 'metric.type = starts_with("compute.googleapis.com")')
2121
puts "Number of compute metric descriptors: #{md.length}"
2222
end
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# All examples presume that you have a ~/.fog credentials file set up.
2+
# # More info on it can be found here: http://fog.io/about/getting_started.html
3+
#
4+
require "bundler"
5+
Bundler.require(:default, :development)
6+
# Uncomment this if you want to make real requests to GCE (you _will_ be billed!)
7+
# WebMock.disable!
8+
#
9+
10+
def test
11+
connection = Fog::Google::Monitoring.new
12+
13+
puts "Listing all MonitoredResourceDescriptors..."
14+
puts "--------------------------------"
15+
md = connection.monitored_resource_descriptors
16+
puts "Number of all monitored resource descriptors: #{md.length}"
17+
18+
puts "\nListing MonitoredResourceDescriptors related to Google Compute Engine..."
19+
puts "-----------------------------------------------------------------"
20+
md = connection.monitored_resource_descriptors.all(:filter => 'resource.type = starts_with("gce_")')
21+
puts "Number of compute monitored resource : #{md.length}"
22+
end
23+
24+
test

examples/monitoring/timeseries_collection.rb

+13-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88

99
def test
1010
connection = Fog::Google::Monitoring.new
11-
12-
puts "Listing all Timeseries for the metric compute.googleapis.com/instance/uptime..."
11+
interval = {
12+
:start_time => (DateTime.now - 1).rfc3339,
13+
:end_time => DateTime.now.rfc3339
14+
}
15+
puts "Listing Timeseries from the last hour for metric compute.googleapis.com/instance/uptime..."
1316
puts "-------------------------------------------------------------------------------"
14-
tc = connection.timeseries_collection.all("compute.googleapis.com/instance/uptime",
15-
DateTime.now.rfc3339)
17+
tc = connection.timeseries_collection.all(:filter => 'metric.type = "compute.googleapis.com/instance/uptime"',
18+
:interval => interval)
1619
puts "Number of matches: #{tc.length}"
1720

18-
puts "\nListing all Timeseries for the metric compute.googleapis.com/instance/uptime &"
21+
puts "\nListing all Timeseries for metric compute.googleapis.com/instance/uptime &"
1922
puts "the region us-central1..."
2023
puts "------------------------------------------------------------------------------"
21-
tc = connection.timeseries_collection.all("compute.googleapis.com/instance/uptime",
22-
DateTime.now.rfc3339,
23-
:labels => "cloud.googleapis.com/location=~us-central1.*")
24+
filter = [
25+
'metric.type = "compute.googleapis.com/instance/uptime"',
26+
'resource.label.zone = "us-central1-c"'
27+
].join(" AND ")
28+
tc = connection.timeseries_collection.all(:filter => filter, :interval => interval)
2429
puts "Number of matches: #{tc.length}"
2530
end
2631

examples/monitoring/timeseries_descriptors.rb

-27
This file was deleted.

lib/fog/google/models/monitoring/metric_descriptor.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ module Fog
44
module Google
55
class Monitoring
66
##
7-
# A metricDescriptor defines the name, label keys, and data type of a particular metric.
7+
# A metricDescriptor defines a metric type and its schema.
88
#
9-
# @see https://cloud.google.com/monitoring/v2beta2/metricDescriptors#resource
9+
# @see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
1010
class MetricDescriptor < Fog::Model
1111
identity :name
1212

1313
attribute :description
14+
attribute :display_name, :aliases => "displayName"
1415
attribute :labels
15-
attribute :project
16-
attribute :type_descriptor, :aliases => "typeDescriptor"
16+
attribute :metric_kind, :aliases => "metricKind"
17+
attribute :type
18+
attribute :value_type, :aliases => "valueType"
19+
attribute :unit
1720
end
1821
end
1922
end

lib/fog/google/models/monitoring/metric_descriptors.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ class MetricDescriptors < Fog::Collection
1111
# Lists all Metric Descriptors.
1212
#
1313
# @param [Hash] options Optional query parameters.
14-
# @option options [String] count Maximum number of time series descriptors per page. Used for pagination.
14+
# @option options [String] page_size Maximum number of metric descriptors per page. Used for pagination.
1515
# @option options [String] page_token The pagination token, which is used to page through large result sets.
16-
# @option options [String] query The query used to search against existing metrics. Separate keywords with a space;
17-
# the service joins all keywords with AND, meaning that all keywords must match for a metric to be returned.
18-
# If this field is omitted, all metrics are returned. If an empty string is passed with this field,
19-
# no metrics are returned.
16+
# @option options [String] filter Monitoring filter specifying which metric descriptors are to be returned.
17+
# @see https://cloud.google.com/monitoring/api/v3/filters filter documentation
2018
# @return [Array<Fog::Google::Monitoring::MetricDescriptor>] List of Metric Descriptors.
2119
def all(options = {})
22-
data = service.list_metric_descriptors(options).body["metrics"] || []
20+
data = service.list_metric_descriptors(options).body["metricDescriptors"] || []
2321
load(data)
2422
end
2523
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "fog/core/model"
2+
3+
module Fog
4+
module Google
5+
class Monitoring
6+
##
7+
# A monitoredResourceDescriptor defines a metric type and its schema.
8+
#
9+
# @see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.monitoredResourceDescriptors#MonitoredResourceDescriptor
10+
class MonitoredResourceDescriptor < Fog::Model
11+
identity :name
12+
13+
attribute :description
14+
attribute :display_name, :aliases => "displayName"
15+
attribute :type
16+
attribute :labels
17+
end
18+
end
19+
end
20+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "fog/core/collection"
2+
require "fog/google/models/monitoring/monitored_resource_descriptor"
3+
4+
module Fog
5+
module Google
6+
class Monitoring
7+
class MonitoredResourceDescriptors < Fog::Collection
8+
model Fog::Google::Monitoring::MonitoredResourceDescriptor
9+
10+
##
11+
# Lists all Monitored Resource Descriptors.
12+
#
13+
# @param [Hash] options Optional query parameters.
14+
# @option options [String] page_size Maximum number of metric descriptors per page. Used for pagination.
15+
# @option options [String] page_token The pagination token, which is used to page through large result sets.
16+
# @option options [String] filter The monitoring filter used to search against existing descriptors.
17+
# See
18+
# @return [Array<Fog::Google::Monitoring::MetricDescriptor>] List of Monitored Resource Descriptors.
19+
def all(options = {})
20+
data = service.list_monitored_resource_descriptors(options).body["resourceDescriptors"] || []
21+
load(data)
22+
end
23+
end
24+
end
25+
end
26+
end

lib/fog/google/models/monitoring/timeseries.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ class Monitoring
66
##
77
# A time series is a collection of data points that represents the value of a metric of a project over time.
88
#
9-
# @see https://developers.google.com/cloud-monitoring/v2beta1/timeseries
9+
# https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list
1010
class Timeseries < Fog::Model
11-
identity :time_series_desc, :aliases => "timeseriesDesc"
12-
11+
attribute :metric
12+
attribute :resource
13+
attribute :metric_kind, :aliases => "metricKind"
14+
attribute :value_type, :aliases => "valueType"
1315
attribute :points
1416
end
1517
end

lib/fog/google/models/monitoring/timeseries_collection.rb

+19-12
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@ class TimeseriesCollection < Fog::Collection
1010
##
1111
# Lists all Timeseries.
1212
#
13-
# @param [String] metric The name of the metric (Metric names are protocol-free URLs).
14-
# @param [String] youngest End of the time interval (inclusive), which is expressed as an RFC 3339 timestamp.
15-
# @param [Hash] options Optional query parameters.
16-
# @option options [String] count Maximum number of time series descriptors per page. Used for pagination.
17-
# @option options [String] labels A collection of labels for the matching time series.
18-
# @option options [String] oldest Start of the time interval (exclusive), which is expressed as an RFC 3339
19-
# timestamp.
20-
# @options options [String] page_token The pagination token, which is used to page through large result sets.
21-
# @options options [String] timespan Length of the time interval to query, which is an alternative way to
22-
# declare the interval.
13+
# @param [Hash] options Query parameters.
14+
# @option [String] filter A monitoring filter that specifies which time series should be returned.
15+
# The filter must specify a single metric type, and can additionally specify metric labels and other
16+
# information.
17+
# @option options [Hash] interval Required. The time interval for which results should be returned.
18+
# @option interval [String] end_time Required RFC3339 timestamp marking the end of interval
19+
# @option interval [String] start_time Optional RFC3339 timestamp marking start of interval.
20+
# @option options [Hash] aggregation
21+
# @option aggregation [String] alignment_period
22+
# @option aggregation [String] cross_series_reducer
23+
# @option aggregation [String] group_by_fields
24+
# @option aggregation [String] per_series_aligner
25+
# @option options [String] order_by
26+
# @option options [String] page_size
27+
# @option options [String] page_token
28+
# @option options [String] view
29+
#
2330
# @return [Array<Fog::Google::Monitoring::Timeseries>] List of Timeseries.
24-
def all(metric, youngest, options = {})
25-
data = service.list_timeseries(metric, youngest, options).body["timeseries"] || []
31+
def all(options = {})
32+
data = service.list_timeseries(options).body["timeSeries"] || []
2633
load(data)
2734
end
2835
end

lib/fog/google/models/monitoring/timeseries_descriptor.rb

-20
This file was deleted.

lib/fog/google/models/monitoring/timeseries_descriptors.rb

-31
This file was deleted.

lib/fog/google/monitoring.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Monitoring < Fog::Service
1717
:google_json_key_string
1818
)
1919

20-
GOOGLE_MONITORING_API_VERSION = "v2beta2".freeze
21-
GOOGLE_MONITORING_BASE_URL = "https://www.googleapis.com/cloudmonitoring/"
22-
GOOGLE_MONITORING_API_SCOPE_URLS = %w(https://www.googleapis.com/auth/monitoring)
20+
GOOGLE_MONITORING_API_VERSION = "v3".freeze
21+
GOOGLE_MONITORING_BASE_URL = "https://monitoring.googleapis.com/".freeze
22+
GOOGLE_MONITORING_API_SCOPE_URLS = %w(https://www.googleapis.com/auth/monitoring).freeze
2323

2424
##
2525
# MODELS
@@ -29,26 +29,26 @@ class Monitoring < Fog::Service
2929
model :timeseries
3030
collection :timeseries_collection
3131

32-
# TimeseriesDescriptors
33-
model :timeseries_descriptor
34-
collection :timeseries_descriptors
35-
3632
# MetricDescriptors
3733
model :metric_descriptor
3834
collection :metric_descriptors
3935

36+
# MonitoredResourceDescriptors
37+
model :monitored_resource_descriptor
38+
collection :monitored_resource_descriptors
39+
4040
##
4141
# REQUESTS
4242
request_path "fog/google/requests/monitoring"
4343

4444
# Timeseries
4545
request :list_timeseries
4646

47-
# TimeseriesDescriptors
48-
request :list_timeseries_descriptors
49-
5047
# MetricDescriptors
5148
request :list_metric_descriptors
49+
50+
# MonitoredResourceDescriptors
51+
request :list_monitored_resource_descriptors
5252
end
5353
end
5454
end

lib/fog/google/monitoring/mock.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def self.data
1212
@data ||= Hash.new do |hash, key|
1313
hash[key] = {
1414
:timeseries => {},
15-
:timeseries_descriptors => {},
15+
:monitored_resource_descriptor => {},
1616
:metric_descriptors => {}
1717
}
1818
end

lib/fog/google/monitoring/real.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(options)
1212
options[:google_api_scope_url] = GOOGLE_MONITORING_API_SCOPE_URLS.join(" ")
1313

1414
@client = initialize_google_client(options)
15-
@monitoring = @client.discovered_api("cloudmonitoring", api_version)
15+
@monitoring = @client.discovered_api("monitoring", api_version)
1616
end
1717
end
1818
end

0 commit comments

Comments
 (0)