lib/prometheus/api_client/cadvisor.rb in prometheus-api-client-0.3.2 vs lib/prometheus/api_client/cadvisor.rb in prometheus-api-client-0.3.3

- old
+ new

@@ -6,81 +6,64 @@ # Client is a ruby implementation for a Prometheus compatible api_client. module ApiClient # Client contains the implementation for a Prometheus compatible api_client, # With special labels for the cadvisor job. module Cadvisor - # Create a Prometheus API client: - # - # @param [Hash] options - # @option options [String] :url Server base URL. - # @option options [Hash] :params URI query unencoded key/value pairs. - # @option options [Hash] :headers Unencoded HTTP header key/value pairs. - # @option options [Hash] :request Request options. - # @option options [Hash] :ssl SSL options. - # @option options [Hash] :proxy Proxy options. - # - # A default client is created if options is omitted. - class Node < Client + # A client with special labels for cadvisor metrics + class CadvisorClient < Client + # Add labels to simple query variables. + # + # Example: + # "cpu_usage" => "cpu_usage{labels...}" + # "sum(cpu_usage)" => "sum(cpu_usage{labels...})" + # "rate(cpu_usage[5m])" => "rate(cpu_usage{labels...}[5m])" + # + # Note: + # Not supporting more complex queries. + def update_query(query, labels) + query.sub(/(?<r>\[.+\])?(?<f>[)])?$/, "{#{labels}}\\k<r>\\k<f>") + end + + # Issues a get request to the low level client. + # Update the query options to use specific labels + def get(command, options) + options[:query] = update_query(options[:query], @labels) + @client.get(command, options) + end + end + + # A client with special labels for node cadvisor metrics + class Node < CadvisorClient def initialize(options = {}) instance = options[:instance] @labels = "job=\"kubernetes-cadvisor\",instance=\"#{instance}\"," \ 'id="/"' super(options) end - - def query(options) - options[:query] = update_query(options[:query], @labels) - super(options) - end - - def query_range(options) - options[:query] = update_query(options[:query], @labels) - super(options) - end end # A client with special labels for pod cadvisor metrics - class Pod < Client + class Pod < CadvisorClient def initialize(options = {}) pod_name = options[:pod_name] namespace = options[:namespace] || 'default' @labels = "job=\"kubernetes-cadvisor\",namespace=\"#{namespace}\"," \ "pod_name=\"#{pod_name}\",container_name=\"POD\"" super(options) end - - def query(options) - options[:query] = update_query(options[:query], @labels) - super(options) - end - - def query_range(options) - options[:query] = update_query(options[:query], @labels) - super(options) - end end # A client with special labels for container cadvisor metrics - class Container < Client + class Container < CadvisorClient def initialize(options = {}) container_name = args[:container_name] pod_name = args[:pod_name] namespace = args[:namespace] || 'default' @labels = "job=\"kubernetes-cadvisor\",namespace=\"#{namespace}\"," \ "pod_name=\"#{pod_name}\",container_name=\"#{container_name}\"" - super(options) - end - - def query(options) - options[:query] = update_query(options[:query], @labels) - super(options) - end - - def query_range(options) - options[:query] = update_query(options[:query], @labels) super(options) end end end end