Skip to content

Commit

Permalink
Merge pull request #249 from emilymye/monitoring
Browse files Browse the repository at this point in the history
Migrate monitoring code to V3 API
  • Loading branch information
icco authored Sep 26, 2017
2 parents a5dbf20 + 3df4758 commit ca96afd
Show file tree
Hide file tree
Showing 26 changed files with 431 additions and 462 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ Fog implements [v1](https://cloud.google.com/dns/api/v1/) of the Google Cloud DN

## Monitoring

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.
Fog mostly implements [v3](https://cloud.google.com/monitoring/api/ref_v3/rest/) of the Google Cloud Monitoring API.

As of 2017-09-26, some less common API methods are missing and may be added as requested. Feature requests or pull requests for
additions are welcome.

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.

## Pubsub

Expand Down
2 changes: 1 addition & 1 deletion examples/monitoring/metric_descriptors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test

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

Expand Down
24 changes: 24 additions & 0 deletions examples/monitoring/monitored_resource_descriptors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# All examples presume that you have a ~/.fog credentials file set up.
# # More info on it can be found here: http://fog.io/about/getting_started.html
#
require "bundler"
Bundler.require(:default, :development)
# Uncomment this if you want to make real requests to GCE (you _will_ be billed!)
# WebMock.disable!
#

def test
connection = Fog::Google::Monitoring.new

puts "Listing all MonitoredResourceDescriptors..."
puts "--------------------------------"
md = connection.monitored_resource_descriptors
puts "Number of all monitored resource descriptors: #{md.length}"

puts "\nListing MonitoredResourceDescriptors related to Google Compute Engine..."
puts "-----------------------------------------------------------------"
md = connection.monitored_resource_descriptors.all(:filter => 'resource.type = starts_with("gce_")')
puts "Number of compute monitored resource : #{md.length}"
end

test
21 changes: 13 additions & 8 deletions examples/monitoring/timeseries_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@

def test
connection = Fog::Google::Monitoring.new

puts "Listing all Timeseries for the metric compute.googleapis.com/instance/uptime..."
interval = {
:start_time => (DateTime.now - 1).rfc3339,
:end_time => DateTime.now.rfc3339
}
puts "Listing Timeseries from the last hour for metric compute.googleapis.com/instance/uptime..."
puts "-------------------------------------------------------------------------------"
tc = connection.timeseries_collection.all("compute.googleapis.com/instance/uptime",
DateTime.now.rfc3339)
tc = connection.timeseries_collection.all(:filter => 'metric.type = "compute.googleapis.com/instance/uptime"',
:interval => interval)
puts "Number of matches: #{tc.length}"

puts "\nListing all Timeseries for the metric compute.googleapis.com/instance/uptime &"
puts "\nListing all Timeseries for metric compute.googleapis.com/instance/uptime &"
puts "the region us-central1..."
puts "------------------------------------------------------------------------------"
tc = connection.timeseries_collection.all("compute.googleapis.com/instance/uptime",
DateTime.now.rfc3339,
:labels => "cloud.googleapis.com/location=~us-central1.*")
filter = [
'metric.type = "compute.googleapis.com/instance/uptime"',
'resource.label.zone = "us-central1-c"'
].join(" AND ")
tc = connection.timeseries_collection.all(:filter => filter, :interval => interval)
puts "Number of matches: #{tc.length}"
end

Expand Down
27 changes: 0 additions & 27 deletions examples/monitoring/timeseries_descriptors.rb

This file was deleted.

11 changes: 7 additions & 4 deletions lib/fog/google/models/monitoring/metric_descriptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ module Fog
module Google
class Monitoring
##
# A metricDescriptor defines the name, label keys, and data type of a particular metric.
# A metricDescriptor defines a metric type and its schema.
#
# @see https://cloud.google.com/monitoring/v2beta2/metricDescriptors#resource
# @see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
class MetricDescriptor < Fog::Model
identity :name

attribute :description
attribute :display_name, :aliases => "displayName"
attribute :labels
attribute :project
attribute :type_descriptor, :aliases => "typeDescriptor"
attribute :metric_kind, :aliases => "metricKind"
attribute :type
attribute :value_type, :aliases => "valueType"
attribute :unit
end
end
end
Expand Down
10 changes: 4 additions & 6 deletions lib/fog/google/models/monitoring/metric_descriptors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class MetricDescriptors < Fog::Collection
# Lists all Metric Descriptors.
#
# @param [Hash] options Optional query parameters.
# @option options [String] count Maximum number of time series descriptors per page. Used for pagination.
# @option options [String] page_size Maximum number of metric descriptors per page. Used for pagination.
# @option options [String] page_token The pagination token, which is used to page through large result sets.
# @option options [String] query The query used to search against existing metrics. Separate keywords with a space;
# the service joins all keywords with AND, meaning that all keywords must match for a metric to be returned.
# If this field is omitted, all metrics are returned. If an empty string is passed with this field,
# no metrics are returned.
# @option options [String] filter Monitoring filter specifying which metric descriptors are to be returned.
# @see https://cloud.google.com/monitoring/api/v3/filters filter documentation
# @return [Array<Fog::Google::Monitoring::MetricDescriptor>] List of Metric Descriptors.
def all(options = {})
data = service.list_metric_descriptors(options).body["metrics"] || []
data = service.list_metric_descriptors(options).body["metricDescriptors"] || []
load(data)
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/fog/google/models/monitoring/monitored_resource_descriptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "fog/core/model"

module Fog
module Google
class Monitoring
##
# A monitoredResourceDescriptor defines a metric type and its schema.
#
# @see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.monitoredResourceDescriptors#MonitoredResourceDescriptor
class MonitoredResourceDescriptor < Fog::Model
identity :name

attribute :description
attribute :display_name, :aliases => "displayName"
attribute :type
attribute :labels
end
end
end
end
26 changes: 26 additions & 0 deletions lib/fog/google/models/monitoring/monitored_resource_descriptors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "fog/core/collection"
require "fog/google/models/monitoring/monitored_resource_descriptor"

module Fog
module Google
class Monitoring
class MonitoredResourceDescriptors < Fog::Collection
model Fog::Google::Monitoring::MonitoredResourceDescriptor

##
# Lists all Monitored Resource Descriptors.
#
# @param [Hash] options Optional query parameters.
# @option options [String] page_size Maximum number of metric descriptors per page. Used for pagination.
# @option options [String] page_token The pagination token, which is used to page through large result sets.
# @option options [String] filter The monitoring filter used to search against existing descriptors.
# See
# @return [Array<Fog::Google::Monitoring::MetricDescriptor>] List of Monitored Resource Descriptors.
def all(options = {})
data = service.list_monitored_resource_descriptors(options).body["resourceDescriptors"] || []
load(data)
end
end
end
end
end
8 changes: 5 additions & 3 deletions lib/fog/google/models/monitoring/timeseries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class Monitoring
##
# A time series is a collection of data points that represents the value of a metric of a project over time.
#
# @see https://developers.google.com/cloud-monitoring/v2beta1/timeseries
# https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list
class Timeseries < Fog::Model
identity :time_series_desc, :aliases => "timeseriesDesc"

attribute :metric
attribute :resource
attribute :metric_kind, :aliases => "metricKind"
attribute :value_type, :aliases => "valueType"
attribute :points
end
end
Expand Down
31 changes: 19 additions & 12 deletions lib/fog/google/models/monitoring/timeseries_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ class TimeseriesCollection < Fog::Collection
##
# Lists all Timeseries.
#
# @param [String] metric The name of the metric (Metric names are protocol-free URLs).
# @param [String] youngest End of the time interval (inclusive), which is expressed as an RFC 3339 timestamp.
# @param [Hash] options Optional query parameters.
# @option options [String] count Maximum number of time series descriptors per page. Used for pagination.
# @option options [String] labels A collection of labels for the matching time series.
# @option options [String] oldest Start of the time interval (exclusive), which is expressed as an RFC 3339
# timestamp.
# @options options [String] page_token The pagination token, which is used to page through large result sets.
# @options options [String] timespan Length of the time interval to query, which is an alternative way to
# declare the interval.
# @param [Hash] options Query parameters.
# @option [String] filter A monitoring filter that specifies which time series should be returned.
# The filter must specify a single metric type, and can additionally specify metric labels and other
# information.
# @option options [Hash] interval Required. The time interval for which results should be returned.
# @option interval [String] end_time Required RFC3339 timestamp marking the end of interval
# @option interval [String] start_time Optional RFC3339 timestamp marking start of interval.
# @option options [Hash] aggregation
# @option aggregation [String] alignment_period
# @option aggregation [String] cross_series_reducer
# @option aggregation [String] group_by_fields
# @option aggregation [String] per_series_aligner
# @option options [String] order_by
# @option options [String] page_size
# @option options [String] page_token
# @option options [String] view
#
# @return [Array<Fog::Google::Monitoring::Timeseries>] List of Timeseries.
def all(metric, youngest, options = {})
data = service.list_timeseries(metric, youngest, options).body["timeseries"] || []
def all(options = {})
data = service.list_timeseries(options).body["timeSeries"] || []
load(data)
end
end
Expand Down
20 changes: 0 additions & 20 deletions lib/fog/google/models/monitoring/timeseries_descriptor.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/fog/google/models/monitoring/timeseries_descriptors.rb

This file was deleted.

20 changes: 10 additions & 10 deletions lib/fog/google/monitoring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Monitoring < Fog::Service
:google_json_key_string
)

GOOGLE_MONITORING_API_VERSION = "v2beta2".freeze
GOOGLE_MONITORING_BASE_URL = "https://www.googleapis.com/cloudmonitoring/"
GOOGLE_MONITORING_API_SCOPE_URLS = %w(https://www.googleapis.com/auth/monitoring)
GOOGLE_MONITORING_API_VERSION = "v3".freeze
GOOGLE_MONITORING_BASE_URL = "https://monitoring.googleapis.com/".freeze
GOOGLE_MONITORING_API_SCOPE_URLS = %w(https://www.googleapis.com/auth/monitoring).freeze

##
# MODELS
Expand All @@ -29,26 +29,26 @@ class Monitoring < Fog::Service
model :timeseries
collection :timeseries_collection

# TimeseriesDescriptors
model :timeseries_descriptor
collection :timeseries_descriptors

# MetricDescriptors
model :metric_descriptor
collection :metric_descriptors

# MonitoredResourceDescriptors
model :monitored_resource_descriptor
collection :monitored_resource_descriptors

##
# REQUESTS
request_path "fog/google/requests/monitoring"

# Timeseries
request :list_timeseries

# TimeseriesDescriptors
request :list_timeseries_descriptors

# MetricDescriptors
request :list_metric_descriptors

# MonitoredResourceDescriptors
request :list_monitored_resource_descriptors
end
end
end
2 changes: 1 addition & 1 deletion lib/fog/google/monitoring/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:timeseries => {},
:timeseries_descriptors => {},
:monitored_resource_descriptor => {},
:metric_descriptors => {}
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/google/monitoring/real.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(options)
options[:google_api_scope_url] = GOOGLE_MONITORING_API_SCOPE_URLS.join(" ")

@client = initialize_google_client(options)
@monitoring = @client.discovered_api("cloudmonitoring", api_version)
@monitoring = @client.discovered_api("monitoring", api_version)
end
end
end
Expand Down
Loading

0 comments on commit ca96afd

Please sign in to comment.