class Fluent::AzureMonitorMetricsInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 33
def initialize
  super
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 37
def configure(conf)
  super

  provider = MsRestAzure::ApplicationTokenProvider.new(@tenant_id, @client_id, @client_secret)
  credentials = MsRest::TokenCredentials.new(provider)
  @client = Azure::ARM::Monitor::MonitorManagementClient.new(credentials);
end
get_param_string(original_param, query_string) click to toggle source
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 56
def get_param_string(original_param, query_string)
  array = original_param.split(',')
  param_string = ''
  array.each {|var|
    if param_string.empty?
      param_string += "#{query_string} eq '#{var}'"
    else
      param_string += " or #{query_string} eq '#{var}'"
    end
  }

  "and (#{param_string})"

end
set_path_options(start_time, end_time, custom_headers) click to toggle source
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 71
def set_path_options(start_time, end_time, custom_headers)
  fail ArgumentError, 'start_time is nil' if start_time.nil?
  request_headers = {}

  # Set Headers
  request_headers['x-ms-client-request-id'] = SecureRandom.uuid
  request_headers['accept-language'] = @client.accept_language unless @client.accept_language.nil?

  timespanstring = "#{start_time.utc.iso8601}/#{end_time.utc.iso8601}"
  top = @filter.nil? ? nil : @top

  {
      middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
      path_params: {'resourceUri' => @resource_uri},
      query_params: {'api-version' => @api_version,
                     '$top' => top,
                     '$filter' => @filter,
                     'timespan' => timespanstring,
                     'interval' => @interval,
                     'metric' => @metrics,
                     'resultType' => @result_type,
                     'aggregation'=> @aggregation},
      headers: request_headers.merge(custom_headers || {}),
      base_url: @client.base_url
  }
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 50
def shutdown
  super
  @watcher.terminate
  @watcher.join
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 45
def start
  super
  @watcher = Thread.new(&method(:watch))
end

Private Instance Methods

get_monitor_metrics_async(start_time, end_time,filter = nil, custom_headers = nil) click to toggle source
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 121
def get_monitor_metrics_async(start_time, end_time,filter = nil, custom_headers = nil)
  path_template = '/{resourceUri}/providers/microsoft.insights/metrics'

  options = set_path_options(start_time, end_time, custom_headers)
  promise = @client.make_request_async(:get, path_template, options)

  promise = promise.then do |result|
    http_response = result.response
    status_code = http_response.status
    response_content = http_response.body
    unless status_code == 200
      error_model = JSON.load(response_content)
      log.error("Error occurred while sending the request")
      log.error(error_model)
    end

    result.request_id = http_response['x-ms-request-id'] unless http_response['x-ms-request-id'].nil?
    # Deserialize Response
    if status_code == 200
      begin
        result.body = response_content.to_s.empty? ? nil : JSON.load(response_content)
      rescue Exception => e
        log.error("Error occurred in parsing the response")
        log.error(e)
      end
    end

    result
  end

  promise.execute
end
watch() click to toggle source
# File lib/fluent/plugin/in_azuremonitormetrics.rb, line 100
def watch
  log.debug "azure monitor metrics: watch thread starting"
  @next_fetch_time = Time.now

  until @finished
      start_time = @next_fetch_time - @timespan
      end_time = @next_fetch_time

      log.debug "start time: #{start_time}, end time: #{end_time}"


      monitor_metrics_promise = get_monitor_metrics_async(start_time, end_time)
      monitor_metrics = monitor_metrics_promise.value!

      router.emit(@tag, Time.now.to_i, monitor_metrics.body['value'])
      @next_fetch_time += @timespan
      sleep @timespan
  end

end