Class: Azure::Loganalytics::Datacollectorapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/loganalytics/datacollectorapi/client.rb

Constant Summary collapse

DEFAUT_MAX_RETRIES =
3.freeze
DEFAULT_RETRY_SLEEP_PERIOD =
5.freeze
MAX_BODY_BYTE_SIZE =
31457280.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_id, shared_key, endpoint = 'ods.opinsights.azure.com') ⇒ Client

30 MB



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 17

def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com')
  require 'rest-client'
  require 'json'
  require 'openssl'
  require 'base64'
  require 'time'

  @customer_id = customer_id
  @shared_key = shared_key
  @endpoint = endpoint
  @default_azure_resource_id = ''

  @max_retries = DEFAUT_MAX_RETRIES
  @retry_sleep_period = DEFAULT_RETRY_SLEEP_PERIOD    
end

Class Method Details

.is_success(res) ⇒ Object



86
87
88
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 86

def self.is_success(res)
  return (res.code == 200) ? true : false
end

Instance Method Details

#post_data(log_type, json_records, record_timestamp = '', azure_resource_id = '') ⇒ Object

Raises:

  • (ConfigError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 33

def post_data(log_type, json_records, record_timestamp ='', azure_resource_id ='' )
  raise ConfigError, 'no log_type' if log_type.empty?
  raise ConfigError, 'log_type must only contain alpha numeric and _, and not exceed 100 chars' if not is_valid_log_type(log_type)
  raise ConfigError, 'no json_records' if json_records.empty?

  body =  json_records.to_json
  body_size = body.bytesize
  raise "too large payload (#{body_size})! max post data size is #{MAX_BODY_BYTE_SIZE} bytes!" if body_size >= MAX_BODY_BYTE_SIZE

  uri = sprintf("https://%s.%s/api/logs?api-version=%s",
                @customer_id, @endpoint, API_VERSION)
  date = rfc1123date()
  sig = signature(date, body.bytesize)

  headers = {
      'Content-Type' => 'application/json',
      'Authorization' => sig,
      'Log-Type' => log_type,
      'x-ms-date' => date,
      'x-ms-AzureResourceId' => azure_resource_id.empty? ? @default_azure_resource_id : azure_resource_id,
      'time-generated-field' => record_timestamp
  }

  retries = 0 
  begin
    res = RestClient.post( uri, body, headers)
    res
  rescue => e
    c = e.response.code.to_i
    if c == 429 || c == 500 || c==503
      if retries < @max_retries
        retries += 1
        sleep(@retry_sleep_period)
        retry 
      end
    end
    raise e
  end
end

#set_default_azure_resoruce_id(azure_resource_id) ⇒ Object



77
78
79
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 77

def set_default_azure_resoruce_id(azure_resource_id)
  @default_azure_resource_id = azure_resource_id
end

#set_proxy(proxy = '') ⇒ Object



73
74
75
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 73

def set_proxy(proxy='')
  RestClient.proxy = proxy.empty? ? ENV['http_proxy'] : proxy
end

#set_retres(max_retries, retry_sleep_period) ⇒ Object



81
82
83
84
# File 'lib/azure/loganalytics/datacollectorapi/client.rb', line 81

def set_retres(max_retries, retry_sleep_period)
  @max_retries = max_retries
  @retry_sleep_period = retry_sleep_period
end