Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplified example of using google-cloud-monitoring to store custom metrics of my daemons RAM/fd leakage
require "google/cloud/monitoring/v3/metric_service_client"
client = Google::Cloud::Monitoring::V3::MetricServiceClient.new
project_path = Google::Cloud::Monitoring::V3::MetricServiceClient.project_path JSON.load(File.read ENV["GOOGLE_APPLICATION_CREDENTIALS"])["project_id"]
create_custom_gauge_metric = lambda do |type, display_name, value_type, labels = {}|
metric_descriptor = Google::Api::MetricDescriptor.new( # googleapis-common-protos-1.3.5/lib/google/api/metric_pb.rb
type: "custom.googleapis.com/#{type}",
metric_kind: :GAUGE, # google-cloud-monitoring-0.24.0/lib/google/cloud/monitoring/v3/doc/google/api/metric.rb
value_type: value_type, # google-cloud-monitoring-0.24.0/lib/google/cloud/monitoring/v3/doc/google/api/metric.rb
display_name: display_name,
)
labels.each do |label|
metric_descriptor.labels.push Google::Api::LabelDescriptor.new(key: label) # gcloud-0.12.0/lib/google/api/label.rb
end
client.create_metric_descriptor project_path, metric_descriptor
end
log_custom_metric = lambda do |type, timestamp, value, resource: nil, labels|
time_series = Google::Monitoring::V3::TimeSeries.new( # google-cloud-monitoring-0.24.0/lib/google/monitoring/v3/metric_pb.rb
metric: Google::Api::Metric.new( # googleapis-common-protos-1.3.5/lib/google/api/metric_pb.rb
type: "custom.googleapis.com/#{type}",
labels: labels,
),
**(resource ? {resource: Google::Api::MonitoredResource.new(**resource)} : {}) # googleapis-common-protos-1.3.5/lib/google/api/monitored_resource_pb.rb
)
time_series.points.push Google::Monitoring::V3::Point.new( # google-cloud-monitoring-0.24.0/lib/google/monitoring/v3/metric_pb.rb
interval: Google::Monitoring::V3::TimeInterval.new(
start_time: Google::Protobuf::Timestamp.new(seconds: timestamp.to_i),
end_time: Google::Protobuf::Timestamp.new(seconds: timestamp.to_i),
),
value: Google::Monitoring::V3::TypedValue.new(**value) # google-cloud-monitoring-0.24.0/lib/google/monitoring/v3/common_pb.rb
)
client.create_time_series project_path, [time_series]
end
create_custom_gauge_metric["test/free", "Vmstat.memory.free (MB)", :INT64]
create_custom_gauge_metric["test/mem", "GetProcessMem (MB)", :DOUBLE, ["daemon_id"]]
create_custom_gauge_metric["test/fd", "File descriptor count", :INT64, ["daemon_id"]]
require "yaml"
report = YAML.load DATA.read
timestamp, free, apps = report[:data]
log_custom_metric["test/free", timestamp, {int64_value: free}, resource: {type: "gce_instance", labels: {
"instance_id"=>"0000000000000000000",
"zone"=>"us-central1-a",
} } ]
apps.each do |daemon_id, mem, fd|
log_custom_metric["test/mem", timestamp, {double_value: mem}, labels: {"daemon_id"=>daemon_id}]
log_custom_metric["test/fd", timestamp, {int64_value: fd}, labels: {"daemon_id"=>daemon_id}]
end
__END__
---
:data:
- 2017-06-13 17:25:05.510724432 +00:00
- 1072
- - - "daemon 1"
- 106.4765625
- 63
- - "daemon 2"
- 26.8125
- 52
- - "daemon 3"
- 743.1796875
- 60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.