Last active
November 10, 2022 05:18
-
-
Save palladius/1e1973067ade07497fc553477df1655f to your computer and use it in GitHub Desktop.
Push a generic metric name/number to GCP (excerpt of bigger script)
This file contains hidden or 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
#!/usr/bin/env ruby | |
# full script here: https://github.com/palladius/sakura/blob/master/bin/gcp-write-metric | |
require 'google/cloud/monitoring' | |
require 'socket' # for hostname | |
require 'openssl' | |
require 'colorize' # makes life worth living | |
# I want to know server side who called me | |
def gethostname | |
Socket.gethostname | |
end | |
def magic_cloud_console_url(project_id) | |
'https://console.cloud.google.com/monitoring/metrics-explorer?project=#{project_id}&pageState=%7B%22xyChart%22:%7B%22dataSets%22:%5B%7B%22timeSeriesFilter%22:%7B%22filter%22:%22metric.type%3D%5C%22custom.googleapis.com%2Friccardo-disc-free-pct%5C%22%20resource.type%3D%5C%22gce_instance%5C%22%22,%22minAlignmentPeriod%22:%2260s%22,%22aggregations%22:%5B%7B%22perSeriesAligner%22:%22ALIGN_MEAN%22,%22crossSeriesReducer%22:%22REDUCE_NONE%22,%22alignmentPeriod%22:%2260s%22,%22groupByFields%22:%5B%5D%7D,%7B%22crossSeriesReducer%22:%22REDUCE_NONE%22,%22alignmentPeriod%22:%2260s%22,%22groupByFields%22:%5B%5D%7D%5D%7D,%22targetAxis%22:%22Y1%22,%22plotType%22:%22LINE%22%7D%5D,%22options%22:%7B%22mode%22:%22COLOR%22%7D,%22constantLines%22:%5B%5D,%22timeshiftDuration%22:%220s%22,%22y1Axis%22:%7B%22label%22:%22y1Axis%22,%22scale%22:%22LINEAR%22%7D%7D,%22isAutoRefresh%22:true,%22timeSelection%22:%7B%22timeRange%22:%221h%22%7D%7D' | |
end | |
def write_generic_metric(project_id, metric_label, metric_value) | |
metric_service_client = Google::Cloud::Monitoring.metric_service | |
project_path = metric_service_client.project_path project: project_id | |
full_hostname = gethostname() | |
hostname = full_hostname.split('.')[0] | |
host_domain = full_hostname.split('.')[1,10].join('.') | |
series = Google::Cloud::Monitoring::V3::TimeSeries.new | |
series.metric = Google::Api::Metric.new type: "custom.googleapis.com/#{metric_label}", | |
labels: { | |
"my_key" => metric_label, | |
"hostname" => hostname, | |
"domain" => host_domain, | |
#"app_verison" => $APP_VERSION, # found out you dont want to slice and dice metric when you change version... but you might want it. | |
} | |
resource = Google::Api::MonitoredResource.new type: "gce_instance" | |
resource.labels["project_id"] = project_id | |
resource.labels["instance_id"] = hostname # "1234567890123456789" | |
resource.labels["zone"] = "europe-west6-a" # Zurich - "us-central1-f" | |
series.resource = resource | |
point = Google::Cloud::Monitoring::V3::Point.new | |
point.value = Google::Cloud::Monitoring::V3::TypedValue.new double_value: metric_value # get_disk_space | |
now = Time.now | |
end_time = Google::Protobuf::Timestamp.new seconds: now.to_i, nanos: now.nsec | |
point.interval = Google::Cloud::Monitoring::V3::TimeInterval.new end_time: end_time | |
series.points << point | |
metric_service_client.create_time_series name: project_path, time_series: [series] | |
puts "📉 Successfully wrote time series '#{metric_label.colorize :yellow}'=#{metric_value.to_s.colorize :cyan} on #{project_id.colorize :red} for '#{hostname}'" | |
end | |
def main | |
metric = ARGV[0] | |
value = ARGV[1].to_f | |
write_generic_metric(project_id, metric, value) | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shortened script for Medium article. Fill script in here however: https://github.com/palladius/sakura/blob/master/bin/gcp-write-metric